Skip to content

Commit 950e125

Browse files
authored
Merge pull request #436 from googs1025/fix_teardown
add teardown when TestExecPod finished
2 parents a8e494e + e8db393 commit 950e125

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

examples/multi_cluster/deployment_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737

3838
var curDir, _ = os.Getwd()
3939

40-
func checkPodStatus(t *testing.T, kubeConfig string, clusterName string) {
40+
func checkDeploymentStatus(t *testing.T, kubeConfig string, clusterName string) {
4141
t.Helper()
4242
client, err := klient.NewWithKubeConfigFile(kubeConfig)
4343
if err != nil {
@@ -79,15 +79,15 @@ func TestScenarioOne(t *testing.T) {
7979
if !ok {
8080
t.Fatalf("Failed to extract kind cluster %s from context", clusterNames[0])
8181
}
82-
checkPodStatus(t, cluster.GetKubeconfig(), clusterNames[0])
82+
checkDeploymentStatus(t, cluster.GetKubeconfig(), clusterNames[0])
8383
return ctx
8484
}).
8585
Assess(fmt.Sprintf("Deployment is running successfully - %s", clusterNames[1]), func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
8686
cluster, ok := envfuncs.GetClusterFromContext(ctx, clusterNames[1])
8787
if !ok {
8888
t.Fatalf("Failed to extract kind cluster %s from context", clusterNames[1])
8989
}
90-
checkPodStatus(t, cluster.GetKubeconfig(), clusterNames[1])
90+
checkDeploymentStatus(t, cluster.GetKubeconfig(), clusterNames[1])
9191
return ctx
9292
}).
9393
Feature()

examples/pod_exec/envtest_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestExecPod(t *testing.T) {
5151
t.Fatal(err)
5252
}
5353

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

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

71-
if err := client.Resources().ExecInPod(context.TODO(), c.Namespace(), podName, containerName, command, &stdout, &stderr); err != nil {
71+
if err := client.Resources().ExecInPod(ctx, c.Namespace(), podName, containerName, command, &stdout, &stderr); err != nil {
7272
t.Log(stderr.String())
7373
t.Fatal(err)
7474
}
@@ -78,6 +78,13 @@ func TestExecPod(t *testing.T) {
7878
t.Fatal("Couldn't connect to en.wikipedia.org")
7979
}
8080
return ctx
81+
}).
82+
Teardown(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
83+
dep := ctx.Value("test-deployment").(*appsv1.Deployment)
84+
if err := c.Client().Resources().Delete(ctx, dep); err != nil {
85+
t.Fatal(err)
86+
}
87+
return ctx
8188
}).Feature()
8289
_ = testEnv.Test(t, feature)
8390
}

0 commit comments

Comments
 (0)