Skip to content

Commit

Permalink
Wait for kubectl to complete before returning in tests
Browse files Browse the repository at this point in the history
This was covering up an error during test development.

Signed-off-by: mprahl <[email protected]>
  • Loading branch information
mprahl committed Aug 1, 2024
1 parent 3cd15d7 commit 0dcb5ef
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 6 deletions.
7 changes: 5 additions & 2 deletions test/e2e/case13_policyset_dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down
1 change: 1 addition & 0 deletions test/e2e/case14_root_policy_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion test/e2e/case2_aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})

Expand Down
1 change: 1 addition & 0 deletions test/e2e/case3_mutation_recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions test/e2e/case6_placement_propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -530,7 +531,7 @@ 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)
})
Expand Down Expand Up @@ -605,14 +606,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)
utils.Kubectl(
"delete", "namespace", "test6-extra",
"--ignore-not-found", "--timeout=2s", "--kubeconfig="+kubeconfigHub,
)
utils.Kubectl(
"delete", "managedcluster", "test6-extra", "--ignore-not-found", "--kubeconfig="+kubeconfigHub,
)
})
})
})
5 changes: 5 additions & 0 deletions test/e2e/case7_bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/case9_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
12 changes: 12 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package utils

import (
"bytes"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0dcb5ef

Please sign in to comment.