From b60e77527dab5ce07320183463e724e59a82cfee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Ma=C5=88=C3=A1k?= Date: Thu, 19 Mar 2026 12:54:52 +0100 Subject: [PATCH] test/machinehealthcheck: fix flaky TestReconcile/machine_with_node_likely_to_go_unhealthy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test set LastTransitionTime = time.Now() once at TestReconcile init, then ran four other test cases before the timing-sensitive one. If those cases took >2s total, durationUnhealthy exceeded the ±1s tolerance window and the test failed intermittently. Fix by resetting LastTransitionTime immediately before buildRunTimeObjects is called for that specific test case, ensuring durationUnhealthy ≈ 0 at reconcile time. --- .../machinehealthcheck_controller_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go b/pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go index 58eb58c24f..66e11c0e54 100644 --- a/pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go +++ b/pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go @@ -53,6 +53,9 @@ type testCase struct { expectedStatus *machinev1.MachineHealthCheckStatus externalRemediationMachine *unstructured.Unstructured externalRemediationTemplate *unstructured.Unstructured + // setup is called immediately before the reconcile to allow time-sensitive + // test cases to refresh state (e.g. LastTransitionTime) right before execution. + setup func() } type expectedReconcile struct { @@ -325,6 +328,9 @@ func TestReconcile(t *testing.T) { machine: machineWithNodeRecentlyUnhealthy, node: nodeRecentlyUnhealthy, mhc: machineHealthCheck, + setup: func() { + nodeRecentlyUnhealthy.Status.Conditions[0].LastTransitionTime = metav1.Time{Time: time.Now()} + }, expected: expectedReconcile{ result: reconcile.Result{ RequeueAfter: 300 * time.Second, @@ -488,6 +494,9 @@ func TestReconcile(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { + if tc.setup != nil { + tc.setup() + } recorder := record.NewFakeRecorder(2) r := newFakeReconcilerWithCustomRecorder(recorder, buildRunTimeObjects(tc)...) assertBaseReconcile(t, tc, ctx, r)