Skip to content

Commit 6ee74bf

Browse files
authored
Merge pull request kubernetes#94031 from knight42/fix/TestAttacherWithCSIDriver
test(csi): deflake TestAttacherWithCSIDriver
2 parents be1d43a + 469f59a commit 6ee74bf

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

pkg/volume/csi/csi_attacher_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func TestAttacherAttach(t *testing.T) {
211211

212212
csiAttacher := attacher.(*csiAttacher)
213213

214+
// FIXME: We need to ensure this goroutine exits in the test.
214215
go func(spec *volume.Spec, nodename string, fail bool) {
215216
attachID, err := csiAttacher.Attach(spec, types.NodeName(nodename))
216217
if !fail && err != nil {
@@ -294,6 +295,7 @@ func TestAttacherAttachWithInline(t *testing.T) {
294295
}
295296
csiAttacher := attacher.(*csiAttacher)
296297

298+
// FIXME: We need to ensure this goroutine exits in the test.
297299
go func(spec *volume.Spec, nodename string, fail bool) {
298300
attachID, err := csiAttacher.Attach(spec, types.NodeName(nodename))
299301
if fail != (err != nil) {
@@ -356,6 +358,22 @@ func TestAttacherWithCSIDriver(t *testing.T) {
356358
plug, _, tmpDir, _ := newTestWatchPlugin(t, fakeClient, true)
357359
defer os.RemoveAll(tmpDir)
358360

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+
359377
attacher, err := plug.NewAttacher()
360378
if err != nil {
361379
t.Fatalf("failed to create new attacher: %v", err)
@@ -377,8 +395,8 @@ func TestAttacherWithCSIDriver(t *testing.T) {
377395
}
378396
var wg sync.WaitGroup
379397
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")
382400
defer wg.Done()
383401

384402
if err != nil {
@@ -387,13 +405,16 @@ func TestAttacherWithCSIDriver(t *testing.T) {
387405
if attachID != "" {
388406
t.Errorf("Expected empty attachID, got %q", attachID)
389407
}
390-
}(spec, test.expectVolumeAttachment)
408+
}(spec)
391409

392410
if test.expectVolumeAttachment {
393411
expectedAttachID := getAttachmentName("test-vol", test.driver, "fakeNode")
394412
status := storage.VolumeAttachmentStatus{
395413
Attached: true,
396414
}
415+
// We want to ensure the watcher, which is created in csiAttacher,
416+
// has been started before updating the status of attachment.
417+
<-attachmentWatchCreated
397418
markVolumeAttached(t, csiAttacher.k8s, nil, expectedAttachID, status)
398419
}
399420
wg.Wait()
@@ -1568,6 +1589,13 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset, setupInf
15681589
}
15691590

15701591
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+
}
15711599

15721600
host := volumetest.NewFakeVolumeHostWithCSINodeName(t,
15731601
tmpDir,
@@ -1589,15 +1617,5 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset, setupInf
15891617
t.Fatalf("cannot assert plugin to be type csiPlugin")
15901618
}
15911619

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-
}
16021620
return csiPlug, fakeWatcher, tmpDir, fakeClient
16031621
}

0 commit comments

Comments
 (0)