Skip to content

Commit 0105765

Browse files
authored
Merge pull request #12818 from ivelichkovich/flakecreateandwait
🌱 Fix TestReconcileMachinePhases flake
2 parents a5f35db + 852622e commit 0105765

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

internal/controllers/machine/machine_controller_status_test.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,8 @@ func TestReconcileMachinePhases(t *testing.T) {
20712071

20722072
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
20732073
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
2074-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2074+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2075+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
20752076

20762077
// Wait until BootstrapConfig has the ownerReference.
20772078
g.Eventually(func(g Gomega) bool {
@@ -2123,7 +2124,8 @@ func TestReconcileMachinePhases(t *testing.T) {
21232124

21242125
g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
21252126
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
2126-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2127+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2128+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
21272129

21282130
// Wait until Machine was reconciled.
21292131
g.Eventually(func(g Gomega) bool {
@@ -2168,7 +2170,7 @@ func TestReconcileMachinePhases(t *testing.T) {
21682170
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
21692171
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
21702172
preUpdate := time.Now().Add(-2 * time.Second)
2171-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2173+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
21722174

21732175
// Set the LastUpdated to be able to verify it is updated when the phase changes
21742176
modifiedMachine := machine.DeepCopy()
@@ -2240,7 +2242,9 @@ func TestReconcileMachinePhases(t *testing.T) {
22402242
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
22412243
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
22422244
preUpdate := time.Now().Add(-2 * time.Second)
2243-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2245+
2246+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2247+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
22442248

22452249
modifiedMachine := machine.DeepCopy()
22462250
// Set NodeRef.
@@ -2329,7 +2333,8 @@ func TestReconcileMachinePhases(t *testing.T) {
23292333
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
23302334
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
23312335
preUpdate := time.Now().Add(-2 * time.Second)
2332-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2336+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2337+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
23332338

23342339
modifiedMachine := machine.DeepCopy()
23352340
// Set NodeRef.
@@ -2407,7 +2412,8 @@ func TestReconcileMachinePhases(t *testing.T) {
24072412
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
24082413
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
24092414
preUpdate := time.Now().Add(-2 * time.Second)
2410-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2415+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2416+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
24112417

24122418
modifiedMachine := machine.DeepCopy()
24132419
// Set NodeRef.
@@ -2474,7 +2480,8 @@ func TestReconcileMachinePhases(t *testing.T) {
24742480
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
24752481
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
24762482
preUpdate := time.Now().Add(-2 * time.Second)
2477-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2483+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2484+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
24782485

24792486
// Set bootstrap ready.
24802487
modifiedBootstrapConfig := bootstrapConfig.DeepCopy()
@@ -2546,7 +2553,8 @@ func TestReconcileMachinePhases(t *testing.T) {
25462553
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
25472554
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
25482555
preUpdate := time.Now().Add(-2 * time.Second)
2549-
g.Expect(env.Create(ctx, machine)).To(Succeed())
2556+
// Create and wait on machine to make sure caches sync and reconciliation triggers.
2557+
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())
25502558

25512559
// Set bootstrap ready.
25522560
modifiedBootstrapConfig := bootstrapConfig.DeepCopy()
@@ -2578,7 +2586,7 @@ func TestReconcileMachinePhases(t *testing.T) {
25782586
g.Expect(env.Patch(ctx, modifiedMachine, client.MergeFrom(machine))).To(Succeed())
25792587

25802588
// Delete Machine
2581-
g.Expect(env.Delete(ctx, machine)).To(Succeed())
2589+
g.Expect(env.DeleteAndWait(ctx, machine)).To(Succeed())
25822590

25832591
// Wait until Machine was reconciled.
25842592
g.Eventually(func(g Gomega) bool {

internal/test/envtest/environment.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,37 @@ func (e *Environment) CreateAndWait(ctx context.Context, obj client.Object, opts
527527
return nil
528528
}
529529

530+
// DeleteAndWait deletes the given object and waits for the cache to be updated accordingly.
531+
//
532+
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.
533+
func (e *Environment) DeleteAndWait(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
534+
if err := e.Delete(ctx, obj, opts...); err != nil {
535+
return err
536+
}
537+
538+
// Makes sure the cache is updated with the new object
539+
objCopy := obj.DeepCopyObject().(client.Object)
540+
key := client.ObjectKeyFromObject(obj)
541+
if err := wait.ExponentialBackoff(
542+
cacheSyncBackoff,
543+
func() (done bool, err error) {
544+
if err := e.Get(ctx, key, objCopy); err != nil {
545+
if apierrors.IsNotFound(err) {
546+
// if not found possible no finalizer and delete just removed the object.
547+
return true, nil
548+
}
549+
return false, err
550+
}
551+
if objCopy.GetDeletionTimestamp() != nil {
552+
return true, nil
553+
}
554+
return false, nil
555+
}); err != nil {
556+
return errors.Wrapf(err, "object %s, %s is not being added to the testenv client cache", obj.GetObjectKind().GroupVersionKind().String(), key)
557+
}
558+
return nil
559+
}
560+
530561
// PatchAndWait creates or updates the given object using server-side apply and waits for the cache to be updated accordingly.
531562
//
532563
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.

0 commit comments

Comments
 (0)