From 63b3085f2a8715d08e2031eac0b84800ffafdd59 Mon Sep 17 00:00:00 2001 From: huweiwen Date: Fri, 27 Oct 2023 23:35:45 +0800 Subject: [PATCH] fix ad controller populators test The informer is not initialized, so no assertion performed before. Fixed this now. Then fixed the test failure by using NewAttachDetachController to initialize adc. --- .../attach_detach_controller_test.go | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/pkg/controller/volume/attachdetach/attach_detach_controller_test.go b/pkg/controller/volume/attachdetach/attach_detach_controller_test.go index 3bb584dde47..8294305e9bd 100644 --- a/pkg/controller/volume/attachdetach/attach_detach_controller_test.go +++ b/pkg/controller/volume/attachdetach/attach_detach_controller_test.go @@ -73,44 +73,42 @@ func Test_NewAttachDetachController_Positive(t *testing.T) { } } -func Test_AttachDetachControllerStateOfWolrdPopulators_Positive(t *testing.T) { +func Test_AttachDetachControllerStateOfWorldPopulators_Positive(t *testing.T) { // Arrange fakeKubeClient := controllervolumetesting.CreateTestClient() informerFactory := informers.NewSharedInformerFactory(fakeKubeClient, controller.NoResyncPeriodFunc()) - podInformer := informerFactory.Core().V1().Pods() - nodeInformer := informerFactory.Core().V1().Nodes() - pvcInformer := informerFactory.Core().V1().PersistentVolumeClaims() - pvInformer := informerFactory.Core().V1().PersistentVolumes() - volumeAttachmentInformer := informerFactory.Storage().V1().VolumeAttachments() - adc := &attachDetachController{ - kubeClient: fakeKubeClient, - pvcLister: pvcInformer.Lister(), - pvcsSynced: pvcInformer.Informer().HasSynced, - pvLister: pvInformer.Lister(), - pvsSynced: pvInformer.Informer().HasSynced, - podLister: podInformer.Lister(), - podsSynced: podInformer.Informer().HasSynced, - nodeLister: nodeInformer.Lister(), - nodesSynced: nodeInformer.Informer().HasSynced, - volumeAttachmentLister: volumeAttachmentInformer.Lister(), - volumeAttachmentSynced: volumeAttachmentInformer.Informer().HasSynced, - cloud: nil, + logger, ctx := ktesting.NewTestContext(t) + ctx, cancel := context.WithCancel(ctx) + defer cancel() + adcObj, err := NewAttachDetachController( + logger, + fakeKubeClient, + informerFactory.Core().V1().Pods(), + informerFactory.Core().V1().Nodes(), + informerFactory.Core().V1().PersistentVolumeClaims(), + informerFactory.Core().V1().PersistentVolumes(), + informerFactory.Storage().V1().CSINodes(), + informerFactory.Storage().V1().CSIDrivers(), + informerFactory.Storage().V1().VolumeAttachments(), + nil, /* cloud */ + controllervolumetesting.CreateTestPlugin(), + nil, /* prober */ + false, + 5*time.Second, + DefaultTimerConfig, + ) + + if err != nil { + t.Fatalf("Run failed with error. Expected: Actual: <%v>", err) } + adc := adcObj.(*attachDetachController) // Act - plugins := controllervolumetesting.CreateTestPlugin() - var prober volume.DynamicPluginProber = nil // TODO (#51147) inject mock + informerFactory.Start(ctx.Done()) + informerFactory.WaitForCacheSync(ctx.Done()) - if err := adc.volumePluginMgr.InitPlugins(plugins, prober, adc); err != nil { - t.Fatalf("Could not initialize volume plugins for Attach/Detach Controller: %+v", err) - } - - adc.actualStateOfWorld = cache.NewActualStateOfWorld(&adc.volumePluginMgr) - adc.desiredStateOfWorld = cache.NewDesiredStateOfWorld(&adc.volumePluginMgr) - - logger, _ := ktesting.NewTestContext(t) - err := adc.populateActualStateOfWorld(logger) + err = adc.populateActualStateOfWorld(logger) if err != nil { t.Fatalf("Run failed with error. Expected: Actual: <%v>", err) }