From f6ec0176b0cca7a5fa82416924e9416fca595cd8 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Wed, 1 Nov 2023 11:39:33 +0000 Subject: [PATCH] Reset the strategy registry on each run These conditions ... - the pipeline reconciler is constructed in TestMain; - the test setup appends the mock strategy to the reconciler's strategies; - the mock strategy operates on values in its closure; - the first strategy answering `true` to Handle(...) will be used; ... all together mean that when you run the test case more than once, it will end up running the mock strategy on apps that aren't used any more. We decided earlier that replacing the whole set of strategies broke encapsulation too much; but, I think it may be the simplest way to make the test repeatable. These are alternatives I considered: - construct the reconciler anew for each test case (drags in the manager, then other things ...); - have a `Reset` method for the strategies (I don't like exporting methods just for testing); - refer to the app values indirectly, e.g., by having the mock strategy refer to a func which is re-assigned, rather than inlining it (would have to ensure the mock is installed once, but the func is reassigned -- awkward) Signed-off-by: Michael Bridgen --- controllers/leveltriggered/promote_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/controllers/leveltriggered/promote_test.go b/controllers/leveltriggered/promote_test.go index 268b306..f1b1d75 100644 --- a/controllers/leveltriggered/promote_test.go +++ b/controllers/leveltriggered/promote_test.go @@ -37,7 +37,7 @@ func TestPromotionAlgorithm(t *testing.T) { prodApp := createApp(ctx, k8sClient, g, appName, prodNs.Name) setAppRevisionAndReadyStatus(ctx, g, prodApp, "v1.0.0") - mockStrategy := setStrategyRegistry(t, pipelineReconciler) + mockStrategy := installMockStrategy(t, pipelineReconciler) mockStrategy.EXPECT().Handles(gomock.Any()).Return(true).AnyTimes() mockStrategy.EXPECT(). @@ -226,7 +226,8 @@ func setAppStatusReadyCondition(ctx context.Context, g Gomega, hr *helmv2.HelmRe g.Expect(k8sClient.Status().Update(ctx, hr)).To(Succeed()) } -func setStrategyRegistry(t *testing.T, r *PipelineReconciler) *strategy.MockStrategy { +func installMockStrategy(t *testing.T, r *PipelineReconciler) *strategy.MockStrategy { + r.stratReg = strategy.StrategyRegistry{} mockCtrl := gomock.NewController(t) mockStrategy := strategy.NewMockStrategy(mockCtrl)