/
reconciler.go
249 lines (217 loc) · 8.88 KB
/
reconciler.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
/*
Copyright 2019 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package testing
import (
"context"
"testing"
"github.com/go-logr/logr"
logrtesting "github.com/go-logr/logr/testing"
"github.com/google/go-cmp/cmp"
"github.com/vmware-labs/reconciler-runtime/reconcilers"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// ReconcilerTestCase holds a single test case of a reconciler test suite.
type ReconcilerTestCase struct {
// Name is a descriptive name for this test suitable as a first argument to t.Run()
Name string
// Focus is true if and only if only this and any other focused tests are to be executed.
// If one or more tests are focused, the overall test suite will fail.
Focus bool
// Skip is true if and only if this test should be skipped.
Skip bool
// Metadata contains arbitrary values that are stored with the test case
Metadata map[string]interface{}
// inputs
// Deprecated use Request
Key types.NamespacedName
// Request identifies the object to be reconciled
Request controllerruntime.Request
// 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
// WithClientBuilder allows a test to modify the fake client initialization.
WithClientBuilder func(*fake.ClientBuilder) *fake.ClientBuilder
// 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
// 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
// AdditionalConfigs holds ExceptConfigs that are available to the test case and will have
// their expectations checked again the observed config interactions. The key in this map is
// set as the ExpectConfig's name.
AdditionalConfigs map[string]ExpectConfig
// outputs
// ShouldErr is true if and only if reconciliation is expected to return an error
ShouldErr bool
// ExpectedResult is compared to the result returned from the reconciler if there was no error
ExpectedResult controllerruntime.Result
// Verify provides the reconciliation Result and error for custom assertions
Verify VerifyFunc
// lifecycle
// Prepare is called before the reconciler is executed. It is intended to prepare the broader
// environment before the specific test case is executed. For example, setting mock
// expectations, or adding values to the context.
Prepare func(t *testing.T, ctx context.Context, tc *ReconcilerTestCase) (context.Context, error)
// CleanUp is called after the test case is finished and all defined assertions complete.
// It is intended to clean up any state created in the Prepare step or during the test
// execution, or to make assertions for mocks.
CleanUp func(t *testing.T, ctx context.Context, tc *ReconcilerTestCase) error
}
// VerifyFunc is a verification function for a reconciler's result
type VerifyFunc func(t *testing.T, result controllerruntime.Result, err error)
// VerifyStashedValueFunc is a verification function for the entries in the stash
type VerifyStashedValueFunc func(t *testing.T, key reconcilers.StashKey, expected, actual interface{})
// ReconcilerTests represents a map of reconciler test cases. The map key is the name of each test
// case. Test cases are executed in random order.
type ReconcilerTests map[string]ReconcilerTestCase
// Run executes the test cases.
func (rt ReconcilerTests) Run(t *testing.T, scheme *runtime.Scheme, factory ReconcilerFactory) {
t.Helper()
rts := ReconcilerTestSuite{}
for name, rtc := range rt {
rtc.Name = name
rts = append(rts, rtc)
}
rts.Run(t, scheme, factory)
}
// ReconcilerTestSuite represents a list of reconciler test cases. The test cases are executed in order.
type ReconcilerTestSuite []ReconcilerTestCase
// Run executes the test case.
func (tc *ReconcilerTestCase) Run(t *testing.T, scheme *runtime.Scheme, factory ReconcilerFactory) {
t.Helper()
if tc.Skip {
t.SkipNow()
}
ctx := context.Background()
ctx = logr.NewContext(ctx, logrtesting.NewTestLogger(t))
if deadline, ok := t.Deadline(); ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, deadline)
defer cancel()
}
if tc.Metadata == nil {
tc.Metadata = map[string]interface{}{}
}
if tc.Prepare != nil {
var err error
if ctx, err = tc.Prepare(t, ctx, tc); err != nil {
t.Fatalf("error during prepare: %s", err)
}
}
if tc.CleanUp != nil {
defer func() {
if err := tc.CleanUp(t, ctx, tc); err != nil {
t.Errorf("error during clean up: %s", err)
}
}()
}
expectConfig := &ExpectConfig{
Name: "default",
Scheme: scheme,
GivenObjects: tc.GivenObjects,
APIGivenObjects: tc.APIGivenObjects,
WithClientBuilder: tc.WithClientBuilder,
WithReactors: tc.WithReactors,
GivenTracks: tc.GivenTracks,
ExpectTracks: tc.ExpectTracks,
ExpectEvents: tc.ExpectEvents,
ExpectCreates: tc.ExpectCreates,
ExpectUpdates: tc.ExpectUpdates,
ExpectPatches: tc.ExpectPatches,
ExpectDeletes: tc.ExpectDeletes,
ExpectDeleteCollections: tc.ExpectDeleteCollections,
ExpectStatusUpdates: tc.ExpectStatusUpdates,
ExpectStatusPatches: tc.ExpectStatusPatches,
}
configs := make(map[string]reconcilers.Config, len(tc.AdditionalConfigs))
for k, v := range tc.AdditionalConfigs {
v.Name = k
configs[k] = v.Config()
}
ctx = reconcilers.StashAdditionalConfigs(ctx, configs)
r := factory(t, tc, expectConfig.Config())
// Run the Reconcile we're testing.
request := tc.Request
if request == (controllerruntime.Request{}) {
request.NamespacedName = tc.Key
}
result, err := r.Reconcile(ctx, request)
if (err != nil) != tc.ShouldErr {
t.Errorf("Reconcile() error = %v, ShouldErr %v", err, tc.ShouldErr)
}
if err == nil {
// result is only significant if there wasn't an error
if diff := cmp.Diff(normalizeResult(tc.ExpectedResult), normalizeResult(result)); diff != "" {
t.Errorf("Unexpected result (-expected, +actual): %s", diff)
}
}
if tc.Verify != nil {
tc.Verify(t, result, err)
}
expectConfig.AssertExpectations(t)
for _, config := range tc.AdditionalConfigs {
config.AssertExpectations(t)
}
}
func normalizeResult(result controllerruntime.Result) controllerruntime.Result {
// RequeueAfter implies Requeue, no need to set both
if result.RequeueAfter != 0 {
result.Requeue = false
}
return result
}
// Run executes the reconciler test suite.
func (ts ReconcilerTestSuite) Run(t *testing.T, scheme *runtime.Scheme, factory ReconcilerFactory) {
t.Helper()
focused := ReconcilerTestSuite{}
for _, test := range ts {
if test.Focus {
focused = append(focused, test)
break
}
}
testsToExecute := ts
if len(focused) > 0 {
testsToExecute = focused
}
for _, test := range testsToExecute {
t.Run(test.Name, func(t *testing.T) {
t.Helper()
test.Run(t, scheme, factory)
})
}
if len(focused) > 0 {
t.Errorf("%d tests out of %d are still focused, so the test suite fails", len(focused), len(ts))
}
}
// ReconcilerFactory returns a Reconciler.Interface to perform reconciliation of a test case,
// ActionRecorderList/EventList to capture k8s actions/events produced during reconciliation
// and FakeStatsReporter to capture stats.
type ReconcilerFactory func(t *testing.T, rtc *ReconcilerTestCase, c reconcilers.Config) reconcile.Reconciler