Skip to content

Commit 527290f

Browse files
committed
Add olm gomega assertions and matchers
Signed-off-by: perdasilva <[email protected]>
1 parent 61b498b commit 527290f

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

Diff for: test/e2e/util/gomega/assertions.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package gomega
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/gomega"
7+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
8+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/util"
9+
k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client"
10+
)
11+
12+
func EventuallyResource(actual k8scontrollerclient.Object, intervals ...interface{}) AsyncAssertion {
13+
client := ctx.Ctx().Client()
14+
clone := actual.DeepCopyObject().(k8scontrollerclient.Object)
15+
key := k8scontrollerclient.ObjectKeyFromObject(actual)
16+
getObjectFn := func() k8scontrollerclient.Object {
17+
if err := client.Get(context.Background(), key, clone); err != nil {
18+
util.Logf("ERROR getting resource '%s'", util.ObjectToJsonString(key), err)
19+
return nil
20+
}
21+
return clone
22+
}
23+
return Eventually(getObjectFn, intervals...)
24+
}

Diff for: test/e2e/util/gomega/catalogsource_matchers.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gomega
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/onsi/gomega/matchers"
7+
"github.com/onsi/gomega/types"
8+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
9+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/util"
10+
"google.golang.org/grpc/connectivity"
11+
)
12+
13+
func HaveGrpcConnectionWithLastConnectionState(state connectivity.State) types.GomegaMatcher {
14+
return &CatalogSourceGrpcConnectionLastConnectionStateMatcher{
15+
EqualMatcher: &matchers.EqualMatcher{
16+
Expected: state.String(),
17+
},
18+
}
19+
}
20+
21+
type CatalogSourceGrpcConnectionLastConnectionStateMatcher struct {
22+
*matchers.EqualMatcher
23+
}
24+
25+
func (s *CatalogSourceGrpcConnectionLastConnectionStateMatcher) FailureMessage(actual interface{}) (message string) {
26+
return fmt.Sprintf("expected catalog source status.grpcConnectionState.lastObservedState to be '%s'\n%s\n", s.Expected, util.ObjectToPrettyJsonString(actual))
27+
}
28+
29+
func (s *CatalogSourceGrpcConnectionLastConnectionStateMatcher) NegatedFailureMessage(actual interface{}) (message string) {
30+
return fmt.Sprintf("expected catalog status.grpcConnectionState.lastObservedState to NOT be '%s':\n%s\n ", s.Expected, util.ObjectToPrettyJsonString(actual))
31+
}
32+
33+
func (s *CatalogSourceGrpcConnectionLastConnectionStateMatcher) Match(actual interface{}) (bool, error) {
34+
switch actual := actual.(type) {
35+
case *v1alpha1.CatalogSource:
36+
if actual.Status.GRPCConnectionState == nil {
37+
return false, fmt.Errorf("catalog source does not have a grpc connection state")
38+
}
39+
util.Logf("expecting catalog source last connection state '%s' to be '%s'", actual.Status.GRPCConnectionState.LastObservedState, s.Expected)
40+
return s.EqualMatcher.Match(actual.Status.GRPCConnectionState.LastObservedState)
41+
default:
42+
return false, fmt.Errorf("actual %v is not a subscription", actual)
43+
}
44+
}

Diff for: test/e2e/util/gomega/subscription_matchers.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package gomega
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/onsi/gomega/matchers"
7+
"github.com/onsi/gomega/types"
8+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
9+
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/util"
10+
)
11+
12+
func HaveSubscriptionState(state v1alpha1.SubscriptionState) types.GomegaMatcher {
13+
return &SubscriptionStateMatcher{
14+
EqualMatcher: &matchers.EqualMatcher{
15+
Expected: state,
16+
},
17+
}
18+
}
19+
20+
type SubscriptionStateMatcher struct {
21+
*matchers.EqualMatcher
22+
}
23+
24+
func (s *SubscriptionStateMatcher) FailureMessage(actual interface{}) (message string) {
25+
return fmt.Sprintf("expected subscription status.subscriptionState to be '%s':\n%s\n", s.Expected, util.ObjectToJsonString(actual))
26+
}
27+
28+
func (s *SubscriptionStateMatcher) NegatedFailureMessage(actual interface{}) (message string) {
29+
return fmt.Sprintf("expected subscription status.subscriptionState to NOT be '%s'\n%s\n", s.Expected, util.ObjectToJsonString(actual))
30+
}
31+
32+
func (s *SubscriptionStateMatcher) Match(actual interface{}) (bool, error) {
33+
switch actual := actual.(type) {
34+
case *v1alpha1.Subscription:
35+
util.Logf("expecting subscription state '%s' to be '%s'", actual.Status.State, s.Expected)
36+
return s.EqualMatcher.Match(actual.Status.State)
37+
default:
38+
return false, fmt.Errorf("actual %v is not a subscription", actual)
39+
}
40+
}
41+
42+
type ContainSubscriptionConditionOfTypeMatcher struct {
43+
*matchers.ContainElementMatcher
44+
}
45+
46+
func ContainSubscriptionConditionOfType(conditionType v1alpha1.SubscriptionConditionType) types.GomegaMatcher {
47+
return &ContainSubscriptionConditionOfTypeMatcher{
48+
ContainElementMatcher: &matchers.ContainElementMatcher{
49+
Element: conditionType,
50+
},
51+
}
52+
}
53+
54+
func (s *ContainSubscriptionConditionOfTypeMatcher) Match(actual interface{}) (bool, error) {
55+
switch actual := actual.(type) {
56+
case *v1alpha1.Subscription:
57+
var conditionTypes []v1alpha1.SubscriptionConditionType
58+
for _, condition := range actual.Status.Conditions {
59+
conditionTypes = append(conditionTypes, condition.Type)
60+
}
61+
util.Logf("expecting subscription condition type '%s' to be in %s", s.Element, conditionTypes)
62+
return s.ContainElementMatcher.Match(conditionTypes)
63+
default:
64+
return false, fmt.Errorf("actual %v is not a subscription", actual)
65+
}
66+
}

Diff for: test/e2e/util/test_logging.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package util
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
8+
g "github.com/onsi/ginkgo/v2"
9+
)
10+
11+
func Logf(f string, v ...interface{}) {
12+
if !strings.HasSuffix(f, "\n") {
13+
f += "\n"
14+
}
15+
_, _ = fmt.Fprintf(g.GinkgoWriter, f, v...)
16+
}
17+
18+
func ObjectToPrettyJsonString(obj interface{}) string {
19+
data, _ := json.MarshalIndent(obj, "", " ")
20+
return string(data)
21+
}
22+
23+
func ObjectToJsonString(obj interface{}) string {
24+
data, _ := json.MarshalIndent(obj, "", " ")
25+
return string(data)
26+
}

0 commit comments

Comments
 (0)