Skip to content

Commit

Permalink
Merge pull request #436 from googs1025/fix_teardown
Browse files Browse the repository at this point in the history
add teardown when TestExecPod finished
  • Loading branch information
k8s-ci-robot authored Jul 15, 2024
2 parents a8e494e + e8db393 commit 950e125
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions examples/multi_cluster/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

var curDir, _ = os.Getwd()

func checkPodStatus(t *testing.T, kubeConfig string, clusterName string) {
func checkDeploymentStatus(t *testing.T, kubeConfig string, clusterName string) {
t.Helper()
client, err := klient.NewWithKubeConfigFile(kubeConfig)
if err != nil {
Expand Down Expand Up @@ -79,15 +79,15 @@ func TestScenarioOne(t *testing.T) {
if !ok {
t.Fatalf("Failed to extract kind cluster %s from context", clusterNames[0])
}
checkPodStatus(t, cluster.GetKubeconfig(), clusterNames[0])
checkDeploymentStatus(t, cluster.GetKubeconfig(), clusterNames[0])
return ctx
}).
Assess(fmt.Sprintf("Deployment is running successfully - %s", clusterNames[1]), func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
cluster, ok := envfuncs.GetClusterFromContext(ctx, clusterNames[1])
if !ok {
t.Fatalf("Failed to extract kind cluster %s from context", clusterNames[1])
}
checkPodStatus(t, cluster.GetKubeconfig(), clusterNames[1])
checkDeploymentStatus(t, cluster.GetKubeconfig(), clusterNames[1])
return ctx
}).
Feature()
Expand Down
13 changes: 10 additions & 3 deletions examples/pod_exec/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestExecPod(t *testing.T) {
t.Fatal(err)
}

return ctx
return context.WithValue(ctx, "test-deployment", deployment)
}).
Assess("check connectivity to wikipedia.org main page", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
client, err := c.NewClient()
Expand All @@ -60,15 +60,15 @@ func TestExecPod(t *testing.T) {
}

pods := &corev1.PodList{}
err = client.Resources(c.Namespace()).List(context.TODO(), pods)
err = client.Resources(c.Namespace()).List(ctx, pods)
if err != nil || pods.Items == nil {
t.Error("error while getting pods", err)
}
var stdout, stderr bytes.Buffer
podName := pods.Items[0].Name
command := []string{"curl", "-I", "https://en.wikipedia.org/wiki/Main_Page"}

if err := client.Resources().ExecInPod(context.TODO(), c.Namespace(), podName, containerName, command, &stdout, &stderr); err != nil {
if err := client.Resources().ExecInPod(ctx, c.Namespace(), podName, containerName, command, &stdout, &stderr); err != nil {
t.Log(stderr.String())
t.Fatal(err)
}
Expand All @@ -78,6 +78,13 @@ func TestExecPod(t *testing.T) {
t.Fatal("Couldn't connect to en.wikipedia.org")
}
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
dep := ctx.Value("test-deployment").(*appsv1.Deployment)
if err := c.Client().Resources().Delete(ctx, dep); err != nil {
t.Fatal(err)
}
return ctx
}).Feature()
_ = testEnv.Test(t, feature)
}
Expand Down

0 comments on commit 950e125

Please sign in to comment.