Skip to content

Commit b60e775

Browse files
committed
test/machinehealthcheck: fix flaky TestReconcile/machine_with_node_likely_to_go_unhealthy
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.
1 parent 4566a9a commit b60e775

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type testCase struct {
5353
expectedStatus *machinev1.MachineHealthCheckStatus
5454
externalRemediationMachine *unstructured.Unstructured
5555
externalRemediationTemplate *unstructured.Unstructured
56+
// setup is called immediately before the reconcile to allow time-sensitive
57+
// test cases to refresh state (e.g. LastTransitionTime) right before execution.
58+
setup func()
5659
}
5760

5861
type expectedReconcile struct {
@@ -325,6 +328,9 @@ func TestReconcile(t *testing.T) {
325328
machine: machineWithNodeRecentlyUnhealthy,
326329
node: nodeRecentlyUnhealthy,
327330
mhc: machineHealthCheck,
331+
setup: func() {
332+
nodeRecentlyUnhealthy.Status.Conditions[0].LastTransitionTime = metav1.Time{Time: time.Now()}
333+
},
328334
expected: expectedReconcile{
329335
result: reconcile.Result{
330336
RequeueAfter: 300 * time.Second,
@@ -488,6 +494,9 @@ func TestReconcile(t *testing.T) {
488494

489495
for _, tc := range testCases {
490496
t.Run(tc.name, func(t *testing.T) {
497+
if tc.setup != nil {
498+
tc.setup()
499+
}
491500
recorder := record.NewFakeRecorder(2)
492501
r := newFakeReconcilerWithCustomRecorder(recorder, buildRunTimeObjects(tc)...)
493502
assertBaseReconcile(t, tc, ctx, r)

0 commit comments

Comments
 (0)