Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 182885e

Browse files
committed
make testclient more precise
1 parent 4271f28 commit 182885e

33 files changed

+1000
-397
lines changed

pkg/client/testclient/actions.go

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testclient
18+
19+
import (
20+
"strings"
21+
22+
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
23+
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
24+
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
25+
)
26+
27+
func NewRootGetAction(resource, name string) GetActionImpl {
28+
action := GetActionImpl{}
29+
action.Verb = "get"
30+
action.Resource = resource
31+
action.Name = name
32+
33+
return action
34+
}
35+
36+
func NewGetAction(resource, namespace, name string) GetActionImpl {
37+
action := GetActionImpl{}
38+
action.Verb = "get"
39+
action.Resource = resource
40+
action.Namespace = namespace
41+
action.Name = name
42+
43+
return action
44+
}
45+
46+
func NewRootListAction(resource string, label labels.Selector, field fields.Selector) ListActionImpl {
47+
action := ListActionImpl{}
48+
action.Verb = "list"
49+
action.Resource = resource
50+
action.ListRestrictions = ListRestrictions{label, field}
51+
52+
return action
53+
}
54+
55+
func NewListAction(resource, namespace string, label labels.Selector, field fields.Selector) ListActionImpl {
56+
action := ListActionImpl{}
57+
action.Verb = "list"
58+
action.Resource = resource
59+
action.Namespace = namespace
60+
action.ListRestrictions = ListRestrictions{label, field}
61+
62+
return action
63+
}
64+
65+
func NewRootCreateAction(resource string, object runtime.Object) CreateActionImpl {
66+
action := CreateActionImpl{}
67+
action.Verb = "create"
68+
action.Resource = resource
69+
action.Object = object
70+
71+
return action
72+
}
73+
74+
func NewCreateAction(resource, namespace string, object runtime.Object) CreateActionImpl {
75+
action := CreateActionImpl{}
76+
action.Verb = "create"
77+
action.Resource = resource
78+
action.Namespace = namespace
79+
action.Object = object
80+
81+
return action
82+
}
83+
84+
func NewRootUpdateAction(resource string, object runtime.Object) UpdateActionImpl {
85+
action := UpdateActionImpl{}
86+
action.Verb = "update"
87+
action.Resource = resource
88+
action.Object = object
89+
90+
return action
91+
}
92+
93+
func NewUpdateAction(resource, namespace string, object runtime.Object) UpdateActionImpl {
94+
action := UpdateActionImpl{}
95+
action.Verb = "update"
96+
action.Resource = resource
97+
action.Namespace = namespace
98+
action.Object = object
99+
100+
return action
101+
}
102+
103+
func NewRootDeleteAction(resource, name string) DeleteActionImpl {
104+
action := DeleteActionImpl{}
105+
action.Verb = "delete"
106+
action.Resource = resource
107+
action.Name = name
108+
109+
return action
110+
}
111+
112+
func NewDeleteAction(resource, namespace, name string) DeleteActionImpl {
113+
action := DeleteActionImpl{}
114+
action.Verb = "delete"
115+
action.Resource = resource
116+
action.Namespace = namespace
117+
action.Name = name
118+
119+
return action
120+
}
121+
122+
func NewRootWatchAction(resource string, label labels.Selector, field fields.Selector, resourceVersion string) WatchActionImpl {
123+
action := WatchActionImpl{}
124+
action.Verb = "watch"
125+
action.Resource = resource
126+
action.WatchRestrictions = WatchRestrictions{label, field, resourceVersion}
127+
128+
return action
129+
}
130+
131+
func NewWatchAction(resource, namespace string, label labels.Selector, field fields.Selector, resourceVersion string) WatchActionImpl {
132+
action := WatchActionImpl{}
133+
action.Verb = "watch"
134+
action.Resource = resource
135+
action.Namespace = namespace
136+
action.WatchRestrictions = WatchRestrictions{label, field, resourceVersion}
137+
138+
return action
139+
}
140+
141+
type ListRestrictions struct {
142+
Labels labels.Selector
143+
Fields fields.Selector
144+
}
145+
type WatchRestrictions struct {
146+
Labels labels.Selector
147+
Fields fields.Selector
148+
ResourceVersion string
149+
}
150+
151+
type Action interface {
152+
GetNamespace() string
153+
GetVerb() string
154+
GetResource() string
155+
GetSubresource() string
156+
Matches(verb, resource string) bool
157+
}
158+
159+
type GenericAction interface {
160+
Action
161+
GetValue() interface{}
162+
}
163+
164+
type GetAction interface {
165+
Action
166+
GetName() string
167+
}
168+
169+
type ListAction interface {
170+
Action
171+
GetListRestrictions() ListRestrictions
172+
}
173+
174+
type CreateAction interface {
175+
Action
176+
GetObject() runtime.Object
177+
}
178+
179+
type UpdateAction interface {
180+
Action
181+
GetObject() runtime.Object
182+
}
183+
184+
type DeleteAction interface {
185+
Action
186+
GetName() string
187+
}
188+
189+
type WatchAction interface {
190+
Action
191+
GetWatchRestrictions() WatchRestrictions
192+
}
193+
194+
type ActionImpl struct {
195+
Namespace string
196+
Verb string
197+
Resource string
198+
Subresource string
199+
}
200+
201+
func (a ActionImpl) GetNamespace() string {
202+
return a.Namespace
203+
}
204+
func (a ActionImpl) GetVerb() string {
205+
return a.Verb
206+
}
207+
func (a ActionImpl) GetResource() string {
208+
return a.Resource
209+
}
210+
func (a ActionImpl) GetSubresource() string {
211+
return a.Subresource
212+
}
213+
func (a ActionImpl) Matches(verb, resource string) bool {
214+
return strings.ToLower(verb) == strings.ToLower(a.Verb) &&
215+
strings.ToLower(resource) == strings.ToLower(a.Resource)
216+
}
217+
218+
type GenericActionImpl struct {
219+
ActionImpl
220+
Value interface{}
221+
}
222+
223+
func (a GenericActionImpl) GetValue() interface{} {
224+
return a.Value
225+
}
226+
227+
type GetActionImpl struct {
228+
ActionImpl
229+
Name string
230+
}
231+
232+
func (a GetActionImpl) GetName() string {
233+
return a.Name
234+
}
235+
236+
type ListActionImpl struct {
237+
ActionImpl
238+
ListRestrictions ListRestrictions
239+
}
240+
241+
func (a ListActionImpl) GetListRestrictions() ListRestrictions {
242+
return a.ListRestrictions
243+
}
244+
245+
type CreateActionImpl struct {
246+
ActionImpl
247+
Object runtime.Object
248+
}
249+
250+
func (a CreateActionImpl) GetObject() runtime.Object {
251+
return a.Object
252+
}
253+
254+
type UpdateActionImpl struct {
255+
ActionImpl
256+
Object runtime.Object
257+
}
258+
259+
func (a UpdateActionImpl) GetObject() runtime.Object {
260+
return a.Object
261+
}
262+
263+
type DeleteActionImpl struct {
264+
ActionImpl
265+
Name string
266+
}
267+
268+
func (a DeleteActionImpl) GetName() string {
269+
return a.Name
270+
}
271+
272+
type WatchActionImpl struct {
273+
ActionImpl
274+
WatchRestrictions WatchRestrictions
275+
}
276+
277+
func (a WatchActionImpl) GetWatchRestrictions() WatchRestrictions {
278+
return a.WatchRestrictions
279+
}

