@@ -6,8 +6,12 @@ import (
6
6
"reflect"
7
7
"testing"
8
8
9
+ . "github.com/onsi/gomega"
10
+ "github.com/onsi/gomega/gcustom"
11
+ "github.com/onsi/gomega/types"
9
12
"k8s.io/apimachinery/pkg/runtime"
10
13
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
14
+ "k8s.io/apimachinery/pkg/util/validation/field"
11
15
"sigs.k8s.io/controller-runtime/pkg/client/fake"
12
16
"sigs.k8s.io/controller-runtime/pkg/log/zap"
13
17
"sigs.k8s.io/gateway-api/apis/v1beta1"
@@ -17,6 +21,7 @@ import (
17
21
"github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/managerfakes"
18
22
"github.com/nginxinc/nginx-kubernetes-gateway/internal/manager/predicate"
19
23
"github.com/nginxinc/nginx-kubernetes-gateway/internal/reconciler"
24
+ "github.com/nginxinc/nginx-kubernetes-gateway/internal/reconciler/reconcilerfakes"
20
25
)
21
26
22
27
func TestRegisterController (t * testing.T ) {
@@ -81,114 +86,69 @@ func TestRegisterController(t *testing.T) {
81
86
namespacedNameFilter := filter .CreateFilterForGatewayClass ("test" )
82
87
fieldIndexes := index .CreateEndpointSliceFieldIndices ()
83
88
84
- eventCh := make (chan interface {})
89
+ webhookValidator := createValidator (func (_ * v1beta1.HTTPRoute ) field.ErrorList {
90
+ return nil
91
+ })
92
+
93
+ eventRecorder := & reconcilerfakes.FakeEventRecorder {}
94
+
95
+ eventCh := make (chan <- interface {})
96
+
97
+ beSameFunctionPointer := func (expected interface {}) types.GomegaMatcher {
98
+ return gcustom .MakeMatcher (func (f interface {}) (bool , error ) {
99
+ // comparing functions is not allowed in Go, so we're comparing the pointers
100
+ return reflect .ValueOf (expected ).Pointer () == reflect .ValueOf (f ).Pointer (), nil
101
+ })
102
+ }
85
103
86
104
for _ , test := range tests {
87
- newReconciler := func (c reconciler.Config ) * reconciler.Implementation {
88
- if c .Getter != test .fakes .mgr .GetClient () {
89
- t .Errorf (
90
- "regiterController() created a reconciler config with Getter %p but expected %p for case of %q" ,
91
- c .Getter ,
92
- test .fakes .mgr .GetClient (),
93
- test .msg ,
94
- )
95
- }
96
- if c .ObjectType != objectType {
97
- t .Errorf (
98
- "registerController() created a reconciler config with ObjectType %T but expected %T for case of %q" ,
99
- c .ObjectType ,
100
- objectType ,
101
- test .msg ,
102
- )
105
+ t .Run (test .msg , func (t * testing.T ) {
106
+ g := NewGomegaWithT (t )
107
+
108
+ newReconciler := func (c reconciler.Config ) * reconciler.Implementation {
109
+ g .Expect (c .Getter ).To (BeIdenticalTo (test .fakes .mgr .GetClient ()))
110
+ g .Expect (c .ObjectType ).To (BeIdenticalTo (objectType ))
111
+ g .Expect (c .EventCh ).To (BeIdenticalTo (eventCh ))
112
+ g .Expect (c .EventRecorder ).To (BeIdenticalTo (eventRecorder ))
113
+ g .Expect (c .WebhookValidator ).Should (beSameFunctionPointer (webhookValidator ))
114
+ g .Expect (c .NamespacedNameFilter ).Should (beSameFunctionPointer (namespacedNameFilter ))
115
+
116
+ return reconciler .NewImplementation (c )
103
117
}
104
- if c .EventCh != eventCh {
105
- t .Errorf (
106
- "registerController() created a reconciler config with EventCh %v but expected %v for case of %q" ,
107
- c .EventCh ,
108
- eventCh ,
109
- test .msg ,
110
- )
111
- }
112
- // comparing functions is not allowed in Go, so we're comparing the pointers
113
- // nolint: lll
114
- if reflect .ValueOf (c .NamespacedNameFilter ).Pointer () != reflect .ValueOf (namespacedNameFilter ).Pointer () {
115
- t .Errorf (
116
- "registerController() created a reconciler config with NamespacedNameFilter %p but expected %p for case of %q" ,
117
- c .NamespacedNameFilter ,
118
- namespacedNameFilter ,
119
- test .msg ,
120
- )
118
+
119
+ err := registerController (
120
+ context .Background (),
121
+ objectType ,
122
+ test .fakes .mgr ,
123
+ eventCh ,
124
+ eventRecorder ,
125
+ withNamespacedNameFilter (namespacedNameFilter ),
126
+ withK8sPredicate (predicate.ServicePortsChangedPredicate {}),
127
+ withFieldIndices (fieldIndexes ),
128
+ withNewReconciler (newReconciler ),
129
+ withWebhookValidator (webhookValidator ),
130
+ )
131
+
132
+ if test .expectedErr == nil {
133
+ g .Expect (err ).To (BeNil ())
134
+ } else {
135
+ g .Expect (err ).To (MatchError (test .expectedErr ))
121
136
}
122
137
123
- return reconciler .NewImplementation (c )
124
- }
138
+ indexCallCount := test .fakes .indexer .IndexFieldCallCount ()
125
139
126
- err := registerController (
127
- context .Background (),
128
- objectType ,
129
- test .fakes .mgr ,
130
- eventCh ,
131
- withNamespacedNameFilter (namespacedNameFilter ),
132
- withK8sPredicate (predicate.ServicePortsChangedPredicate {}),
133
- withFieldIndices (fieldIndexes ),
134
- withNewReconciler (newReconciler ),
135
- )
136
-
137
- if ! errors .Is (err , test .expectedErr ) {
138
- t .Errorf (
139
- "registerController() returned %q but expected %q for case of %q" ,
140
- err ,
141
- test .expectedErr ,
142
- test .msg ,
143
- )
144
- }
140
+ g .Expect (indexCallCount ).To (Equal (1 ))
145
141
146
- indexCallCount := test .fakes .indexer .IndexFieldCallCount ()
147
- if indexCallCount != 1 {
148
- t .Errorf (
149
- "registerController() called indexer.IndexField() %d times but expected 1 for case of %q" ,
150
- indexCallCount ,
151
- test .msg ,
152
- )
153
- } else {
154
142
_ , objType , field , indexFunc := test .fakes .indexer .IndexFieldArgsForCall (0 )
155
143
156
- if objType != objectType {
157
- t .Errorf (
158
- "registerController() called indexer.IndexField() with object type %T but expected %T for case %q" ,
159
- objType ,
160
- objectType ,
161
- test .msg ,
162
- )
163
- }
164
- if field != index .KubernetesServiceNameIndexField {
165
- t .Errorf ("registerController() called indexer.IndexField() with field %q but expected %q for case %q" ,
166
- field ,
167
- index .KubernetesServiceNameIndexField ,
168
- test .msg ,
169
- )
170
- }
144
+ g .Expect (objType ).To (BeIdenticalTo (objectType ))
145
+ g .Expect (field ).To (BeIdenticalTo (index .KubernetesServiceNameIndexField ))
171
146
172
147
expectedIndexFunc := fieldIndexes [index .KubernetesServiceNameIndexField ]
173
- // comparing functions is not allowed in Go, so we're comparing the pointers
174
- // nolint:lll
175
- if reflect .ValueOf (indexFunc ).Pointer () != reflect .ValueOf (expectedIndexFunc ).Pointer () {
176
- t .Errorf ("registerController() called indexer.IndexField() with indexFunc %p but expected %p for case %q" ,
177
- indexFunc ,
178
- expectedIndexFunc ,
179
- test .msg ,
180
- )
181
- }
182
- }
148
+ g .Expect (indexFunc ).To (beSameFunctionPointer (expectedIndexFunc ))
183
149
184
- addCallCount := test .fakes .mgr .AddCallCount ()
185
- if addCallCount != test .expectedMgrAddCallCount {
186
- t .Errorf (
187
- "registerController() called mgr.Add() %d times but expected %d times for case %q" ,
188
- addCallCount ,
189
- test .expectedMgrAddCallCount ,
190
- test .msg ,
191
- )
192
- }
150
+ addCallCount := test .fakes .mgr .AddCallCount ()
151
+ g .Expect (addCallCount ).To (Equal (test .expectedMgrAddCallCount ))
152
+ })
193
153
}
194
154
}
0 commit comments