make testclient more precise

This commit is contained in:
deads2k
2015-08-03 09:21:11 -04:00
parent 4271f28548
commit 182885e897
33 changed files with 1004 additions and 401 deletions

View File

@@ -39,19 +39,14 @@ func NewSimpleFake(objects ...runtime.Object) *Fake {
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)
type ReactionFunc func(Action) (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
actions []Action // these may be castable to other types, but "Action" is the minimum
err error
Watch watch.Interface
@@ -61,9 +56,9 @@ type Fake struct {
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) {
// 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) {
c.Lock()
defer c.Unlock()
@@ -71,7 +66,7 @@ func (c *Fake) Invokes(action FakeAction, obj runtime.Object) (runtime.Object, e
if c.ReactFn != nil {
return c.ReactFn(action)
}
return obj, c.err
return defaultReturnObj, c.err
}
// ClearActions clears the history of actions called on the fake client
@@ -79,14 +74,14 @@ func (c *Fake) ClearActions() {
c.Lock()
c.Unlock()
c.actions = make([]FakeAction, 0)
c.actions = make([]Action, 0)
}
// Actions returns a chronologically ordered slice fake actions called on the fake client
func (c *Fake) Actions() []FakeAction {
func (c *Fake) Actions() []Action {
c.RLock()
defer c.RUnlock()
fa := make([]FakeAction, len(c.actions))
fa := make([]Action, len(c.actions))
copy(fa, c.actions)
return fa
}
@@ -164,13 +159,21 @@ func (c *Fake) Namespaces() client.NamespaceInterface {
}
func (c *Fake) ServerVersion() (*version.Info, error) {
c.Invokes(FakeAction{Action: "get-version", Value: nil}, nil)
action := ActionImpl{}
action.Verb = "get"
action.Resource = "version"
c.Invokes(action, nil)
versionInfo := version.Get()
return &versionInfo, nil
}
func (c *Fake) ServerAPIVersions() (*api.APIVersions, error) {
c.Invokes(FakeAction{Action: "get-apiversions", Value: nil}, nil)
action := ActionImpl{}
action.Verb = "get"
action.Resource = "apiversions"
c.Invokes(action, nil)
return &api.APIVersions{Versions: registered.RegisteredVersions}, nil
}