Skip to content

Commit

Permalink
Merge pull request kubewarden#714 from fabriziosestito/test/clean-up-…
Browse files Browse the repository at this point in the history
…integration-tests

test: clean up integration tests
  • Loading branch information
fabriziosestito authored Apr 11, 2024
2 parents 926e9b0 + 20f439b commit 35171d0
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 151 deletions.
40 changes: 24 additions & 16 deletions controllers/admissionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,26 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(Succeed())

By("changing the policy status to pending")
Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusPending)),
)

By("changing the policy status to active")
Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusActive)),
)
})

It("should create the ValidatingWebhookConfiguration", func() {
Eventually(func(g Gomega) {
Eventually(func() error {
validatingWebhookConfiguration, err := getTestValidatingWebhookConfiguration(fmt.Sprintf("namespaced-%s-%s", policyNamespace, policyName))
Expect(err).ToNot(HaveOccurred())
if err != nil {
return err
}

Expect(validatingWebhookConfiguration.Labels["kubewarden"]).To(Equal("true"))
Expect(validatingWebhookConfiguration.Labels[constants.WebhookConfigurationPolicyScopeLabelKey]).To(Equal("namespace"))
Expand All @@ -89,6 +91,8 @@ var _ = Describe("AdmissionPolicy controller", func() {
caSecret, err := getTestCASecret()
Expect(err).ToNot(HaveOccurred())
Expect(validatingWebhookConfiguration.Webhooks[0].ClientConfig.CABundle).To(Equal(caSecret.Data[constants.PolicyServerCARootPemName]))

return nil
}, timeout, pollInterval).Should(Succeed())
})

Expand All @@ -110,7 +114,7 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(Succeed())

By("reconciling the ValidatingWebhookConfiguration to its original state")
Eventually(func(g Gomega) (*admissionregistrationv1.ValidatingWebhookConfiguration, error) {
Eventually(func() (*admissionregistrationv1.ValidatingWebhookConfiguration, error) {
return getTestValidatingWebhookConfiguration(fmt.Sprintf("namespaced-%s-%s", policyNamespace, policyName))
}, timeout, pollInterval).Should(
And(
Expand All @@ -134,7 +138,7 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(Succeed())

By("reconciling the ValidatingWebhookConfiguration to its original state")
Eventually(func(g Gomega) (*admissionregistrationv1.ValidatingWebhookConfiguration, error) {
Eventually(func() (*admissionregistrationv1.ValidatingWebhookConfiguration, error) {
return getTestValidatingWebhookConfiguration(fmt.Sprintf("namespaced-%s-%s", policyNamespace, policyName))
}, timeout, pollInterval).Should(
And(
Expand Down Expand Up @@ -163,24 +167,26 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(Succeed())

By("changing the policy status to pending")
Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusPending)),
)

By("changing the policy status to active")
Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusActive)),
)
})

It("should create the MutatingWebhookConfiguration", func() {
Eventually(func(g Gomega) {
Eventually(func() error {
mutatingWebhookConfiguration, err := getTestMutatingWebhookConfiguration(fmt.Sprintf("namespaced-%s-%s", policyNamespace, policyName))
Expect(err).ToNot(HaveOccurred())
if err != nil {
return err
}

Expect(mutatingWebhookConfiguration.Labels["kubewarden"]).To(Equal("true"))
Expect(mutatingWebhookConfiguration.Labels[constants.WebhookConfigurationPolicyScopeLabelKey]).To(Equal("namespace"))
Expand All @@ -192,6 +198,8 @@ var _ = Describe("AdmissionPolicy controller", func() {
caSecret, err := getTestCASecret()
Expect(err).ToNot(HaveOccurred())
Expect(mutatingWebhookConfiguration.Webhooks[0].ClientConfig.CABundle).To(Equal(caSecret.Data[constants.PolicyServerCARootPemName]))

return nil
}, timeout, pollInterval).Should(Succeed())
})

Expand All @@ -213,7 +221,7 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(Succeed())

By("reconciling the MutatingWebhookConfiguration to its original state")
Eventually(func(g Gomega) (*admissionregistrationv1.MutatingWebhookConfiguration, error) {
Eventually(func() (*admissionregistrationv1.MutatingWebhookConfiguration, error) {
return getTestMutatingWebhookConfiguration(fmt.Sprintf("namespaced-%s-%s", policyNamespace, policyName))
}, timeout, pollInterval).Should(
And(
Expand All @@ -237,7 +245,7 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(Succeed())

By("reconciling the MutatingWebhookConfiguration to its original state")
Eventually(func(g Gomega) (*admissionregistrationv1.MutatingWebhookConfiguration, error) {
Eventually(func() (*admissionregistrationv1.MutatingWebhookConfiguration, error) {
return getTestMutatingWebhookConfiguration(fmt.Sprintf("namespaced-%s-%s", policyNamespace, policyName))
}, timeout, pollInterval).Should(
And(
Expand All @@ -258,7 +266,7 @@ var _ = Describe("AdmissionPolicy controller", func() {
k8sClient.Create(ctx, admissionPolicyFactory(policyName, policyNamespace, "", false)),
).To(haveSucceededOrAlreadyExisted())

Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, 30*time.Second, 250*time.Millisecond).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusUnscheduled)),
Expand All @@ -277,7 +285,7 @@ var _ = Describe("AdmissionPolicy controller", func() {
k8sClient.Create(ctx, admissionPolicyFactory(policyName, policyNamespace, policyServerName, false)),
).To(haveSucceededOrAlreadyExisted())

Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusScheduled)),
Expand All @@ -291,14 +299,14 @@ var _ = Describe("AdmissionPolicy controller", func() {
).To(haveSucceededOrAlreadyExisted())

By("changing the policy status to pending")
Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusPending)),
)

By("changing the policy status to active")
Eventually(func(g Gomega) (*policiesv1.AdmissionPolicy, error) {
Eventually(func() (*policiesv1.AdmissionPolicy, error) {
return getTestAdmissionPolicy(policyNamespace, policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusActive)),
Expand Down
37 changes: 22 additions & 15 deletions controllers/clusteradmissionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
).To(Succeed())

By("changing the policy status to pending")
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusPending)),
)

By("changing the policy status to active")
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusActive)),
)
})

It("should create the ValidatingWebhookConfiguration", func() {
Eventually(func(g Gomega) {
Eventually(func() error {
validatingWebhookConfiguration, err := getTestValidatingWebhookConfiguration(fmt.Sprintf("clusterwide-%s", policyName))
Expect(err).ToNot(HaveOccurred())
if err != nil {
return err
}

Expect(validatingWebhookConfiguration.Labels["kubewarden"]).To(Equal("true"))
Expect(validatingWebhookConfiguration.Labels[constants.WebhookConfigurationPolicyScopeLabelKey]).To(Equal("cluster"))
Expand All @@ -74,6 +76,8 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
caSecret, err := getTestCASecret()
Expect(err).ToNot(HaveOccurred())
Expect(validatingWebhookConfiguration.Webhooks[0].ClientConfig.CABundle).To(Equal(caSecret.Data[constants.PolicyServerCARootPemName]))

return nil
}, timeout, pollInterval).Should(Succeed())
})

Expand All @@ -95,7 +99,7 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
).To(Succeed())

By("reconciling the ValidatingWebhookConfiguration to its original state")
Eventually(func(g Gomega) (*admissionregistrationv1.ValidatingWebhookConfiguration, error) {
Eventually(func() (*admissionregistrationv1.ValidatingWebhookConfiguration, error) {
return getTestValidatingWebhookConfiguration(fmt.Sprintf("clusterwide-%s", policyName))
}, timeout, pollInterval).Should(
And(
Expand Down Expand Up @@ -124,25 +128,26 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
).To(Succeed())

By("changing the policy status to pending")
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusPending)),
)

By("changing the policy status to active")
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusActive)),
)
})

It("should create the MutatingWebhookConfiguration", func() {
Eventually(func(g Gomega) {
Eventually(func() error {
mutatingWebhookConfiguration, err := getTestMutatingWebhookConfiguration(fmt.Sprintf("clusterwide-%s", policyName))
Expect(err).ToNot(HaveOccurred())

if err != nil {
return err
}
Expect(mutatingWebhookConfiguration.Labels["kubewarden"]).To(Equal("true"))
Expect(mutatingWebhookConfiguration.Labels[constants.WebhookConfigurationPolicyScopeLabelKey]).To(Equal("cluster"))
Expect(mutatingWebhookConfiguration.Annotations[constants.WebhookConfigurationPolicyNameAnnotationKey]).To(Equal(policyName))
Expand All @@ -153,6 +158,8 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
caSecret, err := getTestCASecret()
Expect(err).ToNot(HaveOccurred())
Expect(mutatingWebhookConfiguration.Webhooks[0].ClientConfig.CABundle).To(Equal(caSecret.Data[constants.PolicyServerCARootPemName]))

return nil
}, timeout, pollInterval).Should(Succeed())
})

Expand All @@ -174,7 +181,7 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
).To(Succeed())

By("reconciling the MutatingWebhookConfiguration to its original state")
Eventually(func(g Gomega) (*admissionregistrationv1.MutatingWebhookConfiguration, error) {
Eventually(func() (*admissionregistrationv1.MutatingWebhookConfiguration, error) {
return getTestMutatingWebhookConfiguration(fmt.Sprintf("clusterwide-%s", policyName))
}, timeout, pollInterval).Should(
And(
Expand All @@ -195,7 +202,7 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
k8sClient.Create(ctx, clusterAdmissionPolicyFactory(policyName, "", false)),
).To(haveSucceededOrAlreadyExisted())

Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusUnscheduled)),
Expand All @@ -214,7 +221,7 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
k8sClient.Create(ctx, clusterAdmissionPolicyFactory(policyName, policyServerName, false)),
).To(haveSucceededOrAlreadyExisted())

Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusScheduled)),
Expand All @@ -228,14 +235,14 @@ var _ = Describe("ClusterAdmissionPolicy controller", func() {
).To(haveSucceededOrAlreadyExisted())

By("changing the policy status to pending")
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusPending)),
)

By("changing the policy status to active")
Eventually(func(g Gomega) (*policiesv1.ClusterAdmissionPolicy, error) {
Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
return getTestClusterAdmissionPolicy(policyName)
}, timeout, pollInterval).Should(
HaveField("Status.PolicyStatus", Equal(policiesv1.PolicyStatusActive)),
Expand Down
Loading

0 comments on commit 35171d0

Please sign in to comment.