Skip to content

Commit

Permalink
test/e2e: ensure Teardown run on failure (DoTestNginxDeployment)
Browse files Browse the repository at this point in the history
The teardown function doesn't run if WithSetup fail, which might
leave the deployment on the cluster and it might mess with next
tests.

This replaced some t.Fatal() with t.Error() so that the current
goroutine doesn't exit, in fact, the execution continues after
the t.Error() call but the test is marked failed. So I had to
proper return when t.Error().

On WithSetup() failing, it doesn't make sense to run the Assess()
and the only way to pass the status down to Assess() is through the
`ctx` variable (due to a limitation on the k8s e2e framework, the `t`
object is not shared).

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
  • Loading branch information
wainersm committed Dec 5, 2024
1 parent b131f0a commit a3749ee
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/cloud-api-adaptor/test/e2e/nginx_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func DoTestNginxDeployment(t *testing.T, testEnv env.Environment, assert CloudAs

nginxImageFeature := features.New("Nginx image deployment test").
WithSetup("Create nginx deployment", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
ctx = context.WithValue(ctx, "WithSetupFailed", false)

Check failure on line 106 in src/cloud-api-adaptor/test/e2e/nginx_deployment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
Expand All @@ -112,17 +113,28 @@ func DoTestNginxDeployment(t *testing.T, testEnv env.Environment, assert CloudAs
t.Fatal(err)
}
waitForNginxDeploymentAvailable(ctx, t, client, deployment, replicas)
t.Log("nginx deployment is available now")
if !t.Failed() {
t.Log("nginx deployment is available now")
} else {
ctx = context.WithValue(ctx, "WithSetupFailed", true)

Check failure on line 119 in src/cloud-api-adaptor/test/e2e/nginx_deployment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)
}
return ctx
}).
Assess("Access for nginx deployment test", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
if ctx.Value("WithSetupFailed").(bool) {
// Test setup failed, so skip this assess
t.Skip()
return ctx
}
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
t.Error(err)
return ctx
}
var podlist v1.PodList
if err := client.Resources(deployment.ObjectMeta.Namespace).List(ctx, &podlist); err != nil {
t.Fatal(err)
t.Error(err)
return ctx
}
for _, pod := range podlist.Items {
if pod.ObjectMeta.Labels["app"] == "nginx" {
Expand Down Expand Up @@ -168,8 +180,10 @@ func waitForNginxDeploymentAvailable(ctx context.Context, t *testing.T, client k
return deployObj.Status.AvailableReplicas == rc
}), wait.WithTimeout(WAIT_NGINX_DEPLOYMENT_TIMEOUT), wait.WithInterval(10*time.Second)); err != nil {
var podlist v1.PodList
t.Errorf("%v", err)
if err := client.Resources(deployment.ObjectMeta.Namespace).List(ctx, &podlist); err != nil {
t.Fatal(err)
t.Errorf("%v", err)
return
}
for _, pod := range podlist.Items {
if pod.ObjectMeta.Labels["app"] == "nginx" {
Expand All @@ -195,6 +209,5 @@ func waitForNginxDeploymentAvailable(ctx context.Context, t *testing.T, client k
}
}
}
t.Fatal(err)
}
}

0 comments on commit a3749ee

Please sign in to comment.