Skip to content

Commit

Permalink
e2e-tests: add tests to ensure webhooks outside of the manager namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Sep 14, 2024
1 parent 9a8b2f8 commit 9d8180e
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions test/e2e/v4/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,26 +312,40 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool,
ExpectWithOffset(1, count).To(BeNumerically("==", 5))
}

if hasWebhook && hasNetworkPolicies {
if hasWebhook {
By("validating that webhooks from namespace without the label will fail")

// Define the namespace name and CR sample file path
namespace := "test-namespace-without-webhook-label"
sampleFile := "path/to/your/sample-file.yaml"

// Create the namespace
By("creating a namespace without the webhook: enabled label")
_, err := kbc.Kubectl.Command("create", "namespace", namespace)
Expect(err).NotTo(HaveOccurred(), "namespace should be created successfully")

// Apply the Custom Resource in the new namespace and expect it to fail
By("applying the CR in the namespace without the webhook: enabled label and expecting it to fail")
EventuallyWithOffset(1, func() error {
_, err = kbc.Kubectl.Apply(false, "-n", namespace, "-f", sampleFile)
return err
}, time.Minute, time.Second).Should(HaveOccurred(), "applying the CR should fail due to webhook call timeout")
if hasNetworkPolicies {
By("applying the CR in the namespace without the webhook: enabled label and expecting it to fail")
EventuallyWithOffset(1, func() error {
_, err = kbc.Kubectl.Apply(false, "-n", namespace, "-f", sampleFile)
return err
}, time.Minute, time.Second).Should(HaveOccurred(), "applying the CR should fail due to webhook call timeout")
} else {
By("applying the CR in a namespace without the webhook: should work without network policies")
EventuallyWithOffset(1, func() error {
_, err := kbc.Kubectl.Apply(false, "-n", namespace, "-f", sampleFile)
return err
}, 2*time.Minute, time.Second).ShouldNot(HaveOccurred(), "applying the CR should not fail without network policies")

By("validating that mutating webhooks are working fine outside of the manager's namespace")
cnt, err := kbc.Kubectl.Get(
false,
"-n", namespace,
"-f", sampleFile,
"-o", "go-template={{ .spec.count }}")
ExpectWithOffset(1, err).NotTo(HaveOccurred())

count, err := strconv.Atoi(cnt)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, count).To(BeNumerically("==", 5),
"the mutating webhook should set the count to 5")
}

// Cleanup: Remove the namespace
By("removing the namespace")
_, err = kbc.Kubectl.Command("delete", "namespace", namespace)
Expect(err).NotTo(HaveOccurred(), "namespace should be removed successfully")
Expand Down

0 comments on commit 9d8180e

Please sign in to comment.