Skip to content

Commit bbb191d

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 bbb191d

File tree

7 files changed

+173
-200
lines changed

7 files changed

+173
-200
lines changed

docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller_test.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,24 @@ var _ = Describe("Memcached Controller", func() {
8383
})
8484
Expect(err).NotTo(HaveOccurred())
8585
By("Checking if Deployment was successfully created in the reconciliation")
86-
Eventually(func() error {
86+
Eventually(func(g Gomega) {
8787
found := &appsv1.Deployment{}
88-
return k8sClient.Get(ctx, typeNamespacedName, found)
89-
}, time.Minute, time.Second).Should(Succeed())
88+
g.Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
89+
}).Should(Succeed())
9090

9191
By("Checking the latest Status Condition added to the Memcached instance")
92-
Eventually(func() error {
93-
if memcached.Status.Conditions != nil &&
94-
len(memcached.Status.Conditions) != 0 {
95-
latestStatusCondition := memcached.Status.Conditions[len(memcached.Status.Conditions)-1]
96-
expectedLatestStatusCondition := metav1.Condition{
97-
Type: typeAvailableMemcached,
98-
Status: metav1.ConditionTrue,
99-
Reason: "Reconciling",
100-
Message: fmt.Sprintf(
101-
"Deployment for custom resource (%s) with %d replicas created successfully",
102-
memcached.Name,
103-
memcached.Spec.Size),
104-
}
105-
if latestStatusCondition != expectedLatestStatusCondition {
106-
return fmt.Errorf("The latest status condition added to the Memcached instance is not as expected")
107-
}
92+
Eventually(func(g Gomega) {
93+
reconcilingTrue := metav1.Condition{
94+
Type: typeAvailableMemcached,
95+
Status: metav1.ConditionTrue,
96+
Reason: "Reconciling",
97+
Message: fmt.Sprintf(
98+
"Deployment for custom resource (%s) with %d replicas created successfully",
99+
memcached.Name,
100+
memcached.Spec.Size),
108101
}
109-
return nil
102+
g.Expect(memcached.Status.Conditions).To(
103+
ContainElement(Equal(reconcilingTrue)))
110104
}, time.Minute, time.Second).Should(Succeed())
111105
})
112106
})

hack/docs/internal/getting-started/generate_getting_started.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,24 @@ func (sp *Sample) updateControllerTest() {
7878
`// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
7979
// Example: If you expect a certain status condition after reconciliation, verify it here.`,
8080
`By("Checking if Deployment was successfully created in the reconciliation")
81-
Eventually(func() error {
81+
Eventually(func(g Gomega) {
8282
found := &appsv1.Deployment{}
83-
return k8sClient.Get(ctx, typeNamespacedName, found)
84-
}, time.Minute, time.Second).Should(Succeed())
83+
g.Expect(k8sClient.Get(ctx, typeNamespacedName, found)).To(Succeed())
84+
}).Should(Succeed())
8585
8686
By("Checking the latest Status Condition added to the Memcached instance")
87-
Eventually(func() error {
88-
if memcached.Status.Conditions != nil &&
89-
len(memcached.Status.Conditions) != 0 {
90-
latestStatusCondition := memcached.Status.Conditions[len(memcached.Status.Conditions)-1]
91-
expectedLatestStatusCondition := metav1.Condition{
92-
Type: typeAvailableMemcached,
93-
Status: metav1.ConditionTrue,
94-
Reason: "Reconciling",
95-
Message: fmt.Sprintf(
96-
"Deployment for custom resource (%s) with %d replicas created successfully",
97-
memcached.Name,
98-
memcached.Spec.Size),
99-
}
100-
if latestStatusCondition != expectedLatestStatusCondition {
101-
return fmt.Errorf("The latest status condition added to the Memcached instance is not as expected")
102-
}
87+
Eventually(func(g Gomega) {
88+
reconcilingTrue := metav1.Condition{
89+
Type: typeAvailableMemcached,
90+
Status: metav1.ConditionTrue,
91+
Reason: "Reconciling",
92+
Message: fmt.Sprintf(
93+
"Deployment for custom resource (%s) with %d replicas created successfully",
94+
memcached.Name,
95+
memcached.Spec.Size),
10396
}
104-
return nil
97+
g.Expect(memcached.Status.Conditions).To(
98+
ContainElement(Equal(reconcilingTrue)))
10599
}, time.Minute, time.Second).Should(Succeed())`,
106100
)
107101
hackutils.CheckError("add spec apis", err)

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
})

0 commit comments

Comments
 (0)