Skip to content

Commit d9c14df

Browse files
authored
Merge pull request #3364 from camilamacedo86/fix-deploy-image-test
✨ (deployImage) improve the controller tests scaffolded
2 parents bf654e0 + 200d7ad commit d9c14df

File tree

3 files changed

+79
-49
lines changed

3 files changed

+79
-49
lines changed

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
109109
}
110110
111111
typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name}
112+
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
112113
113114
BeforeEach(func() {
114115
By("Creating the Namespace to perform the tests")
@@ -118,22 +119,9 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
118119
By("Setting the Image ENV VAR which stores the Operand image")
119120
err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test")
120121
Expect(err).To(Not(HaveOccurred()))
121-
})
122-
123-
AfterEach(func() {
124-
// TODO(user): Attention if you improve this code by adding other context test you MUST
125-
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
126-
By("Deleting the Namespace to perform the tests")
127-
_ = k8sClient.Delete(ctx, namespace);
128-
129-
By("Removing the Image ENV VAR which stores the Operand image")
130-
_ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE")
131-
})
132122
133-
It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() {
134-
By("Creating the custom resource for the Kind {{ .Resource.Kind }}")
135-
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
136-
err := k8sClient.Get(ctx, typeNamespaceName, {{ lower .Resource.Kind }})
123+
By("creating the custom resource for the Kind {{ .Resource.Kind }}")
124+
err = k8sClient.Get(ctx, typeNamespaceName, {{ lower .Resource.Kind }})
137125
if err != nil && errors.IsNotFound(err) {
138126
// Let's mock our custom resource at the same way that we would
139127
// apply on the cluster the manifest under config/samples
@@ -152,8 +140,30 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
152140
153141
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
154142
Expect(err).To(Not(HaveOccurred()))
155-
}
143+
}
144+
})
156145
146+
AfterEach(func() {
147+
By("removing the custom resource for the Kind {{ .Resource.Kind }}")
148+
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
149+
err := k8sClient.Get(ctx, typeNamespaceName, found)
150+
Expect(err).To(Not(HaveOccurred()))
151+
152+
Eventually(func() error {
153+
return k8sClient.Delete(context.TODO(), found)
154+
}, 2*time.Minute, time.Second).Should(Succeed())
155+
156+
// TODO(user): Attention if you improve this code by adding other context test you MUST
157+
// be aware of the current delete namespace limitations.
158+
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
159+
By("Deleting the Namespace to perform the tests")
160+
_ = k8sClient.Delete(ctx, namespace);
161+
162+
By("Removing the Image ENV VAR which stores the Operand image")
163+
_ = os.Unsetenv("{{ upper .Resource.Kind }}_IMAGE")
164+
})
165+
166+
It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() {
157167
By("Checking if the custom resource was successfully created")
158168
Eventually(func() error {
159169
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
@@ -166,7 +176,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
166176
Scheme: k8sClient.Scheme(),
167177
}
168178
169-
_, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
179+
_, err := {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
170180
NamespacedName: typeNamespaceName,
171181
})
172182
Expect(err).To(Not(HaveOccurred()))

testdata/project-v4-with-deploy-image/internal/controller/busybox_controller_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var _ = Describe("Busybox controller", func() {
4949
}
5050

5151
typeNamespaceName := types.NamespacedName{Name: BusyboxName, Namespace: BusyboxName}
52+
busybox := &examplecomv1alpha1.Busybox{}
5253

5354
BeforeEach(func() {
5455
By("Creating the Namespace to perform the tests")
@@ -58,22 +59,9 @@ var _ = Describe("Busybox controller", func() {
5859
By("Setting the Image ENV VAR which stores the Operand image")
5960
err = os.Setenv("BUSYBOX_IMAGE", "example.com/image:test")
6061
Expect(err).To(Not(HaveOccurred()))
61-
})
6262

63-
AfterEach(func() {
64-
// TODO(user): Attention if you improve this code by adding other context test you MUST
65-
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
66-
By("Deleting the Namespace to perform the tests")
67-
_ = k8sClient.Delete(ctx, namespace)
68-
69-
By("Removing the Image ENV VAR which stores the Operand image")
70-
_ = os.Unsetenv("BUSYBOX_IMAGE")
71-
})
72-
73-
It("should successfully reconcile a custom resource for Busybox", func() {
74-
By("Creating the custom resource for the Kind Busybox")
75-
busybox := &examplecomv1alpha1.Busybox{}
76-
err := k8sClient.Get(ctx, typeNamespaceName, busybox)
63+
By("creating the custom resource for the Kind Busybox")
64+
err = k8sClient.Get(ctx, typeNamespaceName, busybox)
7765
if err != nil && errors.IsNotFound(err) {
7866
// Let's mock our custom resource at the same way that we would
7967
// apply on the cluster the manifest under config/samples
@@ -90,7 +78,29 @@ var _ = Describe("Busybox controller", func() {
9078
err = k8sClient.Create(ctx, busybox)
9179
Expect(err).To(Not(HaveOccurred()))
9280
}
81+
})
82+
83+
AfterEach(func() {
84+
By("removing the custom resource for the Kind Busybox")
85+
found := &examplecomv1alpha1.Busybox{}
86+
err := k8sClient.Get(ctx, typeNamespaceName, found)
87+
Expect(err).To(Not(HaveOccurred()))
88+
89+
Eventually(func() error {
90+
return k8sClient.Delete(context.TODO(), found)
91+
}, 2*time.Minute, time.Second).Should(Succeed())
9392

93+
// TODO(user): Attention if you improve this code by adding other context test you MUST
94+
// be aware of the current delete namespace limitations.
95+
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
96+
By("Deleting the Namespace to perform the tests")
97+
_ = k8sClient.Delete(ctx, namespace)
98+
99+
By("Removing the Image ENV VAR which stores the Operand image")
100+
_ = os.Unsetenv("BUSYBOX_IMAGE")
101+
})
102+
103+
It("should successfully reconcile a custom resource for Busybox", func() {
94104
By("Checking if the custom resource was successfully created")
95105
Eventually(func() error {
96106
found := &examplecomv1alpha1.Busybox{}
@@ -103,7 +113,7 @@ var _ = Describe("Busybox controller", func() {
103113
Scheme: k8sClient.Scheme(),
104114
}
105115

106-
_, err = busyboxReconciler.Reconcile(ctx, reconcile.Request{
116+
_, err := busyboxReconciler.Reconcile(ctx, reconcile.Request{
107117
NamespacedName: typeNamespaceName,
108118
})
109119
Expect(err).To(Not(HaveOccurred()))

testdata/project-v4-with-deploy-image/internal/controller/memcached_controller_test.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var _ = Describe("Memcached controller", func() {
4949
}
5050

5151
typeNamespaceName := types.NamespacedName{Name: MemcachedName, Namespace: MemcachedName}
52+
memcached := &examplecomv1alpha1.Memcached{}
5253

5354
BeforeEach(func() {
5455
By("Creating the Namespace to perform the tests")
@@ -58,22 +59,9 @@ var _ = Describe("Memcached controller", func() {
5859
By("Setting the Image ENV VAR which stores the Operand image")
5960
err = os.Setenv("MEMCACHED_IMAGE", "example.com/image:test")
6061
Expect(err).To(Not(HaveOccurred()))
61-
})
6262

63-
AfterEach(func() {
64-
// TODO(user): Attention if you improve this code by adding other context test you MUST
65-
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
66-
By("Deleting the Namespace to perform the tests")
67-
_ = k8sClient.Delete(ctx, namespace)
68-
69-
By("Removing the Image ENV VAR which stores the Operand image")
70-
_ = os.Unsetenv("MEMCACHED_IMAGE")
71-
})
72-
73-
It("should successfully reconcile a custom resource for Memcached", func() {
74-
By("Creating the custom resource for the Kind Memcached")
75-
memcached := &examplecomv1alpha1.Memcached{}
76-
err := k8sClient.Get(ctx, typeNamespaceName, memcached)
63+
By("creating the custom resource for the Kind Memcached")
64+
err = k8sClient.Get(ctx, typeNamespaceName, memcached)
7765
if err != nil && errors.IsNotFound(err) {
7866
// Let's mock our custom resource at the same way that we would
7967
// apply on the cluster the manifest under config/samples
@@ -91,7 +79,29 @@ var _ = Describe("Memcached controller", func() {
9179
err = k8sClient.Create(ctx, memcached)
9280
Expect(err).To(Not(HaveOccurred()))
9381
}
82+
})
83+
84+
AfterEach(func() {
85+
By("removing the custom resource for the Kind Memcached")
86+
found := &examplecomv1alpha1.Memcached{}
87+
err := k8sClient.Get(ctx, typeNamespaceName, found)
88+
Expect(err).To(Not(HaveOccurred()))
89+
90+
Eventually(func() error {
91+
return k8sClient.Delete(context.TODO(), found)
92+
}, 2*time.Minute, time.Second).Should(Succeed())
9493

94+
// TODO(user): Attention if you improve this code by adding other context test you MUST
95+
// be aware of the current delete namespace limitations.
96+
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
97+
By("Deleting the Namespace to perform the tests")
98+
_ = k8sClient.Delete(ctx, namespace)
99+
100+
By("Removing the Image ENV VAR which stores the Operand image")
101+
_ = os.Unsetenv("MEMCACHED_IMAGE")
102+
})
103+
104+
It("should successfully reconcile a custom resource for Memcached", func() {
95105
By("Checking if the custom resource was successfully created")
96106
Eventually(func() error {
97107
found := &examplecomv1alpha1.Memcached{}
@@ -104,7 +114,7 @@ var _ = Describe("Memcached controller", func() {
104114
Scheme: k8sClient.Scheme(),
105115
}
106116

107-
_, err = memcachedReconciler.Reconcile(ctx, reconcile.Request{
117+
_, err := memcachedReconciler.Reconcile(ctx, reconcile.Request{
108118
NamespacedName: typeNamespaceName,
109119
})
110120
Expect(err).To(Not(HaveOccurred()))

0 commit comments

Comments
 (0)