Skip to content

Commit 1e03acd

Browse files
committed
Replace assertions within async assertions.
Functions passed to Gomega's async assertions (Eventually, Consistently, and friends) are called repeatedly until the first return value satisfies the given matcher and any other return values are their respective type's zero value. In several cases, functions passed to async assertions are themselves directly making assertions. If one of those fails, it can cause the entire test to fail immediately rather than continue retrying the async condition.
1 parent a90b83a commit 1e03acd

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

test/e2e/crd_e2e_test.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package e2e
33
import (
44
"context"
55
"fmt"
6+
"time"
7+
68
"github.com/blang/semver"
79
. "github.com/onsi/ginkgo"
810
. "github.com/onsi/gomega"
@@ -13,7 +15,6 @@ import (
1315
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
1416
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1517
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16-
"time"
1718
)
1819

1920
var _ = Describe("CRD Versions", func() {
@@ -313,14 +314,22 @@ var _ = Describe("CRD Versions", func() {
313314
Expect(s.Status.InstallPlanRef).ToNot(Equal(nil))
314315

315316
// Check the error on the installplan - should be related to data loss and the CRD upgrade missing a stored version
316-
Eventually(func() bool {
317-
ip, err := crc.OperatorsV1alpha1().InstallPlans(testNamespace).Get(context.TODO(), s.Status.InstallPlanRef.Name, metav1.GetOptions{})
318-
Expect(err).ToNot(HaveOccurred(), "could not get installplan")
319-
320-
Expect(ip.Status.Phase).To(Equal(operatorsv1alpha1.InstallPlanPhaseFailed))
321-
Expect(ip.Status.Conditions[len(ip.Status.Conditions)-1].Message).To(ContainSubstring("risk of data loss"))
322-
return true
323-
}).Should(BeTrue())
317+
Eventually(func() (*operatorsv1alpha1.InstallPlan, error) {
318+
return crc.OperatorsV1alpha1().InstallPlans(testNamespace).Get(context.TODO(), s.Status.InstallPlanRef.Name, metav1.GetOptions{})
319+
}).Should(And(
320+
WithTransform(
321+
func(v *operatorsv1alpha1.InstallPlan) operatorsv1alpha1.InstallPlanPhase {
322+
return v.Status.Phase
323+
},
324+
Equal(operatorsv1alpha1.InstallPlanPhaseFailed),
325+
),
326+
WithTransform(
327+
func(v *operatorsv1alpha1.InstallPlan) string {
328+
return v.Status.Conditions[len(v.Status.Conditions)-1].Message
329+
},
330+
ContainSubstring("risk of data loss"),
331+
),
332+
))
324333
})
325334

326335
// Create a CRD on cluster with v1alpha1 (storage)
@@ -369,7 +378,9 @@ var _ = Describe("CRD Versions", func() {
369378
// wrap CRD update in a poll because of the object has been modified related errors
370379
Eventually(func() error {
371380
oldCRD, err = c.ApiextensionsInterface().ApiextensionsV1beta1().CustomResourceDefinitions().Get(context.TODO(), oldCRD.GetName(), metav1.GetOptions{})
372-
Expect(err).ToNot(HaveOccurred(), "error getting old CRD")
381+
if err != nil {
382+
return err
383+
}
373384
GinkgoT().Logf("old crd status stored versions: %#v", oldCRD.Status.StoredVersions)
374385

375386
// set v1alpha1 to no longer served
@@ -448,12 +459,12 @@ var _ = Describe("CRD Versions", func() {
448459
Expect(catalogCSV.GetName()).To(Equal(subscription.Status.CurrentCSV))
449460

450461
// Check the error on the installplan - should be related to data loss and the CRD upgrade missing a stored version (v1alpha1)
451-
Eventually(func() bool {
452-
ip, err := crc.OperatorsV1alpha1().InstallPlans(testNamespace).Get(context.TODO(), subscription.Status.InstallPlanRef.Name, metav1.GetOptions{})
453-
Expect(err).ToNot(HaveOccurred(), "could not get installplan")
454-
455-
return ip.Status.Phase == operatorsv1alpha1.InstallPlanPhaseFailed
456-
}).Should(BeTrue())
462+
Eventually(func() (*operatorsv1alpha1.InstallPlan, error) {
463+
return crc.OperatorsV1alpha1().InstallPlans(testNamespace).Get(context.TODO(), subscription.Status.InstallPlanRef.Name, metav1.GetOptions{})
464+
}).Should(WithTransform(
465+
func(v *operatorsv1alpha1.InstallPlan) operatorsv1alpha1.InstallPlanPhase { return v.Status.Phase },
466+
Equal(operatorsv1alpha1.InstallPlanPhaseFailed),
467+
))
457468

458469
// update CRD status to remove the v1alpha1 stored version
459470
newCRD, err := c.ApiextensionsInterface().ApiextensionsV1beta1().CustomResourceDefinitions().Get(context.TODO(), oldCRD.GetName(), metav1.GetOptions{})

test/e2e/gc_e2e_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,15 @@ var _ = Describe("Garbage collection for dependent resources", func() {
458458
})
459459

460460
It("OLM should have upgraded associated configmap in place", func() {
461-
Eventually(func() bool {
461+
Eventually(func() (string, error) {
462462
cfg, err := kubeClient.GetConfigMap(testNamespace, configmapName)
463463
if err != nil {
464-
return false
464+
return "", err
465465
}
466466
// check data in configmap to ensure it is the new data (configmap was updated in the newer bundle)
467467
// new value in the configmap is "updated-very-much"
468-
data := cfg.Data["special.how"]
469-
return data == "updated-very-much"
470-
}).Should(BeTrue())
468+
return cfg.Data["special.how"], nil
469+
}).Should(Equal("updated-very-much"))
471470
ctx.Ctx().Logf("dependent successfully updated after csv owner was updated")
472471
})
473472
})

test/e2e/packagemanifest_e2e_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
. "github.com/onsi/ginkgo"
1010
. "github.com/onsi/gomega"
1111
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
12-
"k8s.io/apimachinery/pkg/api/errors"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"k8s.io/client-go/rest"
1514

@@ -264,11 +263,13 @@ var _ = Describe("Package Manifest API lists available Operators from Catalog So
264263
})
265264
It("should successfully update the CatalogSource field", func() {
266265

267-
Eventually(func() string {
266+
Eventually(func() (string, error) {
268267
pm, err := fetchPackageManifest(pmc, testNamespace, packageName,
269268
packageManifestHasStatus)
270-
Expect(err).NotTo(HaveOccurred(), "error getting package manifest after updating catsrc")
271-
return pm.Status.CatalogSourceDisplayName
269+
if err != nil {
270+
return "", err
271+
}
272+
return pm.Status.CatalogSourceDisplayName, nil
272273
}).Should(Equal(displayName))
273274
})
274275
})
@@ -286,11 +287,11 @@ func fetchPackageManifest(pmc pmversioned.Interface, namespace, name string, che
286287
var fetched *packagev1.PackageManifest
287288
var err error
288289

289-
Eventually(func() (bool, error) {
290+
EventuallyWithOffset(1, func() (bool, error) {
290291
ctx.Ctx().Logf("Polling...")
291292
fetched, err = pmc.OperatorsV1().PackageManifests(namespace).Get(context.TODO(), name, metav1.GetOptions{})
292-
if err != nil && !errors.IsNotFound(err) {
293-
return true, err
293+
if err != nil {
294+
return false, err
294295
}
295296
return check(fetched), nil
296297
}).Should(BeTrue())

0 commit comments

Comments
 (0)