Skip to content

Commit e0e08f4

Browse files
committed
Impove deploy-image controller-test.go
* To(Not()) → NotTo() * Use default eventually timeouts * Eventually(func() error) → Eventually(func(g Gomega)) * Use Gomega assertions for the condition check, which is more robust And then use g.Expect inside those Eventually() functions
1 parent 27f4acc commit e0e08f4

File tree

5 files changed

+145
-160
lines changed

5 files changed

+145
-160
lines changed

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
105105
}
106106
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
107107
108+
SetDefaultEventuallyTimeout(2 * time.Minute)
109+
SetDefaultEventuallyPollingInterval(time.Second)
110+
108111
BeforeEach(func() {
109112
By("Creating the Namespace to perform the tests")
110113
err := k8sClient.Create(ctx, namespace);
111-
Expect(err).To(Not(HaveOccurred()))
114+
Expect(err).NotTo(HaveOccurred())
112115
113116
By("Setting the Image ENV VAR which stores the Operand image")
114117
err= os.Setenv("{{ upper .Resource.Kind }}_IMAGE", "example.com/image:test")
115-
Expect(err).To(Not(HaveOccurred()))
118+
Expect(err).NotTo(HaveOccurred())
116119
117120
By("creating the custom resource for the Kind {{ .Resource.Kind }}")
118121
err = k8sClient.Get(ctx, typeNamespacedName, {{ lower .Resource.Kind }})
@@ -133,19 +136,19 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
133136
}
134137
135138
err = k8sClient.Create(ctx, {{ lower .Resource.Kind }})
136-
Expect(err).To(Not(HaveOccurred()))
139+
Expect(err).NotTo(HaveOccurred())
137140
}
138141
})
139142
140143
AfterEach(func() {
141144
By("removing the custom resource for the Kind {{ .Resource.Kind }}")
142145
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
143146
err := k8sClient.Get(ctx, typeNamespacedName, found)
144-
Expect(err).To(Not(HaveOccurred()))
147+
Expect(err).NotTo(HaveOccurred())
145148
146-
Eventually(func() error {
147-
return k8sClient.Delete(context.TODO(), found)
148-
}, 2*time.Minute, time.Second).Should(Succeed())
149+
Eventually(func(g Gomega) {
150+
g.Expect(k8sClient.Delete(context.TODO(), found)).To(Succeed())
151+
}).Should(Succeed())
149152
150153
// TODO(user): Attention if you improve this code by adding other context test you MUST
151154
// be aware of the current delete namespace limitations.
@@ -159,10 +162,10 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
159162
160163
It("should successfully reconcile a custom resource for {{ .Resource.Kind }}", func() {
161164
By("Checking if the custom resource was successfully created")
162-
Eventually(func() error {
165+
Eventually(func(g Gomega) {
163166
found := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
164-
return k8sClient.Get(ctx, typeNamespacedName, found)
165-
}, time.Minute, time.Second).Should(Succeed())
167+
Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
168+
}).Should(Succeed())
166169
167170
By("Reconciling the custom resource created")
168171
{{ lower .Resource.Kind }}Reconciler := &{{ .Resource.Kind }}Reconciler{
@@ -173,34 +176,28 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
173176
_, err := {{ lower .Resource.Kind }}Reconciler.Reconcile(ctx, reconcile.Request{
174177
NamespacedName: typeNamespacedName,
175178
})
176-
Expect(err).To(Not(HaveOccurred()))
179+
Expect(err).NotTo(HaveOccurred())
177180
178181
By("Checking if Deployment was successfully created in the reconciliation")
179-
Eventually(func() error {
182+
Eventually(func(g Gomega) {
180183
found := &appsv1.Deployment{}
181-
return k8sClient.Get(ctx, typeNamespacedName, found)
182-
}, time.Minute, time.Second).Should(Succeed())
184+
Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
185+
}).Should(Succeed())
183186
184187
By("Checking the latest Status Condition added to the {{ .Resource.Kind }} instance")
185-
Eventually(func() error {
186-
if {{ lower .Resource.Kind }}.Status.Conditions != nil &&
187-
len({{ lower .Resource.Kind }}.Status.Conditions) != 0 {
188-
latestStatusCondition := {{ lower .Resource.Kind }}.Status.Conditions[len({{ lower .Resource.Kind }}.Status.Conditions)-1]
189-
expectedLatestStatusCondition := metav1.Condition{
190-
Type: typeAvailable{{ .Resource.Kind }},
191-
Status: metav1.ConditionTrue,
192-
Reason: "Reconciling",
193-
Message: fmt.Sprintf(
194-
"Deployment for custom resource (%s) with %d replicas created successfully",
195-
{{ lower .Resource.Kind }}.Name,
196-
{{ lower .Resource.Kind }}.Spec.Size),
197-
}
198-
if latestStatusCondition != expectedLatestStatusCondition {
199-
return fmt.Errorf("The latest status condition added to the {{ .Resource.Kind }} instance is not as expected")
200-
}
188+
Eventually(func(g Gomega) {
189+
reconcilingTrue := metav1.Condition{
190+
Type: typeAvailable{{ .Resource.Kind }},
191+
Status: metav1.ConditionTrue,
192+
Reason: "Reconciling",
193+
Message: fmt.Sprintf(
194+
"Deployment for custom resource (%s) with %d replicas created successfully",
195+
{{ lower .Resource.Kind }}.Name,
196+
{{ lower .Resource.Kind }}.Spec.Size),
201197
}
202-
return nil
203-
}, time.Minute, time.Second).Should(Succeed())
198+
g.Expect({{ lower .Resource.Kind }}.Status.Conditions).To(
199+
ContainElement(Equal(reconcilingTrue)))
200+
}).Should(Succeed())
204201
})
205202
})
206203
})

