Skip to content

Commit

Permalink
Add DeleteAndWait function for ClusterClass flaky test
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <kmuldoon@vmware.com>
  • Loading branch information
killianmuldoon committed Nov 22, 2022
1 parent 0ba4a7a commit 62ed2dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions internal/test/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ func (e *Environment) CreateAndWait(ctx context.Context, obj client.Object, opts
return nil
}

// DeleteAndWait deletes the given object and waits for it to be not available from the API Server.
func (e *Environment) DeleteAndWait(ctx context.Context, obj client.Object) error {
err := e.Client.Delete(ctx, obj)
if !apierrors.IsNotFound(err) {
return err
}
oCopy := obj.DeepCopyObject().(client.Object)
key := client.ObjectKeyFromObject(obj)
err = wait.ExponentialBackoff(
cacheSyncBackoff,
func() (done bool, err error) {
if err := e.Get(ctx, key, oCopy); err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
})
return err
}

// 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
2 changes: 1 addition & 1 deletion internal/webhooks/test/clusterclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func TestClusterClassWebhook_Delete_MultipleExistingClusters(t *testing.T) {
defer func() {
// Delete each of the clusters.
for _, c := range clusters {
g.Expect(env.Delete(ctx, c)).To(Succeed())
g.Expect(env.DeleteAndWait(ctx, c)).To(Succeed())
}

// Delete the ClusterClasses in the API server.
Expand Down

0 comments on commit 62ed2dc

Please sign in to comment.