Skip to content

Commit

Permalink
Reset the strategy registry on each run
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
squaremo committed Nov 7, 2023
1 parent 504116a commit 8ae22fc
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions controllers/leveltriggered/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 8ae22fc

Please sign in to comment.