Skip to content

Commit 3ed5f64

Browse files
authored
Merge pull request #8576 from chrischdi/fix-flakes-ma-ctrl-phases
🐛 internal/machine-controller fix flakes in phases test
2 parents 883d52c + dbe9988 commit 3ed5f64

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

internal/controllers/machine/machine_controller_phases_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ func TestReconcileMachinePhases(t *testing.T) {
213213

214214
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
215215
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
216+
// We have to subtract 2 seconds, because .status.lastUpdated does not contain miliseconds.
217+
preUpdate := time.Now().Add(-2 * time.Second)
216218
g.Expect(env.Create(ctx, machine)).To(Succeed())
217219

218220
// Set the LastUpdated to be able to verify it is updated when the phase changes
219221
modifiedMachine := machine.DeepCopy()
220-
lastUpdated := metav1.NewTime(time.Now().Add(-10 * time.Second))
221-
machine.Status.LastUpdated = &lastUpdated
222222
g.Expect(env.Status().Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
223223

224224
// Set bootstrap ready.
@@ -235,7 +235,7 @@ func TestReconcileMachinePhases(t *testing.T) {
235235
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseProvisioning))
236236
// Verify that the LastUpdated timestamp was updated
237237
g.Expect(machine.Status.LastUpdated).NotTo(BeNil())
238-
g.Expect(machine.Status.LastUpdated.After(lastUpdated.Time)).To(BeTrue())
238+
g.Expect(machine.Status.LastUpdated.After(preUpdate)).To(BeTrue())
239239
return true
240240
}, 10*time.Second).Should(BeTrue())
241241
})
@@ -281,14 +281,13 @@ func TestReconcileMachinePhases(t *testing.T) {
281281

282282
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
283283
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
284+
// We have to subtract 2 seconds, because .status.lastUpdated does not contain miliseconds.
285+
preUpdate := time.Now().Add(-2 * time.Second)
284286
g.Expect(env.Create(ctx, machine)).To(Succeed())
285287

286288
modifiedMachine := machine.DeepCopy()
287289
// Set NodeRef.
288290
machine.Status.NodeRef = &corev1.ObjectReference{Kind: "Node", Name: node.Name}
289-
// Set the LastUpdated to be able to verify it is updated when the phase changes
290-
lastUpdated := metav1.NewTime(time.Now().Add(-10 * time.Second))
291-
machine.Status.LastUpdated = &lastUpdated
292291
g.Expect(env.Status().Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
293292

294293
// Set bootstrap ready.
@@ -322,7 +321,7 @@ func TestReconcileMachinePhases(t *testing.T) {
322321
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning))
323322
// Verify that the LastUpdated timestamp was updated
324323
g.Expect(machine.Status.LastUpdated).NotTo(BeNil())
325-
g.Expect(machine.Status.LastUpdated.After(lastUpdated.Time)).To(BeTrue())
324+
g.Expect(machine.Status.LastUpdated.After(preUpdate)).To(BeTrue())
326325
return true
327326
}, 10*time.Second).Should(BeTrue())
328327
})
@@ -441,14 +440,13 @@ func TestReconcileMachinePhases(t *testing.T) {
441440

442441
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
443442
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
443+
// We have to subtract 2 seconds, because .status.lastUpdated does not contain miliseconds.
444+
preUpdate := time.Now().Add(-2 * time.Second)
444445
g.Expect(env.Create(ctx, machine)).To(Succeed())
445446

446447
modifiedMachine := machine.DeepCopy()
447448
// Set NodeRef.
448449
machine.Status.NodeRef = &corev1.ObjectReference{Kind: "Node", Name: node.Name}
449-
// Set the LastUpdated to be able to verify it is updated when the phase changes
450-
lastUpdated := metav1.NewTime(time.Now().Add(-10 * time.Second))
451-
machine.Status.LastUpdated = &lastUpdated
452450
g.Expect(env.Status().Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
453451

454452
// Set bootstrap ready.
@@ -470,7 +468,7 @@ func TestReconcileMachinePhases(t *testing.T) {
470468
g.Expect(machine.Status.GetTypedPhase()).To(Equal(clusterv1.MachinePhaseRunning))
471469
// Verify that the LastUpdated timestamp was updated
472470
g.Expect(machine.Status.LastUpdated).NotTo(BeNil())
473-
g.Expect(machine.Status.LastUpdated.After(lastUpdated.Time)).To(BeTrue())
471+
g.Expect(machine.Status.LastUpdated.After(preUpdate)).To(BeTrue())
474472
return true
475473
}, 10*time.Second).Should(BeTrue())
476474
})
@@ -573,6 +571,8 @@ func TestReconcileMachinePhases(t *testing.T) {
573571

574572
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
575573
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
574+
// We have to subtract 2 seconds, because .status.lastUpdated does not contain miliseconds.
575+
preUpdate := time.Now().Add(-2 * time.Second)
576576
g.Expect(env.Create(ctx, machine)).To(Succeed())
577577

578578
// Set bootstrap ready.
@@ -597,9 +597,6 @@ func TestReconcileMachinePhases(t *testing.T) {
597597
modifiedMachine := machine.DeepCopy()
598598
// Set NodeRef.
599599
machine.Status.NodeRef = &corev1.ObjectReference{Kind: "Node", Name: node.Name}
600-
// Set the LastUpdated to be able to verify it is updated when the phase changes
601-
lastUpdated := metav1.NewTime(time.Now().Add(-10 * time.Second))
602-
machine.Status.LastUpdated = &lastUpdated
603600
g.Expect(env.Status().Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
604601

605602
modifiedMachine = machine.DeepCopy()
@@ -621,7 +618,7 @@ func TestReconcileMachinePhases(t *testing.T) {
621618
g.Expect(nodeHealthyCondition.Reason).To(Equal(clusterv1.DeletingReason))
622619
// Verify that the LastUpdated timestamp was updated
623620
g.Expect(machine.Status.LastUpdated).NotTo(BeNil())
624-
g.Expect(machine.Status.LastUpdated.After(lastUpdated.Time)).To(BeTrue())
621+
g.Expect(machine.Status.LastUpdated.After(preUpdate)).To(BeTrue())
625622
return true
626623
}, 10*time.Second).Should(BeTrue())
627624
})

0 commit comments

Comments
 (0)