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 <[email protected]>
  • Loading branch information
wainersm authored and stevenhorsman committed Dec 11, 2024
1 parent c76f2fa commit 2435e0e
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/cloud-api-adaptor/test/e2e/nginx_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import (
"sigs.k8s.io/e2e-framework/pkg/features"
)

const WAIT_NGINX_DEPLOYMENT_TIMEOUT = time.Second * 900
const (
FEATURE_SETUP_FAILED contextValueString = "WithSetupFailed"
WAIT_NGINX_DEPLOYMENT_TIMEOUT = time.Second * 900
)

type contextValueString string
type deploymentOption func(*appsv1.Deployment)

func WithReplicaCount(replicas int32) deploymentOption {
Expand Down Expand Up @@ -103,6 +107,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, FEATURE_SETUP_FAILED, false)
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
Expand All @@ -112,17 +117,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, FEATURE_SETUP_FAILED, true)
}
return ctx
}).
Assess("Access for nginx deployment test", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
if ctx.Value(FEATURE_SETUP_FAILED).(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 +184,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 +213,5 @@ func waitForNginxDeploymentAvailable(ctx context.Context, t *testing.T, client k
}
}
}
t.Fatal(err)
}
}

0 comments on commit 2435e0e

Please sign in to comment.