Ensure CSI test plugin informers are synced before returning

This way of syncing informers ensures all informers generated from these
factories are synced. Informers are lazy loaded, and only created once a
user calls .Informer() (which was why the .PollImmediate() worked).
Asserting the number of synced types to ensure everything is set up
correctly.
This commit is contained in:
Chris Henzie 2020-11-24 17:03:17 -08:00
parent 257704b30d
commit 359b9e3383
3 changed files with 27 additions and 16 deletions

View File

@ -1639,7 +1639,11 @@ func newTestWatchPlugin(t *testing.T, fakeClient *fakeclient.Clientset) (*csiPlu
factory.Start(wait.NeverStop)
ctx, cancel := context.WithTimeout(context.Background(), TestInformerSyncTimeout)
defer cancel()
for ty, ok := range factory.WaitForCacheSync(ctx.Done()) {
syncedTypes := factory.WaitForCacheSync(ctx.Done())
if len(syncedTypes) != 2 {
t.Fatalf("informers are not synced")
}
for ty, ok := range syncedTypes {
if !ok {
t.Fatalf("failed to sync: %#v", ty)
}

View File

@ -65,7 +65,17 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri
csiDriverLister := csiDriverInformer.Lister()
volumeAttachmentInformer := factory.Storage().V1().VolumeAttachments()
volumeAttachmentLister := volumeAttachmentInformer.Lister()
go factory.Start(wait.NeverStop)
factory.Start(wait.NeverStop)
syncedTypes := factory.WaitForCacheSync(wait.NeverStop)
if len(syncedTypes) != 2 {
t.Fatalf("informers are not synced")
}
for ty, ok := range syncedTypes {
if !ok {
t.Fatalf("failed to sync: %#v", ty)
}
}
host := volumetest.NewFakeKubeletVolumeHostWithCSINodeName(t,
tmpDir,
@ -87,14 +97,6 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri
t.Fatalf("cannot assert plugin to be type csiPlugin")
}
// Wait until the informer in CSI volume plugin has all CSIDrivers.
wait.PollImmediate(TestInformerSyncPeriod, TestInformerSyncTimeout, func() (bool, error) {
return csiDriverInformer.Informer().HasSynced(), nil
})
wait.PollImmediate(TestInformerSyncPeriod, TestInformerSyncTimeout, func() (bool, error) {
return volumeAttachmentInformer.Informer().HasSynced(), nil
})
return csiPlug, tmpDir
}

View File

@ -52,7 +52,17 @@ func NewTestPlugin(t *testing.T, client *fakeclient.Clientset) (*volume.VolumePl
factory := informers.NewSharedInformerFactory(client, csi.CsiResyncPeriod)
csiDriverInformer := factory.Storage().V1().CSIDrivers()
csiDriverLister := csiDriverInformer.Lister()
go factory.Start(wait.NeverStop)
factory.Start(wait.NeverStop)
syncedTypes := factory.WaitForCacheSync(wait.NeverStop)
if len(syncedTypes) != 1 {
t.Fatalf("informers are not synced")
}
for ty, ok := range syncedTypes {
if !ok {
t.Fatalf("failed to sync: %#v", ty)
}
}
host := volumetest.NewFakeVolumeHostWithCSINodeName(t,
tmpDir,
@ -69,10 +79,5 @@ func NewTestPlugin(t *testing.T, client *fakeclient.Clientset) (*volume.VolumePl
t.Fatalf("can't find plugin %v", csi.CSIPluginName)
}
// Wait until the informer in CSI volume plugin has all CSIDrivers.
wait.PollImmediate(csi.TestInformerSyncPeriod, csi.TestInformerSyncTimeout, func() (bool, error) {
return csiDriverInformer.Informer().HasSynced(), nil
})
return plugMgr, &plug, tmpDir
}