Skip to content

Commit

Permalink
Cleanup AuthPolicies after each test case
Browse files Browse the repository at this point in the history
  • Loading branch information
guicassolato committed Dec 4, 2023
1 parent c800e34 commit 9f6f1f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions controllers/authpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -160,6 +162,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 60*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -196,6 +200,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -295,6 +301,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(gwPolicy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, routePolicy, gwPolicy)

// check policy status
Eventually(testPolicyIsReady(gwPolicy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -380,6 +388,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(gwPolicy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, routePolicy, gwPolicy)

// check policy status
Eventually(func() bool {
existingPolicy := &api.AuthPolicy{}
Expand Down Expand Up @@ -437,6 +447,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(func() bool {
existingPolicy := &api.AuthPolicy{}
Expand Down Expand Up @@ -496,6 +508,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(func() bool {
existingPolicy := &api.AuthPolicy{}
Expand Down Expand Up @@ -802,6 +816,8 @@ var _ = Describe("AuthPolicy controller", func() {
logf.Log.V(1).Info("Creating AuthPolicy", "key", client.ObjectKeyFromObject(policy).String(), "error", err)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -849,6 +865,8 @@ var _ = Describe("AuthPolicy controller", func() {
err := k8sClient.Create(context.Background(), policy)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -957,6 +975,8 @@ var _ = Describe("AuthPolicy controller", func() {
err := k8sClient.Create(context.Background(), policy)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -1066,6 +1086,8 @@ var _ = Describe("AuthPolicy controller", func() {
err := k8sClient.Create(context.Background(), policy)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down Expand Up @@ -1202,6 +1224,8 @@ var _ = Describe("AuthPolicy controller", func() {
err := k8sClient.Create(context.Background(), policy)
Expect(err).ToNot(HaveOccurred())

defer DeleteK8sObjects(k8sClient, policy)

// check policy status
Eventually(testPolicyIsReady(policy), 30*time.Second, 5*time.Second).Should(BeTrue())

Expand Down
15 changes: 15 additions & 0 deletions controllers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ func CreateOrUpdateK8SObject(obj runtime.Object, k8sClient client.Client) error
return k8sClient.Update(context.Background(), k8sObjCopy)
}

func DeleteK8sObjects(k8sClient client.Client, objs ...runtime.Object) {
for _, obj := range objs {
k8sObj, ok := obj.(client.Object)
if !ok {
logf.Log.Info("DeleteK8sObjects", "error", "runtime.Object could not be casted to client.Object")
continue
}
logf.Log.Info("DeleteK8sObjects", "GKV", k8sObj.GetObjectKind(), "name", k8sObj.GetName(), "namespace", k8sObj.GetNamespace())
err := k8sClient.Delete(context.Background(), k8sObj)
if err != nil {
logf.Log.Info("DeleteK8sObjects", "error", err)
}
}
}

func testBuildBasicGateway(gwName, ns string) *gatewayapiv1.Gateway {
return &gatewayapiv1.Gateway{
TypeMeta: metav1.TypeMeta{
Expand Down

0 comments on commit 9f6f1f9

Please sign in to comment.