/
context_builder.go
399 lines (351 loc) · 13.9 KB
/
context_builder.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
/*
Copyright 2020 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"context"
"flag"
"fmt"
"testing"
"time"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/rand"
kubefake "k8s.io/client-go/kubernetes/fake"
metadatafake "k8s.io/client-go/metadata/fake"
"k8s.io/client-go/metadata/metadatainformer"
"k8s.io/client-go/rest"
coretesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/clock"
fakeclock "k8s.io/utils/clock/testing"
gwfake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"
gwinformers "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions"
internalinformers "github.com/cert-manager/cert-manager/internal/informers"
apiutil "github.com/cert-manager/cert-manager/pkg/api/util"
cmfake "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned/fake"
informers "github.com/cert-manager/cert-manager/pkg/client/informers/externalversions"
"github.com/cert-manager/cert-manager/pkg/controller"
"github.com/cert-manager/cert-manager/pkg/logs"
"github.com/cert-manager/cert-manager/pkg/metrics"
"github.com/cert-manager/cert-manager/pkg/util"
discoveryfake "github.com/cert-manager/cert-manager/test/unit/discovery"
)
func init() {
logs.InitLogs()
_ = flag.Set("alsologtostderr", "true")
_ = flag.Set("v", "4")
}
// Builder is a structure used to construct new Contexts for use during tests.
// Currently, only KubeObjects, CertManagerObjects and GWObjects can be
// specified. These will be auto loaded into the constructed fake Clientsets.
// Call ToContext() to construct a new context using the given values.
type Builder struct {
T *testing.T
KubeObjects []runtime.Object
CertManagerObjects []runtime.Object
GWObjects []runtime.Object
PartialMetadataObjects []runtime.Object
ExpectedActions []Action
ExpectedEvents []string
StringGenerator StringGenerator
// Clock will be the Clock set on the controller context.
// If not specified, the RealClock will be used.
Clock *fakeclock.FakeClock
// CheckFn is a custom check function that will be executed when the
// CheckAndFinish method is called on the builder, after all other checks.
// It will be passed a reference to the Builder in order to access state,
// as well as a list of all the arguments passed to the CheckAndFinish
// function (typically the list of return arguments from the function under
// test).
CheckFn func(*Builder, ...interface{})
stopCh chan struct{}
requiredReactors map[string]bool
additionalSyncFuncs []cache.InformerSynced
*controller.Context
}
func (b *Builder) generateNameReactor(action coretesting.Action) (handled bool, ret runtime.Object, err error) {
obj := action.(coretesting.CreateAction).GetObject().(metav1.Object)
genName := obj.GetGenerateName()
if genName != "" {
obj.SetName(genName + b.StringGenerator(5))
return false, obj.(runtime.Object), nil
}
return false, obj.(runtime.Object), nil
}
// informerResyncPeriod is the resync period used by the test informers. We
// want this period to be as short as possible to make the tests faster.
// However, client-go imposes a minimum resync period of 1 second, so that
// is the lowest we can go.
// https://github.com/kubernetes/client-go/blob/5a019202120ab4dd7dfb3788e5cb87269f343ebe/tools/cache/shared_informer.go#L575
const informerResyncPeriod = time.Second
// Init will construct a new context for this builder and set default values
// for any unset fields.
func (b *Builder) Init() {
if b.Context == nil {
b.Context = &controller.Context{
RootContext: context.Background(),
}
}
if b.StringGenerator == nil {
b.StringGenerator = rand.String
}
scheme := metadatafake.NewTestScheme()
metav1.AddMetaToScheme(scheme)
b.requiredReactors = make(map[string]bool)
b.Client = kubefake.NewSimpleClientset(b.KubeObjects...)
b.CMClient = cmfake.NewSimpleClientset(b.CertManagerObjects...)
b.GWClient = gwfake.NewSimpleClientset(b.GWObjects...)
b.MetadataClient = metadatafake.NewSimpleMetadataClient(scheme, b.PartialMetadataObjects...)
b.DiscoveryClient = discoveryfake.NewDiscovery().WithServerResourcesForGroupVersion(func(groupVersion string) (*metav1.APIResourceList, error) {
if groupVersion == networkingv1.SchemeGroupVersion.String() {
return &metav1.APIResourceList{
TypeMeta: metav1.TypeMeta{},
GroupVersion: networkingv1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{
Name: "ingresses",
SingularName: "Ingress",
Namespaced: true,
Group: networkingv1.GroupName,
Version: networkingv1.SchemeGroupVersion.Version,
Kind: networkingv1.SchemeGroupVersion.WithKind("Ingress").Kind,
Verbs: metav1.Verbs{"get", "list", "watch", "create", "update", "patch", "delete", "deletecollection"},
ShortNames: []string{"ing"},
Categories: []string{"all"},
StorageVersionHash: "testing",
},
},
}, nil
}
return &metav1.APIResourceList{}, nil
})
b.Recorder = new(FakeRecorder)
b.FakeKubeClient().PrependReactor("create", "*", b.generateNameReactor)
b.FakeCMClient().PrependReactor("create", "*", b.generateNameReactor)
b.FakeGWClient().PrependReactor("create", "*", b.generateNameReactor)
b.FakeMetadataClient().PrependReactor("create", "*", b.generateNameReactor)
b.KubeSharedInformerFactory = internalinformers.NewBaseKubeInformerFactory(b.Client, informerResyncPeriod, "")
b.SharedInformerFactory = informers.NewSharedInformerFactory(b.CMClient, informerResyncPeriod)
b.GWShared = gwinformers.NewSharedInformerFactory(b.GWClient, informerResyncPeriod)
b.HTTP01ResourceMetadataInformersFactory = metadatainformer.NewFilteredSharedInformerFactory(b.MetadataClient, informerResyncPeriod, "", func(listOptions *metav1.ListOptions) {})
b.stopCh = make(chan struct{})
b.Metrics = metrics.New(logs.Log, clock.RealClock{})
// set the Clock on the context
if b.Clock == nil {
b.Context.Clock = clock.RealClock{}
} else {
b.Context.Clock = b.Clock
}
// Fix the clock used in apiutil so that calls to set status conditions
// can be predictably tested
apiutil.Clock = b.Context.Clock
}
// InitWithRESTConfig() will call builder.Init(), then assign an initialised
// RESTConfig with a `cert-manager/unit-test` User Agent.
func (b *Builder) InitWithRESTConfig() {
b.Init()
b.RESTConfig = util.RestConfigWithUserAgent(new(rest.Config), "unit-testing")
}
func (b *Builder) FakeKubeClient() *kubefake.Clientset {
return b.Context.Client.(*kubefake.Clientset)
}
func (b *Builder) FakeKubeInformerFactory() internalinformers.KubeInformerFactory {
return b.Context.KubeSharedInformerFactory
}
func (b *Builder) FakeCMClient() *cmfake.Clientset {
return b.Context.CMClient.(*cmfake.Clientset)
}
func (b *Builder) FakeGWClient() *gwfake.Clientset {
return b.Context.GWClient.(*gwfake.Clientset)
}
func (b *Builder) FakeCMInformerFactory() informers.SharedInformerFactory {
return b.Context.SharedInformerFactory
}
func (b *Builder) FakeMetadataClient() *metadatafake.FakeMetadataClient {
return b.Context.MetadataClient.(*metadatafake.FakeMetadataClient)
}
func (b *Builder) FakeDiscoveryClient() *discoveryfake.Discovery {
return b.Context.DiscoveryClient.(*discoveryfake.Discovery)
}
func (b *Builder) EnsureReactorCalled(testName string, fn coretesting.ReactionFunc) coretesting.ReactionFunc {
b.requiredReactors[testName] = false
return func(action coretesting.Action) (handled bool, ret runtime.Object, err error) {
handled, ret, err = fn(action)
if !handled {
return
}
b.requiredReactors[testName] = true
return
}
}
// CheckAndFinish will run ensure: all reactors are called, all actions are
// expected, and all events are as expected.
// It will then call the Builder's CheckFn, if defined.
func (b *Builder) CheckAndFinish(args ...interface{}) {
defer b.Stop()
if err := b.AllReactorsCalled(); err != nil {
b.T.Errorf("Not all expected reactors were called: %v", err)
}
if err := b.AllActionsExecuted(); err != nil {
b.T.Errorf(err.Error())
}
if err := b.AllEventsCalled(); err != nil {
b.T.Errorf(err.Error())
}
// resync listers before running checks
b.Sync()
// run custom checks
if b.CheckFn != nil {
b.CheckFn(b, args...)
}
}
func (b *Builder) AllReactorsCalled() error {
var errs []error
for n, reactorCalled := range b.requiredReactors {
if !reactorCalled {
errs = append(errs, fmt.Errorf("reactor not called: %s", n))
}
}
return utilerrors.NewAggregate(errs)
}
func (b *Builder) AllEventsCalled() error {
var errs []error
if !util.EqualUnsorted(b.ExpectedEvents, b.Events()) {
errs = append(errs, fmt.Errorf("got unexpected events, exp='%s' got='%s'",
b.ExpectedEvents, b.Events()))
}
return utilerrors.NewAggregate(errs)
}
// AllActionsExecuted skips the "list" and "watch" action verbs.
func (b *Builder) AllActionsExecuted() error {
firedActions := b.FakeCMClient().Actions()
firedActions = append(firedActions, b.FakeKubeClient().Actions()...)
firedActions = append(firedActions, b.FakeGWClient().Actions()...)
var unexpectedActions []coretesting.Action
var errs []error
missingActions := make([]Action, len(b.ExpectedActions))
copy(missingActions, b.ExpectedActions)
for _, a := range firedActions {
// skip list and watch actions
if a.GetVerb() == "list" || a.GetVerb() == "watch" {
continue
}
found := false
var err error
for i, expA := range missingActions {
if expA.Action().GetNamespace() != a.GetNamespace() ||
expA.Action().GetResource() != a.GetResource() ||
expA.Action().GetSubresource() != a.GetSubresource() ||
expA.Action().GetVerb() != a.GetVerb() {
continue
}
err = expA.Matches(a)
// if this action doesn't match, we record the error and continue
// as there may be multiple action matchers for the same resource
if err != nil {
continue
}
missingActions = append(missingActions[:i], missingActions[i+1:]...)
found = true
break
}
if !found {
unexpectedActions = append(unexpectedActions, a)
if err != nil {
errs = append(errs, err)
}
}
}
for _, a := range missingActions {
errs = append(errs, fmt.Errorf("missing action: %v", actionToString(a.Action())))
}
for _, a := range unexpectedActions {
errs = append(errs, fmt.Errorf("unexpected action: %v", actionToString(a)))
}
return utilerrors.NewAggregate(errs)
}
func actionToString(a coretesting.Action) string {
return fmt.Sprintf("%s %s %q in namespace %s", a.GetVerb(), a.GetSubresource(), a.GetResource(), a.GetNamespace())
}
// Stop will signal the informers to stop watching changes
// This method is *not* safe to be called concurrently
func (b *Builder) Stop() {
if b.stopCh == nil {
return
}
close(b.stopCh)
b.stopCh = nil
// Reset the clock back to the RealClock in apiutil
apiutil.Clock = clock.RealClock{}
}
func (b *Builder) Start() {
b.KubeSharedInformerFactory.Start(b.stopCh)
b.SharedInformerFactory.Start(b.stopCh)
b.GWShared.Start(b.stopCh)
b.HTTP01ResourceMetadataInformersFactory.Start(b.stopCh)
// wait for caches to sync
b.Sync()
}
// Sync is a function used by tests to wait for all informers to be synced. This function
// is called initially by the Start method, to wait for the caches to be populated. It is
// also called directly by tests to wait for any updates made by the fake clients to be
// reflected in the informer caches.
// Sync calls the WaitForCacheSync method on all informers to make sure they have populated
// their caches. The WaitForCacheSync method is only useful at startup. In order to wait
// for updates made by the fake clients to be reflected in the informer caches, we need
// to sleep for the informerResyncPeriod.
func (b *Builder) Sync() {
if err := mustAllSync(b.KubeSharedInformerFactory.WaitForCacheSync(b.stopCh)); err != nil {
panic("Error waiting for kubeSharedInformerFactory to sync: " + err.Error())
}
if err := mustAllSync(b.SharedInformerFactory.WaitForCacheSync(b.stopCh)); err != nil {
panic("Error waiting for SharedInformerFactory to sync: " + err.Error())
}
if err := mustAllSync(b.GWShared.WaitForCacheSync(b.stopCh)); err != nil {
panic("Error waiting for GWShared to sync: " + err.Error())
}
if err := mustAllSync(b.HTTP01ResourceMetadataInformersFactory.WaitForCacheSync(b.stopCh)); err != nil {
panic("Error waiting for MetadataInformerFactory to sync:" + err.Error())
}
if b.additionalSyncFuncs != nil {
cache.WaitForCacheSync(b.stopCh, b.additionalSyncFuncs...)
}
// Wait for the informerResyncPeriod to make sure any update made by any of the fake clients
// is reflected in the informer caches.
time.Sleep(informerResyncPeriod)
}
// RegisterAdditionalSyncFuncs registers an additional InformerSynced function
// with the builder.
// When the Sync method is called, the builder will also wait for the given
// listers to be synced as well as the listers that were registered with the
// informer factories that the builder provides.
func (b *Builder) RegisterAdditionalSyncFuncs(fns ...cache.InformerSynced) {
b.additionalSyncFuncs = append(b.additionalSyncFuncs, fns...)
}
func (b *Builder) Events() []string {
if e, ok := b.Recorder.(*FakeRecorder); ok {
return e.Events
}
return nil
}
func mustAllSync[E comparable](in map[E]bool) error {
var errs []error
for t, started := range in {
if !started {
errs = append(errs, fmt.Errorf("informer for %v not synced", t))
}
}
return utilerrors.NewAggregate(errs)
}