From 1a6b5697e939854647f10f25b3e207e795e99b1e Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sun, 23 Apr 2023 17:19:03 +0100 Subject: [PATCH] :bug: (deployImage) fix controller tests implemented by removing unecessary code implementation --- .../templates/controllers/controller-test.go | 42 +++++++++---------- .../controller/busybox_controller_test.go | 42 +++++++++---------- .../controller/memcached_controller_test.go | 42 +++++++++---------- 3 files changed, 63 insertions(+), 63 deletions(-) diff --git a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go index f39cdd0e8f1..1fdff292283 100644 --- a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go +++ b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go @@ -111,27 +111,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name} BeforeEach(func() { - By("Creating the Namespace to perform the tests") - err := k8sClient.Create(ctx, namespace); - Expect(err).To(Not(HaveOccurred())) - - By("Setting the Image ENV VAR which stores the Operand image") - err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test") - Expect(err).To(Not(HaveOccurred())) - }) - - AfterEach(func() { - // TODO(user): Attention if you improve this code by adding other context test you MUST - // be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations - By("Deleting the Namespace to perform the tests") - _ = k8sClient.Delete(ctx, namespace); - - By("Removing the Image ENV VAR which stores the Operand image") - _ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE") - }) - - It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() { - By("Creating the custom resource for the Kind {{ .Resource.Kind }}") + By("creating the custom resource for the Kind {{ .Resource.Kind }}") {{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{} err := k8sClient.Get(ctx, typeNamespaceName, {{ lower .Resource.Kind }}) if err != nil && errors.IsNotFound(err) { @@ -154,6 +134,26 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { Expect(err).To(Not(HaveOccurred())) } + By("Setting the Image ENV VAR which stores the Operand image") + err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test") + Expect(err).To(Not(HaveOccurred())) + }) + + AfterEach(func() { + By("removing the custom resource for the Kind {{ .Resource.Kind }}") + found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{} + err := k8sClient.Get(context.TODO(), typeNamespaceName, found) + Expect(err).To(Not(HaveOccurred())) + + Eventually(func() error { + return k8sClient.Delete(context.TODO(), found) + }, 2*time.Minute, time.Second).Should(Succeed()) + + By("Removing the Image ENV VAR which stores the Operand image") + _ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE") + }) + + It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() { By("Checking if the custom resource was successfully created") Eventually(func() error { found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{} diff --git a/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go b/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go index 5af1372dd2f..92d5292c034 100644 --- a/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go +++ b/testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go @@ -51,27 +51,7 @@ var _ = Describe("Busybox controller", func() { typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName} BeforeEach(func() { - By("Creating the Namespace to perform the tests") - err := k8sClient.Create(ctx, namespace) - Expect(err).To(Not(HaveOccurred())) - - By("Setting the Image ENV VAR which stores the Operand image") - err = os.Setenv("BUSYBOX_IMAGE", "example.com/image:test") - Expect(err).To(Not(HaveOccurred())) - }) - - AfterEach(func() { - // TODO(user): Attention if you improve this code by adding other context test you MUST - // be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations - By("Deleting the Namespace to perform the tests") - _ = k8sClient.Delete(ctx, namespace) - - By("Removing the Image ENV VAR which stores the Operand image") - _ = os.Unsetenv("BUSYBOX_IMAGE") - }) - - It("should successfully reconcile a custom resource for Busybox", func() { - By("Creating the custom resource for the Kind Busybox") + By("creating the custom resource for the Kind Busybox") busybox := &examplecomv1alpha1.Busybox{} err := k8sClient.Get(ctx, typeNamespaceName, busybox) if err != nil && errors.IsNotFound(err) { @@ -91,6 +71,26 @@ var _ = Describe("Busybox controller", func() { Expect(err).To(Not(HaveOccurred())) } + By("Setting the Image ENV VAR which stores the Operand image") + err = os.Setenv("BUSYBOX_IMAGE", "example.com/image:test") + Expect(err).To(Not(HaveOccurred())) + }) + + AfterEach(func() { + By("removing the custom resource for the Kind Busybox") + found := &examplecomv1alpha1.Busybox{} + err := k8sClient.Get(context.TODO(), typeNamespaceName, found) + Expect(err).To(Not(HaveOccurred())) + + Eventually(func() error { + return k8sClient.Delete(context.TODO(), found) + }, 2*time.Minute, time.Second).Should(Succeed()) + + By("Removing the Image ENV VAR which stores the Operand image") + _ = os.Unsetenv("BUSYBOX_IMAGE") + }) + + It("should successfully reconcile a custom resource for Busybox", func() { By("Checking if the custom resource was successfully created") Eventually(func() error { found := &examplecomv1alpha1.Busybox{} diff --git a/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go b/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go index c4eef67c60b..5beae016f22 100644 --- a/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go +++ b/testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go @@ -51,27 +51,7 @@ var _ = Describe("Memcached controller", func() { typeNamespaceName := types.NamespacedName{Name: MemcachedName, Namespace: MemcachedName} BeforeEach(func() { - By("Creating the Namespace to perform the tests") - err := k8sClient.Create(ctx, namespace) - Expect(err).To(Not(HaveOccurred())) - - By("Setting the Image ENV VAR which stores the Operand image") - err = os.Setenv("MEMCACHED_IMAGE", "example.com/image:test") - Expect(err).To(Not(HaveOccurred())) - }) - - AfterEach(func() { - // TODO(user): Attention if you improve this code by adding other context test you MUST - // be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations - By("Deleting the Namespace to perform the tests") - _ = k8sClient.Delete(ctx, namespace) - - By("Removing the Image ENV VAR which stores the Operand image") - _ = os.Unsetenv("MEMCACHED_IMAGE") - }) - - It("should successfully reconcile a custom resource for Memcached", func() { - By("Creating the custom resource for the Kind Memcached") + By("creating the custom resource for the Kind Memcached") memcached := &examplecomv1alpha1.Memcached{} err := k8sClient.Get(ctx, typeNamespaceName, memcached) if err != nil && errors.IsNotFound(err) { @@ -92,6 +72,26 @@ var _ = Describe("Memcached controller", func() { Expect(err).To(Not(HaveOccurred())) } + By("Setting the Image ENV VAR which stores the Operand image") + err = os.Setenv("MEMCACHED_IMAGE", "example.com/image:test") + Expect(err).To(Not(HaveOccurred())) + }) + + AfterEach(func() { + By("removing the custom resource for the Kind Memcached") + found := &examplecomv1alpha1.Memcached{} + err := k8sClient.Get(context.TODO(), typeNamespaceName, found) + Expect(err).To(Not(HaveOccurred())) + + Eventually(func() error { + return k8sClient.Delete(context.TODO(), found) + }, 2*time.Minute, time.Second).Should(Succeed()) + + By("Removing the Image ENV VAR which stores the Operand image") + _ = os.Unsetenv("MEMCACHED_IMAGE") + }) + + It("should successfully reconcile a custom resource for Memcached", func() { By("Checking if the custom resource was successfully created") Eventually(func() error { found := &examplecomv1alpha1.Memcached{}