@@ -10,7 +10,6 @@ import (
1010 "k8s.io/apimachinery/pkg/runtime"
1111 "k8s.io/client-go/kubernetes"
1212 "k8s.io/client-go/kubernetes/fake"
13- restclient "k8s.io/client-go/rest"
1413 k8stesting "k8s.io/client-go/testing"
1514 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/unittestcommon"
1615 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
@@ -162,19 +161,6 @@ func newMockPod(name, namespace, nodeName string, volumes []string,
162161}
163162
164163func TestGetSnapshotLimitForNamespace (t * testing.T ) {
165- // Save original functions and restore after tests
166- originalGetConfig := getK8sConfig
167- originalNewK8sClientFromConfig := newK8sClientFromConfig
168- defer func () {
169- getK8sConfig = originalGetConfig
170- newK8sClientFromConfig = originalNewK8sClientFromConfig
171- }()
172-
173- // Mock getK8sConfig to return a fake config
174- getK8sConfig = func () (* restclient.Config , error ) {
175- return & restclient.Config {}, nil
176- }
177-
178164 t .Run ("WhenConfigMapExists_ValidValue" , func (t * testing.T ) {
179165 // Setup
180166 cm := & v1.ConfigMap {
@@ -187,12 +173,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
187173 },
188174 }
189175 fakeClient := fake .NewClientset (cm )
190- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
191- return fakeClient , nil
192- }
193176
194177 // Execute
195- limit , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
178+ limit , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
196179
197180 // Verify
198181 assert .Nil (t , err )
@@ -211,12 +194,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
211194 },
212195 }
213196 fakeClient := fake .NewClientset (cm )
214- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
215- return fakeClient , nil
216- }
217197
218198 // Execute
219- limit , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
199+ limit , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
220200
221201 // Verify
222202 assert .Nil (t , err )
@@ -235,12 +215,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
235215 },
236216 }
237217 fakeClient := fake .NewClientset (cm )
238- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
239- return fakeClient , nil
240- }
241218
242219 // Execute
243- limit , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
220+ limit , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
244221
245222 // Verify
246223 assert .Nil (t , err )
@@ -259,12 +236,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
259236 },
260237 }
261238 fakeClient := fake .NewClientset (cm )
262- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
263- return fakeClient , nil
264- }
265239
266240 // Execute
267- limit , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
241+ limit , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
268242
269243 // Verify
270244 assert .Nil (t , err )
@@ -283,12 +257,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
283257 },
284258 }
285259 fakeClient := fake .NewClientset (cm )
286- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
287- return fakeClient , nil
288- }
289260
290261 // Execute
291- _ , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
262+ _ , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
292263
293264 // Verify
294265 assert .NotNil (t , err )
@@ -308,12 +279,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
308279 },
309280 }
310281 fakeClient := fake .NewClientset (cm )
311- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
312- return fakeClient , nil
313- }
314282
315283 // Execute
316- _ , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
284+ _ , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
317285
318286 // Verify
319287 assert .NotNil (t , err )
@@ -331,12 +299,9 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
331299 Data : map [string ]string {}, // ConfigMap exists but key is missing
332300 }
333301 fakeClient := fake .NewClientset (cm )
334- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
335- return fakeClient , nil
336- }
337302
338303 // Execute
339- _ , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
304+ _ , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
340305
341306 // Verify
342307 assert .NotNil (t , err )
@@ -346,29 +311,12 @@ func TestGetSnapshotLimitForNamespace(t *testing.T) {
346311 t .Run ("WhenConfigMapNotFound" , func (t * testing.T ) {
347312 // Setup
348313 fakeClient := fake .NewClientset () // Empty clientset
349- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
350- return fakeClient , nil
351- }
352314
353315 // Execute
354- limit , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
316+ limit , err := getSnapshotLimitForNamespace (context .Background (), fakeClient , "test-namespace" )
355317
356318 // Verify
357319 assert .Nil (t , err )
358320 assert .Equal (t , common .DefaultMaxSnapshotsPerVolume , limit ) // Should return default (4)
359321 })
360-
361- t .Run ("WhenK8sClientCreationFails" , func (t * testing.T ) {
362- // Setup
363- newK8sClientFromConfig = func (c * restclient.Config ) (kubernetes.Interface , error ) {
364- return nil , assert .AnError
365- }
366-
367- // Execute
368- limit , err := getSnapshotLimitForNamespace (context .Background (), "test-namespace" )
369-
370- // Verify - should return default instead of error
371- assert .Nil (t , err )
372- assert .Equal (t , common .DefaultMaxSnapshotsPerVolume , limit )
373- })
374322}
0 commit comments