Skip to content

Commit 5694273

Browse files
committed
Use expressive Gomega matchers with Eventually
The matchers used with `Eventually` checks boiled down to a boolean comparison, which is often unhelpful when debugging. This change uses more detailed Gomega matchers in order to aid developers making changes. Also, all Eventually calls now use anonymous functions, in order to assure that the conditions they're checking are actually invoked multiple times. Signed-off-by: Nolan Brubaker <[email protected]>
1 parent 6cbe9c1 commit 5694273

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

pkg/controllers/cloud_config_sync_controller_test.go

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,16 @@ var _ = Describe("Cloud config sync controller", func() {
224224
co := &configv1.ClusterOperator{}
225225
err := cl.Get(context.Background(), client.ObjectKey{Name: clusterOperatorName}, co)
226226
if err == nil || !apierrors.IsNotFound(err) {
227-
Eventually(func() bool {
228-
err := cl.Delete(context.Background(), co)
229-
return err == nil || apierrors.IsNotFound(err)
230-
}).Should(BeTrue())
227+
Eventually(func() error {
228+
return cl.Delete(context.Background(), co)
229+
}).Should(SatisfyAny(
230+
Not(HaveOccurred()),
231+
MatchError(apierrors.IsNotFound, "IsNotFound"),
232+
))
231233
}
232-
Eventually(apierrors.IsNotFound(cl.Get(context.Background(), client.ObjectKey{Name: clusterOperatorName}, co))).Should(BeTrue())
234+
Eventually(func() error {
235+
return cl.Get(context.Background(), client.ObjectKey{Name: clusterOperatorName}, co)
236+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
233237

234238
By("Cleanup resources")
235239
deleteOptions := &client.DeleteOptions{
@@ -240,27 +244,26 @@ var _ = Describe("Cloud config sync controller", func() {
240244
Expect(cl.List(ctx, allCMs)).To(Succeed())
241245
for _, cm := range allCMs.Items {
242246
Expect(cl.Delete(ctx, cm.DeepCopy(), deleteOptions)).To(Succeed())
243-
Eventually(
244-
apierrors.IsNotFound(cl.Get(ctx, client.ObjectKeyFromObject(cm.DeepCopy()), &corev1.ConfigMap{})),
245-
).Should(BeTrue())
247+
Eventually(func() error {
248+
return cl.Get(ctx, client.ObjectKeyFromObject(cm.DeepCopy()), &corev1.ConfigMap{})
249+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
246250
}
247251

248252
infraCloudConfig = nil
249253
managedCloudConfig = nil
250254

251255
infra := makeInfrastructureResource(configv1.AzurePlatformType)
252256
Expect(cl.Delete(ctx, infra)).To(Succeed())
253-
Eventually(
254-
apierrors.IsNotFound(cl.Get(ctx, client.ObjectKeyFromObject(infra), infra)),
255-
).Should(BeTrue())
257+
Eventually(func() error {
258+
return cl.Get(ctx, client.ObjectKeyFromObject(infra), infra)
259+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
256260

257261
networkResource := makeNetworkResource()
258262
Expect(cl.Delete(ctx, networkResource)).To(Succeed())
259263

260-
Eventually(func() bool {
261-
err := cl.Get(ctx, client.ObjectKeyFromObject(networkResource), networkResource)
262-
return apierrors.IsNotFound(err)
263-
}).Should(BeTrue(), "Expected not found error")
264+
Eventually(func() error {
265+
return cl.Get(ctx, client.ObjectKeyFromObject(networkResource), networkResource)
266+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
264267
})
265268

266269
It("config should be synced up after first reconcile", func() {
@@ -302,13 +305,11 @@ var _ = Describe("Cloud config sync controller", func() {
302305
}).Should(Equal(defaultAzureConfig))
303306

304307
Expect(cl.Delete(ctx, syncedCloudConfigMap)).To(Succeed())
305-
Eventually(func() (bool, error) {
308+
Eventually(func(g Gomega) string {
306309
err := cl.Get(ctx, syncedConfigMapKey, syncedCloudConfigMap)
307-
if err != nil {
308-
return false, err
309-
}
310-
return syncedCloudConfigMap.Data[defaultConfigKey] == defaultAzureConfig, nil
311-
}).Should(BeTrue())
310+
g.Expect(err).NotTo(HaveOccurred())
311+
return syncedCloudConfigMap.Data[defaultConfigKey]
312+
}).Should(Equal(defaultAzureConfig))
312313
})
313314

314315
It("config should not be updated if source and target config content are identical", func() {
@@ -463,12 +464,16 @@ var _ = Describe("Cloud config sync reconciler", func() {
463464
co := &configv1.ClusterOperator{}
464465
err := cl.Get(context.Background(), client.ObjectKey{Name: clusterOperatorName}, co)
465466
if err == nil || !apierrors.IsNotFound(err) {
466-
Eventually(func() bool {
467-
err := cl.Delete(context.Background(), co)
468-
return err == nil || apierrors.IsNotFound(err)
469-
}).Should(BeTrue())
467+
Eventually(func() error {
468+
return cl.Delete(context.Background(), co)
469+
}).Should(SatisfyAny(
470+
Not(HaveOccurred()),
471+
MatchError(apierrors.IsNotFound, "IsNotFound"),
472+
))
470473
}
471-
Eventually(apierrors.IsNotFound(cl.Get(context.Background(), client.ObjectKey{Name: clusterOperatorName}, co))).Should(BeTrue())
474+
Eventually(func() error {
475+
return cl.Get(context.Background(), client.ObjectKey{Name: clusterOperatorName}, co)
476+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
472477

473478
infra := &configv1.Infrastructure{
474479
ObjectMeta: metav1.ObjectMeta{
@@ -477,25 +482,24 @@ var _ = Describe("Cloud config sync reconciler", func() {
477482
}
478483
// omitted error intentionally, 404 might be there for some cases
479484
cl.Delete(ctx, infra) //nolint:errcheck
480-
Eventually(
481-
apierrors.IsNotFound(cl.Get(ctx, client.ObjectKeyFromObject(infra), infra)),
482-
).Should(BeTrue())
485+
Eventually(func() error {
486+
return cl.Get(ctx, client.ObjectKeyFromObject(infra), infra)
487+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
483488

484489
networkResource := makeNetworkResource()
485490
Expect(cl.Delete(ctx, networkResource)).To(Succeed())
486491

487-
Eventually(func() bool {
488-
err := cl.Get(ctx, client.ObjectKeyFromObject(networkResource), networkResource)
489-
return apierrors.IsNotFound(err)
490-
}).Should(BeTrue(), "Expected not found error")
492+
Eventually(func() error {
493+
return cl.Get(ctx, client.ObjectKeyFromObject(networkResource), networkResource)
494+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
491495

492496
allCMs := &corev1.ConfigMapList{}
493497
Expect(cl.List(ctx, allCMs)).To(Succeed())
494498
for _, cm := range allCMs.Items {
495499
Expect(cl.Delete(ctx, cm.DeepCopy(), deleteOptions)).To(Succeed())
496-
Eventually(
497-
apierrors.IsNotFound(cl.Get(ctx, client.ObjectKeyFromObject(cm.DeepCopy()), &corev1.ConfigMap{})),
498-
).Should(BeTrue())
500+
Eventually(func() error {
501+
return cl.Get(ctx, client.ObjectKeyFromObject(cm.DeepCopy()), &corev1.ConfigMap{})
502+
}).Should(MatchError(apierrors.IsNotFound, "IsNotFound"))
499503
}
500504
})
501505
})

0 commit comments

Comments
 (0)