/
mock_client.go
176 lines (151 loc) · 5.79 KB
/
mock_client.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
/*
Copyright 2018 The Knative 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 testing
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type MockHandled int
const (
// This mock has handled the function call, no further mocks nor the real client should be
// called.
Handled MockHandled = iota
// This mock has not handled the function call, subsequent mocks or the real client should be
// called.
Unhandled
)
// All of the funcions in client.Client get mocked equivalents. For the function
// client.Client.Foo(), the mocked equivalent will be:
// func(innerClient client.Client[, arguments to Foo()]) (MockHandled[, returns from Foo()])
type MockGet func(innerClient client.Client, ctx context.Context, key client.ObjectKey, obj runtime.Object) (MockHandled, error)
type MockList func(innerClient client.Client, ctx context.Context, opts *client.ListOptions, list runtime.Object) (MockHandled, error)
type MockCreate func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)
type MockDelete func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)
type MockUpdate func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)
type MockStatusUpdate func(innerClient client.Client, ctx context.Context, obj runtime.Object) (MockHandled, error)
var _ client.Client = (*MockClient)(nil)
// mockClient is a client.Client that allows mock responses to be returned, instead of calling the
// inner client.Client.
type MockClient struct {
innerClient client.Client
mocks Mocks
}
type mockStatusWriter struct {
parent *MockClient
}
// The mocks to run on each function type. Each function will run through the mocks in its list
// until one responds with 'Handled'. If there is more than one mock in the list, then the one that
// responds 'Handled' will be removed and not run on subsequent calls to the function. If no mocks
// respond 'Handled', then the real underlying client is called.
type Mocks struct {
MockGets []MockGet
MockLists []MockList
MockCreates []MockCreate
MockDeletes []MockDelete
MockUpdates []MockUpdate
MockStatusUpdates []MockStatusUpdate
}
func NewMockClient(innerClient client.Client, mocks Mocks) *MockClient {
return &MockClient{
innerClient: innerClient,
mocks: mocks,
}
}
func (m *MockClient) stopMocking() {
m.mocks = Mocks{}
}
// All of the functions are handled almost identically:
// 1. Run through the mocks in order:
// a. If the mock handled the request, then:
// i. If there is at least one other mock in the list, remove this mock.
// ii. Return the response from the mock.
// 2. No mock handled the request, so call the inner client.
func (m *MockClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
for i, mockGet := range m.mocks.MockGets {
handled, err := mockGet(m.innerClient, ctx, key, obj)
if handled == Handled {
if len(m.mocks.MockGets) > 1 {
m.mocks.MockGets = append(m.mocks.MockGets[:i], m.mocks.MockGets[i+1:]...)
}
return err
}
}
return m.innerClient.Get(ctx, key, obj)
}
func (m *MockClient) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
for i, mockList := range m.mocks.MockLists {
handled, err := mockList(m.innerClient, ctx, opts, list)
if handled == Handled {
if len(m.mocks.MockLists) > 1 {
m.mocks.MockLists = append(m.mocks.MockLists[:i], m.mocks.MockLists[i+1:]...)
}
return err
}
}
return m.innerClient.List(ctx, opts, list)
}
func (m *MockClient) Create(ctx context.Context, obj runtime.Object) error {
for i, mockCreate := range m.mocks.MockCreates {
handled, err := mockCreate(m.innerClient, ctx, obj)
if handled == Handled {
if len(m.mocks.MockCreates) > 1 {
m.mocks.MockCreates = append(m.mocks.MockCreates[:i], m.mocks.MockCreates[i+1:]...)
}
return err
}
}
return m.innerClient.Create(ctx, obj)
}
func (m *MockClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOptionFunc) error {
for i, mockDelete := range m.mocks.MockDeletes {
handled, err := mockDelete(m.innerClient, ctx, obj)
if handled == Handled {
if len(m.mocks.MockDeletes) > 1 {
m.mocks.MockDeletes = append(m.mocks.MockDeletes[:i], m.mocks.MockDeletes[i+1:]...)
}
return err
}
}
return m.innerClient.Delete(ctx, obj, opts...)
}
func (m *MockClient) Update(ctx context.Context, obj runtime.Object) error {
for i, mockUpdate := range m.mocks.MockUpdates {
handled, err := mockUpdate(m.innerClient, ctx, obj)
if handled == Handled {
if len(m.mocks.MockUpdates) > 1 {
m.mocks.MockUpdates = append(m.mocks.MockUpdates[:i], m.mocks.MockUpdates[i+1:]...)
}
return err
}
}
return m.innerClient.Update(ctx, obj)
}
func (m *MockClient) Status() client.StatusWriter {
return &mockStatusWriter{
parent: m,
}
}
func (w *mockStatusWriter) Update(ctx context.Context, obj runtime.Object) error {
mocks := w.parent.mocks.MockStatusUpdates
for i, mock := range mocks {
handled, err := mock(w.parent.innerClient, ctx, obj)
if handled == Handled {
if len(w.parent.mocks.MockStatusUpdates) > 1 {
w.parent.mocks.MockStatusUpdates = append(mocks[:i], mocks[i+1:]...)
}
return err
}
}
return w.parent.innerClient.Status().Update(ctx, obj)
}