Skip to content

Commit 37a5453

Browse files
committed
Use apierrors, move deferred cleanup, less nesting
Signed-off-by: Brett Tofel <[email protected]>
1 parent c6c9b21 commit 37a5453

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

test/e2e/metrics_test.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
authenticationv1 "k8s.io/api/authentication/v1"
2626
corev1 "k8s.io/api/core/v1"
2727
rbacv1 "k8s.io/api/rbac/v1"
28+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/util/wait"
3031
"k8s.io/client-go/kubernetes"
@@ -73,14 +74,10 @@ func TestCatalogdMetricsExportedEndpoint(t *testing.T) {
7374

7475
func findK8sClient(t *testing.T) (kubernetes.Interface, *rest.Config) {
7576
cfg, err := config.GetConfig()
76-
if err != nil {
77-
t.Fatalf("Failed to get Kubernetes config: %v", err)
78-
}
77+
require.NoError(t, err, "Failed to get Kubernetes config")
7978

8079
clientset, err := kubernetes.NewForConfig(cfg)
81-
if err != nil {
82-
t.Fatalf("Failed to create client from config: %v", err)
83-
}
80+
require.NoError(t, err, "Failed to create client from config")
8481

8582
t.Log("Successfully created Kubernetes client via controller-runtime config")
8683
return clientset, cfg
@@ -133,13 +130,13 @@ func NewMetricsTestConfig(
133130
// run executes the entire test flow
134131
func (c *MetricsTestConfig) run() {
135132
ctx := context.Background()
133+
defer c.cleanup(ctx)
136134
c.createMetricsClusterRoleBinding(ctx)
137135
token := c.getServiceAccountToken(ctx)
138136
c.createCurlMetricsPod(ctx)
139137
c.waitForPodReady(ctx)
140138
// Exec `curl` in the Pod to validate the metrics
141139
c.validateMetricsEndpoint(ctx, token)
142-
defer c.cleanup(ctx)
143140
}
144141

145142
// createMetricsClusterRoleBinding to bind the cluster role so metrics are accessible
@@ -242,13 +239,10 @@ func (c *MetricsTestConfig) waitForPodReady(ctx context.Context) {
242239
}
243240
return false, nil
244241
})
245-
if err != nil {
246-
// If the context timed out, the test should fail with a more direct message
247-
if errors.Is(err, context.DeadlineExceeded) {
248-
c.t.Fatal("Timed out waiting for the curl pod to become Ready")
249-
}
250-
require.NoError(c.t, err, "Error waiting for curl pod to become Ready")
242+
if errors.Is(err, context.DeadlineExceeded) {
243+
c.t.Fatal("Timed out waiting for the curl pod to become Ready")
251244
}
245+
require.NoError(c.t, err, "Error waiting for curl pod to become Ready")
252246
}
253247

254248
// validateMetricsEndpoint performs `kubectl exec ... curl <metricsURL>` logic
@@ -323,7 +317,7 @@ func waitForClusterRoleBindingDeletion(ctx context.Context, t *testing.T, kubeCl
323317
err := wait.PollUntilContextTimeout(ctx, 2*time.Second, 60*time.Second, false, func(ctx context.Context) (bool, error) {
324318
_, err := kubeClient.RbacV1().ClusterRoleBindings().Get(ctx, name, metav1.GetOptions{})
325319
if err != nil {
326-
if strings.Contains(err.Error(), "not found") {
320+
if apierrors.IsNotFound(err) {
327321
return true, nil
328322
}
329323
return false, err
@@ -345,8 +339,7 @@ func waitForPodDeletion(ctx context.Context, t *testing.T, kubeClient kubernetes
345339
err := wait.PollUntilContextTimeout(ctx, 2*time.Second, 90*time.Second, false, func(ctx context.Context) (bool, error) {
346340
pod, getErr := kubeClient.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
347341
if getErr != nil {
348-
// The standard "not found" check
349-
if strings.Contains(getErr.Error(), "not found") {
342+
if apierrors.IsNotFound(getErr) {
350343
return true, nil
351344
}
352345
return false, getErr

0 commit comments

Comments
 (0)