From ff86025fbd7706a1c721731791f9054c9449ad63 Mon Sep 17 00:00:00 2001 From: mprahl Date: Thu, 1 Aug 2024 15:45:38 -0400 Subject: [PATCH] Wait for kubectl to complete before returning in tests This was covering up an error during test development. Signed-off-by: mprahl --- .../e2e/case13_policyset_dependencies_test.go | 7 +++++-- test/e2e/case14_root_policy_metrics_test.go | 1 + test/e2e/case2_aggregation_test.go | 12 ++++++++++- test/e2e/case3_mutation_recovery_test.go | 1 + test/e2e/case6_placement_propagation_test.go | 21 ++++++++++++++----- test/e2e/case7_bindings_test.go | 5 +++++ test/e2e/case9_templates_test.go | 9 ++++++++ test/utils/utils.go | 12 +++++++++++ 8 files changed, 60 insertions(+), 8 deletions(-) diff --git a/test/e2e/case13_policyset_dependencies_test.go b/test/e2e/case13_policyset_dependencies_test.go index b42dd519..7e51193f 100644 --- a/test/e2e/case13_policyset_dependencies_test.go +++ b/test/e2e/case13_policyset_dependencies_test.go @@ -59,8 +59,11 @@ var _ = Describe("Test replacement of policysets in dependencies", Ordered, func DeferCleanup(func() { By("Running cleanup") - utils.Kubectl("delete", "-f", case13PolicyYaml, "-n", testNamespace, "--kubeconfig="+kubeconfigHub) - utils.Kubectl("delete", "-f", case13Set2Yaml, "--kubeconfig="+kubeconfigHub) + utils.Kubectl( + "delete", "-f", case13PolicyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub, + ) + utils.Kubectl("delete", "-f", case13Set2Yaml, "--ignore-not-found", "--kubeconfig="+kubeconfigHub) time.Sleep(5 * time.Second) // this helps everything get cleaned up completely }) } diff --git a/test/e2e/case14_root_policy_metrics_test.go b/test/e2e/case14_root_policy_metrics_test.go index 714f55dd..7246fb23 100644 --- a/test/e2e/case14_root_policy_metrics_test.go +++ b/test/e2e/case14_root_policy_metrics_test.go @@ -110,6 +110,7 @@ var _ = Describe("Test root policy metrics", Ordered, func() { utils.Kubectl("delete", "-f", policyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, defaultTimeoutSeconds) diff --git a/test/e2e/case2_aggregation_test.go b/test/e2e/case2_aggregation_test.go index 91fb7087..7cd7c54d 100644 --- a/test/e2e/case2_aggregation_test.go +++ b/test/e2e/case2_aggregation_test.go @@ -28,10 +28,12 @@ var _ = Describe("Test policy status aggregation", func() { utils.Kubectl("delete", "-f", faultyPBYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) utils.Kubectl("delete", "-f", case2PolicyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, 10) @@ -285,7 +287,15 @@ var _ = Describe("Test policy status aggregation", func() { AfterAll(func() { By("Cleaning up") - utils.Kubectl("delete", "-f", case2PolicyYaml, "-n", testNamespace, "--kubeconfig="+kubeconfigHub) + utils.Kubectl( + "delete", + "-f", + case2PolicyYaml, + "-n", + testNamespace, + "--ignore-not-found", + "--kubeconfig="+kubeconfigHub, + ) utils.ListWithTimeout(clientHubDynamic, gvrPolicy, metav1.ListOptions{}, 0, false, 10) }) diff --git a/test/e2e/case3_mutation_recovery_test.go b/test/e2e/case3_mutation_recovery_test.go index 75cc19f3..c7de27b3 100644 --- a/test/e2e/case3_mutation_recovery_test.go +++ b/test/e2e/case3_mutation_recovery_test.go @@ -67,6 +67,7 @@ var _ = Describe("Test unexpected policy mutation", func() { utils.Kubectl("delete", "-f", case3PolicyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, true, defaultTimeoutSeconds) diff --git a/test/e2e/case6_placement_propagation_test.go b/test/e2e/case6_placement_propagation_test.go index c43e8dc0..df1f5eb9 100644 --- a/test/e2e/case6_placement_propagation_test.go +++ b/test/e2e/case6_placement_propagation_test.go @@ -373,6 +373,7 @@ var _ = Describe("Test policy propagation", func() { utils.Kubectl("delete", "-f", case6PolicyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, 30) @@ -530,13 +531,15 @@ var _ = Describe("Test policy propagation", func() { AfterAll(func() { utils.Kubectl("delete", "-f", "../resources/case6_placement_propagation/case6-test-policy2.yaml", - "-n", testNamespace, "--kubeconfig="+kubeconfigHub) + "-n", testNamespace, "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, 10) }) }) Describe("Handling propagation to terminating clusters", Ordered, func() { + var finalizerRemoved bool + BeforeAll(func() { By("Creating " + case6PolicyYaml) utils.Kubectl("apply", @@ -588,6 +591,8 @@ var _ = Describe("Test policy propagation", func() { utils.Kubectl("patch", "namespace", "test6-extra", "--type=json", `-p=[{"op":"remove","path":"/metadata/finalizers"}]`, "--kubeconfig="+kubeconfigHub) + finalizerRemoved = true + By("Verifying that the namespace is removed") ns := utils.GetClusterLevelWithTimeout(clientHubDynamic, gvrNamespace, "test6-extra", false, defaultTimeoutSeconds) @@ -605,14 +610,20 @@ var _ = Describe("Test policy propagation", func() { utils.Kubectl("delete", "-f", case6PolicyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, 10) - utils.Kubectl("patch", "namespace", "test6-extra", "--type=json", - `-p=[{"op":"remove","path":"/metadata/finalizers"}]`, "--kubeconfig="+kubeconfigHub) - utils.Kubectl("delete", "namespace", "test6-extra", "--timeout=2s", "--kubeconfig="+kubeconfigHub) - utils.Kubectl("delete", "managedcluster", "test6-extra", "--kubeconfig="+kubeconfigHub) + if !finalizerRemoved { + utils.Kubectl("patch", "namespace", "test6-extra", "--type=json", + `-p=[{"op":"remove","path":"/metadata/finalizers"}]`, "--kubeconfig="+kubeconfigHub) + } + + utils.Kubectl("delete", "namespace", "test6-extra", "--ignore-not-found", "--kubeconfig="+kubeconfigHub) + utils.Kubectl( + "delete", "managedcluster", "test6-extra", "--ignore-not-found", "--kubeconfig="+kubeconfigHub, + ) }) }) }) diff --git a/test/e2e/case7_bindings_test.go b/test/e2e/case7_bindings_test.go index d7eedfd0..2596f5a7 100644 --- a/test/e2e/case7_bindings_test.go +++ b/test/e2e/case7_bindings_test.go @@ -224,24 +224,29 @@ var _ = Describe("Test policy propagation", func() { utils.Kubectl("delete", "-f", case7PolicyYaml, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) opt := metav1.ListOptions{} utils.ListWithTimeout(clientHubDynamic, gvrPolicy, opt, 0, false, 10) utils.Kubectl("delete", "-f", case7BindingYaml1, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) utils.Kubectl("delete", "-f", case7BindingYaml2, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) utils.Kubectl("delete", "-f", case7BindingYaml3, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) utils.Kubectl("delete", "-f", case7BindingYaml4, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) }) }) diff --git a/test/e2e/case9_templates_test.go b/test/e2e/case9_templates_test.go index 379cdb49..ae9bc7d3 100644 --- a/test/e2e/case9_templates_test.go +++ b/test/e2e/case9_templates_test.go @@ -469,9 +469,11 @@ var _ = Describe("Test policy templates", func() { utils.Kubectl("delete", "secret", case9EncryptionSecretName, "-n", managedCluster, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) utils.Kubectl("delete", "-f", case9PolicyYamlCopy, "-n", testNamespace, + "--ignore-not-found", "--kubeconfig="+kubeconfigHub) }) } @@ -544,6 +546,13 @@ var _ = Describe("Test policy templates", func() { "-n", testNamespace, "delete", "sa", "case9-sa-does-not-exist", "--ignore-not-found", "--kubeconfig="+kubeconfigHub, ) + + for i := 0; i < 3; i++ { + utils.Kubectl( + "delete", "secret", "policy-encryption-key", "-n", fmt.Sprintf("managed%d", i+1), + "--ignore-not-found", "--kubeconfig="+kubeconfigHub, + ) + } }) It("Template resolution with a custom service account", func(ctx SpecContext) { diff --git a/test/utils/utils.go b/test/utils/utils.go index 88d8b43b..f2104567 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -4,6 +4,7 @@ package utils import ( + "bytes" "context" "fmt" "os" @@ -296,12 +297,23 @@ func ListWithTimeoutByNamespace( // Kubectl execute kubectl cli func Kubectl(args ...string) { + GinkgoHelper() + cmd := exec.Command("kubectl", args...) + var stderr bytes.Buffer + + cmd.Stderr = &stderr + err := cmd.Start() if err != nil { Fail(fmt.Sprintf("Error: %v", err)) } + + err = cmd.Wait() + if err != nil { + Fail(fmt.Sprintf("`kubctl %s` failed: %s", strings.Join(args, " "), stderr.String())) + } } // KubectlWithOutput execute kubectl cli and return output and error