-
Notifications
You must be signed in to change notification settings - Fork 95
/
fake.go
383 lines (321 loc) · 13.6 KB
/
fake.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
/*
Copyright 2019 The Crossplane 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"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ client.Client = &MockClient{}
// A MockGetFn is used to mock client.Client's Get implementation.
type MockGetFn func(ctx context.Context, key client.ObjectKey, obj client.Object) error
// A MockListFn is used to mock client.Client's List implementation.
type MockListFn func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
// A MockCreateFn is used to mock client.Client's Create implementation.
type MockCreateFn func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
// A MockDeleteFn is used to mock client.Client's Delete implementation.
type MockDeleteFn func(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error
// A MockDeleteAllOfFn is used to mock client.Client's Delete implementation.
type MockDeleteAllOfFn func(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error
// A MockUpdateFn is used to mock client.Client's Update implementation.
type MockUpdateFn func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
// A MockPatchFn is used to mock client.Client's Patch implementation.
type MockPatchFn func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
// A MockSubResourceGetFn is used to mock client.SubResourceClient's get implementation.
type MockSubResourceGetFn func(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceGetOption) error
// A MockSubResourceCreateFn is used to mock client.SubResourceClient's create implementation.
type MockSubResourceCreateFn func(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceCreateOption) error
// A MockSubResourceUpdateFn is used to mock client.SubResourceClient's update implementation.
type MockSubResourceUpdateFn func(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error
// A MockSubResourcePatchFn is used to mock client.SubResourceClient's patch implementation.
type MockSubResourcePatchFn func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error
// A MockSchemeFn is used to mock client.Client's Scheme implementation.
type MockSchemeFn func() *runtime.Scheme
// A MockGroupVersionKindForFn is used to mock client.Client's GroupVersionKindFor implementation.
type MockGroupVersionKindForFn func(runtime.Object) (schema.GroupVersionKind, error)
// A MockIsObjectNamespacedFn is used to mock client.Client's IsObjectNamespaced implementation.
type MockIsObjectNamespacedFn func(runtime.Object) (bool, error)
// An ObjectFn operates on the supplied Object. You might use an ObjectFn to
// test or update the contents of an Object.
type ObjectFn func(obj client.Object) error
// An RuntimeObjectFn operates on the supplied Object. You might use an RuntimeObjectFn to
// test or update the contents of an runtime.Object.
type RuntimeObjectFn func(obj runtime.Object) error
// An ObjectListFn operates on the supplied ObjectList. You might use an
// ObjectListFn to test or update the contents of an ObjectList.
type ObjectListFn func(obj client.ObjectList) error
// NewMockGetFn returns a MockGetFn that returns the supplied error.
func NewMockGetFn(err error, ofn ...ObjectFn) MockGetFn {
return func(_ context.Context, _ client.ObjectKey, obj client.Object) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockListFn returns a MockListFn that returns the supplied error.
func NewMockListFn(err error, ofn ...ObjectListFn) MockListFn {
return func(_ context.Context, obj client.ObjectList, _ ...client.ListOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockCreateFn returns a MockCreateFn that returns the supplied error.
func NewMockCreateFn(err error, ofn ...ObjectFn) MockCreateFn {
return func(_ context.Context, obj client.Object, _ ...client.CreateOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockDeleteFn returns a MockDeleteFn that returns the supplied error.
func NewMockDeleteFn(err error, ofn ...ObjectFn) MockDeleteFn {
return func(_ context.Context, obj client.Object, _ ...client.DeleteOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockDeleteAllOfFn returns a MockDeleteAllOfFn that returns the supplied error.
func NewMockDeleteAllOfFn(err error, ofn ...ObjectFn) MockDeleteAllOfFn {
return func(_ context.Context, obj client.Object, _ ...client.DeleteAllOfOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockUpdateFn returns a MockUpdateFn that returns the supplied error.
func NewMockUpdateFn(err error, ofn ...ObjectFn) MockUpdateFn {
return func(_ context.Context, obj client.Object, _ ...client.UpdateOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockPatchFn returns a MockPatchFn that returns the supplied error.
func NewMockPatchFn(err error, ofn ...ObjectFn) MockPatchFn {
return func(_ context.Context, obj client.Object, _ client.Patch, _ ...client.PatchOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockSubResourceCreateFn returns a MockSubResourceCreateFn that returns the supplied error.
func NewMockSubResourceCreateFn(err error, ofn ...ObjectFn) MockSubResourceCreateFn {
return func(_ context.Context, obj, _ client.Object, _ ...client.SubResourceCreateOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockSubResourceUpdateFn returns a MockSubResourceUpdateFn that returns the supplied error.
func NewMockSubResourceUpdateFn(err error, ofn ...ObjectFn) MockSubResourceUpdateFn {
return func(_ context.Context, obj client.Object, _ ...client.SubResourceUpdateOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockSubResourcePatchFn returns a MockSubResourcePatchFn that returns the supplied error.
func NewMockSubResourcePatchFn(err error, ofn ...ObjectFn) MockSubResourcePatchFn {
return func(_ context.Context, obj client.Object, _ client.Patch, _ ...client.SubResourcePatchOption) error {
for _, fn := range ofn {
if err := fn(obj); err != nil {
return err
}
}
return err
}
}
// NewMockSchemeFn returns a MockSchemeFn that returns the scheme.
func NewMockSchemeFn(scheme *runtime.Scheme) MockSchemeFn {
return func() *runtime.Scheme {
return scheme
}
}
// NewMockGroupVersionKindForFn returns a MockGroupVersionKindForFn that returns the supplied GVK and error.
func NewMockGroupVersionKindForFn(err error, gvk schema.GroupVersionKind, rofn ...RuntimeObjectFn) MockGroupVersionKindForFn {
return func(obj runtime.Object) (schema.GroupVersionKind, error) {
for _, fn := range rofn {
if err := fn(obj); err != nil {
return gvk, err
}
}
return gvk, err
}
}
// NewMockIsObjectNamespacedFn returns a MockGroupVersionKindForFn that returns the supplied GVK and error.
func NewMockIsObjectNamespacedFn(err error, isNamespaced bool, rofn ...RuntimeObjectFn) MockIsObjectNamespacedFn {
return func(obj runtime.Object) (bool, error) {
for _, fn := range rofn {
if err := fn(obj); err != nil {
return isNamespaced, err
}
}
return isNamespaced, err
}
}
// MockClient implements controller-runtime's Client interface, allowing each
// method to be overridden for testing. The controller-runtime provides a fake
// client, but it is has surprising side effects (e.g. silently calling
// os.Exit(1)) and does not allow us control over the errors it returns.
type MockClient struct {
MockGet MockGetFn
MockList MockListFn
MockCreate MockCreateFn
MockDelete MockDeleteFn
MockDeleteAllOf MockDeleteAllOfFn
MockUpdate MockUpdateFn
MockPatch MockPatchFn
MockStatusCreate MockSubResourceCreateFn
MockStatusUpdate MockSubResourceUpdateFn
MockStatusPatch MockSubResourcePatchFn
MockSubResourceGet MockSubResourceGetFn
MockSubResourceCreate MockSubResourceCreateFn
MockSubResourceUpdate MockSubResourceUpdateFn
MockSubResourcePatch MockSubResourcePatchFn
MockScheme MockSchemeFn
MockGroupVersionKindFor MockGroupVersionKindForFn
MockIsObjectNamespaced MockIsObjectNamespacedFn
}
// NewMockClient returns a MockClient that does nothing when its methods are
// called.
func NewMockClient() *MockClient {
return &MockClient{
MockGet: NewMockGetFn(nil),
MockList: NewMockListFn(nil),
MockCreate: NewMockCreateFn(nil),
MockDelete: NewMockDeleteFn(nil),
MockDeleteAllOf: NewMockDeleteAllOfFn(nil),
MockUpdate: NewMockUpdateFn(nil),
MockPatch: NewMockPatchFn(nil),
MockStatusUpdate: NewMockSubResourceUpdateFn(nil),
MockStatusPatch: NewMockSubResourcePatchFn(nil),
MockScheme: NewMockSchemeFn(nil),
MockGroupVersionKindFor: NewMockGroupVersionKindForFn(nil, schema.GroupVersionKind{}),
MockIsObjectNamespaced: NewMockIsObjectNamespacedFn(nil, false),
}
}
// Get calls MockClient's MockGet function.
func (c *MockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
return c.MockGet(ctx, key, obj)
}
// List calls MockClient's MockList function.
func (c *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return c.MockList(ctx, list, opts...)
}
// Create calls MockClient's MockCreate function.
func (c *MockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return c.MockCreate(ctx, obj, opts...)
}
// Delete calls MockClient's MockDelete function.
func (c *MockClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return c.MockDelete(ctx, obj, opts...)
}
// DeleteAllOf calls MockClient's DeleteAllOf function.
func (c *MockClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
return c.MockDeleteAllOf(ctx, obj, opts...)
}
// Update calls MockClient's MockUpdate function.
func (c *MockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return c.MockUpdate(ctx, obj, opts...)
}
// Patch calls MockClient's MockPatch function.
func (c *MockClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return c.MockPatch(ctx, obj, patch, opts...)
}
// Status returns status writer for status sub-resource.
func (c *MockClient) Status() client.SubResourceWriter {
return &MockSubResourceClient{
MockCreate: c.MockStatusCreate,
MockUpdate: c.MockStatusUpdate,
MockPatch: c.MockStatusPatch,
}
}
// SubResource is unimplemented. It panics if called.
func (c *MockClient) SubResource(_ string) client.SubResourceClient {
return &MockSubResourceClient{
MockGet: c.MockSubResourceGet,
MockCreate: c.MockSubResourceCreate,
MockUpdate: c.MockSubResourceUpdate,
MockPatch: c.MockSubResourcePatch,
}
}
// RESTMapper returns the REST mapper.
func (c *MockClient) RESTMapper() meta.RESTMapper {
return nil
}
// Scheme calls MockClient's MockScheme function.
func (c *MockClient) Scheme() *runtime.Scheme {
return c.MockScheme()
}
// GroupVersionKindFor calls MockClient's MockGroupVersionKindFor function.
func (c *MockClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
return c.MockGroupVersionKindFor(obj)
}
// IsObjectNamespaced calls MockClient's MockIsObjectNamespaced function.
func (c *MockClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
return c.MockIsObjectNamespaced(obj)
}
// MockSubResourceClient provides mock functionality for status sub-resource.
type MockSubResourceClient struct {
MockGet MockSubResourceGetFn
MockCreate MockSubResourceCreateFn
MockUpdate MockSubResourceUpdateFn
MockPatch MockSubResourcePatchFn
}
// Get a sub-resource.
func (m *MockSubResourceClient) Get(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceGetOption) error {
return m.MockGet(ctx, obj, subResource, opts...)
}
// Create a sub-resource.
func (m *MockSubResourceClient) Create(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceCreateOption) error {
return m.MockCreate(ctx, obj, subResource, opts...)
}
// Update a sub-resource.
func (m *MockSubResourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return m.MockUpdate(ctx, obj, opts...)
}
// Patch a sub-resource.
func (m *MockSubResourceClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
return m.MockPatch(ctx, obj, patch, opts...)
}