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..a34934de1e6 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 @@ -98,9 +98,6 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { Context("{{ .Resource.Kind }} controller test", func() { const {{ .Resource.Kind }}Name = "test-{{ lower .Resource.Kind }}" - - ctx := context.Background() - namespace := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: {{ .Resource.Kind }}Name, @@ -111,21 +108,12 @@ 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") }) @@ -133,7 +121,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() { 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 }}) + err := k8sClient.Get(context.TODO, typeNamespaceName, {{ lower .Resource.Kind }}) if err != nil && errors.IsNotFound(err) { // Let's mock our custom resource at the same way that we would // apply on the cluster the manifest under config/samples @@ -150,14 +138,14 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { }, } - err = k8sClient.Create(ctx, {{ lower .Resource.Kind }}) + err = k8sClient.Create(context.TODO, {{ lower .Resource.Kind }}) Expect(err).To(Not(HaveOccurred())) } By("Checking if the custom resource was successfully created") Eventually(func() error { found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{} - return k8sClient.Get(ctx, typeNamespaceName, found) + return k8sClient.Get(context.TODO, typeNamespaceName, found) }, time.Minute, time.Second).Should(Succeed()) By("Reconciling the custom resource created") @@ -166,7 +154,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { Scheme: k8sClient.Scheme(), } - _, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{ + _, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(context.TODO, reconcile.Request{ NamespacedName: typeNamespaceName, }) Expect(err).To(Not(HaveOccurred())) @@ -174,7 +162,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() { By("Checking if Deployment was successfully created in the reconciliation") Eventually(func() error { found := &appsv1.Deployment{} - return k8sClient.Get(ctx, typeNamespaceName, found) + return k8sClient.Get(context.TODO, typeNamespaceName, found) }, time.Minute, time.Second).Should(Succeed()) By("Checking the latest Status Condition added to the {{ .Resource.Kind }} instance") 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..258ecac567d 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 @@ -38,9 +38,6 @@ var _ = Describe("Busybox controller", func() { Context("Busybox controller test", func() { const BusyboxName = "test-busybox" - - ctx := context.Background() - namespace := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: BusyboxName, @@ -51,21 +48,12 @@ 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") }) @@ -73,7 +61,7 @@ var _ = Describe("Busybox controller", func() { It("should successfully reconcile a custom resource for Busybox", func() { By("Creating the custom resource for the Kind Busybox") busybox := &examplecomv1alpha1.Busybox{} - err := k8sClient.Get(ctx, typeNamespaceName, busybox) + err := k8sClient.Get(context.TODO, typeNamespaceName, busybox) if err != nil && errors.IsNotFound(err) { // Let's mock our custom resource at the same way that we would // apply on the cluster the manifest under config/samples @@ -87,14 +75,14 @@ var _ = Describe("Busybox controller", func() { }, } - err = k8sClient.Create(ctx, busybox) + err = k8sClient.Create(context.TODO, busybox) Expect(err).To(Not(HaveOccurred())) } By("Checking if the custom resource was successfully created") Eventually(func() error { found := &examplecomv1alpha1.Busybox{} - return k8sClient.Get(ctx, typeNamespaceName, found) + return k8sClient.Get(context.TODO, typeNamespaceName, found) }, time.Minute, time.Second).Should(Succeed()) By("Reconciling the custom resource created") @@ -103,7 +91,7 @@ var _ = Describe("Busybox controller", func() { Scheme: k8sClient.Scheme(), } - _, err = busyboxReconciler.Reconcile(ctx, reconcile.Request{ + _, err = busyboxReconciler.Reconcile(context.TODO, reconcile.Request{ NamespacedName: typeNamespaceName, }) Expect(err).To(Not(HaveOccurred())) @@ -111,7 +99,7 @@ var _ = Describe("Busybox controller", func() { By("Checking if Deployment was successfully created in the reconciliation") Eventually(func() error { found := &appsv1.Deployment{} - return k8sClient.Get(ctx, typeNamespaceName, found) + return k8sClient.Get(context.TODO, typeNamespaceName, found) }, time.Minute, time.Second).Should(Succeed()) By("Checking the latest Status Condition added to the Busybox instance") 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..c1a8a4ed015 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 @@ -38,9 +38,6 @@ var _ = Describe("Memcached controller", func() { Context("Memcached controller test", func() { const MemcachedName = "test-memcached" - - ctx := context.Background() - namespace := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: MemcachedName, @@ -51,21 +48,12 @@ 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") }) @@ -73,7 +61,7 @@ var _ = Describe("Memcached controller", func() { It("should successfully reconcile a custom resource for Memcached", func() { By("Creating the custom resource for the Kind Memcached") memcached := &examplecomv1alpha1.Memcached{} - err := k8sClient.Get(ctx, typeNamespaceName, memcached) + err := k8sClient.Get(context.TODO, typeNamespaceName, memcached) if err != nil && errors.IsNotFound(err) { // Let's mock our custom resource at the same way that we would // apply on the cluster the manifest under config/samples @@ -88,14 +76,14 @@ var _ = Describe("Memcached controller", func() { }, } - err = k8sClient.Create(ctx, memcached) + err = k8sClient.Create(context.TODO, memcached) Expect(err).To(Not(HaveOccurred())) } By("Checking if the custom resource was successfully created") Eventually(func() error { found := &examplecomv1alpha1.Memcached{} - return k8sClient.Get(ctx, typeNamespaceName, found) + return k8sClient.Get(context.TODO, typeNamespaceName, found) }, time.Minute, time.Second).Should(Succeed()) By("Reconciling the custom resource created") @@ -104,7 +92,7 @@ var _ = Describe("Memcached controller", func() { Scheme: k8sClient.Scheme(), } - _, err = memcachedReconciler.Reconcile(ctx, reconcile.Request{ + _, err = memcachedReconciler.Reconcile(context.TODO, reconcile.Request{ NamespacedName: typeNamespaceName, }) Expect(err).To(Not(HaveOccurred())) @@ -112,7 +100,7 @@ var _ = Describe("Memcached controller", func() { By("Checking if Deployment was successfully created in the reconciliation") Eventually(func() error { found := &appsv1.Deployment{} - return k8sClient.Get(ctx, typeNamespaceName, found) + return k8sClient.Get(context.TODO, typeNamespaceName, found) }, time.Minute, time.Second).Should(Succeed()) By("Checking the latest Status Condition added to the Memcached instance")