From 9f6f1f9660a0e8358a17da732e8c777e89accf89 Mon Sep 17 00:00:00 2001 From: Guilherme Cassolato Date: Mon, 4 Dec 2023 09:30:27 +0100 Subject: [PATCH] Cleanup AuthPolicies after each test case --- controllers/authpolicy_controller_test.go | 24 +++++++++++++++++++++++ controllers/helper_test.go | 15 ++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/controllers/authpolicy_controller_test.go b/controllers/authpolicy_controller_test.go index dba7cd1fd..495d0b866 100644 --- a/controllers/authpolicy_controller_test.go +++ b/controllers/authpolicy_controller_test.go @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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{} @@ -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{} @@ -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{} @@ -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()) @@ -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()) @@ -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()) @@ -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()) @@ -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()) diff --git a/controllers/helper_test.go b/controllers/helper_test.go index dd7b8583f..e0cd179dc 100644 --- a/controllers/helper_test.go +++ b/controllers/helper_test.go @@ -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{