Skip to content

Commit

Permalink
test: add policy deletion -> webhook deletion tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <[email protected]>
  • Loading branch information
fabriziosestito committed Sep 23, 2024
1 parent 3c5ef17 commit f4d9d6b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/controller/admissionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
Expand Down Expand Up @@ -155,6 +156,20 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {
),
)
})

It("should delete the ValidatingWebhookConfiguration when the AdmissionPolicy is deleted", func() {
By("deleting the AdmissionPolicy")
Expect(
k8sClient.Delete(ctx, policy),
).To(Succeed())

By("waiting for the ValidatingWebhookConfiguration to be deleted")
Eventually(func(g Gomega) {
_, err := getTestValidatingWebhookConfiguration(ctx, policy.GetUniqueName())

g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, timeout, pollInterval).Should(Succeed())
})
})

When("creating a mutating AdmissionPolicy", Ordered, func() {
Expand Down Expand Up @@ -267,6 +282,20 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {
),
)
})

It("should delete the MutatingWebhookConfiguration when the AdmissionPolicy is deleted", func() {
By("deleting the AdmissionPolicy")
Expect(
k8sClient.Delete(ctx, policy),
).To(Succeed())

By("waiting for the MutatingWebhookConfiguration to be deleted")
Eventually(func(g Gomega) {
_, err := getTestMutatingWebhookConfiguration(ctx, policy.GetUniqueName())

g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, timeout, pollInterval).Should(Succeed())
})
})

It("should set policy status to unscheduled when creating an AdmissionPolicy without a PolicyServer assigned", func() {
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/admissionpolicygroup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
Expand Down Expand Up @@ -154,6 +155,20 @@ var _ = Describe("AdmissionPolicyGroup controller", Label("real-cluster"), func(
),
)
})

It("should delete the ValidatingWebhookConfiguration when the AdmissionPolicyGroup is deleted", func() {
By("deleting the AdmissionPolicyGroup")
Expect(
k8sClient.Delete(ctx, policy),
).To(Succeed())

By("waiting for the ValidatingWebhookConfiguration to be deleted")
Eventually(func(g Gomega) {
_, err := getTestValidatingWebhookConfiguration(ctx, policy.GetUniqueName())

g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, timeout, pollInterval).Should(Succeed())
})
})

It("should set policy status to unscheduled when creating an AdmissionPolicyGroup without a PolicyServer assigned", func() {
Expand Down
29 changes: 29 additions & 0 deletions internal/controller/clusteradmissionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
. "github.com/onsi/gomega/gstruct"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"

policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
"github.com/kubewarden/kubewarden-controller/internal/constants"
Expand Down Expand Up @@ -126,6 +127,20 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun
),
)
})

It("should delete the ValidatingWebhookConfiguration when the ClusterAdmissionPolicy is deleted", func() {
By("deleting the ClusterAdmissionPolicy")
Expect(
k8sClient.Delete(ctx, policy),
).To(Succeed())

By("waiting for the ValidatingWebhookConfiguration to be deleted")
Eventually(func(g Gomega) {
_, err := getTestValidatingWebhookConfiguration(ctx, policy.GetUniqueName())

g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, timeout, pollInterval).Should(Succeed())
})
})

When("creating a mutating ClusterAdmissionPolicy", Ordered, func() {
Expand Down Expand Up @@ -221,6 +236,20 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun
),
)
})

It("should delete the MutatingWebhookConfiguration when the ClusterAdmissionPolicy is deleted", func() {
By("deleting the ClusterAdmissionPolicy")
Expect(
k8sClient.Delete(ctx, policy),
).To(Succeed())

By("waiting for the MutatingWebhookConfiguration to be deleted")
Eventually(func(g Gomega) {
_, err := getTestMutatingWebhookConfiguration(ctx, policy.GetUniqueName())

g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, timeout, pollInterval).Should(Succeed())
})
})

It("should set policy status to unscheduled when creating an ClusterAdmissionPolicy without a PolicyServer assigned", func() {
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/clusteradmissionpolicygroup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
. "github.com/onsi/gomega/gstruct"

admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"

policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
"github.com/kubewarden/kubewarden-controller/internal/constants"
Expand Down Expand Up @@ -126,6 +127,20 @@ var _ = Describe("ClusterAdmissionPolicyGroup controller", Label("real-cluster")
),
)
})

It("should delete the ValidatingWebhookConfiguration when the ClusterAdmissionPolicyGroup is deleted", func() {
By("deleting the ClusterAdmissionPolicyGroup")
Expect(
k8sClient.Delete(ctx, policy),
).To(Succeed())

By("waiting for the ValidatingWebhookConfiguration to be deleted")
Eventually(func(g Gomega) {
_, err := getTestValidatingWebhookConfiguration(ctx, policy.GetUniqueName())

g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
}, timeout, pollInterval).Should(Succeed())
})
})

It("should set policy status to unscheduled when creating an ClusterAdmissionPolicyGroup without a PolicyServer assigned", func() {
Expand Down

0 comments on commit f4d9d6b

Please sign in to comment.