Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions internal/controllers/machine/machine_controller_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,8 @@ func TestReconcileMachinePhases(t *testing.T) {

g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
g.Expect(env.Create(ctx, machine)).To(Succeed())
// Create and wait on machine to make sure caches sync and reconciliation triggers.
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())

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

g.Expect(env.Create(ctx, bootstrapConfig)).To(Succeed())
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
g.Expect(env.Create(ctx, machine)).To(Succeed())
// Create and wait on machine to make sure caches sync and reconciliation triggers.
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())

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

// Set the LastUpdated to be able to verify it is updated when the phase changes
modifiedMachine := machine.DeepCopy()
Expand Down Expand Up @@ -2240,7 +2242,9 @@ func TestReconcileMachinePhases(t *testing.T) {
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
// We have to subtract 2 seconds, because .status.lastUpdated does not contain milliseconds.
preUpdate := time.Now().Add(-2 * time.Second)
g.Expect(env.Create(ctx, machine)).To(Succeed())

// Create and wait on machine to make sure caches sync and reconciliation triggers.
g.Expect(env.CreateAndWait(ctx, machine)).To(Succeed())

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

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

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

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

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

// Delete Machine
g.Expect(env.Delete(ctx, machine)).To(Succeed())
g.Expect(env.DeleteAndWait(ctx, machine)).To(Succeed())

// Wait until Machine was reconciled.
g.Eventually(func(g Gomega) bool {
Expand Down
31 changes: 31 additions & 0 deletions internal/test/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,37 @@ func (e *Environment) CreateAndWait(ctx context.Context, obj client.Object, opts
return nil
}

// DeleteAndWait deletes the given object and waits for the cache to be updated accordingly.
//
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.
func (e *Environment) DeleteAndWait(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
if err := e.Delete(ctx, obj, opts...); err != nil {
return err
}

// Makes sure the cache is updated with the new object
objCopy := obj.DeepCopyObject().(client.Object)
key := client.ObjectKeyFromObject(obj)
if err := wait.ExponentialBackoff(
cacheSyncBackoff,
func() (done bool, err error) {
if err := e.Get(ctx, key, objCopy); err != nil {
if apierrors.IsNotFound(err) {
// if not found possible no finalizer and delete just removed the object.
return true, nil
}
return false, err
}
if objCopy.GetDeletionTimestamp() != nil {
return true, nil
}
return false, nil
}); err != nil {
return errors.Wrapf(err, "object %s, %s is not being added to the testenv client cache", obj.GetObjectKind().GroupVersionKind().String(), key)
}
return nil
}

// PatchAndWait creates or updates the given object using server-side apply and waits for the cache to be updated accordingly.
//
// NOTE: Waiting for the cache to be updated helps in preventing test flakes due to the cache sync delays.
Expand Down
Loading