This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
248 lines (209 loc) · 6.81 KB
/
interface.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
// Copyright 2018 Istio 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 mock
import (
"fmt"
"runtime/debug"
"sync"
"sync/atomic"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
)
// Interface is the mock of apiext.CustomResourceDefinitionInterface.
type Interface struct {
responses chan response
callers int32
shutdown chan struct{}
EagerPanic bool
log string
logLock sync.Mutex
}
type response struct {
create1 *apiext.CustomResourceDefinition
create2 error
update1 *apiext.CustomResourceDefinition
update2 error
updateStatus1 *apiext.CustomResourceDefinition
updateStatus2 error
delete1 error
deleteCollection1 error
get1 *apiext.CustomResourceDefinition
get2 error
list1 *apiext.CustomResourceDefinitionList
list2 error
watch1 watch.Interface
watch2 error
patch1 *apiext.CustomResourceDefinition
patch2 error
panic string
}
var _ v1beta1.CustomResourceDefinitionInterface = &Interface{}
// NewInterface returns a new mock Interface.
func NewInterface() *Interface {
return &Interface{
responses: make(chan response, 1024),
shutdown: make(chan struct{}),
}
}
// Create is implementation of CustomResourceDefinitionInterface.Create.
func (m *Interface) Create(c *apiext.CustomResourceDefinition) (*apiext.CustomResourceDefinition, error) {
m.appendToLog("CREATE: %s\n", c.Name)
r := m.dequeueResponse()
return r.create1, r.create2
}
// Update is implementation of CustomResourceDefinitionInterface.Update.
func (m *Interface) Update(c *apiext.CustomResourceDefinition) (*apiext.CustomResourceDefinition, error) {
m.appendToLog("UPDATE: %s\n", c.Name)
r := m.dequeueResponse()
return r.update1, r.update2
}
// UpdateStatus is implementation of CustomResourceDefinitionInterface.UpdateStatus.
func (m *Interface) UpdateStatus(c *apiext.CustomResourceDefinition) (*apiext.CustomResourceDefinition, error) {
m.appendToLog("UPSTATESTATUS: %s\n", c.Name)
r := m.dequeueResponse()
return r.updateStatus1, r.updateStatus2
}
// Delete is implementation of CustomResourceDefinitionInterface.Deelte.
func (m *Interface) Delete(name string, options *v1.DeleteOptions) error {
m.appendToLog("DELETE: %s\n", name)
r := m.dequeueResponse()
return r.delete1
}
// DeleteCollection is implementation of CustomResourceDefinitionInterface.DeleteCollection.
func (m *Interface) DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error {
m.appendToLog("DELETECOLLECTION\n")
r := m.dequeueResponse()
return r.deleteCollection1
}
// Get is implementation of CustomResourceDefinitionInterface.Get.
func (m *Interface) Get(name string, options v1.GetOptions) (*apiext.CustomResourceDefinition, error) {
m.appendToLog("GET: %s\n", name)
r := m.dequeueResponse()
return r.get1, r.get2
}
// List is implementation of CustomResourceDefinitionInterface.List.
func (m *Interface) List(opts v1.ListOptions) (*apiext.CustomResourceDefinitionList, error) {
m.appendToLog("LIST\n")
r := m.dequeueResponse()
return r.list1, r.list2
}
// Watch is implementation of CustomResourceDefinitionInterface.Watch.
func (m *Interface) Watch(opts v1.ListOptions) (watch.Interface, error) {
m.appendToLog("WATCH\n")
r := m.dequeueResponse()
return r.watch1, r.watch2
}
// Patch is implementation of CustomResourceDefinitionInterface.Patch.
func (m *Interface) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *apiext.CustomResourceDefinition, err error) {
m.appendToLog("PATCH %s\n", name)
r := m.dequeueResponse()
return r.patch1, r.patch2
}
func (m *Interface) dequeueResponse() (r response) {
atomic.AddInt32(&m.callers, 1)
sh := false
if m.EagerPanic {
select {
case <-m.shutdown:
sh = true
case r = <-m.responses:
default:
panic("Unable to dequeue response")
}
} else {
select {
case <-m.shutdown:
sh = true
case r = <-m.responses:
}
}
atomic.AddInt32(&m.callers, -1)
if sh {
m.appendToLog("!! Straggler found !!%v\n", string(debug.Stack()))
panic(m.String())
}
// Handle panic at this level. It applies to all external methods.
if r.panic != "" {
panic(r.panic)
}
return
}
func (m *Interface) enqueueResponse(r response) {
m.responses <- r
}
// AddListResponse enqueues a new List response.
func (m *Interface) AddListResponse(list *apiext.CustomResourceDefinitionList, err error) {
m.enqueueResponse(response{
list1: list,
list2: err,
})
}
// AddCreateResponse enqueues a new Create response.
func (m *Interface) AddCreateResponse(crd *apiext.CustomResourceDefinition, err error) {
m.enqueueResponse(response{
create1: crd,
create2: err,
})
}
// AddUpdateResponse enqueues a new Update response.
func (m *Interface) AddUpdateResponse(crd *apiext.CustomResourceDefinition, err error) {
m.enqueueResponse(response{
update1: crd,
update2: err,
})
}
// AddDeleteResponse enqueues a new Delete response.
func (m *Interface) AddDeleteResponse(err error) {
m.enqueueResponse(response{
delete1: err,
})
}
// AddWatchResponse enqueues a new Watch response.
func (m *Interface) AddWatchResponse(w watch.Interface, err error) {
m.enqueueResponse(response{
watch1: w,
watch2: err,
})
}
// AddPanicResponse adds a response to the queue that causes a panic.
func (m *Interface) AddPanicResponse(s string) {
m.enqueueResponse(response{
panic: s,
})
}
// String returns the current inncoming request log as string.
func (m *Interface) String() string {
m.logLock.Lock()
defer m.logLock.Unlock()
return m.log
}
// Close the mock. If there are any pending events or listeners, there will be panic.
func (m *Interface) Close() {
if len(m.responses) != 0 {
panic(fmt.Sprintf("There still are pending undelivered pending responses\n%v", m.log))
}
if atomic.LoadInt32(&m.callers) != 0 {
panic(fmt.Sprintf("There still are callers waiting for responses\n%v", m.log))
}
close(m.shutdown)
}
func (m *Interface) appendToLog(format string, args ...interface{}) {
m.logLock.Lock()
defer m.logLock.Unlock()
str := fmt.Sprintf(format, args...)
m.log += str
}