2015-04-06 23:27:53 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-04-06 23:27:53 +00:00
|
|
|
|
|
|
|
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 (
|
2015-07-06 21:37:46 +00:00
|
|
|
"sync"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/latest"
|
|
|
|
"k8s.io/kubernetes/pkg/api/registered"
|
|
|
|
"k8s.io/kubernetes/pkg/client"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/version"
|
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
2015-04-06 23:27:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewSimpleFake returns a client that will respond with the provided objects
|
|
|
|
func NewSimpleFake(objects ...runtime.Object) *Fake {
|
2015-04-29 03:15:16 +00:00
|
|
|
o := NewObjects(api.Scheme, api.Scheme)
|
2015-04-06 23:27:53 +00:00
|
|
|
for _, obj := range objects {
|
|
|
|
if err := o.Add(obj); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Fake{ReactFn: ObjectReaction(o, latest.RESTMapper)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReactionFunc is a function that returns an object or error for a given Action
|
2015-08-03 13:21:11 +00:00
|
|
|
type ReactionFunc func(Action) (runtime.Object, error)
|
2015-04-06 23:27:53 +00:00
|
|
|
|
|
|
|
// 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 {
|
2015-07-06 21:37:46 +00:00
|
|
|
sync.RWMutex
|
2015-08-03 13:21:11 +00:00
|
|
|
actions []Action // these may be castable to other types, but "Action" is the minimum
|
2015-07-06 21:37:46 +00:00
|
|
|
err error
|
2015-04-06 23:27:53 +00:00
|
|
|
|
2015-07-06 21:37:46 +00:00
|
|
|
Watch watch.Interface
|
2015-04-06 23:27:53 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:21:11 +00:00
|
|
|
// 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 Action, defaultReturnObj runtime.Object) (runtime.Object, error) {
|
2015-07-06 21:37:46 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.actions = append(c.actions, action)
|
2015-04-06 23:27:53 +00:00
|
|
|
if c.ReactFn != nil {
|
|
|
|
return c.ReactFn(action)
|
|
|
|
}
|
2015-08-03 13:21:11 +00:00
|
|
|
return defaultReturnObj, c.err
|
2015-07-06 21:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClearActions clears the history of actions called on the fake client
|
|
|
|
func (c *Fake) ClearActions() {
|
|
|
|
c.Lock()
|
|
|
|
c.Unlock()
|
|
|
|
|
2015-08-03 13:21:11 +00:00
|
|
|
c.actions = make([]Action, 0)
|
2015-07-06 21:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actions returns a chronologically ordered slice fake actions called on the fake client
|
2015-08-03 13:21:11 +00:00
|
|
|
func (c *Fake) Actions() []Action {
|
2015-07-06 21:37:46 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2015-08-03 13:21:11 +00:00
|
|
|
fa := make([]Action, len(c.actions))
|
2015-07-06 21:37:46 +00:00
|
|
|
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
|
2015-04-06 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-08-06 19:22:52 +00:00
|
|
|
return &FakeEvents{Fake: c, Namespace: namespace}
|
2015-04-06 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
2015-05-14 13:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Fake) PodTemplates(namespace string) client.PodTemplateInterface {
|
|
|
|
return &FakePodTemplates{Fake: c, Namespace: namespace}
|
2015-04-06 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Fake) Services(namespace string) client.ServiceInterface {
|
|
|
|
return &FakeServices{Fake: c, Namespace: namespace}
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:53:28 +00:00
|
|
|
func (c *Fake) ServiceAccounts(namespace string) client.ServiceAccountsInterface {
|
|
|
|
return &FakeServiceAccounts{Fake: c, Namespace: namespace}
|
|
|
|
}
|
|
|
|
|
2015-04-06 23:27:53 +00:00
|
|
|
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) {
|
2015-08-03 13:21:11 +00:00
|
|
|
action := ActionImpl{}
|
|
|
|
action.Verb = "get"
|
|
|
|
action.Resource = "version"
|
|
|
|
|
|
|
|
c.Invokes(action, nil)
|
2015-04-06 23:27:53 +00:00
|
|
|
versionInfo := version.Get()
|
|
|
|
return &versionInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Fake) ServerAPIVersions() (*api.APIVersions, error) {
|
2015-08-03 13:21:11 +00:00
|
|
|
action := ActionImpl{}
|
|
|
|
action.Verb = "get"
|
|
|
|
action.Resource = "apiversions"
|
|
|
|
|
|
|
|
c.Invokes(action, nil)
|
2015-07-07 23:52:38 +00:00
|
|
|
return &api.APIVersions{Versions: registered.RegisteredVersions}, nil
|
2015-04-06 23:27:53 +00:00
|
|
|
}
|
2015-04-15 19:23:02 +00:00
|
|
|
|
|
|
|
func (c *Fake) ComponentStatuses() client.ComponentStatusInterface {
|
|
|
|
return &FakeComponentStatuses{Fake: c}
|
|
|
|
}
|