-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkubernetes_dynamic_client_mock.go
More file actions
386 lines (314 loc) · 15.4 KB
/
Copy pathkubernetes_dynamic_client_mock.go
File metadata and controls
386 lines (314 loc) · 15.4 KB
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
// Code generated by http://github.com/gojuno/minimock (v3.4.7). DO NOT EDIT.
package mock
//go:generate minimock -i k8s.io/client-go/dynamic.Interface -o kubernetes_dynamic_client_mock.go -n KubernetesDynamicClientMock -p mock
import (
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock/v3"
"k8s.io/apimachinery/pkg/runtime/schema"
mm_dynamic "k8s.io/client-go/dynamic"
)
// KubernetesDynamicClientMock implements mm_dynamic.Interface
type KubernetesDynamicClientMock struct {
t minimock.Tester
finishOnce sync.Once
funcResource func(resource schema.GroupVersionResource) (n1 mm_dynamic.NamespaceableResourceInterface)
funcResourceOrigin string
inspectFuncResource func(resource schema.GroupVersionResource)
afterResourceCounter uint64
beforeResourceCounter uint64
ResourceMock mKubernetesDynamicClientMockResource
}
// NewKubernetesDynamicClientMock returns a mock for mm_dynamic.Interface
func NewKubernetesDynamicClientMock(t minimock.Tester) *KubernetesDynamicClientMock {
m := &KubernetesDynamicClientMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.ResourceMock = mKubernetesDynamicClientMockResource{mock: m}
m.ResourceMock.callArgs = []*KubernetesDynamicClientMockResourceParams{}
t.Cleanup(m.MinimockFinish)
return m
}
type mKubernetesDynamicClientMockResource struct {
optional bool
mock *KubernetesDynamicClientMock
defaultExpectation *KubernetesDynamicClientMockResourceExpectation
expectations []*KubernetesDynamicClientMockResourceExpectation
callArgs []*KubernetesDynamicClientMockResourceParams
mutex sync.RWMutex
expectedInvocations uint64
expectedInvocationsOrigin string
}
// KubernetesDynamicClientMockResourceExpectation specifies expectation struct of the Interface.Resource
type KubernetesDynamicClientMockResourceExpectation struct {
mock *KubernetesDynamicClientMock
params *KubernetesDynamicClientMockResourceParams
paramPtrs *KubernetesDynamicClientMockResourceParamPtrs
expectationOrigins KubernetesDynamicClientMockResourceExpectationOrigins
results *KubernetesDynamicClientMockResourceResults
returnOrigin string
Counter uint64
}
// KubernetesDynamicClientMockResourceParams contains parameters of the Interface.Resource
type KubernetesDynamicClientMockResourceParams struct {
resource schema.GroupVersionResource
}
// KubernetesDynamicClientMockResourceParamPtrs contains pointers to parameters of the Interface.Resource
type KubernetesDynamicClientMockResourceParamPtrs struct {
resource *schema.GroupVersionResource
}
// KubernetesDynamicClientMockResourceResults contains results of the Interface.Resource
type KubernetesDynamicClientMockResourceResults struct {
n1 mm_dynamic.NamespaceableResourceInterface
}
// KubernetesDynamicClientMockResourceOrigins contains origins of expectations of the Interface.Resource
type KubernetesDynamicClientMockResourceExpectationOrigins struct {
origin string
originResource string
}
// Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning
// the test will fail minimock's automatic final call check if the mocked method was not called at least once.
// Optional() makes method check to work in '0 or more' mode.
// It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to
// catch the problems when the expected method call is totally skipped during test run.
func (mmResource *mKubernetesDynamicClientMockResource) Optional() *mKubernetesDynamicClientMockResource {
mmResource.optional = true
return mmResource
}
// Expect sets up expected params for Interface.Resource
func (mmResource *mKubernetesDynamicClientMockResource) Expect(resource schema.GroupVersionResource) *mKubernetesDynamicClientMockResource {
if mmResource.mock.funcResource != nil {
mmResource.mock.t.Fatalf("KubernetesDynamicClientMock.Resource mock is already set by Set")
}
if mmResource.defaultExpectation == nil {
mmResource.defaultExpectation = &KubernetesDynamicClientMockResourceExpectation{}
}
if mmResource.defaultExpectation.paramPtrs != nil {
mmResource.mock.t.Fatalf("KubernetesDynamicClientMock.Resource mock is already set by ExpectParams functions")
}
mmResource.defaultExpectation.params = &KubernetesDynamicClientMockResourceParams{resource}
mmResource.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1)
for _, e := range mmResource.expectations {
if minimock.Equal(e.params, mmResource.defaultExpectation.params) {
mmResource.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmResource.defaultExpectation.params)
}
}
return mmResource
}
// ExpectResourceParam1 sets up expected param resource for Interface.Resource
func (mmResource *mKubernetesDynamicClientMockResource) ExpectResourceParam1(resource schema.GroupVersionResource) *mKubernetesDynamicClientMockResource {
if mmResource.mock.funcResource != nil {
mmResource.mock.t.Fatalf("KubernetesDynamicClientMock.Resource mock is already set by Set")
}
if mmResource.defaultExpectation == nil {
mmResource.defaultExpectation = &KubernetesDynamicClientMockResourceExpectation{}
}
if mmResource.defaultExpectation.params != nil {
mmResource.mock.t.Fatalf("KubernetesDynamicClientMock.Resource mock is already set by Expect")
}
if mmResource.defaultExpectation.paramPtrs == nil {
mmResource.defaultExpectation.paramPtrs = &KubernetesDynamicClientMockResourceParamPtrs{}
}
mmResource.defaultExpectation.paramPtrs.resource = &resource
mmResource.defaultExpectation.expectationOrigins.originResource = minimock.CallerInfo(1)
return mmResource
}
// Inspect accepts an inspector function that has same arguments as the Interface.Resource
func (mmResource *mKubernetesDynamicClientMockResource) Inspect(f func(resource schema.GroupVersionResource)) *mKubernetesDynamicClientMockResource {
if mmResource.mock.inspectFuncResource != nil {
mmResource.mock.t.Fatalf("Inspect function is already set for KubernetesDynamicClientMock.Resource")
}
mmResource.mock.inspectFuncResource = f
return mmResource
}
// Return sets up results that will be returned by Interface.Resource
func (mmResource *mKubernetesDynamicClientMockResource) Return(n1 mm_dynamic.NamespaceableResourceInterface) *KubernetesDynamicClientMock {
if mmResource.mock.funcResource != nil {
mmResource.mock.t.Fatalf("KubernetesDynamicClientMock.Resource mock is already set by Set")
}
if mmResource.defaultExpectation == nil {
mmResource.defaultExpectation = &KubernetesDynamicClientMockResourceExpectation{mock: mmResource.mock}
}
mmResource.defaultExpectation.results = &KubernetesDynamicClientMockResourceResults{n1}
mmResource.defaultExpectation.returnOrigin = minimock.CallerInfo(1)
return mmResource.mock
}
// Set uses given function f to mock the Interface.Resource method
func (mmResource *mKubernetesDynamicClientMockResource) Set(f func(resource schema.GroupVersionResource) (n1 mm_dynamic.NamespaceableResourceInterface)) *KubernetesDynamicClientMock {
if mmResource.defaultExpectation != nil {
mmResource.mock.t.Fatalf("Default expectation is already set for the Interface.Resource method")
}
if len(mmResource.expectations) > 0 {
mmResource.mock.t.Fatalf("Some expectations are already set for the Interface.Resource method")
}
mmResource.mock.funcResource = f
mmResource.mock.funcResourceOrigin = minimock.CallerInfo(1)
return mmResource.mock
}
// When sets expectation for the Interface.Resource which will trigger the result defined by the following
// Then helper
func (mmResource *mKubernetesDynamicClientMockResource) When(resource schema.GroupVersionResource) *KubernetesDynamicClientMockResourceExpectation {
if mmResource.mock.funcResource != nil {
mmResource.mock.t.Fatalf("KubernetesDynamicClientMock.Resource mock is already set by Set")
}
expectation := &KubernetesDynamicClientMockResourceExpectation{
mock: mmResource.mock,
params: &KubernetesDynamicClientMockResourceParams{resource},
expectationOrigins: KubernetesDynamicClientMockResourceExpectationOrigins{origin: minimock.CallerInfo(1)},
}
mmResource.expectations = append(mmResource.expectations, expectation)
return expectation
}
// Then sets up Interface.Resource return parameters for the expectation previously defined by the When method
func (e *KubernetesDynamicClientMockResourceExpectation) Then(n1 mm_dynamic.NamespaceableResourceInterface) *KubernetesDynamicClientMock {
e.results = &KubernetesDynamicClientMockResourceResults{n1}
return e.mock
}
// Times sets number of times Interface.Resource should be invoked
func (mmResource *mKubernetesDynamicClientMockResource) Times(n uint64) *mKubernetesDynamicClientMockResource {
if n == 0 {
mmResource.mock.t.Fatalf("Times of KubernetesDynamicClientMock.Resource mock can not be zero")
}
mm_atomic.StoreUint64(&mmResource.expectedInvocations, n)
mmResource.expectedInvocationsOrigin = minimock.CallerInfo(1)
return mmResource
}
func (mmResource *mKubernetesDynamicClientMockResource) invocationsDone() bool {
if len(mmResource.expectations) == 0 && mmResource.defaultExpectation == nil && mmResource.mock.funcResource == nil {
return true
}
totalInvocations := mm_atomic.LoadUint64(&mmResource.mock.afterResourceCounter)
expectedInvocations := mm_atomic.LoadUint64(&mmResource.expectedInvocations)
return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations)
}
// Resource implements mm_dynamic.Interface
func (mmResource *KubernetesDynamicClientMock) Resource(resource schema.GroupVersionResource) (n1 mm_dynamic.NamespaceableResourceInterface) {
mm_atomic.AddUint64(&mmResource.beforeResourceCounter, 1)
defer mm_atomic.AddUint64(&mmResource.afterResourceCounter, 1)
mmResource.t.Helper()
if mmResource.inspectFuncResource != nil {
mmResource.inspectFuncResource(resource)
}
mm_params := KubernetesDynamicClientMockResourceParams{resource}
// Record call args
mmResource.ResourceMock.mutex.Lock()
mmResource.ResourceMock.callArgs = append(mmResource.ResourceMock.callArgs, &mm_params)
mmResource.ResourceMock.mutex.Unlock()
for _, e := range mmResource.ResourceMock.expectations {
if minimock.Equal(*e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.n1
}
}
if mmResource.ResourceMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmResource.ResourceMock.defaultExpectation.Counter, 1)
mm_want := mmResource.ResourceMock.defaultExpectation.params
mm_want_ptrs := mmResource.ResourceMock.defaultExpectation.paramPtrs
mm_got := KubernetesDynamicClientMockResourceParams{resource}
if mm_want_ptrs != nil {
if mm_want_ptrs.resource != nil && !minimock.Equal(*mm_want_ptrs.resource, mm_got.resource) {
mmResource.t.Errorf("KubernetesDynamicClientMock.Resource got unexpected parameter resource, expected at\n%s:\nwant: %#v\n got: %#v%s\n",
mmResource.ResourceMock.defaultExpectation.expectationOrigins.originResource, *mm_want_ptrs.resource, mm_got.resource, minimock.Diff(*mm_want_ptrs.resource, mm_got.resource))
}
} else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmResource.t.Errorf("KubernetesDynamicClientMock.Resource got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n",
mmResource.ResourceMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmResource.ResourceMock.defaultExpectation.results
if mm_results == nil {
mmResource.t.Fatal("No results are set for the KubernetesDynamicClientMock.Resource")
}
return (*mm_results).n1
}
if mmResource.funcResource != nil {
return mmResource.funcResource(resource)
}
mmResource.t.Fatalf("Unexpected call to KubernetesDynamicClientMock.Resource. %v", resource)
return
}
// ResourceAfterCounter returns a count of finished KubernetesDynamicClientMock.Resource invocations
func (mmResource *KubernetesDynamicClientMock) ResourceAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmResource.afterResourceCounter)
}
// ResourceBeforeCounter returns a count of KubernetesDynamicClientMock.Resource invocations
func (mmResource *KubernetesDynamicClientMock) ResourceBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmResource.beforeResourceCounter)
}
// Calls returns a list of arguments used in each call to KubernetesDynamicClientMock.Resource.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmResource *mKubernetesDynamicClientMockResource) Calls() []*KubernetesDynamicClientMockResourceParams {
mmResource.mutex.RLock()
argCopy := make([]*KubernetesDynamicClientMockResourceParams, len(mmResource.callArgs))
copy(argCopy, mmResource.callArgs)
mmResource.mutex.RUnlock()
return argCopy
}
// MinimockResourceDone returns true if the count of the Resource invocations corresponds
// the number of defined expectations
func (m *KubernetesDynamicClientMock) MinimockResourceDone() bool {
if m.ResourceMock.optional {
// Optional methods provide '0 or more' call count restriction.
return true
}
for _, e := range m.ResourceMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
return m.ResourceMock.invocationsDone()
}
// MinimockResourceInspect logs each unmet expectation
func (m *KubernetesDynamicClientMock) MinimockResourceInspect() {
for _, e := range m.ResourceMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to KubernetesDynamicClientMock.Resource at\n%s with params: %#v", e.expectationOrigins.origin, *e.params)
}
}
afterResourceCounter := mm_atomic.LoadUint64(&m.afterResourceCounter)
// if default expectation was set then invocations count should be greater than zero
if m.ResourceMock.defaultExpectation != nil && afterResourceCounter < 1 {
if m.ResourceMock.defaultExpectation.params == nil {
m.t.Errorf("Expected call to KubernetesDynamicClientMock.Resource at\n%s", m.ResourceMock.defaultExpectation.returnOrigin)
} else {
m.t.Errorf("Expected call to KubernetesDynamicClientMock.Resource at\n%s with params: %#v", m.ResourceMock.defaultExpectation.expectationOrigins.origin, *m.ResourceMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcResource != nil && afterResourceCounter < 1 {
m.t.Errorf("Expected call to KubernetesDynamicClientMock.Resource at\n%s", m.funcResourceOrigin)
}
if !m.ResourceMock.invocationsDone() && afterResourceCounter > 0 {
m.t.Errorf("Expected %d calls to KubernetesDynamicClientMock.Resource at\n%s but found %d calls",
mm_atomic.LoadUint64(&m.ResourceMock.expectedInvocations), m.ResourceMock.expectedInvocationsOrigin, afterResourceCounter)
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *KubernetesDynamicClientMock) MinimockFinish() {
m.finishOnce.Do(func() {
if !m.minimockDone() {
m.MinimockResourceInspect()
}
})
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *KubernetesDynamicClientMock) MinimockWait(timeout mm_time.Duration) {
timeoutCh := mm_time.After(timeout)
for {
if m.minimockDone() {
return
}
select {
case <-timeoutCh:
m.MinimockFinish()
return
case <-mm_time.After(10 * mm_time.Millisecond):
}
}
}
func (m *KubernetesDynamicClientMock) minimockDone() bool {
done := true
return done &&
m.MinimockResourceDone()
}