forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
312 lines (248 loc) · 10.7 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
package testclient
import (
"fmt"
"sync"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apimachinery/registered"
ktestclient "k8s.io/kubernetes/pkg/client/unversioned/testclient"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
_ "github.com/openshift/origin/pkg/api/install"
"github.com/openshift/origin/pkg/client"
)
// Fake implements Interface. Meant to be embedded into a struct to get a default
// implementation. This makes faking out just the method you want to test easier.
type Fake struct {
sync.RWMutex
actions []ktestclient.Action // these may be castable to other types, but "Action" is the minimum
// ReactionChain is the list of reactors that will be attempted for every request in the order they are tried
ReactionChain []ktestclient.Reactor
// WatchReactionChain is the list of watch reactors that will be attempted for every request in the order they are tried
WatchReactionChain []ktestclient.WatchReactor
}
// NewSimpleFake returns a client that will respond with the provided objects
func NewSimpleFake(objects ...runtime.Object) *Fake {
o := ktestclient.NewObjects(kapi.Scheme, kapi.Codecs.UniversalDecoder())
for _, obj := range objects {
if err := o.Add(obj); err != nil {
panic(err)
}
}
fakeClient := &Fake{}
fakeClient.AddReactor("*", "*", ktestclient.ObjectReaction(o, registered.RESTMapper()))
return fakeClient
}
// AddReactor appends a reactor to the end of the chain
func (c *Fake) AddReactor(verb, resource string, reaction ktestclient.ReactionFunc) {
c.ReactionChain = append(c.ReactionChain, &ktestclient.SimpleReactor{Verb: verb, Resource: resource, Reaction: reaction})
}
// PrependReactor adds a reactor to the beginning of the chain
func (c *Fake) PrependReactor(verb, resource string, reaction ktestclient.ReactionFunc) {
c.ReactionChain = append([]ktestclient.Reactor{&ktestclient.SimpleReactor{Verb: verb, Resource: resource, Reaction: reaction}}, c.ReactionChain...)
}
// AddWatchReactor appends a reactor to the end of the chain
func (c *Fake) AddWatchReactor(resource string, reaction ktestclient.WatchReactionFunc) {
c.WatchReactionChain = append(c.WatchReactionChain, &ktestclient.SimpleWatchReactor{Resource: resource, Reaction: reaction})
}
// Invokes records the provided Action and then invokes the ReactFn (if provided).
// defaultReturnObj is expected to be of the same type a normal call would return.
func (c *Fake) Invokes(action ktestclient.Action, defaultReturnObj runtime.Object) (runtime.Object, error) {
c.Lock()
defer c.Unlock()
c.actions = append(c.actions, action)
for _, reactor := range c.ReactionChain {
if !reactor.Handles(action) {
continue
}
handled, ret, err := reactor.React(action)
if !handled {
continue
}
return ret, err
}
return defaultReturnObj, nil
}
// InvokesWatch records the provided Action and then invokes the ReactFn (if provided).
func (c *Fake) InvokesWatch(action ktestclient.Action) (watch.Interface, error) {
c.Lock()
defer c.Unlock()
c.actions = append(c.actions, action)
for _, reactor := range c.WatchReactionChain {
if !reactor.Handles(action) {
continue
}
handled, ret, err := reactor.React(action)
if !handled {
continue
}
return ret, err
}
return nil, fmt.Errorf("unhandled watch: %#v", action)
}
// ClearActions clears the history of actions called on the fake client
func (c *Fake) ClearActions() {
c.Lock()
c.Unlock()
c.actions = make([]ktestclient.Action, 0)
}
// Actions returns a chronologically ordered slice fake actions called on the fake client
func (c *Fake) Actions() []ktestclient.Action {
c.RLock()
defer c.RUnlock()
fa := make([]ktestclient.Action, len(c.actions))
copy(fa, c.actions)
return fa
}
var _ client.Interface = &Fake{}
// Builds provides a fake REST client for Builds
func (c *Fake) Builds(namespace string) client.BuildInterface {
return &FakeBuilds{Fake: c, Namespace: namespace}
}
// BuildConfigs provides a fake REST client for BuildConfigs
func (c *Fake) BuildConfigs(namespace string) client.BuildConfigInterface {
return &FakeBuildConfigs{Fake: c, Namespace: namespace}
}
// BuildLogs provides a fake REST client for BuildLogs
func (c *Fake) BuildLogs(namespace string) client.BuildLogsInterface {
return &FakeBuildLogs{Fake: c, Namespace: namespace}
}
// Images provides a fake REST client for Images
func (c *Fake) Images() client.ImageInterface {
return &FakeImages{Fake: c}
}
// ImageStreams provides a fake REST client for ImageStreams
func (c *Fake) ImageStreamSecrets(namespace string) client.ImageStreamSecretInterface {
return &FakeImageStreamSecrets{Fake: c, Namespace: namespace}
}
// ImageStreams provides a fake REST client for ImageStreams
func (c *Fake) ImageStreams(namespace string) client.ImageStreamInterface {
return &FakeImageStreams{Fake: c, Namespace: namespace}
}
// ImageStreamMappings provides a fake REST client for ImageStreamMappings
func (c *Fake) ImageStreamMappings(namespace string) client.ImageStreamMappingInterface {
return &FakeImageStreamMappings{Fake: c, Namespace: namespace}
}
// ImageStreamTags provides a fake REST client for ImageStreamTags
func (c *Fake) ImageStreamTags(namespace string) client.ImageStreamTagInterface {
return &FakeImageStreamTags{Fake: c, Namespace: namespace}
}
// ImageStreamImages provides a fake REST client for ImageStreamImages
func (c *Fake) ImageStreamImages(namespace string) client.ImageStreamImageInterface {
return &FakeImageStreamImages{Fake: c, Namespace: namespace}
}
// DeploymentConfigs provides a fake REST client for DeploymentConfigs
func (c *Fake) DeploymentConfigs(namespace string) client.DeploymentConfigInterface {
return &FakeDeploymentConfigs{Fake: c, Namespace: namespace}
}
// DeploymentLogs provides a fake REST client for DeploymentLogs
func (c *Fake) DeploymentLogs(namespace string) client.DeploymentLogInterface {
return &FakeDeploymentLogs{Fake: c, Namespace: namespace}
}
// Routes provides a fake REST client for Routes
func (c *Fake) Routes(namespace string) client.RouteInterface {
return &FakeRoutes{Fake: c, Namespace: namespace}
}
// HostSubnets provides a fake REST client for HostSubnets
func (c *Fake) HostSubnets() client.HostSubnetInterface {
return &FakeHostSubnet{Fake: c}
}
// NetNamespaces provides a fake REST client for NetNamespaces
func (c *Fake) NetNamespaces() client.NetNamespaceInterface {
return &FakeNetNamespace{Fake: c}
}
// ClusterNetwork provides a fake REST client for ClusterNetwork
func (c *Fake) ClusterNetwork() client.ClusterNetworkInterface {
return &FakeClusterNetwork{Fake: c}
}
// Templates provides a fake REST client for Templates
func (c *Fake) Templates(namespace string) client.TemplateInterface {
return &FakeTemplates{Fake: c, Namespace: namespace}
}
// TemplateConfigs provides a fake REST client for TemplateConfigs
func (c *Fake) TemplateConfigs(namespace string) client.TemplateConfigInterface {
return &FakeTemplateConfigs{Fake: c, Namespace: namespace}
}
// Identities provides a fake REST client for Identities
func (c *Fake) Identities() client.IdentityInterface {
return &FakeIdentities{Fake: c}
}
// Users provides a fake REST client for Users
func (c *Fake) Users() client.UserInterface {
return &FakeUsers{Fake: c}
}
// UserIdentityMappings provides a fake REST client for UserIdentityMappings
func (c *Fake) UserIdentityMappings() client.UserIdentityMappingInterface {
return &FakeUserIdentityMappings{Fake: c}
}
// Groups provides a fake REST client for Groups
func (c *Fake) Groups() client.GroupInterface {
return &FakeGroups{Fake: c}
}
// Projects provides a fake REST client for Projects
func (c *Fake) Projects() client.ProjectInterface {
return &FakeProjects{Fake: c}
}
// ProjectRequests provides a fake REST client for ProjectRequests
func (c *Fake) ProjectRequests() client.ProjectRequestInterface {
return &FakeProjectRequests{Fake: c}
}
// Policies provides a fake REST client for Policies
func (c *Fake) Policies(namespace string) client.PolicyInterface {
return &FakePolicies{Fake: c, Namespace: namespace}
}
// Roles provides a fake REST client for Roles
func (c *Fake) Roles(namespace string) client.RoleInterface {
return &FakeRoles{Fake: c, Namespace: namespace}
}
// RoleBindings provides a fake REST client for RoleBindings
func (c *Fake) RoleBindings(namespace string) client.RoleBindingInterface {
return &FakeRoleBindings{Fake: c, Namespace: namespace}
}
// PolicyBindings provides a fake REST client for PolicyBindings
func (c *Fake) PolicyBindings(namespace string) client.PolicyBindingInterface {
return &FakePolicyBindings{Fake: c, Namespace: namespace}
}
// LocalResourceAccessReviews provides a fake REST client for ResourceAccessReviews
func (c *Fake) LocalResourceAccessReviews(namespace string) client.LocalResourceAccessReviewInterface {
return &FakeLocalResourceAccessReviews{Fake: c}
}
// ResourceAccessReviews provides a fake REST client for ClusterResourceAccessReviews
func (c *Fake) ResourceAccessReviews() client.ResourceAccessReviewInterface {
return &FakeClusterResourceAccessReviews{Fake: c}
}
// ImpersonateSubjectAccessReviews provides a fake REST client for SubjectAccessReviews
func (c *Fake) ImpersonateSubjectAccessReviews(token string) client.SubjectAccessReviewInterface {
return &FakeClusterSubjectAccessReviews{Fake: c}
}
// ImpersonateSubjectAccessReviews provides a fake REST client for SubjectAccessReviews
func (c *Fake) ImpersonateLocalSubjectAccessReviews(namespace, token string) client.LocalSubjectAccessReviewInterface {
return &FakeLocalSubjectAccessReviews{Fake: c, Namespace: namespace}
}
// OAuthAccessTokens provides a fake REST client for OAuthAccessTokens
func (c *Fake) OAuthAccessTokens() client.OAuthAccessTokenInterface {
return &FakeOAuthAccessTokens{Fake: c}
}
// LocalSubjectAccessReviews provides a fake REST client for SubjectAccessReviews
func (c *Fake) LocalSubjectAccessReviews(namespace string) client.LocalSubjectAccessReviewInterface {
return &FakeLocalSubjectAccessReviews{Fake: c}
}
// SubjectAccessReviews provides a fake REST client for ClusterSubjectAccessReviews
func (c *Fake) SubjectAccessReviews() client.SubjectAccessReviewInterface {
return &FakeClusterSubjectAccessReviews{Fake: c}
}
// ClusterPolicies provides a fake REST client for ClusterPolicies
func (c *Fake) ClusterPolicies() client.ClusterPolicyInterface {
return &FakeClusterPolicies{Fake: c}
}
// ClusterPolicyBindings provides a fake REST client for ClusterPolicyBindings
func (c *Fake) ClusterPolicyBindings() client.ClusterPolicyBindingInterface {
return &FakeClusterPolicyBindings{Fake: c}
}
// ClusterRoles provides a fake REST client for ClusterRoles
func (c *Fake) ClusterRoles() client.ClusterRoleInterface {
return &FakeClusterRoles{Fake: c}
}
// ClusterRoleBindings provides a fake REST client for ClusterRoleBindings
func (c *Fake) ClusterRoleBindings() client.ClusterRoleBindingInterface {
return &FakeClusterRoleBindings{Fake: c}
}