Skip to content

Commit ad88f3a

Browse files
committed
extend testing library (error matching with ShouldNotReconcile)
1 parent fb426b2 commit ad88f3a

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

pkg/testing/complex_environment.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/onsi/gomega"
11+
"github.com/onsi/gomega/types"
1112
"k8s.io/apimachinery/pkg/runtime"
1213
"k8s.io/apimachinery/pkg/util/uuid"
1314
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -18,6 +19,7 @@ import (
1819
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1920

2021
"github.com/openmcp-project/controller-utils/pkg/logging"
22+
"github.com/openmcp-project/controller-utils/pkg/testing/matchers"
2123
)
2224

2325
/////////////////
@@ -84,27 +86,37 @@ func (e *ComplexEnvironment) shouldEventuallyReconcile(reconciler string, req re
8486

8587
// ShouldNotReconcile calls the given reconciler with the given request and expects an error.
8688
func (e *ComplexEnvironment) ShouldNotReconcile(reconciler string, req reconcile.Request, optionalDescription ...interface{}) reconcile.Result {
87-
return e.shouldNotReconcile(reconciler, req, optionalDescription...)
89+
return e.shouldNotReconcile(reconciler, req, nil, optionalDescription...)
8890
}
8991

90-
func (e *ComplexEnvironment) shouldNotReconcile(reconciler string, req reconcile.Request, optionalDescription ...interface{}) reconcile.Result {
92+
// ShouldNotReconcileWithError calls the given reconciler with the given request and expects an error that matches the given matcher.
93+
func (e *ComplexEnvironment) ShouldNotReconcileWithError(reconciler string, req reconcile.Request, matcher types.GomegaMatcher, optionalDescription ...interface{}) reconcile.Result {
94+
return e.shouldNotReconcile(reconciler, req, matcher, optionalDescription...)
95+
}
96+
97+
func (e *ComplexEnvironment) shouldNotReconcile(reconciler string, req reconcile.Request, matcher types.GomegaMatcher, optionalDescription ...interface{}) reconcile.Result {
9198
res, err := e.Reconcilers[reconciler].Reconcile(e.Ctx, req)
92-
gomega.ExpectWithOffset(2, err).To(gomega.HaveOccurred(), optionalDescription...)
99+
gomega.ExpectWithOffset(2, err).To(gomega.And(gomega.HaveOccurred(), matchers.MaybeMatch(matcher)), optionalDescription...)
93100
return res
94101
}
95102

96103
// ShouldEventuallyNotReconcile calls the given reconciler with the given request and retries until an error occurred or the timeout is reached.
97104
func (e *ComplexEnvironment) ShouldEventuallyNotReconcile(reconciler string, req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result {
98-
return e.shouldEventuallyNotReconcile(reconciler, req, timeout, poll, optionalDescription...)
105+
return e.shouldEventuallyNotReconcile(reconciler, req, nil, timeout, poll, optionalDescription...)
106+
}
107+
108+
// ShouldEventuallyNotReconcileWithError calls the given reconciler with the given request and retries until an error that matches the given matcher occurred or the timeout is reached.
109+
func (e *ComplexEnvironment) ShouldEventuallyNotReconcileWithError(reconciler string, req reconcile.Request, matcher types.GomegaMatcher, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result {
110+
return e.shouldEventuallyNotReconcile(reconciler, req, matcher, timeout, poll, optionalDescription...)
99111
}
100112

101-
func (e *ComplexEnvironment) shouldEventuallyNotReconcile(reconciler string, req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result {
113+
func (e *ComplexEnvironment) shouldEventuallyNotReconcile(reconciler string, req reconcile.Request, matcher types.GomegaMatcher, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result {
102114
var err error
103115
var res reconcile.Result
104116
gomega.EventuallyWithOffset(1, func() error {
105117
res, err = e.Reconcilers[reconciler].Reconcile(e.Ctx, req)
106118
return err
107-
}, timeout, poll).ShouldNot(gomega.Succeed(), optionalDescription...)
119+
}, timeout, poll).ShouldNot(gomega.And(gomega.Succeed(), matchers.MaybeMatch(matcher)), optionalDescription...)
108120
return res
109121
}
110122

pkg/testing/environment.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/client"
99
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1010

11+
"github.com/onsi/gomega/types"
12+
1113
"github.com/openmcp-project/controller-utils/pkg/logging"
1214
)
1315

@@ -48,12 +50,22 @@ func (e *Environment) ShouldEventuallyReconcile(req reconcile.Request, timeout,
4850

4951
// ShouldNotReconcile calls the given reconciler with the given request and expects an error.
5052
func (e *Environment) ShouldNotReconcile(req reconcile.Request, optionalDescription ...interface{}) reconcile.Result {
51-
return e.shouldNotReconcile(SimpleEnvironmentDefaultKey, req, optionalDescription...)
53+
return e.shouldNotReconcile(SimpleEnvironmentDefaultKey, req, nil, optionalDescription...)
5254
}
5355

5456
// ShouldEventuallyNotReconcile calls the given reconciler with the given request and retries until an error occurred or the timeout is reached.
5557
func (e *Environment) ShouldEventuallyNotReconcile(req reconcile.Request, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result {
56-
return e.shouldEventuallyNotReconcile(SimpleEnvironmentDefaultKey, req, timeout, poll, optionalDescription...)
58+
return e.shouldEventuallyNotReconcile(SimpleEnvironmentDefaultKey, req, nil, timeout, poll, optionalDescription...)
59+
}
60+
61+
// ShouldNotReconcileWithError calls the given reconciler with the given request and expects an error that matches the given matcher.
62+
func (e *Environment) ShouldNotReconcileWithError(req reconcile.Request, matcher types.GomegaMatcher, optionalDescription ...interface{}) reconcile.Result {
63+
return e.shouldNotReconcile(SimpleEnvironmentDefaultKey, req, matcher, optionalDescription...)
64+
}
65+
66+
// ShouldEventuallyNotReconcileWithError calls the given reconciler with the given request and retries until an error that matches the given matcher occurred or the timeout is reached.
67+
func (e *Environment) ShouldEventuallyNotReconcileWithError(req reconcile.Request, matcher types.GomegaMatcher, timeout, poll time.Duration, optionalDescription ...interface{}) reconcile.Result {
68+
return e.shouldEventuallyNotReconcile(SimpleEnvironmentDefaultKey, req, matcher, timeout, poll, optionalDescription...)
5769
}
5870

5971
//////////////////////////////////

pkg/testing/matchers/maybematch.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package matchers
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/onsi/gomega/types"
7+
)
8+
9+
// MaybeMatch returns a Gomega matcher that passes the matching logic to the provided matcher,
10+
// but always succeeds if the passed in matcher is nil.
11+
func MaybeMatch(matcher types.GomegaMatcher) types.GomegaMatcher {
12+
return &maybeMatcher{matcher: matcher}
13+
}
14+
15+
type maybeMatcher struct {
16+
matcher types.GomegaMatcher
17+
}
18+
19+
func (m *maybeMatcher) GomegaString() string {
20+
if m == nil || m.matcher == nil {
21+
return "<nil>"
22+
}
23+
return fmt.Sprintf("MaybeMatch(%v)", m.matcher)
24+
}
25+
26+
var _ types.GomegaMatcher = &maybeMatcher{}
27+
28+
// Match implements types.GomegaMatcher.
29+
func (m *maybeMatcher) Match(actualRaw any) (success bool, err error) {
30+
if m.matcher == nil {
31+
return true, nil
32+
}
33+
return m.matcher.Match(actualRaw)
34+
}
35+
36+
// FailureMessage implements types.GomegaMatcher.
37+
func (m *maybeMatcher) FailureMessage(actual any) (message string) {
38+
return m.matcher.FailureMessage(actual)
39+
}
40+
41+
// NegatedFailureMessage implements types.GomegaMatcher.
42+
func (m *maybeMatcher) NegatedFailureMessage(actual any) (message string) {
43+
return m.matcher.NegatedFailureMessage(actual)
44+
}

0 commit comments

Comments
 (0)