@@ -109,6 +109,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
109
109
}
110
110
111
111
typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name}
112
+ {{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
112
113
113
114
BeforeEach(func() {
114
115
By("Creating the Namespace to perform the tests")
@@ -118,22 +119,9 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
118
119
By("Setting the Image ENV VAR which stores the Operand image")
119
120
err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test")
120
121
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
- })
132
122
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 }})
137
125
if err != nil && errors.IsNotFound(err) {
138
126
// Let's mock our custom resource at the same way that we would
139
127
// apply on the cluster the manifest under config/samples
@@ -152,8 +140,30 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
152
140
153
141
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
154
142
Expect(err).To(Not(HaveOccurred()))
155
- }
143
+ }
144
+ })
156
145
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() {
157
167
By("Checking if the custom resource was successfully created")
158
168
Eventually(func() error {
159
169
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
@@ -166,7 +176,7 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
166
176
Scheme: k8sClient.Scheme(),
167
177
}
168
178
169
- _, err = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
179
+ _, err : = {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
170
180
NamespacedName: typeNamespaceName,
171
181
})
172
182
Expect(err).To(Not(HaveOccurred()))
0 commit comments