@@ -211,6 +211,7 @@ func TestAttacherAttach(t *testing.T) {
211
211
212
212
csiAttacher := attacher .(* csiAttacher )
213
213
214
+ // FIXME: We need to ensure this goroutine exits in the test.
214
215
go func (spec * volume.Spec , nodename string , fail bool ) {
215
216
attachID , err := csiAttacher .Attach (spec , types .NodeName (nodename ))
216
217
if ! fail && err != nil {
@@ -294,6 +295,7 @@ func TestAttacherAttachWithInline(t *testing.T) {
294
295
}
295
296
csiAttacher := attacher .(* csiAttacher )
296
297
298
+ // FIXME: We need to ensure this goroutine exits in the test.
297
299
go func (spec * volume.Spec , nodename string , fail bool ) {
298
300
attachID , err := csiAttacher .Attach (spec , types .NodeName (nodename ))
299
301
if fail != (err != nil ) {
@@ -356,6 +358,22 @@ func TestAttacherWithCSIDriver(t *testing.T) {
356
358
plug , _ , tmpDir , _ := newTestWatchPlugin (t , fakeClient , true )
357
359
defer os .RemoveAll (tmpDir )
358
360
361
+ attachmentWatchCreated := make (chan core.Action )
362
+ // Make sure this is the first reactor
363
+ fakeClient .Fake .PrependWatchReactor ("volumeattachments" , func (action core.Action ) (bool , watch.Interface , error ) {
364
+ select {
365
+ case <- attachmentWatchCreated :
366
+ // already closed
367
+ default :
368
+ // The attacher is already watching the attachment, notify the test goroutine to
369
+ // update the status of attachment.
370
+ // TODO: In theory this still has a race condition, because the actual watch is created by
371
+ // the next reactor in the chain and we unblock the test goroutine before returning here.
372
+ close (attachmentWatchCreated )
373
+ }
374
+ return false , nil , nil
375
+ })
376
+
359
377
attacher , err := plug .NewAttacher ()
360
378
if err != nil {
361
379
t .Fatalf ("failed to create new attacher: %v" , err )
@@ -377,8 +395,8 @@ func TestAttacherWithCSIDriver(t *testing.T) {
377
395
}
378
396
var wg sync.WaitGroup
379
397
wg .Add (1 )
380
- go func (volSpec * volume.Spec , expectAttach bool ) {
381
- attachID , err := csiAttacher .Attach (volSpec , types . NodeName ( "fakeNode" ) )
398
+ go func (volSpec * volume.Spec ) {
399
+ attachID , err := csiAttacher .Attach (volSpec , "fakeNode" )
382
400
defer wg .Done ()
383
401
384
402
if err != nil {
@@ -387,13 +405,16 @@ func TestAttacherWithCSIDriver(t *testing.T) {
387
405
if attachID != "" {
388
406
t .Errorf ("Expected empty attachID, got %q" , attachID )
389
407
}
390
- }(spec , test . expectVolumeAttachment )
408
+ }(spec )
391
409
392
410
if test .expectVolumeAttachment {
393
411
expectedAttachID := getAttachmentName ("test-vol" , test .driver , "fakeNode" )
394
412
status := storage.VolumeAttachmentStatus {
395
413
Attached : true ,
396
414
}
415
+ // We want to ensure the watcher, which is created in csiAttacher,
416
+ // has been started before updating the status of attachment.
417
+ <- attachmentWatchCreated
397
418
markVolumeAttached (t , csiAttacher .k8s , nil , expectedAttachID , status )
398
419
}
399
420
wg .Wait ()
@@ -1568,6 +1589,13 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset, setupInf
1568
1589
}
1569
1590
1570
1591
factory .Start (wait .NeverStop )
1592
+ ctx , cancel := context .WithTimeout (context .Background (), TestInformerSyncTimeout )
1593
+ defer cancel ()
1594
+ for ty , ok := range factory .WaitForCacheSync (ctx .Done ()) {
1595
+ if ! ok {
1596
+ t .Fatalf ("failed to sync: %#v" , ty )
1597
+ }
1598
+ }
1571
1599
1572
1600
host := volumetest .NewFakeVolumeHostWithCSINodeName (t ,
1573
1601
tmpDir ,
@@ -1589,15 +1617,5 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset, setupInf
1589
1617
t .Fatalf ("cannot assert plugin to be type csiPlugin" )
1590
1618
}
1591
1619
1592
- // Wait until the informer in CSI volume plugin has all CSIDrivers.
1593
- wait .PollImmediate (TestInformerSyncPeriod , TestInformerSyncTimeout , func () (bool , error ) {
1594
- return csiDriverInformer .Informer ().HasSynced (), nil
1595
- })
1596
-
1597
- if volumeAttachmentInformer != nil {
1598
- wait .PollImmediate (TestInformerSyncPeriod , TestInformerSyncTimeout , func () (bool , error ) {
1599
- return volumeAttachmentInformer .Informer ().HasSynced (), nil
1600
- })
1601
- }
1602
1620
return csiPlug , fakeWatcher , tmpDir , fakeClient
1603
1621
}
0 commit comments