/
config.go
496 lines (438 loc) · 17.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
Copyright 2022 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package testing
import (
"fmt"
"strings"
"sync"
"testing"
"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/vmware-labs/reconciler-runtime/reconcilers"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
// ExpectConfig encompasses the creation of a config object using given state, captures observed
// behavior of the reconciler and asserts expected behavior against the observed behavior.
//
// This object is driven implicitly by ReconcilerTestCase and SubReconcilerTestCase. A reconciler
// that needs to interact with multiple configs can create and manage additional ExpectConfigs with
// their own expectations. For example, when a WithConfig reconciler is used the SubReconcilers
// under it use a config separate from the config originally used to load the reconciled resource.
type ExpectConfig struct {
// Name is used when reporting assertion failures to distinguish configs
Name string
// Scheme allows the client to map Go structs to Kubernetes GVKs. All structured resources
// that are expected to interact with this config should be registered within the scheme.
Scheme *runtime.Scheme
// GivenObjects build the kubernetes objects which are present at the onset of reconciliation
GivenObjects []client.Object
// APIGivenObjects contains objects that are only available via an API reader instead of the normal cache
APIGivenObjects []client.Object
// WithClientBuilder allows a test to modify the fake client initialization.
WithClientBuilder func(*fake.ClientBuilder) *fake.ClientBuilder
// WithReactors installs each ReactionFunc into each fake clientset. ReactionFuncs intercept
// each call to the clientset providing the ability to mutate the resource or inject an error.
WithReactors []ReactionFunc
// GivenTracks provide a set of tracked resources to seed the tracker with
GivenTracks []TrackRequest
// side effects
// ExpectTracks holds the ordered list of Track calls expected during reconciliation
ExpectTracks []TrackRequest
// ExpectEvents holds the ordered list of events recorded during the reconciliation
ExpectEvents []Event
// ExpectCreates builds the ordered list of objects expected to be created during reconciliation
ExpectCreates []client.Object
// ExpectUpdates builds the ordered list of objects expected to be updated during reconciliation
ExpectUpdates []client.Object
// ExpectPatches builds the ordered list of objects expected to be patched during reconciliation
ExpectPatches []PatchRef
// ExpectDeletes holds the ordered list of objects expected to be deleted during reconciliation
ExpectDeletes []DeleteRef
// ExpectDeleteCollections holds the ordered list of collections expected to be deleted during reconciliation
ExpectDeleteCollections []DeleteCollectionRef
// ExpectStatusUpdates builds the ordered list of objects whose status is updated during reconciliation
ExpectStatusUpdates []client.Object
// ExpectStatusPatches builds the ordered list of objects whose status is patched during reconciliation
ExpectStatusPatches []PatchRef
once sync.Once
client *clientWrapper
apiReader *clientWrapper
recorder *eventRecorder
tracker *mockTracker
observedErrors []string
}
func (c *ExpectConfig) init() {
c.once.Do(func() {
// copy given objects to unwrap factories and prevent accidental mutations leaking between test cases
givenObjects := make([]client.Object, len(c.GivenObjects))
for i := range c.GivenObjects {
givenObjects[i] = c.GivenObjects[i].DeepCopyObject().(client.Object)
}
apiGivenObjects := make([]client.Object, len(c.APIGivenObjects))
for i := range c.APIGivenObjects {
apiGivenObjects[i] = c.APIGivenObjects[i].DeepCopyObject().(client.Object)
}
c.client = c.createClient(givenObjects)
for i := range c.WithReactors {
// in reverse order since we prepend
reactor := c.WithReactors[len(c.WithReactors)-1-i]
c.client.PrependReactor("*", "*", reactor)
}
c.apiReader = c.createClient(apiGivenObjects)
c.recorder = &eventRecorder{
events: []Event{},
scheme: c.Scheme,
}
c.tracker = createTracker(c.GivenTracks)
c.observedErrors = []string{}
})
}
func (c *ExpectConfig) createClient(objs []client.Object) *clientWrapper {
builder := fake.NewClientBuilder()
builder.WithScheme(c.Scheme)
builder.WithObjects(prepareObjects(objs)...)
if c.WithClientBuilder != nil {
builder = c.WithClientBuilder(builder)
}
return NewFakeClientWrapper(builder.Build())
}
// Config returns the Config object. This method should only be called once. Subsequent calls are
// ignored returning the Config from the first call.
func (c *ExpectConfig) Config() reconcilers.Config {
c.init()
return reconcilers.Config{
Client: c.client,
APIReader: c.apiReader,
Recorder: c.recorder,
Tracker: c.tracker,
// Log is deprecated and should not be used. Setting the discard logger until it is fully removed.
Log: logr.Discard(),
}
}
func (c *ExpectConfig) errorf(t *testing.T, message string, args ...interface{}) {
if t != nil {
t.Errorf(message, args...)
}
c.observedErrors = append(c.observedErrors, fmt.Sprintf(message, args...))
}
// AssertExpectations asserts all observed reconciler behavior matches the expected behavior
func (c *ExpectConfig) AssertExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
c.AssertClientExpectations(t)
c.AssertRecorderExpectations(t)
c.AssertTrackerExpectations(t)
}
// AssertClientExpectations asserts observed reconciler client behavior matches the expected client behavior
func (c *ExpectConfig) AssertClientExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
c.AssertClientCreateExpectations(t)
c.AssertClientUpdateExpectations(t)
c.AssertClientPatchExpectations(t)
c.AssertClientDeleteExpectations(t)
c.AssertClientDeleteCollectionExpectations(t)
c.AssertClientStatusUpdateExpectations(t)
c.AssertClientStatusPatchExpectations(t)
}
// AssertClientCreateExpectations asserts observed reconciler client create behavior matches the expected client create behavior
func (c *ExpectConfig) AssertClientCreateExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
c.compareActions(t, "create", c.ExpectCreates, c.client.CreateActions, IgnoreLastTransitionTime, SafeDeployDiff, IgnoreTypeMeta, IgnoreResourceVersion, cmpopts.EquateEmpty())
}
// AssertClientUpdateExpectations asserts observed reconciler client update behavior matches the expected client update behavior
func (c *ExpectConfig) AssertClientUpdateExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
c.compareActions(t, "update", c.ExpectUpdates, c.client.UpdateActions, IgnoreLastTransitionTime, SafeDeployDiff, IgnoreTypeMeta, IgnoreCreationTimestamp, IgnoreResourceVersion, cmpopts.EquateEmpty())
}
// AssertClientPatchExpectations asserts observed reconciler client patch behavior matches the expected client patch behavior
func (c *ExpectConfig) AssertClientPatchExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
for i, exp := range c.ExpectPatches {
if i >= len(c.client.PatchActions) {
c.errorf(t, "Missing patch for config %q: %#v", c.Name, exp)
continue
}
actual := NewPatchRef(c.client.PatchActions[i])
if diff := cmp.Diff(exp, actual); diff != "" {
c.errorf(t, "Unexpected patch for config %q (-expected, +actual): %s", c.Name, diff)
}
}
if actual, expected := len(c.client.PatchActions), len(c.ExpectPatches); actual > expected {
for _, extra := range c.client.PatchActions[expected:] {
c.errorf(t, "Extra patch for config %q: %#v", c.Name, extra)
}
}
}
// AssertClientDeleteExpectations asserts observed reconciler client delete behavior matches the expected client delete behavior
func (c *ExpectConfig) AssertClientDeleteExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
for i, exp := range c.ExpectDeletes {
if i >= len(c.client.DeleteActions) {
c.errorf(t, "Missing delete for config %q: %#v", c.Name, exp)
continue
}
actual := NewDeleteRef(c.client.DeleteActions[i])
if diff := cmp.Diff(exp, actual); diff != "" {
c.errorf(t, "Unexpected delete for config %q (-expected, +actual): %s", c.Name, diff)
}
}
if actual, expected := len(c.client.DeleteActions), len(c.ExpectDeletes); actual > expected {
for _, extra := range c.client.DeleteActions[expected:] {
c.errorf(t, "Extra delete for config %q: %#v", c.Name, extra)
}
}
}
// AssertClientDeleteCollectionExpectations asserts observed reconciler client delete collection behavior matches the expected client delete collection behavior
func (c *ExpectConfig) AssertClientDeleteCollectionExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
for i, exp := range c.ExpectDeleteCollections {
if i >= len(c.client.DeleteCollectionActions) {
c.errorf(t, "Missing delete collection for config %q: %#v", c.Name, exp)
continue
}
actual := NewDeleteCollectionRef(c.client.DeleteCollectionActions[i])
if diff := cmp.Diff(exp, actual, NormalizeLabelSelector, NormalizeFieldSelector); diff != "" {
c.errorf(t, "Unexpected delete collection for config %q (-expected, +actual): %s", c.Name, diff)
}
}
if actual, expected := len(c.client.DeleteCollectionActions), len(c.ExpectDeleteCollections); actual > expected {
for _, extra := range c.client.DeleteCollectionActions[expected:] {
c.errorf(t, "Extra delete collection for config %q: %#v", c.Name, extra)
}
}
}
// AssertClientStatusUpdateExpectations asserts observed reconciler client status update behavior matches the expected client status update behavior
func (c *ExpectConfig) AssertClientStatusUpdateExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
c.compareActions(t, "status update", c.ExpectStatusUpdates, c.client.StatusUpdateActions, statusSubresourceOnly, IgnoreLastTransitionTime, SafeDeployDiff, cmpopts.EquateEmpty())
}
// AssertClientStatusPatchExpectations asserts observed reconciler client status patch behavior matches the expected client status patch behavior
func (c *ExpectConfig) AssertClientStatusPatchExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
for i, exp := range c.ExpectStatusPatches {
if i >= len(c.client.StatusPatchActions) {
c.errorf(t, "Missing status patch for config %q: %#v", c.Name, exp)
continue
}
actual := NewPatchRef(c.client.StatusPatchActions[i])
if diff := cmp.Diff(exp, actual); diff != "" {
c.errorf(t, "Unexpected status patch for config %q (-expected, +actual): %s", c.Name, diff)
}
}
if actual, expected := len(c.client.StatusPatchActions), len(c.ExpectStatusPatches); actual > expected {
for _, extra := range c.client.StatusPatchActions[expected:] {
c.errorf(t, "Extra status patch for config %q: %#v", c.Name, extra)
}
}
}
// AssertRecorderExpectations asserts observed event recorder behavior matches the expected event recorder behavior
func (c *ExpectConfig) AssertRecorderExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
actualEvents := c.recorder.events
for i, exp := range c.ExpectEvents {
if i >= len(actualEvents) {
c.errorf(t, "Missing recorded event for config %q: %s", c.Name, exp)
continue
}
if diff := cmp.Diff(exp, actualEvents[i]); diff != "" {
c.errorf(t, "Unexpected recorded event for config %q (-expected, +actual): %s", c.Name, diff)
}
}
if actual, exp := len(actualEvents), len(c.ExpectEvents); actual > exp {
for _, extra := range actualEvents[exp:] {
c.errorf(t, "Extra recorded event for config %q: %s", c.Name, extra)
}
}
}
// AssertTrackerExpectations asserts observed tracker behavior matches the expected tracker behavior
func (c *ExpectConfig) AssertTrackerExpectations(t *testing.T) {
if t != nil {
t.Helper()
}
c.init()
actualTracks := c.tracker.getTrackRequests()
for i, exp := range c.ExpectTracks {
if i >= len(actualTracks) {
c.errorf(t, "Missing tracking request for config %q: %s", c.Name, exp)
continue
}
if diff := cmp.Diff(exp, actualTracks[i]); diff != "" {
c.errorf(t, "Unexpected tracking request for config %q (-expected, +actual): %s", c.Name, diff)
}
}
if actual, exp := len(actualTracks), len(c.ExpectTracks); actual > exp {
for _, extra := range actualTracks[exp:] {
c.errorf(t, "Extra tracking request for config %q: %s", c.Name, extra)
}
}
}
func (c *ExpectConfig) compareActions(t *testing.T, actionName string, expectedActionFactories []client.Object, actualActions []objectAction, diffOptions ...cmp.Option) {
if t != nil {
t.Helper()
}
c.init()
for i, exp := range expectedActionFactories {
if i >= len(actualActions) {
c.errorf(t, "Missing %s for config %q: %#v", actionName, c.Name, exp.DeepCopyObject())
continue
}
actual := actualActions[i].GetObject()
if diff := cmp.Diff(exp.DeepCopyObject(), actual, diffOptions...); diff != "" {
c.errorf(t, "Unexpected %s for config %q (-expected, +actual): %s", actionName, c.Name, diff)
}
}
if actual, expected := len(actualActions), len(expectedActionFactories); actual > expected {
for _, extra := range actualActions[expected:] {
c.errorf(t, "Extra %s for config %q: %#v", actionName, c.Name, extra)
}
}
}
var (
IgnoreLastTransitionTime = cmp.FilterPath(func(p cmp.Path) bool {
str := p.String()
gostr := p.GoString()
return strings.HasSuffix(str, "LastTransitionTime") ||
strings.HasSuffix(gostr, `["lastTransitionTime"]`)
}, cmp.Ignore())
IgnoreTypeMeta = cmp.FilterPath(func(p cmp.Path) bool {
str := p.String()
// only ignore for typed resources, compare TypeMeta values for unstructured
return strings.HasSuffix(str, "TypeMeta.APIVersion") ||
strings.HasSuffix(str, "TypeMeta.Kind")
}, cmp.Ignore())
IgnoreCreationTimestamp = cmp.FilterPath(func(p cmp.Path) bool {
str := p.String()
gostr := p.GoString()
return strings.HasSuffix(str, "ObjectMeta.CreationTimestamp") ||
strings.HasSuffix(gostr, `(*unstructured.Unstructured).Object["metadata"].(map[string]any)["creationTimestamp"]`) ||
strings.HasSuffix(gostr, `{*unstructured.Unstructured}.Object["metadata"].(map[string]any)["creationTimestamp"]`) ||
strings.HasSuffix(gostr, `(*unstructured.Unstructured).Object["metadata"].(map[string]interface {})["creationTimestamp"]`) ||
strings.HasSuffix(gostr, `{*unstructured.Unstructured}.Object["metadata"].(map[string]interface {})["creationTimestamp"]`)
}, cmp.Ignore())
IgnoreResourceVersion = cmp.FilterPath(func(p cmp.Path) bool {
str := p.String()
gostr := p.GoString()
return strings.HasSuffix(str, "ObjectMeta.ResourceVersion") ||
strings.HasSuffix(gostr, `(*unstructured.Unstructured).Object["metadata"].(map[string]any)["resourceVersion"]`) ||
strings.HasSuffix(gostr, `{*unstructured.Unstructured}.Object["metadata"].(map[string]any)["resourceVersion"]`) ||
strings.HasSuffix(gostr, `(*unstructured.Unstructured).Object["metadata"].(map[string]interface {})["resourceVersion"]`) ||
strings.HasSuffix(gostr, `{*unstructured.Unstructured}.Object["metadata"].(map[string]interface {})["resourceVersion"]`)
}, cmp.Ignore())
statusSubresourceOnly = cmp.FilterPath(func(p cmp.Path) bool {
str := p.String()
return str != "" && !strings.HasPrefix(str, "Status")
}, cmp.Ignore())
SafeDeployDiff = cmpopts.IgnoreUnexported(resource.Quantity{})
NormalizeLabelSelector = cmp.Transformer("labels.Selector", func(s labels.Selector) *string {
if s == nil || s.Empty() {
return nil
}
return StringPtr(s.String())
})
NormalizeFieldSelector = cmp.Transformer("fields.Selector", func(s fields.Selector) *string {
if s == nil || s.Empty() {
return nil
}
return StringPtr(s.String())
})
)
type PatchRef struct {
Group string
Kind string
Namespace string
Name string
SubResource string
PatchType types.PatchType
Patch []byte
}
func NewPatchRef(action PatchAction) PatchRef {
return PatchRef{
Group: action.GetResource().Group,
Kind: action.GetResource().Resource,
Namespace: action.GetNamespace(),
Name: action.GetName(),
SubResource: action.GetSubresource(),
PatchType: action.GetPatchType(),
Patch: action.GetPatch(),
}
}
type DeleteRef struct {
Group string
Kind string
Namespace string
Name string
}
func NewDeleteRef(action DeleteAction) DeleteRef {
return DeleteRef{
Group: action.GetResource().Group,
Kind: action.GetResource().Resource,
Namespace: action.GetNamespace(),
Name: action.GetName(),
}
}
func NewDeleteRefFromObject(obj client.Object, scheme *runtime.Scheme) DeleteRef {
gvks, _, err := scheme.ObjectKinds(obj.DeepCopyObject())
if err != nil {
panic(err)
}
return DeleteRef{
Group: gvks[0].Group,
Kind: gvks[0].Kind,
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
}
type DeleteCollectionRef struct {
Group string
Kind string
Namespace string
Labels labels.Selector
Fields fields.Selector
}
func NewDeleteCollectionRef(action DeleteCollectionAction) DeleteCollectionRef {
return DeleteCollectionRef{
Group: action.GetResource().Group,
Kind: action.GetResource().Resource,
Namespace: action.GetNamespace(),
Labels: action.GetListRestrictions().Labels,
Fields: action.GetListRestrictions().Fields,
}
}