forked from kubernetes/kubernetes
/
testclient.go
179 lines (142 loc) · 5.25 KB
/
testclient.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 testclient
import (
"sync"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/registered"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
// NewSimpleFake returns a client that will respond with the provided objects
func NewSimpleFake(objects ...runtime.Object) *Fake {
o := NewObjects(api.Scheme, api.Scheme)
for _, obj := range objects {
if err := o.Add(obj); err != nil {
panic(err)
}
}
return &Fake{ReactFn: ObjectReaction(o, latest.RESTMapper)}
}
type FakeAction struct {
Action string
Value interface{}
}
// ReactionFunc is a function that returns an object or error for a given Action
type ReactionFunc func(FakeAction) (runtime.Object, error)
// Fake implements client.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 []FakeAction
err error
Watch watch.Interface
// ReactFn is an optional function that will be invoked with the provided action
// and return a response. It can implement scenario specific behavior. The type
// of object returned must match the expected type from the caller (even if nil).
ReactFn ReactionFunc
}
// Invokes records the provided FakeAction and then invokes the ReactFn (if provided).
// obj is expected to be of the same type a normal call would return.
func (c *Fake) Invokes(action FakeAction, obj runtime.Object) (runtime.Object, error) {
c.Lock()
defer c.Unlock()
c.actions = append(c.actions, action)
if c.ReactFn != nil {
return c.ReactFn(action)
}
return obj, c.err
}
// ClearActions clears the history of actions called on the fake client
func (c *Fake) ClearActions() {
c.Lock()
c.Unlock()
c.actions = make([]FakeAction, 0)
}
// Actions returns a chronologically ordered slice fake actions called on the fake client
func (c *Fake) Actions() []FakeAction {
c.RLock()
defer c.RUnlock()
fa := make([]FakeAction, len(c.actions))
copy(fa, c.actions)
return fa
}
// SetErr sets the error to return for client calls
func (c *Fake) SetErr(err error) {
c.Lock()
defer c.Unlock()
c.err = err
}
// Err returns any a client error or nil
func (c *Fake) Err() error {
c.RLock()
c.RUnlock()
return c.err
}
func (c *Fake) LimitRanges(namespace string) client.LimitRangeInterface {
return &FakeLimitRanges{Fake: c, Namespace: namespace}
}
func (c *Fake) ResourceQuotas(namespace string) client.ResourceQuotaInterface {
return &FakeResourceQuotas{Fake: c, Namespace: namespace}
}
func (c *Fake) ReplicationControllers(namespace string) client.ReplicationControllerInterface {
return &FakeReplicationControllers{Fake: c, Namespace: namespace}
}
func (c *Fake) Nodes() client.NodeInterface {
return &FakeNodes{Fake: c}
}
func (c *Fake) Events(namespace string) client.EventInterface {
return &FakeEvents{Fake: c}
}
func (c *Fake) Endpoints(namespace string) client.EndpointsInterface {
return &FakeEndpoints{Fake: c, Namespace: namespace}
}
func (c *Fake) PersistentVolumes() client.PersistentVolumeInterface {
return &FakePersistentVolumes{Fake: c}
}
func (c *Fake) PersistentVolumeClaims(namespace string) client.PersistentVolumeClaimInterface {
return &FakePersistentVolumeClaims{Fake: c, Namespace: namespace}
}
func (c *Fake) Pods(namespace string) client.PodInterface {
return &FakePods{Fake: c, Namespace: namespace}
}
func (c *Fake) PodTemplates(namespace string) client.PodTemplateInterface {
return &FakePodTemplates{Fake: c, Namespace: namespace}
}
func (c *Fake) Services(namespace string) client.ServiceInterface {
return &FakeServices{Fake: c, Namespace: namespace}
}
func (c *Fake) ServiceAccounts(namespace string) client.ServiceAccountsInterface {
return &FakeServiceAccounts{Fake: c, Namespace: namespace}
}
func (c *Fake) Secrets(namespace string) client.SecretsInterface {
return &FakeSecrets{Fake: c, Namespace: namespace}
}
func (c *Fake) Namespaces() client.NamespaceInterface {
return &FakeNamespaces{Fake: c}
}
func (c *Fake) ServerVersion() (*version.Info, error) {
c.Invokes(FakeAction{Action: "get-version", Value: nil}, nil)
versionInfo := version.Get()
return &versionInfo, nil
}
func (c *Fake) ServerAPIVersions() (*api.APIVersions, error) {
c.Invokes(FakeAction{Action: "get-apiversions", Value: nil}, nil)
return &api.APIVersions{Versions: registered.RegisteredVersions}, nil
}
func (c *Fake) ComponentStatuses() client.ComponentStatusInterface {
return &FakeComponentStatuses{Fake: c}
}