Skip to content

Commit 1e0ac4c

Browse files
committed
Adds checks for deletion on cleaned objects
Signed-off-by: Brett Tofel <[email protected]>
1 parent bfad24a commit 1e0ac4c

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

Diff for: test/e2e/metrics_test.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package e2e
1616
import (
1717
"bytes"
1818
"context"
19+
"fmt"
1920
"io"
2021
"os/exec"
2122
"strings"
@@ -161,18 +162,40 @@ func (c *MetricsTestConfig) validate(token string) {
161162
// cleanup removes the created resources. Uses a context with timeout to prevent hangs.
162163
func (c *MetricsTestConfig) cleanup() {
163164
c.t.Log("Cleaning up resources")
164-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
165+
_ = exec.Command(c.client, "delete", "clusterrolebinding", c.clusterBinding, "--ignore-not-found=true").Run()
166+
_ = exec.Command(c.client, "delete", "pod", c.curlPodName, "-n", c.namespace, "--ignore-not-found=true").Run()
167+
168+
// Create a context with a 60-second timeout.
169+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
165170
defer cancel()
166171

167-
cmd := exec.CommandContext(ctx, c.client, "delete", "clusterrolebinding", c.clusterBinding, "--ignore-not-found=true")
168-
if output, err := cmd.CombinedOutput(); err != nil {
169-
c.t.Logf("Error cleaning up clusterrolebinding: %s", string(output))
172+
// Wait for the ClusterRoleBinding to be deleted.
173+
if err := waitForDeletion(ctx, c.client, "clusterrolebinding", c.clusterBinding); err != nil {
174+
c.t.Logf("Error waiting for clusterrolebinding deletion: %v", err)
175+
} else {
176+
c.t.Log("ClusterRoleBinding deleted")
170177
}
171178

172-
cmd = exec.CommandContext(ctx, c.client, "delete", "pod", c.curlPodName, "-n", c.namespace, "--ignore-not-found=true")
173-
if output, err := cmd.CombinedOutput(); err != nil {
174-
c.t.Logf("Error cleaning up pod: %s", string(output))
179+
// Wait for the Pod to be deleted.
180+
if err := waitForDeletion(ctx, c.client, "pod", c.curlPodName, "-n", c.namespace); err != nil {
181+
c.t.Logf("Error waiting for pod deletion: %v", err)
182+
} else {
183+
c.t.Log("Pod deleted")
184+
}
185+
}
186+
187+
// waitForDeletion uses "kubectl wait" to block until the specified resource is deleted
188+
// or until the 30-second timeout is reached.
189+
func waitForDeletion(ctx context.Context, client, resourceType, resourceName string, extraArgs ...string) error {
190+
args := []string{"wait", "--for=delete", resourceType, resourceName}
191+
args = append(args, extraArgs...)
192+
args = append(args, "--timeout=30s")
193+
cmd := exec.CommandContext(ctx, client, args...)
194+
output, err := cmd.CombinedOutput()
195+
if err != nil {
196+
return fmt.Errorf("error waiting for deletion of %s %s: %v, output: %s", resourceType, resourceName, err, string(output))
175197
}
198+
return nil
176199
}
177200

178201
// getComponentNamespace returns the namespace where operator-controller or catalogd is running

0 commit comments

Comments
 (0)