Skip to content

Commit

Permalink
feat(test): use builder to build test objects.
Browse files Browse the repository at this point in the history
Creates builders to allow dynamic object building during tests.

Signed-off-by: José Guilherme Vanz <jguilhermevanz@suse.com>
  • Loading branch information
jvanz committed Jan 9, 2025
1 parent 7110ea8 commit f17d163
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 237 deletions.
26 changes: 19 additions & 7 deletions internal/controller/admissionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {

BeforeAll(func() {
policyServerName = newName("policy-server")
createPolicyServerAndWaitForItsService(ctx, policyServerFactory(policyServerName))
policyServer := newPolicyServerFactory().withName(policyServerName).build()
createPolicyServerAndWaitForItsService(ctx, policyServer)

policyName = newName("validating-policy")
policy = admissionPolicyFactory(policyName, policyNamespace, policyServerName, false)
policy = newAdmissionPolicyFactory().
withName(policyName).
withNamespace(policyNamespace).
withPolicyServer(policyServerName).
withMutating(false).
build()
Expect(k8sClient.Create(ctx, policy)).To(Succeed())
})

Expand Down Expand Up @@ -177,10 +183,16 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {

BeforeAll(func() {
policyServerName = newName("policy-server")
createPolicyServerAndWaitForItsService(ctx, policyServerFactory(policyServerName))
policyServer := newPolicyServerFactory().withName(policyServerName).build()
createPolicyServerAndWaitForItsService(ctx, policyServer)

policyName = newName("mutating-policy")
policy = admissionPolicyFactory(policyName, policyNamespace, policyServerName, true)
policy = newAdmissionPolicyFactory().
withName(policyName).
withNamespace(policyNamespace).
withPolicyServer(policyServerName).
withMutating(true).
build()
Expect(k8sClient.Create(ctx, policy)).To(Succeed())
})

Expand Down Expand Up @@ -297,7 +309,7 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {
It("should set policy status to unscheduled when creating an AdmissionPolicy without a PolicyServer assigned", func() {
policyName := newName("unscheduled-policy")
Expect(
k8sClient.Create(ctx, admissionPolicyFactory(policyName, policyNamespace, "", false)),
k8sClient.Create(ctx, newAdmissionPolicyFactory().withName(policyName).withNamespace(policyNamespace).build()),
).To(haveSucceededOrAlreadyExisted())

Eventually(func() (*policiesv1.AdmissionPolicy, error) {
Expand All @@ -313,7 +325,7 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {

BeforeAll(func() {
Expect(
k8sClient.Create(ctx, admissionPolicyFactory(policyName, policyNamespace, policyServerName, false)),
k8sClient.Create(ctx, newAdmissionPolicyFactory().withName(policyName).withNamespace(policyNamespace).withPolicyServer(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())
})

Expand All @@ -328,7 +340,7 @@ var _ = Describe("AdmissionPolicy controller", Label("real-cluster"), func() {
It("should set the policy status to active when the PolicyServer is created", func() {
By("creating the PolicyServer")
Expect(
k8sClient.Create(ctx, policyServerFactory(policyServerName)),
k8sClient.Create(ctx, newPolicyServerFactory().withName(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())

By("changing the policy status to pending")
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/admissionpolicygroup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ var _ = Describe("AdmissionPolicyGroup controller", Label("real-cluster"), func(

BeforeAll(func() {
policyServerName = newName("policy-server")
createPolicyServerAndWaitForItsService(ctx, policyServerFactory(policyServerName))
createPolicyServerAndWaitForItsService(ctx, newPolicyServerFactory().withName(policyServerName).build())

policyName = newName("validating-policy")
policy = admissionPolicyGroupFactory(policyName, policyNamespace, policyServerName)
policy = newAdmissionPolicyGroupFactory().withName(policyName).withNamespace(policyNamespace).withPolicyServer(policyServerName).build()
Expect(k8sClient.Create(ctx, policy)).To(Succeed())
})

Expand Down Expand Up @@ -172,7 +172,7 @@ var _ = Describe("AdmissionPolicyGroup controller", Label("real-cluster"), func(
It("should set policy status to unscheduled when creating an AdmissionPolicyGroup without a PolicyServer assigned", func() {
policyName := newName("unscheduled-policy")
Expect(
k8sClient.Create(ctx, admissionPolicyGroupFactory(policyName, policyNamespace, "")),
k8sClient.Create(ctx, newAdmissionPolicyGroupFactory().withName(policyName).withNamespace(policyNamespace).withPolicyServer("").build()),
).To(haveSucceededOrAlreadyExisted())

Eventually(func() (*policiesv1.AdmissionPolicyGroup, error) {
Expand All @@ -188,7 +188,7 @@ var _ = Describe("AdmissionPolicyGroup controller", Label("real-cluster"), func(

BeforeAll(func() {
Expect(
k8sClient.Create(ctx, admissionPolicyGroupFactory(policyName, policyNamespace, policyServerName)),
k8sClient.Create(ctx, newAdmissionPolicyGroupFactory().withName(policyName).withNamespace(policyNamespace).withPolicyServer(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())
})

Expand All @@ -203,7 +203,7 @@ var _ = Describe("AdmissionPolicyGroup controller", Label("real-cluster"), func(
It("should set the policy status to active when the PolicyServer is created", func() {
By("creating the PolicyServer")
Expect(
k8sClient.Create(ctx, policyServerFactory(policyServerName)),
k8sClient.Create(ctx, newPolicyServerFactory().withName(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())

By("changing the policy status to pending")
Expand Down
16 changes: 8 additions & 8 deletions internal/controller/clusteradmissionpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun

BeforeAll(func() {
policyServerName = newName("policy-server")
createPolicyServerAndWaitForItsService(ctx, policyServerFactory(policyServerName))
createPolicyServerAndWaitForItsService(ctx, newPolicyServerFactory().withName(policyServerName).build())

policyName = newName("validating-policy")
policy = clusterAdmissionPolicyFactory(policyName, policyServerName, false)
policy = newClusterAdmissionPolicyFactory().withName(policyName).withPolicyServer(policyServerName).withMutating(false).build()
Expect(k8sClient.Create(ctx, policy)).To(Succeed())
})

Expand Down Expand Up @@ -148,10 +148,10 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun

BeforeAll(func() {
policyServerName = newName("policy-server")
createPolicyServerAndWaitForItsService(ctx, policyServerFactory(policyServerName))
createPolicyServerAndWaitForItsService(ctx, newPolicyServerFactory().withName(policyServerName).build())

policyName = newName("mutating-policy")
policy = clusterAdmissionPolicyFactory(policyName, policyServerName, true)
policy = newClusterAdmissionPolicyFactory().withName(policyName).withPolicyServer(policyServerName).withMutating(true).build()
Expect(k8sClient.Create(ctx, policy)).To(Succeed())
})

Expand Down Expand Up @@ -251,7 +251,7 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun
It("should set policy status to unscheduled when creating an ClusterAdmissionPolicy without a PolicyServer assigned", func() {
policyName := newName("unscheduled-policy")
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyFactory(policyName, "", false)),
k8sClient.Create(ctx, newClusterAdmissionPolicyFactory().withName(policyName).withPolicyServer("").withMutating(false).build()),
).To(haveSucceededOrAlreadyExisted())

Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
Expand All @@ -267,13 +267,13 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun

BeforeAll(func() {
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyFactory(policyName, policyServerName, false)),
k8sClient.Create(ctx, newClusterAdmissionPolicyFactory().withName(policyName).withPolicyServer(policyServerName).withMutating(false).build()),
).To(haveSucceededOrAlreadyExisted())
})

It("should set the policy status to scheduled", func() {
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyFactory(policyName, policyServerName, false)),
k8sClient.Create(ctx, newClusterAdmissionPolicyFactory().withName(policyName).withPolicyServer(policyServerName).withMutating(false).build()),
).To(haveSucceededOrAlreadyExisted())

Eventually(func() (*policiesv1.ClusterAdmissionPolicy, error) {
Expand All @@ -286,7 +286,7 @@ var _ = Describe("ClusterAdmissionPolicy controller", Label("real-cluster"), fun
It("should set the policy status to active when the PolicyServer is created", func() {
By("creating the PolicyServer")
Expect(
k8sClient.Create(ctx, policyServerFactory(policyServerName)),
k8sClient.Create(ctx, newPolicyServerFactory().withName(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())

By("changing the policy status to pending")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ var _ = Describe("ClusterAdmissionPolicyGroup controller", Label("real-cluster")

BeforeAll(func() {
policyServerName = newName("policy-server")
createPolicyServerAndWaitForItsService(ctx, policyServerFactory(policyServerName))
createPolicyServerAndWaitForItsService(ctx, newPolicyServerFactory().withName(policyServerName).build())

policyName = newName("validating-policy")
policy = clusterAdmissionPolicyGroupFactory(policyName, policyServerName)
policy = newClusterAdmissionPolicyGroupFactory().withName(policyName).withPolicyServer(policyServerName).build()
Expect(k8sClient.Create(ctx, policy)).To(Succeed())
})

Expand Down Expand Up @@ -144,7 +144,7 @@ var _ = Describe("ClusterAdmissionPolicyGroup controller", Label("real-cluster")
It("should set policy status to unscheduled when creating an ClusterAdmissionPolicyGroup without a PolicyServer assigned", func() {
policyName := newName("unscheduled-policy")
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyGroupFactory(policyName, "")),
k8sClient.Create(ctx, newClusterAdmissionPolicyGroupFactory().withName(policyName).withPolicyServer("").build()),
).To(haveSucceededOrAlreadyExisted())

Eventually(func() (*policiesv1.ClusterAdmissionPolicyGroup, error) {
Expand All @@ -160,13 +160,13 @@ var _ = Describe("ClusterAdmissionPolicyGroup controller", Label("real-cluster")

BeforeAll(func() {
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyGroupFactory(policyName, policyServerName)),
k8sClient.Create(ctx, newClusterAdmissionPolicyGroupFactory().withName(policyName).withPolicyServer(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())
})

It("should set the policy status to scheduled", func() {
Expect(
k8sClient.Create(ctx, clusterAdmissionPolicyGroupFactory(policyName, policyServerName)),
k8sClient.Create(ctx, newClusterAdmissionPolicyGroupFactory().withName(policyName).withPolicyServer(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())

Eventually(func() (*policiesv1.ClusterAdmissionPolicyGroup, error) {
Expand All @@ -179,7 +179,7 @@ var _ = Describe("ClusterAdmissionPolicyGroup controller", Label("real-cluster")
It("should set the policy status to active when the PolicyServer is created", func() {
By("creating the PolicyServer")
Expect(
k8sClient.Create(ctx, policyServerFactory(policyServerName)),
k8sClient.Create(ctx, newPolicyServerFactory().withName(policyServerName).build()),
).To(haveSucceededOrAlreadyExisted())

By("changing the policy status to pending")
Expand Down
Loading

0 comments on commit f17d163

Please sign in to comment.