From a9dd6f5c02cb471c46fb569f200e092ca73ca1d1 Mon Sep 17 00:00:00 2001 From: Melissa Kam <3768460+mkam@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:59:44 -0600 Subject: [PATCH] Add a separate test for initializer retries (#20298) Separate test for initializer retries --- internal/controller/controller_test.go | 48 ++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go index 992e45f8e020..edcb57d1c0f2 100644 --- a/internal/controller/controller_test.go +++ b/internal/controller/controller_test.go @@ -50,8 +50,7 @@ func TestController_API(t *testing.T) { }) rec := newTestReconciler() - expectedInitAttempts := 2 // testing retries - init := newTestInitializer(expectedInitAttempts) + init := newTestInitializer(1) client := svctest.NewResourceServiceBuilder(). WithRegisterFns(demo.RegisterTypes). Run(t) @@ -80,11 +79,8 @@ func TestController_API(t *testing.T) { mgr.SetRaftLeader(true) go mgr.Run(testContext(t)) - t.Run("initialize", func(t *testing.T) { - for i := 0; i < expectedInitAttempts; i++ { - init.wait(t) - } - }) + // Wait for initialization to complete + init.wait(t) t.Run("managed resource type", func(t *testing.T) { res, err := demo.GenerateV2Artist() @@ -225,6 +221,44 @@ func TestController_API(t *testing.T) { }) } +func TestController_API_InitializeRetry(t *testing.T) { + t.Parallel() + + // Configure initializer to error initially in order to test retries + expectedInitAttempts := 2 + init := newTestInitializer(expectedInitAttempts) + + client := svctest.NewResourceServiceBuilder(). + WithRegisterFns(demo.RegisterTypes). + Run(t) + rec := newTestReconciler() + + ctrl := controller. + NewController("artist", pbdemov2.ArtistType). + WithBackoff(10*time.Millisecond, 100*time.Millisecond). + WithReconciler(rec). + WithInitializer(init) + + mgr := controller.NewManager(client, testutil.Logger(t)) + mgr.Register(ctrl) + mgr.SetRaftLeader(true) + go mgr.Run(testContext(t)) + + // Wait for initialization attempts to complete + for i := 0; i < expectedInitAttempts; i++ { + init.wait(t) + } + + // Create a resource and expect it to reconcile now that initialization is complete + res, err := demo.GenerateV2Artist() + require.NoError(t, err) + + _, err = client.Write(testContext(t), &pbresource.WriteRequest{Resource: res}) + require.NoError(t, err) + + rec.wait(t) +} + func waitForAtomicBoolValue(t testutil.TestingTB, actual *atomic.Bool, expected bool) { t.Helper() retry.Run(t, func(r *retry.R) {