Skip to content

Commit 8e52cea

Browse files
committed
machine-controller: fix phase tests race condition in tests on lastUpdated field
The test patches the status of the Machine, while the reconciliation may be already happening. This leads to inconsistent state which is not deterministic and into the Machine's status to never reach the targeted state.
1 parent 94f5468 commit 8e52cea

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

internal/controllers/machine/machine_controller_phases_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,13 @@ func TestReconcileMachinePhases(t *testing.T) {
367367

368368
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
369369
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
370+
// We have to subtract 2 seconds, because .status.lastUpdated does not contain miliseconds.
371+
preUpdate := time.Now().Add(-2 * time.Second)
370372
g.Expect(env.Create(ctx, machine)).To(Succeed())
371373

372374
modifiedMachine := machine.DeepCopy()
373375
// Set NodeRef.
374376
machine.Status.NodeRef = &corev1.ObjectReference{Kind: "Node", Name: node.Name}
375-
// Set the LastUpdated to be able to verify it is updated when the phase changes
376-
lastUpdated := metav1.NewTime(time.Now().Add(-10 * time.Second))
377-
machine.Status.LastUpdated = &lastUpdated
378377
g.Expect(env.Status().Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
379378

380379
// Set bootstrap ready.
@@ -397,7 +396,7 @@ func TestReconcileMachinePhases(t *testing.T) {
397396
g.Expect(machine.Status.Addresses).To(BeEmpty())
398397
// Verify that the LastUpdated timestamp was updated
399398
g.Expect(machine.Status.LastUpdated).NotTo(BeNil())
400-
g.Expect(machine.Status.LastUpdated.After(lastUpdated.Time)).To(BeTrue())
399+
g.Expect(machine.Status.LastUpdated.After(preUpdate)).To(BeTrue())
401400
return true
402401
}, 10*time.Second).Should(BeTrue())
403402
})
@@ -506,16 +505,10 @@ func TestReconcileMachinePhases(t *testing.T) {
506505

507506
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
508507
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
508+
// We have to subtract 2 seconds, because .status.lastUpdated does not contain miliseconds.
509+
preUpdate := time.Now().Add(-2 * time.Second)
509510
g.Expect(env.Create(ctx, machine)).To(Succeed())
510511

511-
modifiedMachine := machine.DeepCopy()
512-
// Set NodeRef to nil.
513-
machine.Status.NodeRef = nil
514-
// Set the LastUpdated to be able to verify it is updated when the phase changes
515-
lastUpdated := metav1.NewTime(time.Now().Add(-10 * time.Second))
516-
machine.Status.LastUpdated = &lastUpdated
517-
g.Expect(env.Status().Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
518-
519512
// Set bootstrap ready.
520513
modifiedBootstrapConfig := bootstrapConfig.DeepCopy()
521514
g.Expect(unstructured.SetNestedField(modifiedBootstrapConfig.Object, true, "status", "ready")).To(Succeed())
@@ -535,7 +528,7 @@ func TestReconcileMachinePhases(t *testing.T) {
535528
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioned))
536529
// Verify that the LastUpdated timestamp was updated
537530
g.Expect(machine.Status.LastUpdated).NotTo(BeNil())
538-
g.Expect(machine.Status.LastUpdated.After(lastUpdated.Time)).To(BeTrue())
531+
g.Expect(machine.Status.LastUpdated.After(preUpdate)).To(BeTrue())
539532
return true
540533
}, 10*time.Second).Should(BeTrue())
541534
})

0 commit comments

Comments
 (0)