Skip to content

chore(test): Added CustomMatcher for asserting on k8 errors for better readability #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions test/e2e/bundle_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ var _ = Describe("Installing bundles with new object types", func() {
err = json.Unmarshal(data, &vpaCRD)
Expect(err).ToNot(HaveOccurred(), "could not convert vpa crd to unstructured")

Eventually(func() error {
Eventually(func() bool {
err := ctx.Ctx().Client().Create(context.TODO(), &vpaCRD)
if err != nil {
if !k8serrors.IsAlreadyExists(err) {
return err
}
return k8serrors.IsAlreadyExists(err)
}
return nil
}).Should(Succeed())
return true
}).Should(BeTrue())

// ensure vpa crd is established and accepted on the cluster before continuing
Eventually(func() (bool, error) {
Expand Down
23 changes: 21 additions & 2 deletions test/e2e/gc_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package e2e
import (
"context"
"fmt"
"reflect"
"runtime"
"strings"

"github.com/blang/semver"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/operator-framework/operator-lifecycle-manager/test/e2e/dsl"
"github.com/onsi/gomega/types"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -21,8 +24,11 @@ import (
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
. "github.com/operator-framework/operator-lifecycle-manager/test/e2e/dsl"
)

const k8ErrorImportPath = "k8s.io/apimachinery/pkg/api/errors."

var _ = Describe("Garbage collection for dependent resources", func() {
var (
kubeClient operatorclient.ClientInterface
Expand Down Expand Up @@ -276,7 +282,7 @@ var _ = Describe("Garbage collection for dependent resources", func() {
It("should have deleted the dependent since both the owners were deleted", func() {
_, err := kubeClient.KubernetesInterface().CoreV1().ConfigMaps(testNamespace).Get(context.TODO(), dependent.GetName(), metav1.GetOptions{})
Expect(err).To(HaveOccurred())
Expect(k8serrors.IsNotFound(err)).To(BeTrue())
Expect(k8serrors.IsNotFound).Should(assertOnk8Error("IsNotFound", err))
ctx.Ctx().Logf("dependent successfully garbage collected after both owners were deleted")
})

Expand Down Expand Up @@ -602,3 +608,16 @@ var _ = Describe("Garbage collection for dependent resources", func() {
})
})
})

// assertOnk8Error validates that the actual error passed in matches the expected k8 error
// using gomega's matcher
func assertOnk8Error(expectedk8Error string, actualError error) types.GomegaMatcher {
return WithTransform(func(f func(e error) bool) string {
var errFuncName string
if f(actualError) {
errFuncName = runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://golang.org/pkg/runtime/#FuncForPC can also return nil, which would cause a panic and force users to stop and understand the implementation of this function. Magic like reflection needs to be extra robust because it's so much harder to understand when it breaks.

errFuncName = strings.Split(errFuncName, k8ErrorImportPath)[1]
}
return errFuncName
}, Equal(expectedk8Error))
}
5 changes: 3 additions & 2 deletions test/e2e/scoped_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -61,7 +62,7 @@ var _ = Describe("Scoped Client", func() {
// lack of permission.
name: "ServiceAccountDoesNotHaveAnyPermission",
assertFunc: func(errGot error) {
require.True(GinkgoT(), k8serrors.IsForbidden(errGot))
Expect(k8serrors.IsForbidden).Should(assertOnk8Error("IsForbidden", errGot))
},
}),
table.Entry("ServiceAccountHasPermission", testParameter{
Expand All @@ -73,7 +74,7 @@ var _ = Describe("Scoped Client", func() {
return
},
assertFunc: func(errGot error) {
require.True(GinkgoT(), k8serrors.IsNotFound(errGot))
Expect(k8serrors.IsNotFound).Should(assertOnk8Error("IsNotFound", errGot))
},
}),
}
Expand Down