make testclient threadsafe by guarding internal state with accessors
This commit is contained in:
@@ -51,8 +51,8 @@ func (c *FakeEndpoints) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeEndpoints) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-endpoints", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-endpoints", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
||||
func (c *FakeEndpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
|
||||
|
@@ -56,8 +56,8 @@ func (c *FakeEvents) Get(id string) (*api.Event, error) {
|
||||
|
||||
// Watch starts watching for events matching the given selectors.
|
||||
func (c *FakeEvents) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-events", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-events", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
||||
// Search returns a list of events matching the specified object.
|
||||
@@ -72,6 +72,6 @@ func (c *FakeEvents) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeEvents) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-field-selector"})
|
||||
c.Fake.Invokes(FakeAction{Action: "get-field-selector"}, nil)
|
||||
return fields.Everything()
|
||||
}
|
||||
|
@@ -56,6 +56,6 @@ func (c *FakeLimitRanges) Update(limitRange *api.LimitRange) (*api.LimitRange, e
|
||||
}
|
||||
|
||||
func (c *FakeLimitRanges) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-limitRange", Value: resourceVersion})
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-limitRange", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, nil
|
||||
}
|
||||
|
@@ -45,8 +45,8 @@ func (c *FakeNamespaces) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeNamespaces) Create(namespace *api.Namespace) (*api.Namespace, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-namespace"})
|
||||
return &api.Namespace{}, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "create-namespace"}, nil)
|
||||
return &api.Namespace{}, c.Fake.Err()
|
||||
}
|
||||
|
||||
func (c *FakeNamespaces) Update(namespace *api.Namespace) (*api.Namespace, error) {
|
||||
@@ -55,7 +55,7 @@ func (c *FakeNamespaces) Update(namespace *api.Namespace) (*api.Namespace, error
|
||||
}
|
||||
|
||||
func (c *FakeNamespaces) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-namespaces", Value: resourceVersion})
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-namespaces", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, nil
|
||||
}
|
||||
|
||||
|
@@ -60,6 +60,6 @@ func (c *FakeNodes) UpdateStatus(minion *api.Node) (*api.Node, error) {
|
||||
}
|
||||
|
||||
func (c *FakeNodes) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-nodes", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-nodes", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -59,6 +59,6 @@ func (c *FakePersistentVolumeClaims) UpdateStatus(claim *api.PersistentVolumeCla
|
||||
}
|
||||
|
||||
func (c *FakePersistentVolumeClaims) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-persistentVolumeClaims", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-persistentVolumeClaims", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -59,6 +59,6 @@ func (c *FakePersistentVolumes) UpdateStatus(pv *api.PersistentVolume) (*api.Per
|
||||
}
|
||||
|
||||
func (c *FakePersistentVolumes) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-persistentVolumes", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-persistentVolumes", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -56,6 +56,6 @@ func (c *FakePodTemplates) Update(pod *api.PodTemplate) (*api.PodTemplate, error
|
||||
}
|
||||
|
||||
func (c *FakePodTemplates) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-podTemplates", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-podTemplates", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -56,12 +56,12 @@ func (c *FakePods) Update(pod *api.Pod) (*api.Pod, error) {
|
||||
}
|
||||
|
||||
func (c *FakePods) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-pods", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-pods", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
||||
func (c *FakePods) Bind(bind *api.Binding) error {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "bind-pod", Value: bind.Name})
|
||||
c.Fake.Invokes(FakeAction{Action: "bind-pod", Value: bind.Name}, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -65,6 +65,6 @@ func (c *FakeReplicationControllers) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeReplicationControllers) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: WatchControllerAction, Value: resourceVersion})
|
||||
c.Fake.Invokes(FakeAction{Action: WatchControllerAction, Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, nil
|
||||
}
|
||||
|
@@ -61,6 +61,6 @@ func (c *FakeResourceQuotas) UpdateStatus(resourceQuota *api.ResourceQuota) (*ap
|
||||
}
|
||||
|
||||
func (c *FakeResourceQuotas) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-resourceQuota", Value: resourceVersion})
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-resourceQuota", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, nil
|
||||
}
|
||||
|
@@ -56,6 +56,6 @@ func (c *FakeSecrets) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeSecrets) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-secrets", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-secrets", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -56,6 +56,6 @@ func (c *FakeServiceAccounts) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeServiceAccounts) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-serviceAccounts", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-serviceAccounts", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -56,6 +56,6 @@ func (c *FakeServices) Delete(name string) error {
|
||||
}
|
||||
|
||||
func (c *FakeServices) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-services", Value: resourceVersion})
|
||||
return c.Fake.Watch, c.Fake.Err
|
||||
c.Fake.Invokes(FakeAction{Action: "watch-services", Value: resourceVersion}, nil)
|
||||
return c.Fake.Watch, c.Fake.Err()
|
||||
}
|
||||
|
@@ -17,6 +17,8 @@ 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"
|
||||
@@ -48,10 +50,11 @@ 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 {
|
||||
Actions []FakeAction
|
||||
Watch watch.Interface
|
||||
Err error
|
||||
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).
|
||||
@@ -61,11 +64,47 @@ type Fake struct {
|
||||
// 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.Actions = append(c.Actions, action)
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
c.actions = append(c.actions, action)
|
||||
if c.ReactFn != nil {
|
||||
return c.ReactFn(action)
|
||||
}
|
||||
return obj, c.Err
|
||||
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 {
|
||||
@@ -125,13 +164,13 @@ func (c *Fake) Namespaces() client.NamespaceInterface {
|
||||
}
|
||||
|
||||
func (c *Fake) ServerVersion() (*version.Info, error) {
|
||||
c.Actions = append(c.Actions, FakeAction{Action: "get-version", Value: nil})
|
||||
c.Invokes(FakeAction{Action: "get-version", Value: nil}, nil)
|
||||
versionInfo := version.Get()
|
||||
return &versionInfo, nil
|
||||
}
|
||||
|
||||
func (c *Fake) ServerAPIVersions() (*api.APIVersions, error) {
|
||||
c.Actions = append(c.Actions, FakeAction{Action: "get-apiversions", Value: nil})
|
||||
c.Invokes(FakeAction{Action: "get-apiversions", Value: nil}, nil)
|
||||
return &api.APIVersions{Versions: registered.RegisteredVersions}, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user