Skip to content

Commit 33de9da

Browse files
committed
Added CustomMatcher for asserting on k8 errors
1 parent 1fdd347 commit 33de9da

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

test/e2e/bundle_e2e_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,13 @@ var _ = Describe("Installing bundles with new object types", func() {
6464
err = json.Unmarshal(data, &vpaCRD)
6565
Expect(err).ToNot(HaveOccurred(), "could not convert vpa crd to unstructured")
6666

67-
Eventually(func() error {
67+
Eventually(func() bool {
6868
err := ctx.Ctx().Client().Create(context.TODO(), &vpaCRD)
6969
if err != nil {
70-
if !k8serrors.IsAlreadyExists(err) {
71-
return err
72-
}
70+
return k8serrors.IsAlreadyExists(err)
7371
}
74-
return nil
75-
}).Should(Succeed())
72+
return true
73+
}).Should(BeTrue())
7674

7775
// ensure vpa crd is established and accepted on the cluster before continuing
7876
Eventually(func() (bool, error) {

test/e2e/gc_e2e_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package e2e
33
import (
44
"context"
55
"fmt"
6+
"reflect"
7+
"runtime"
8+
"strings"
69

710
"github.com/blang/semver"
811
. "github.com/onsi/ginkgo"
912
. "github.com/onsi/gomega"
10-
. "github.com/operator-framework/operator-lifecycle-manager/test/e2e/dsl"
13+
"github.com/onsi/gomega/types"
1114
corev1 "k8s.io/api/core/v1"
1215
rbacv1 "k8s.io/api/rbac/v1"
1316
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -21,8 +24,11 @@ import (
2124
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
2225
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
2326
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
27+
. "github.com/operator-framework/operator-lifecycle-manager/test/e2e/dsl"
2428
)
2529

30+
const k8ErrorImportPath = "k8s.io/apimachinery/pkg/api/errors."
31+
2632
var _ = Describe("Garbage collection for dependent resources", func() {
2733
var (
2834
kubeClient operatorclient.ClientInterface
@@ -276,7 +282,7 @@ var _ = Describe("Garbage collection for dependent resources", func() {
276282
It("should have deleted the dependent since both the owners were deleted", func() {
277283
_, err := kubeClient.KubernetesInterface().CoreV1().ConfigMaps(testNamespace).Get(context.TODO(), dependent.GetName(), metav1.GetOptions{})
278284
Expect(err).To(HaveOccurred())
279-
Expect(k8serrors.IsNotFound(err)).To(BeTrue())
285+
Expect(k8serrors.IsNotFound).Should(assertOnk8Error("IsNotFound", err))
280286
ctx.Ctx().Logf("dependent successfully garbage collected after both owners were deleted")
281287
})
282288

@@ -602,3 +608,16 @@ var _ = Describe("Garbage collection for dependent resources", func() {
602608
})
603609
})
604610
})
611+
612+
// assertOnk8Error validates that the actual error passed in matches the expected k8 error
613+
// using gomega's matcher
614+
func assertOnk8Error(expectedk8Error string, actualError error) types.GomegaMatcher {
615+
return WithTransform(func(f func(e error) bool) string {
616+
var errFuncName string
617+
if f(actualError) {
618+
errFuncName = runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
619+
errFuncName = strings.Split(errFuncName, k8ErrorImportPath)[1]
620+
}
621+
return errFuncName
622+
}, Equal(expectedk8Error))
623+
}

test/e2e/scoped_client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
. "github.com/onsi/ginkgo"
88
"github.com/onsi/ginkgo/extensions/table"
9+
. "github.com/onsi/gomega"
910
"github.com/sirupsen/logrus"
1011
"github.com/stretchr/testify/require"
1112
corev1 "k8s.io/api/core/v1"
@@ -61,7 +62,7 @@ var _ = Describe("Scoped Client", func() {
6162
// lack of permission.
6263
name: "ServiceAccountDoesNotHaveAnyPermission",
6364
assertFunc: func(errGot error) {
64-
require.True(GinkgoT(), k8serrors.IsForbidden(errGot))
65+
Expect(k8serrors.IsForbidden).Should(assertOnk8Error("IsForbidden", errGot))
6566
},
6667
}),
6768
table.Entry("ServiceAccountHasPermission", testParameter{
@@ -73,7 +74,7 @@ var _ = Describe("Scoped Client", func() {
7374
return
7475
},
7576
assertFunc: func(errGot error) {
76-
require.True(GinkgoT(), k8serrors.IsNotFound(errGot))
77+
Expect(k8serrors.IsNotFound).Should(assertOnk8Error("IsNotFound", errGot))
7778
},
7879
}),
7980
}

0 commit comments

Comments
 (0)