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 2, 2024
1 parent 06748e0 commit ff86025
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 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
21 changes: 16 additions & 5 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,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",
Expand Down Expand Up @@ -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)
Expand All @@ -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,
)
})
})
})
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
9 changes: 9 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 Expand Up @@ -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) {
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 ff86025

Please sign in to comment.