@@ -19,8 +19,14 @@ package common
19
19
import (
20
20
"context"
21
21
"testing"
22
+ "time"
22
23
24
+ snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
25
+ snapshotfake "github.com/kubernetes-csi/external-snapshotter/client/v8/clientset/versioned/fake"
23
26
vim25types "github.com/vmware/govmomi/vim25/types"
27
+ v1 "k8s.io/api/core/v1"
28
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
+ "k8s.io/client-go/kubernetes/fake"
24
30
)
25
31
26
32
// TestUseVslmAPIsFuncForVC67Update3l tests UseVslmAPIs method for VC version 6.7 Update 3l
@@ -198,3 +204,141 @@ func TestCheckAPIForVC70u3(t *testing.T) {
198
204
t .Fatalf ("CheckAPI method failing for VC %q" , vcVersion )
199
205
}
200
206
}
207
+
208
+ // TestWaitForPVCDeletedWithWatch tests the WaitForPVCDeletedWithWatch function
209
+ func TestWaitForPVCDeletedWithWatch (t * testing.T ) {
210
+ ctx , cancel := context .WithCancel (context .Background ())
211
+ defer cancel ()
212
+
213
+ // Test case 1: PVC already deleted (not found)
214
+ t .Run ("PVC already deleted" , func (t * testing.T ) {
215
+ k8sclient := fake .NewSimpleClientset ()
216
+
217
+ err := WaitForPVCDeletedWithWatch (ctx , k8sclient , "test-pvc" , "test-namespace" , time .Second * 5 )
218
+ if err != nil {
219
+ t .Fatalf ("Expected no error when PVC is already deleted, got: %v" , err )
220
+ }
221
+ })
222
+
223
+ // Test case 2: PVC exists and gets deleted during watch
224
+ t .Run ("PVC gets deleted during watch" , func (t * testing.T ) {
225
+ pvc := & v1.PersistentVolumeClaim {
226
+ ObjectMeta : metav1.ObjectMeta {
227
+ Name : "test-pvc" ,
228
+ Namespace : "test-namespace" ,
229
+ },
230
+ }
231
+ k8sclient := fake .NewSimpleClientset (pvc )
232
+
233
+ // Start watching in a goroutine
234
+ errChan := make (chan error , 1 )
235
+ go func () {
236
+ err := WaitForPVCDeletedWithWatch (ctx , k8sclient , "test-pvc" , "test-namespace" , time .Second * 5 )
237
+ errChan <- err
238
+ }()
239
+
240
+ // Give the watch time to start
241
+ time .Sleep (time .Millisecond * 100 )
242
+
243
+ // Delete the PVC
244
+ err := k8sclient .CoreV1 ().PersistentVolumeClaims ("test-namespace" ).Delete (ctx , "test-pvc" , metav1.DeleteOptions {})
245
+ if err != nil {
246
+ t .Fatalf ("Failed to delete PVC: %v" , err )
247
+ }
248
+
249
+ // Wait for the watch to complete
250
+ select {
251
+ case err := <- errChan :
252
+ if err != nil {
253
+ t .Fatalf ("Expected no error when PVC is deleted during watch, got: %v" , err )
254
+ }
255
+ case <- time .After (time .Second * 2 ):
256
+ t .Fatal ("Watch did not complete within expected time" )
257
+ }
258
+ })
259
+
260
+ // Test case 3: Watch timeout
261
+ t .Run ("Watch timeout" , func (t * testing.T ) {
262
+ pvc := & v1.PersistentVolumeClaim {
263
+ ObjectMeta : metav1.ObjectMeta {
264
+ Name : "test-pvc" ,
265
+ Namespace : "test-namespace" ,
266
+ },
267
+ }
268
+ k8sclient := fake .NewSimpleClientset (pvc )
269
+
270
+ err := WaitForPVCDeletedWithWatch (ctx , k8sclient , "test-pvc" , "test-namespace" , time .Millisecond * 100 )
271
+ if err == nil {
272
+ t .Fatal ("Expected timeout error when PVC is not deleted" )
273
+ }
274
+ })
275
+ }
276
+
277
+ // TestWaitForVolumeSnapshotDeletedWithWatch tests the WaitForVolumeSnapshotDeletedWithWatch function
278
+ func TestWaitForVolumeSnapshotDeletedWithWatch (t * testing.T ) {
279
+ ctx , cancel := context .WithCancel (context .Background ())
280
+ defer cancel ()
281
+
282
+ // Test case 1: VolumeSnapshot already deleted (not found)
283
+ t .Run ("VolumeSnapshot already deleted" , func (t * testing.T ) {
284
+ snapshotClient := snapshotfake .NewSimpleClientset ()
285
+
286
+ err := WaitForVolumeSnapshotDeletedWithWatch (ctx , snapshotClient , "test-snapshot" , "test-namespace" , time .Second * 5 )
287
+ if err != nil {
288
+ t .Fatalf ("Expected no error when VolumeSnapshot is already deleted, got: %v" , err )
289
+ }
290
+ })
291
+
292
+ // Test case 2: VolumeSnapshot exists and gets deleted during watch
293
+ t .Run ("VolumeSnapshot gets deleted during watch" , func (t * testing.T ) {
294
+ snapshot := & snapshotv1.VolumeSnapshot {
295
+ ObjectMeta : metav1.ObjectMeta {
296
+ Name : "test-snapshot" ,
297
+ Namespace : "test-namespace" ,
298
+ },
299
+ }
300
+ snapshotClient := snapshotfake .NewSimpleClientset (snapshot )
301
+
302
+ // Start watching in a goroutine
303
+ errChan := make (chan error , 1 )
304
+ go func () {
305
+ err := WaitForVolumeSnapshotDeletedWithWatch (ctx , snapshotClient , "test-snapshot" , "test-namespace" , time .Second * 5 )
306
+ errChan <- err
307
+ }()
308
+
309
+ // Give the watch time to start
310
+ time .Sleep (time .Millisecond * 100 )
311
+
312
+ // Delete the VolumeSnapshot
313
+ err := snapshotClient .SnapshotV1 ().VolumeSnapshots ("test-namespace" ).Delete (ctx , "test-snapshot" , metav1.DeleteOptions {})
314
+ if err != nil {
315
+ t .Fatalf ("Failed to delete VolumeSnapshot: %v" , err )
316
+ }
317
+
318
+ // Wait for the watch to complete
319
+ select {
320
+ case err := <- errChan :
321
+ if err != nil {
322
+ t .Fatalf ("Expected no error when VolumeSnapshot is deleted during watch, got: %v" , err )
323
+ }
324
+ case <- time .After (time .Second * 2 ):
325
+ t .Fatal ("Watch did not complete within expected time" )
326
+ }
327
+ })
328
+
329
+ // Test case 3: Watch timeout
330
+ t .Run ("Watch timeout" , func (t * testing.T ) {
331
+ snapshot := & snapshotv1.VolumeSnapshot {
332
+ ObjectMeta : metav1.ObjectMeta {
333
+ Name : "test-snapshot" ,
334
+ Namespace : "test-namespace" ,
335
+ },
336
+ }
337
+ snapshotClient := snapshotfake .NewSimpleClientset (snapshot )
338
+
339
+ err := WaitForVolumeSnapshotDeletedWithWatch (ctx , snapshotClient , "test-snapshot" , "test-namespace" , time .Millisecond * 100 )
340
+ if err == nil {
341
+ t .Fatal ("Expected timeout error when VolumeSnapshot is not deleted" )
342
+ }
343
+ })
344
+ }
0 commit comments