pkg/client/testclient/fake_componentstatuses.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ type FakeComponentStatuses struct {
2727
Fake *Fake
2828
}
2929

30-
func (c *FakeComponentStatuses) List(label labels.Selector, field fields.Selector) (result *api.ComponentStatusList, err error) {
31-
obj, err := c.Fake.Invokes(FakeAction{Action: "list-componentstatuses"}, &api.ComponentStatusList{})
32-
return obj.(*api.ComponentStatusList), err
33-
}
34-
3530
func (c *FakeComponentStatuses) Get(name string) (*api.ComponentStatus, error) {
36-
obj, err := c.Fake.Invokes(FakeAction{Action: "get-componentstatus", Value: name}, &api.ComponentStatus{})
37-
// c.Actions = append(c.Actions, FakeAction{Action: "get-componentstatuses", Value: nil})
38-
// testStatus := &api.ComponentStatus{
39-
// Name: "test",
40-
// Health: "ok",
41-
// HealthCode: int(probe.Success),
42-
// Message: "ok",
43-
// Error: "",
44-
// }
45-
// return &api.ComponentStatusList{Items: []api.ComponentStatus{*testStatus}}, nil
31+
obj, err := c.Fake.Invokes(NewRootGetAction("componentstatuses", name), &api.ComponentStatus{})
32+
if obj == nil {
33+
return nil, err
34+
}
35+
4636
return obj.(*api.ComponentStatus), err
4737
}
38+
39+
func (c *FakeComponentStatuses) List(label labels.Selector, field fields.Selector) (result *api.ComponentStatusList, err error) {
40+
obj, err := c.Fake.Invokes(NewRootListAction("componentstatuses", label, field), &api.ComponentStatusList{})
41+
if obj == nil {
42+
return nil, err
43+
}
44+
45+
return obj.(*api.ComponentStatusList), err
46+
}

