diff --git a/pkg/controller/build/ocl_metrics_test.go b/pkg/controller/build/ocl_metrics_test.go index 70c28d95d7..05fbc64220 100644 --- a/pkg/controller/build/ocl_metrics_test.go +++ b/pkg/controller/build/ocl_metrics_test.go @@ -14,14 +14,27 @@ func resetGaugeVec(g *prometheus.GaugeVec, labels prometheus.Labels) { g.DeletePartialMatch(labels) } +// clearPushStartTimes removes all entries from the package-level pushStartTimes +// sync.Map so that stale entries do not accumulate across -count iterations. +func clearPushStartTimes(t *testing.T) { + t.Helper() + pushStartTimes.Range(func(key, value any) bool { + pushStartTimes.Delete(key) + return true + }) +} + func TestRecordImagePushStarted(t *testing.T) { t.Parallel() - resetGaugeVec(oclImagePushState, prometheus.Labels{"pool": "worker"}) + pool := "push-started-test-pool" + t.Cleanup(func() { clearPushStartTimes(t) }) - RecordImagePushStarted("worker") + resetGaugeVec(oclImagePushState, prometheus.Labels{"pool": pool}) + + RecordImagePushStarted(pool) - v := testutil.ToFloat64(oclImagePushState.WithLabelValues("worker", StatePushing)) + v := testutil.ToFloat64(oclImagePushState.WithLabelValues(pool, StatePushing)) if v != 1 { t.Errorf("expected ocl_image_push_state{state=%q} = 1, got %v", StatePushing, v) } @@ -30,6 +43,7 @@ func TestRecordImagePushStarted(t *testing.T) { func TestRecordImagePushCompleted(t *testing.T) { t.Parallel() + t.Cleanup(func() { clearPushStartTimes(t) }) resetGaugeVec(oclImagePushState, prometheus.Labels{"pool": "worker2"}) RecordImagePushStarted("worker2") @@ -50,6 +64,7 @@ func TestRecordImagePushCompleted(t *testing.T) { func TestRecordImagePushFailed(t *testing.T) { t.Parallel() + t.Cleanup(func() { clearPushStartTimes(t) }) resetGaugeVec(oclImagePushState, prometheus.Labels{"pool": "worker3"}) RecordImagePushStarted("worker3") @@ -179,6 +194,7 @@ func TestBuildQueueDuration(t *testing.T) { func TestImagePushDurationRecorded(t *testing.T) { t.Parallel() + t.Cleanup(func() { clearPushStartTimes(t) }) pool := "push-duration-pool" before := testutil.CollectAndCount(oclImagePushDuration) @@ -195,6 +211,7 @@ func TestImagePushDurationRecorded(t *testing.T) { func TestImagePushDurationOnFailure(t *testing.T) { t.Parallel() + t.Cleanup(func() { clearPushStartTimes(t) }) pool := "push-duration-fail-pool" before := testutil.CollectAndCount(oclImagePushDuration) diff --git a/pkg/controller/build/osbuildcontroller_test.go b/pkg/controller/build/osbuildcontroller_test.go index 46bc87f27f..90045b275e 100644 --- a/pkg/controller/build/osbuildcontroller_test.go +++ b/pkg/controller/build/osbuildcontroller_test.go @@ -74,13 +74,13 @@ func TestOSBuildControllerDoesNothing(t *testing.T) { // rendered MachineConfig is detected on the associated MachineConfigPool. func TestOSBuildControllerDeletesRunningBuildBeforeStartingANewOne(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) - t.Cleanup(cancel) - poolName := "worker" t.Run("MachineOSConfig change", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + t.Cleanup(cancel) + kubeclient, mcfgclient, _, _, mosc, initialMosb, mcp, kubeassert, lobj, _ := setupOSBuildControllerForTestWithRunningBuild(ctx, t, poolName) // Now that the build is in the running state, we update the MachineOSConfig. @@ -117,6 +117,9 @@ func TestOSBuildControllerDeletesRunningBuildBeforeStartingANewOne(t *testing.T) t.Run("MachineConfig change", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + t.Cleanup(cancel) + _, mcfgclient, _, _, mosc, initialMosb, mcp, kubeassert, _, _ := setupOSBuildControllerForTestWithRunningBuild(ctx, t, poolName) apiMCP, apiMC := insertNewRenderedMachineConfigAndUpdatePool(ctx, t, mcfgclient, mosc.Spec.MachineConfigPool.Name, "rendered-worker-2") @@ -219,13 +222,13 @@ func TestOSBuildControllerLeavesSuccessfulBuildAlone(t *testing.T) { // MachineConfigPool. func TestOSBuildControllerFailure(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) - t.Cleanup(cancel) - poolName := "worker" t.Run("Failed build objects remain", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + t.Cleanup(cancel) + _, _, _, _, _, failedMosb, _, kubeassert, _ := setupOSBuildControllerForTestWithFailedBuild(ctx, t, poolName) // Ensure that even after failure, the build objects remain. @@ -234,6 +237,9 @@ func TestOSBuildControllerFailure(t *testing.T) { t.Run("MachineOSConfig change clears failed build", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + t.Cleanup(cancel) + kubeclient, mcfgclient, _, _, mosc, failedMosb, mcp, kubeassert, lobj := setupOSBuildControllerForTestWithFailedBuild(ctx, t, poolName) // Modify the MachineOSConfig to start a new build. @@ -262,6 +268,9 @@ func TestOSBuildControllerFailure(t *testing.T) { t.Run("MachineConfig change clears failed build", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + t.Cleanup(cancel) + _, mcfgclient, _, _, mosc, failedMosb, mcp, kubeassert, _ := setupOSBuildControllerForTestWithFailedBuild(ctx, t, poolName) apiMCP, apiMC := insertNewRenderedMachineConfigAndUpdatePool(ctx, t, mcfgclient, mosc.Spec.MachineConfigPool.Name, "rendered-worker-2") @@ -294,9 +303,6 @@ func TestOSBuildControllerFailure(t *testing.T) { func TestOSBuildController(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*25) - t.Cleanup(cancel) - poolName := "worker" getConfigNameForPool := func(num int) string { @@ -305,6 +311,9 @@ func TestOSBuildController(t *testing.T) { t.Run("MachineOSConfig changes creates a new MachineOSBuild", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*25) + t.Cleanup(cancel) + kubeclient, mcfgclient, _, _, mosc, _, _, lobj, kubeassert := setupOSBuildControllerForTestWithSuccessfulBuild(ctx, t, poolName) // Update the BuildInputs section on the MachineOSConfig and verify that a @@ -351,6 +360,9 @@ func TestOSBuildController(t *testing.T) { t.Run("MachineConfig changes creates a new MachineOSBuild", func(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*25) + t.Cleanup(cancel) + kubeclient, mcfgclient, _, _, mosc, _, mcp, _, kubeassert := setupOSBuildControllerForTestWithSuccessfulBuild(ctx, t, poolName) // Update the rendered MachineConfig on the MachineConfigPool and verify that a new MachineOSBuild is produced. We'll do this 10 times. @@ -516,8 +528,6 @@ func TestOSBuildControllerReconcilesMachineConfigPoolsAfterRestart(t *testing.T) // performs all of the setup steps and creates a successful Job before starting // the controller. func TestOSBuildControllerReconcilesJobsAfterRestart(t *testing.T) { - mainCtx, mainCancel := context.WithTimeout(context.Background(), time.Second*5) - t.Cleanup(mainCancel) testCases := []struct { name string @@ -556,7 +566,7 @@ func TestOSBuildControllerReconcilesJobsAfterRestart(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - ctx, cancel := context.WithCancel(mainCtx) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) t.Cleanup(cancel) poolName := "worker"