From 359b9e33830754c06ced68aada614e99ad68ba53 Mon Sep 17 00:00:00 2001 From: Chris Henzie Date: Tue, 24 Nov 2020 17:03:17 -0800 Subject: [PATCH] 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. --- pkg/volume/csi/csi_attacher_test.go | 6 +++++- pkg/volume/csi/csi_plugin_test.go | 20 +++++++++++--------- pkg/volume/csi/testing/testing.go | 17 +++++++++++------ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/pkg/volume/csi/csi_attacher_test.go b/pkg/volume/csi/csi_attacher_test.go index f381d2aaa36..fbcdc5f59d2 100644 --- a/pkg/volume/csi/csi_attacher_test.go +++ b/pkg/volume/csi/csi_attacher_test.go @@ -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) } diff --git a/pkg/volume/csi/csi_plugin_test.go b/pkg/volume/csi/csi_plugin_test.go index 4f0db8de0cb..140ca4834ec 100644 --- a/pkg/volume/csi/csi_plugin_test.go +++ b/pkg/volume/csi/csi_plugin_test.go @@ -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 } diff --git a/pkg/volume/csi/testing/testing.go b/pkg/volume/csi/testing/testing.go index 2c0a26169f3..89a740d8a74 100644 --- a/pkg/volume/csi/testing/testing.go +++ b/pkg/volume/csi/testing/testing.go @@ -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 }