Skip to content

🐛 Add checks to catch race-driven panics in metrics endpoint e2e #1795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
41 changes: 38 additions & 3 deletions test/e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ package e2e

import (
"bytes"
"context"
"fmt"
"io"
"os/exec"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -156,11 +159,43 @@ func (c *MetricsTestConfig) validate(token string) {
require.Contains(c.t, string(output), "200 OK", "Metrics endpoint did not return 200 OK")
}

// cleanup created resources
// cleanup removes the created resources. Uses a context with timeout to prevent hangs.
func (c *MetricsTestConfig) cleanup() {
c.t.Log("Cleaning up resources")
_ = exec.Command(c.client, "delete", "clusterrolebinding", c.clusterBinding, "--ignore-not-found=true").Run()
_ = exec.Command(c.client, "delete", "pod", c.curlPodName, "-n", c.namespace, "--ignore-not-found=true").Run()
_ = exec.Command(c.client, "delete", "clusterrolebinding", c.clusterBinding, "--ignore-not-found=true", "--force").Run()
_ = exec.Command(c.client, "delete", "pod", c.curlPodName, "-n", c.namespace, "--ignore-not-found=true", "--force").Run()

// Create a context with a 60-second timeout.
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

// Wait for the ClusterRoleBinding to be deleted.
if err := waitForDeletion(ctx, c.client, "clusterrolebinding", c.clusterBinding); err != nil {
c.t.Logf("Error waiting for clusterrolebinding deletion: %v", err)
} else {
c.t.Log("ClusterRoleBinding deleted")
}

// Wait for the Pod to be deleted.
if err := waitForDeletion(ctx, c.client, "pod", c.curlPodName, "-n", c.namespace); err != nil {
c.t.Logf("Error waiting for pod deletion: %v", err)
} else {
c.t.Log("Pod deleted")
}
Comment on lines +172 to +184
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to even wait for these to be deleted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, good point.
But I am betting that is the fix.
In the scenarios where it fails, because the Pod get stuck, we are now just logging in instead of failing.

Copy link
Contributor Author

@bentito bentito Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear: I have a pretty quick test ((takes about ~50m) -- run e2e 10 times. This test always fails on my local box when run on main, at least one fail out of 10, usually 3-5 fails. With the changes on this PR, that same test passes. I think @camilamacedo86 is right, the change in behavior is key to the fix, that we log instead of fail. I guess I could try not waiting, but I'd rather move on as something is fixing the flake.

}

// waitForDeletion uses "kubectl wait" to block until the specified resource is deleted
// or until the 60-second timeout is reached.
func waitForDeletion(ctx context.Context, client, resourceType, resourceName string, extraArgs ...string) error {
args := []string{"wait", "--for=delete", resourceType, resourceName}
args = append(args, extraArgs...)
args = append(args, "--timeout=60s")
cmd := exec.CommandContext(ctx, client, args...)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error waiting for deletion of %s %s: %v, output: %s", resourceType, resourceName, err, string(output))
}
return nil
}

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