pkg/client/testclient/fake_endpoints.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,48 @@ type FakeEndpoints struct {
3030
Namespace string
3131
}
3232

33-
func (c *FakeEndpoints) Create(endpoints *api.Endpoints) (*api.Endpoints, error) {
34-
obj, err := c.Fake.Invokes(FakeAction{Action: "create-endpoints"}, &api.Endpoints{})
33+
func (c *FakeEndpoints) Get(name string) (*api.Endpoints, error) {
34+
obj, err := c.Fake.Invokes(NewGetAction("endpoints", c.Namespace, name), &api.Endpoints{})
35+
if obj == nil {
36+
return nil, err
37+
}
38+
3539
return obj.(*api.Endpoints), err
3640
}
3741

38-
func (c *FakeEndpoints) List(selector labels.Selector) (*api.EndpointsList, error) {
39-
obj, err := c.Fake.Invokes(FakeAction{Action: "list-endpoints"}, &api.EndpointsList{})
42+
func (c *FakeEndpoints) List(label labels.Selector) (*api.EndpointsList, error) {
43+
obj, err := c.Fake.Invokes(NewListAction("endpoints", c.Namespace, label, nil), &api.EndpointsList{})
44+
if obj == nil {
45+
return nil, err
46+
}
47+
4048
return obj.(*api.EndpointsList), err
4149
}
4250

43-
func (c *FakeEndpoints) Get(name string) (*api.Endpoints, error) {
44-
obj, err := c.Fake.Invokes(FakeAction{Action: "get-endpoints", Value: name}, &api.Endpoints{})
51+
func (c *FakeEndpoints) Create(endpoints *api.Endpoints) (*api.Endpoints, error) {
52+
obj, err := c.Fake.Invokes(NewCreateAction("endpoints", c.Namespace, endpoints), endpoints)
53+
if obj == nil {
54+
return nil, err
55+
}
56+
57+
return obj.(*api.Endpoints), err
58+
}
59+
60+
func (c *FakeEndpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
61+
obj, err := c.Fake.Invokes(NewUpdateAction("endpoints", c.Namespace, endpoints), endpoints)
62+
if obj == nil {
63+
return nil, err
64+
}
65+
4566
return obj.(*api.Endpoints), err
4667
}
4768

4869
func (c *FakeEndpoints) Delete(name string) error {
49-
_, err := c.Fake.Invokes(FakeAction{Action: "delete-endpoints", Value: name}, &api.Endpoints{})
70+
_, err := c.Fake.Invokes(NewDeleteAction("endpoints", c.Namespace, name), &api.Endpoints{})
5071
return err
5172
}
5273

5374
func (c *FakeEndpoints) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
54-
c.Fake.Invokes(FakeAction{Action: "watch-endpoints", Value: resourceVersion}, nil)
75+
c.Fake.Invokes(NewWatchAction("endpoints", c.Namespace, label, field, resourceVersion), nil)
5576
return c.Fake.Watch, c.Fake.Err()
5677
}
57-
58-
func (c *FakeEndpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
59-
obj, err := c.Fake.Invokes(FakeAction{Action: "update-endpoints", Value: endpoints.Name}, &api.Endpoints{})
60-
return obj.(*api.Endpoints), err
61-
}

0 commit comments

Comments
 (0)