testdata/project-v4-multigroup/internal/controller/example.com/busybox_controller_test.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ var _ = Describe("Busybox controller", func() {
5555
}
5656
busybox := &examplecomv1alpha1.Busybox{}
5757

58+
SetDefaultEventuallyTimeout(2 * time.Minute)
59+
SetDefaultEventuallyPollingInterval(time.Second)
60+
5861
BeforeEach(func() {
5962
By("Creating the Namespace to perform the tests")
6063
err := k8sClient.Create(ctx, namespace)
61-
Expect(err).To(Not(HaveOccurred()))
64+
Expect(err).NotTo(HaveOccurred())
6265

6366
By("Setting the Image ENV VAR which stores the Operand image")
6467
err = os.Setenv("BUSYBOX_IMAGE", "example.com/image:test")
65-
Expect(err).To(Not(HaveOccurred()))
68+
Expect(err).NotTo(HaveOccurred())
6669

6770
By("creating the custom resource for the Kind Busybox")
6871
err = k8sClient.Get(ctx, typeNamespacedName, busybox)
@@ -80,19 +83,19 @@ var _ = Describe("Busybox controller", func() {
8083
}
8184

8285
err = k8sClient.Create(ctx, busybox)
83-
Expect(err).To(Not(HaveOccurred()))
86+
Expect(err).NotTo(HaveOccurred())
8487
}
8588
})
8689

8790
AfterEach(func() {
8891
By("removing the custom resource for the Kind Busybox")
8992
found := &examplecomv1alpha1.Busybox{}
9093
err := k8sClient.Get(ctx, typeNamespacedName, found)
91-
Expect(err).To(Not(HaveOccurred()))
94+
Expect(err).NotTo(HaveOccurred())
9295

93-
Eventually(func() error {
94-
return k8sClient.Delete(context.TODO(), found)
95-
}, 2*time.Minute, time.Second).Should(Succeed())
96+
Eventually(func(g Gomega) {
97+
g.Expect(k8sClient.Delete(context.TODO(), found)).To(Succeed())
98+
}).Should(Succeed())
9699

97100
// TODO(user): Attention if you improve this code by adding other context test you MUST
98101
// be aware of the current delete namespace limitations.
@@ -106,10 +109,10 @@ var _ = Describe("Busybox controller", func() {
106109

107110
It("should successfully reconcile a custom resource for Busybox", func() {
108111
By("Checking if the custom resource was successfully created")
109-
Eventually(func() error {
112+
Eventually(func(g Gomega) {
110113
found := &examplecomv1alpha1.Busybox{}
111-
return k8sClient.Get(ctx, typeNamespacedName, found)
112-
}, time.Minute, time.Second).Should(Succeed())
114+
Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
115+
}).Should(Succeed())
113116

114117
By("Reconciling the custom resource created")
115118
busyboxReconciler := &BusyboxReconciler{
@@ -120,34 +123,28 @@ var _ = Describe("Busybox controller", func() {
120123
_, err := busyboxReconciler.Reconcile(ctx, reconcile.Request{
121124
NamespacedName: typeNamespacedName,
122125
})
123-
Expect(err).To(Not(HaveOccurred()))
126+
Expect(err).NotTo(HaveOccurred())
124127

125128
By("Checking if Deployment was successfully created in the reconciliation")
126-
Eventually(func() error {
129+
Eventually(func(g Gomega) {
127130
found := &appsv1.Deployment{}
128-
return k8sClient.Get(ctx, typeNamespacedName, found)
129-
}, time.Minute, time.Second).Should(Succeed())
131+
Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
132+
}).Should(Succeed())
130133

131134
By("Checking the latest Status Condition added to the Busybox instance")
132-
Eventually(func() error {
133-
if busybox.Status.Conditions != nil &&
134-
len(busybox.Status.Conditions) != 0 {
135-
latestStatusCondition := busybox.Status.Conditions[len(busybox.Status.Conditions)-1]
136-
expectedLatestStatusCondition := metav1.Condition{
137-
Type: typeAvailableBusybox,
138-
Status: metav1.ConditionTrue,
139-
Reason: "Reconciling",
140-
Message: fmt.Sprintf(
141-
"Deployment for custom resource (%s) with %d replicas created successfully",
142-
busybox.Name,
143-
busybox.Spec.Size),
144-
}
145-
if latestStatusCondition != expectedLatestStatusCondition {
146-
return fmt.Errorf("The latest status condition added to the Busybox instance is not as expected")
147-
}
135+
Eventually(func(g Gomega) {
136+
reconcilingTrue := metav1.Condition{
137+
Type: typeAvailableBusybox,
138+
Status: metav1.ConditionTrue,
139+
Reason: "Reconciling",
140+
Message: fmt.Sprintf(
141+
"Deployment for custom resource (%s) with %d replicas created successfully",
142+
busybox.Name,
143+
busybox.Spec.Size),
148144
}
149-
return nil
150-
}, time.Minute, time.Second).Should(Succeed())
145+
g.Expect(busybox.Status.Conditions).To(
146+
ContainElement(Equal(reconcilingTrue)))
147+
}).Should(Succeed())
151148
})
152149
})
153150
})

testdata/project-v4-multigroup/internal/controller/example.com/memcached_controller_test.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,17 @@ var _ = Describe("Memcached controller", func() {
5555
}
5656
memcached := &examplecomv1alpha1.Memcached{}
5757

58+
SetDefaultEventuallyTimeout(2 * time.Minute)
59+
SetDefaultEventuallyPollingInterval(time.Second)
60+
5861
BeforeEach(func() {
5962
By("Creating the Namespace to perform the tests")
6063
err := k8sClient.Create(ctx, namespace)
61-
Expect(err).To(Not(HaveOccurred()))
64+
Expect(err).NotTo(HaveOccurred())
6265

6366
By("Setting the Image ENV VAR which stores the Operand image")
6467
err = os.Setenv("MEMCACHED_IMAGE", "example.com/image:test")
65-
Expect(err).To(Not(HaveOccurred()))
68+
Expect(err).NotTo(HaveOccurred())
6669

6770
By("creating the custom resource for the Kind Memcached")
6871
err = k8sClient.Get(ctx, typeNamespacedName, memcached)
@@ -81,19 +84,19 @@ var _ = Describe("Memcached controller", func() {
8184
}
8285

8386
err = k8sClient.Create(ctx, memcached)
84-
Expect(err).To(Not(HaveOccurred()))
87+
Expect(err).NotTo(HaveOccurred())
8588
}
8689
})
8790

8891
AfterEach(func() {
8992
By("removing the custom resource for the Kind Memcached")
9093
found := &examplecomv1alpha1.Memcached{}
9194
err := k8sClient.Get(ctx, typeNamespacedName, found)
92-
Expect(err).To(Not(HaveOccurred()))
95+
Expect(err).NotTo(HaveOccurred())
9396

94-
Eventually(func() error {
95-
return k8sClient.Delete(context.TODO(), found)
96-
}, 2*time.Minute, time.Second).Should(Succeed())
97+
Eventually(func(g Gomega) {
98+
g.Expect(k8sClient.Delete(context.TODO(), found)).To(Succeed())
99+
}).Should(Succeed())
97100

98101
// TODO(user): Attention if you improve this code by adding other context test you MUST
99102
// be aware of the current delete namespace limitations.
@@ -107,10 +110,10 @@ var _ = Describe("Memcached controller", func() {
107110

108111
It("should successfully reconcile a custom resource for Memcached", func() {
109112
By("Checking if the custom resource was successfully created")
110-
Eventually(func() error {
113+
Eventually(func(g Gomega) {
111114
found := &examplecomv1alpha1.Memcached{}
112-
return k8sClient.Get(ctx, typeNamespacedName, found)
113-
}, time.Minute, time.Second).Should(Succeed())
115+
Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
116+
}).Should(Succeed())
114117

115118
By("Reconciling the custom resource created")
116119
memcachedReconciler := &MemcachedReconciler{
@@ -121,34 +124,28 @@ var _ = Describe("Memcached controller", func() {
121124
_, err := memcachedReconciler.Reconcile(ctx, reconcile.Request{
122125
NamespacedName: typeNamespacedName,
123126
})
124-
Expect(err).To(Not(HaveOccurred()))
127+
Expect(err).NotTo(HaveOccurred())
125128

126129
By("Checking if Deployment was successfully created in the reconciliation")
127-
Eventually(func() error {
130+
Eventually(func(g Gomega) {
128131
found := &appsv1.Deployment{}
129-
return k8sClient.Get(ctx, typeNamespacedName, found)
130-
}, time.Minute, time.Second).Should(Succeed())
132+
Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
133+
}).Should(Succeed())
131134

132135
By("Checking the latest Status Condition added to the Memcached instance")
133-
Eventually(func() error {
134-
if memcached.Status.Conditions != nil &&
135-
len(memcached.Status.Conditions) != 0 {
136-
latestStatusCondition := memcached.Status.Conditions[len(memcached.Status.Conditions)-1]
137-
expectedLatestStatusCondition := metav1.Condition{
138-
Type: typeAvailableMemcached,
139-
Status: metav1.ConditionTrue,
140-
Reason: "Reconciling",
141-
Message: fmt.Sprintf(
142-
"Deployment for custom resource (%s) with %d replicas created successfully",
143-
memcached.Name,
144-
memcached.Spec.Size),
145-
}
146-
if latestStatusCondition != expectedLatestStatusCondition {
147-
return fmt.Errorf("The latest status condition added to the Memcached instance is not as expected")
148-
}
136+
Eventually(func(g Gomega) {
137+
reconcilingTrue := metav1.Condition{
138+
Type: typeAvailableMemcached,
139+
Status: metav1.ConditionTrue,
140+
Reason: "Reconciling",
141+
Message: fmt.Sprintf(
142+
"Deployment for custom resource (%s) with %d replicas created successfully",
143+
memcached.Name,
144+
memcached.Spec.Size),
149145
}
150-
return nil
151-
}, time.Minute, time.Second).Should(Succeed())
146+
g.Expect(memcached.Status.Conditions).To(
147+
ContainElement(Equal(reconcilingTrue)))
148+
}).Should(Succeed())
152149
})
153150
})
154151
})

0 commit comments

Comments
 (0)