Create a new testclient package that can be backed by disk files

Standardize how our fakes are used so that a test case can use a
simpler mechanism for providing large, complex data sets, as well
as represent queries over time.
This commit is contained in:
Clayton Coleman
2015-04-06 19:27:53 -04:00
parent 402bf60366
commit 51db3bd654
34 changed files with 872 additions and 588 deletions

View File

@@ -20,103 +20,10 @@ import (
"net/http"
"net/url"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
type FakeAction struct {
Action string
Value interface{}
}
// Fake implements 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
PodsList api.PodList
CtrlList api.ReplicationControllerList
Ctrl api.ReplicationController
ServiceList api.ServiceList
EndpointsList api.EndpointsList
MinionsList api.NodeList
EventsList api.EventList
LimitRangesList api.LimitRangeList
ResourceQuotaStatus api.ResourceQuota
ResourceQuotasList api.ResourceQuotaList
NamespacesList api.NamespaceList
Namespace api.Namespace
SecretList api.SecretList
Secret api.Secret
Err error
Watch watch.Interface
PersistentVolume api.PersistentVolume
PersistentVolumesList api.PersistentVolumeList
PersistentVolumeClaim api.PersistentVolumeClaim
PersistentVolumeClaimList api.PersistentVolumeClaimList
}
func (c *Fake) LimitRanges(namespace string) LimitRangeInterface {
return &FakeLimitRanges{Fake: c, Namespace: namespace}
}
func (c *Fake) ResourceQuotas(namespace string) ResourceQuotaInterface {
return &FakeResourceQuotas{Fake: c, Namespace: namespace}
}
func (c *Fake) ReplicationControllers(namespace string) ReplicationControllerInterface {
return &FakeReplicationControllers{Fake: c, Namespace: namespace}
}
func (c *Fake) Nodes() NodeInterface {
return &FakeNodes{Fake: c}
}
func (c *Fake) Events(namespace string) EventInterface {
return &FakeEvents{Fake: c}
}
func (c *Fake) Endpoints(namespace string) EndpointsInterface {
return &FakeEndpoints{Fake: c, Namespace: namespace}
}
func (c *Fake) Pods(namespace string) PodInterface {
return &FakePods{Fake: c, Namespace: namespace}
}
func (c *Fake) PersistentVolumes() PersistentVolumeInterface {
return &FakePersistentVolumes{Fake: c}
}
func (c *Fake) PersistentVolumeClaims(namespace string) PersistentVolumeClaimInterface {
return &FakePersistentVolumeClaims{Fake: c, Namespace: namespace}
}
func (c *Fake) Services(namespace string) ServiceInterface {
return &FakeServices{Fake: c, Namespace: namespace}
}
func (c *Fake) Secrets(namespace string) SecretsInterface {
return &FakeSecrets{Fake: c, Namespace: namespace}
}
func (c *Fake) Namespaces() NamespaceInterface {
return &FakeNamespaces{Fake: c}
}
func (c *Fake) ServerVersion() (*version.Info, error) {
c.Actions = append(c.Actions, FakeAction{Action: "get-version", Value: 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})
return &api.APIVersions{Versions: []string{"v1beta1", "v1beta2"}}, nil
}
type HTTPClientFunc func(*http.Request) (*http.Response, error)
func (f HTTPClientFunc) Do(req *http.Request) (*http.Response, error) {

View File

@@ -1,58 +0,0 @@
/*
Copyright 2014 Google Inc. 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 client
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
type FakePersistentVolumeClaims struct {
Fake *Fake
Namespace string
}
func (c *FakePersistentVolumeClaims) List(labels labels.Selector, field fields.Selector) (*api.PersistentVolumeClaimList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-persistentVolumeClaims"})
return api.Scheme.CopyOrDie(&c.Fake.PersistentVolumeClaimList).(*api.PersistentVolumeClaimList), c.Fake.Err
}
func (c *FakePersistentVolumeClaims) Get(name string) (*api.PersistentVolumeClaim, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-persistentVolumeClaim", Value: name})
return api.Scheme.CopyOrDie(&c.Fake.PersistentVolumeClaim).(*api.PersistentVolumeClaim), nil
}
func (c *FakePersistentVolumeClaims) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-persistentVolumeClaim", Value: name})
return nil
}
func (c *FakePersistentVolumeClaims) Create(persistentvolumeclaim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-persistentVolumeClaim"})
return &api.PersistentVolumeClaim{}, nil
}
func (c *FakePersistentVolumeClaims) Update(persistentvolumeclaim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-persistentVolumeClaim", Value: persistentvolumeclaim.Name})
return &api.PersistentVolumeClaim{}, nil
}
func (c *FakePersistentVolumeClaims) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
return c.Fake.Watch, c.Fake.Err
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,18 +31,18 @@ type FakeEndpoints struct {
}
func (c *FakeEndpoints) Create(endpoints *api.Endpoints) (*api.Endpoints, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-endpoints"})
return &api.Endpoints{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-endpoints"}, &api.Endpoints{})
return obj.(*api.Endpoints), err
}
func (c *FakeEndpoints) List(selector labels.Selector) (*api.EndpointsList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-endpoints"})
return api.Scheme.CopyOrDie(&c.Fake.EndpointsList).(*api.EndpointsList), c.Fake.Err
obj, err := c.Fake.Invokes(FakeAction{Action: "list-endpoints"}, &api.EndpointsList{})
return obj.(*api.EndpointsList), err
}
func (c *FakeEndpoints) Get(name string) (*api.Endpoints, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-endpoints"})
return &api.Endpoints{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-endpoints", Value: name}, &api.Endpoints{})
return obj.(*api.Endpoints), err
}
func (c *FakeEndpoints) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
@@ -51,6 +51,6 @@ func (c *FakeEndpoints) Watch(label labels.Selector, field fields.Selector, reso
}
func (c *FakeEndpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-endpoints", Value: endpoints.Name})
return &api.Endpoints{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-endpoints", Value: endpoints.Name}, &api.Endpoints{})
return obj.(*api.Endpoints), err
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -32,26 +32,26 @@ type FakeEvents struct {
// Create makes a new event. Returns the copy of the event the server returns, or an error.
func (c *FakeEvents) Create(event *api.Event) (*api.Event, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-event", Value: event.Name})
return &api.Event{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-event", Value: event.Name}, &api.Event{})
return obj.(*api.Event), err
}
// Update replaces an existing event. Returns the copy of the event the server returns, or an error.
func (c *FakeEvents) Update(event *api.Event) (*api.Event, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-event", Value: event.Name})
return &api.Event{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-event", Value: event.Name}, &api.Event{})
return obj.(*api.Event), err
}
// List returns a list of events matching the selectors.
func (c *FakeEvents) List(label labels.Selector, field fields.Selector) (*api.EventList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-events"})
return &c.Fake.EventsList, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-events"}, &api.EventList{})
return obj.(*api.EventList), err
}
// Get returns the given event, or an error.
func (c *FakeEvents) Get(id string) (*api.Event, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-event", Value: id})
return &api.Event{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-endpoints", Value: id}, &api.Event{})
return obj.(*api.Event), err
}
// Watch starts watching for events matching the given selectors.
@@ -62,13 +62,13 @@ func (c *FakeEvents) Watch(label labels.Selector, field fields.Selector, resourc
// Search returns a list of events matching the specified object.
func (c *FakeEvents) Search(objOrRef runtime.Object) (*api.EventList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "search-events"})
return &c.Fake.EventsList, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "search-events"}, &api.EventList{})
return obj.(*api.EventList), err
}
func (c *FakeEvents) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-event", Value: name})
return nil
_, err := c.Fake.Invokes(FakeAction{Action: "delete-event", Value: name}, &api.Event{})
return err
}
func (c *FakeEvents) GetFieldSelector(involvedObjectName, involvedObjectNamespace, involvedObjectKind, involvedObjectUID *string) fields.Selector {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,28 +31,28 @@ type FakeLimitRanges struct {
}
func (c *FakeLimitRanges) List(selector labels.Selector) (*api.LimitRangeList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-limitRanges"})
return api.Scheme.CopyOrDie(&c.Fake.LimitRangesList).(*api.LimitRangeList), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-limitRanges"}, &api.LimitRangeList{})
return obj.(*api.LimitRangeList), err
}
func (c *FakeLimitRanges) Get(name string) (*api.LimitRange, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-limitRange", Value: name})
return &api.LimitRange{ObjectMeta: api.ObjectMeta{Name: name, Namespace: c.Namespace}}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-limitRange", Value: name}, &api.LimitRange{})
return obj.(*api.LimitRange), err
}
func (c *FakeLimitRanges) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-limitRange", Value: name})
return nil
_, err := c.Fake.Invokes(FakeAction{Action: "delete-limitRange", Value: name}, &api.LimitRange{})
return err
}
func (c *FakeLimitRanges) Create(limitRange *api.LimitRange) (*api.LimitRange, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-limitRange"})
return &api.LimitRange{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-limitRange"}, &api.LimitRange{})
return obj.(*api.LimitRange), err
}
func (c *FakeLimitRanges) Update(limitRange *api.LimitRange) (*api.LimitRange, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-limitRange", Value: limitRange.Name})
return &api.LimitRange{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-limitRange", Value: limitRange.Name}, &api.LimitRange{})
return obj.(*api.LimitRange), err
}
func (c *FakeLimitRanges) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -30,18 +30,18 @@ type FakeNamespaces struct {
}
func (c *FakeNamespaces) List(labels labels.Selector, field fields.Selector) (*api.NamespaceList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-namespaces"})
return api.Scheme.CopyOrDie(&c.Fake.NamespacesList).(*api.NamespaceList), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-namespaces"}, &api.NamespaceList{})
return obj.(*api.NamespaceList), err
}
func (c *FakeNamespaces) Get(name string) (*api.Namespace, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-namespace", Value: name})
return &api.Namespace{ObjectMeta: api.ObjectMeta{Name: name}}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-namespace", Value: name}, &api.Namespace{})
return obj.(*api.Namespace), err
}
func (c *FakeNamespaces) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-namespace", Value: name})
return nil
_, err := c.Fake.Invokes(FakeAction{Action: "delete-namespace", Value: name}, &api.Namespace{})
return err
}
func (c *FakeNamespaces) Create(namespace *api.Namespace) (*api.Namespace, error) {
@@ -50,8 +50,8 @@ func (c *FakeNamespaces) Create(namespace *api.Namespace) (*api.Namespace, error
}
func (c *FakeNamespaces) Update(namespace *api.Namespace) (*api.Namespace, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-namespace", Value: namespace.Name})
return &api.Namespace{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-namespace", Value: namespace}, &api.Namespace{})
return obj.(*api.Namespace), err
}
func (c *FakeNamespaces) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
@@ -60,12 +60,11 @@ func (c *FakeNamespaces) Watch(label labels.Selector, field fields.Selector, res
}
func (c *FakeNamespaces) Finalize(namespace *api.Namespace) (*api.Namespace, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "finalize-namespace", Value: namespace.Name})
c.Fake.Namespace = *namespace
return namespace, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "finalize-namespace", Value: namespace}, &api.Namespace{})
return obj.(*api.Namespace), err
}
func (c *FakeNamespaces) Status(namespace *api.Namespace) (*api.Namespace, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "status-namespace", Value: namespace.Name})
return &api.Namespace{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "status-namespace", Value: namespace}, &api.Namespace{})
return obj.(*api.Namespace), err
}

View File

@@ -14,11 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
@@ -31,36 +30,31 @@ type FakeNodes struct {
}
func (c *FakeNodes) Get(name string) (*api.Node, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-minion", Value: name})
for i := range c.Fake.MinionsList.Items {
if c.Fake.MinionsList.Items[i].Name == name {
return &c.Fake.MinionsList.Items[i], nil
}
}
return nil, errors.NewNotFound("Minions", name)
obj, err := c.Fake.Invokes(FakeAction{Action: "get-node", Value: name}, &api.Node{})
return obj.(*api.Node), err
}
func (c *FakeNodes) List() (*api.NodeList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-minions", Value: nil})
return &c.Fake.MinionsList, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-nodes"}, &api.NodeList{})
return obj.(*api.NodeList), err
}
func (c *FakeNodes) Create(minion *api.Node) (*api.Node, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-minion", Value: minion})
return &api.Node{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-node", Value: minion}, &api.Node{})
return obj.(*api.Node), err
}
func (c *FakeNodes) Delete(id string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-minion", Value: id})
return nil
func (c *FakeNodes) Delete(name string) error {
_, err := c.Fake.Invokes(FakeAction{Action: "delete-node", Value: name}, &api.Node{})
return err
}
func (c *FakeNodes) Update(minion *api.Node) (*api.Node, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-minion", Value: minion})
return &api.Node{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-node", Value: minion}, &api.Node{})
return obj.(*api.Node), err
}
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-minions", Value: resourceVersion})
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-nodes", Value: resourceVersion})
return c.Fake.Watch, c.Fake.Err
}

View File

@@ -0,0 +1,59 @@
/*
Copyright 2014 Google Inc. 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 (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
type FakePersistentVolumeClaims struct {
Fake *Fake
Namespace string
}
func (c *FakePersistentVolumeClaims) List(label labels.Selector, field fields.Selector) (*api.PersistentVolumeClaimList, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "list-persistentVolumeClaims"}, &api.PersistentVolumeClaimList{})
return obj.(*api.PersistentVolumeClaimList), err
}
func (c *FakePersistentVolumeClaims) Get(name string) (*api.PersistentVolumeClaim, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "get-persistentVolumeClaims", Value: name}, &api.PersistentVolumeClaim{})
return obj.(*api.PersistentVolumeClaim), err
}
func (c *FakePersistentVolumeClaims) Delete(name string) error {
_, err := c.Fake.Invokes(FakeAction{Action: "delete-persistentVolumeClaims", Value: name}, &api.PersistentVolumeClaim{})
return err
}
func (c *FakePersistentVolumeClaims) Create(persistentVolumeClaims *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "create-persistentVolumeClaims"}, &api.PersistentVolumeClaim{})
return obj.(*api.PersistentVolumeClaim), err
}
func (c *FakePersistentVolumeClaims) Update(persistentVolumeClaims *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumeClaims", Value: persistentVolumeClaims.Name}, &api.PersistentVolumeClaim{})
return obj.(*api.PersistentVolumeClaim), err
}
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
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -24,32 +24,33 @@ import (
)
type FakePersistentVolumes struct {
Fake *Fake
Fake *Fake
Namespace string
}
func (c *FakePersistentVolumes) List(labels labels.Selector, field fields.Selector) (*api.PersistentVolumeList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-persistentVolumes"})
return api.Scheme.CopyOrDie(&c.Fake.PersistentVolumesList).(*api.PersistentVolumeList), nil
func (c *FakePersistentVolumes) List(label labels.Selector, field fields.Selector) (*api.PersistentVolumeList, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "list-persistentVolumes"}, &api.PersistentVolumeList{})
return obj.(*api.PersistentVolumeList), err
}
func (c *FakePersistentVolumes) Get(name string) (*api.PersistentVolume, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-persistentVolume", Value: name})
return api.Scheme.CopyOrDie(&c.Fake.PersistentVolume).(*api.PersistentVolume), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-persistentVolumes", Value: name}, &api.PersistentVolume{})
return obj.(*api.PersistentVolume), err
}
func (c *FakePersistentVolumes) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-persistentVolume", Value: name})
return nil
_, err := c.Fake.Invokes(FakeAction{Action: "delete-persistentVolumes", Value: name}, &api.PersistentVolume{})
return err
}
func (c *FakePersistentVolumes) Create(persistentvolume *api.PersistentVolume) (*api.PersistentVolume, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-persistentVolume"})
return &api.PersistentVolume{}, nil
func (c *FakePersistentVolumes) Create(persistentVolumes *api.PersistentVolume) (*api.PersistentVolume, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "create-persistentVolumes"}, &api.PersistentVolume{})
return obj.(*api.PersistentVolume), err
}
func (c *FakePersistentVolumes) Update(persistentvolume *api.PersistentVolume) (*api.PersistentVolume, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-persistentVolume", Value: persistentvolume.Name})
return &api.PersistentVolume{}, nil
func (c *FakePersistentVolumes) Update(persistentVolumes *api.PersistentVolume) (*api.PersistentVolume, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumes", Value: persistentVolumes.Name}, &api.PersistentVolume{})
return obj.(*api.PersistentVolume), err
}
func (c *FakePersistentVolumes) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,28 +31,28 @@ type FakePods struct {
}
func (c *FakePods) List(selector labels.Selector) (*api.PodList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-pods"})
return api.Scheme.CopyOrDie(&c.Fake.PodsList).(*api.PodList), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-pods"}, &api.PodList{})
return obj.(*api.PodList), err
}
func (c *FakePods) Get(name string) (*api.Pod, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-pod", Value: name})
return &api.Pod{ObjectMeta: api.ObjectMeta{Name: name, Namespace: c.Namespace}}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-pod", Value: name}, &api.Pod{})
return obj.(*api.Pod), err
}
func (c *FakePods) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-pod", Value: name})
return nil
_, err := c.Fake.Invokes(FakeAction{Action: "delete-pod", Value: name}, &api.Pod{})
return err
}
func (c *FakePods) Create(pod *api.Pod) (*api.Pod, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-pod"})
return &api.Pod{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-pod"}, &api.Pod{})
return obj.(*api.Pod), err
}
func (c *FakePods) Update(pod *api.Pod) (*api.Pod, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-pod", Value: pod.Name})
return &api.Pod{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-pod", Value: pod.Name}, &api.Pod{})
return obj.(*api.Pod), err
}
func (c *FakePods) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
@@ -66,6 +66,6 @@ func (c *FakePods) Bind(bind *api.Binding) error {
}
func (c *FakePods) UpdateStatus(pod *api.Pod) (*api.Pod, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-status-pod", Value: pod.Name})
return &api.Pod{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-status-pod", Value: pod.Name}, &api.Pod{})
return obj.(*api.Pod), err
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,31 +31,31 @@ type FakeReplicationControllers struct {
}
func (c *FakeReplicationControllers) List(selector labels.Selector) (*api.ReplicationControllerList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-controllers"})
return api.Scheme.CopyOrDie(&c.Fake.CtrlList).(*api.ReplicationControllerList), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-replicationControllers"}, &api.ReplicationControllerList{})
return obj.(*api.ReplicationControllerList), err
}
func (c *FakeReplicationControllers) Get(name string) (*api.ReplicationController, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-controller", Value: name})
return api.Scheme.CopyOrDie(&c.Fake.Ctrl).(*api.ReplicationController), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-replicationController", Value: name}, &api.ReplicationController{})
return obj.(*api.ReplicationController), err
}
func (c *FakeReplicationControllers) Create(controller *api.ReplicationController) (*api.ReplicationController, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-controller", Value: controller})
return &api.ReplicationController{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-replicationController", Value: controller}, &api.ReplicationController{})
return obj.(*api.ReplicationController), err
}
func (c *FakeReplicationControllers) Update(controller *api.ReplicationController) (*api.ReplicationController, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-controller", Value: controller})
return &api.ReplicationController{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-replicationController", Value: controller}, &api.ReplicationController{})
return obj.(*api.ReplicationController), err
}
func (c *FakeReplicationControllers) Delete(controller string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-controller", Value: controller})
return nil
func (c *FakeReplicationControllers) Delete(name string) error {
_, err := c.Fake.Invokes(FakeAction{Action: "delete-replicationController", Value: name}, &api.ReplicationController{})
return err
}
func (c *FakeReplicationControllers) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-controllers", Value: resourceVersion})
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "watch-replicationController", Value: resourceVersion})
return c.Fake.Watch, nil
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,34 +31,33 @@ type FakeResourceQuotas struct {
}
func (c *FakeResourceQuotas) List(selector labels.Selector) (*api.ResourceQuotaList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-resourceQuotas"})
return api.Scheme.CopyOrDie(&c.Fake.ResourceQuotasList).(*api.ResourceQuotaList), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "list-resourceQuotas"}, &api.ResourceQuotaList{})
return obj.(*api.ResourceQuotaList), err
}
func (c *FakeResourceQuotas) Get(name string) (*api.ResourceQuota, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-resourceQuota", Value: name})
return &api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: name, Namespace: c.Namespace}}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-resourceQuota", Value: name}, &api.ResourceQuota{})
return obj.(*api.ResourceQuota), err
}
func (c *FakeResourceQuotas) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-resourceQuota", Value: name})
return nil
_, err := c.Fake.Invokes(FakeAction{Action: "delete-resourceQuota", Value: name}, &api.ResourceQuota{})
return err
}
func (c *FakeResourceQuotas) Create(resourceQuota *api.ResourceQuota) (*api.ResourceQuota, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-resourceQuota"})
return &api.ResourceQuota{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-resourceQuota", Value: resourceQuota}, &api.ResourceQuota{})
return obj.(*api.ResourceQuota), err
}
func (c *FakeResourceQuotas) Update(resourceQuota *api.ResourceQuota) (*api.ResourceQuota, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-resourceQuota", Value: resourceQuota.Name})
return &api.ResourceQuota{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-resourceQuota", Value: resourceQuota}, &api.ResourceQuota{})
return obj.(*api.ResourceQuota), err
}
func (c *FakeResourceQuotas) UpdateStatus(resourceQuota *api.ResourceQuota) (*api.ResourceQuota, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-status-resourceQuota", Value: resourceQuota.Name})
c.Fake.ResourceQuotaStatus = *resourceQuota
return &api.ResourceQuota{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-status-resourceQuota", Value: resourceQuota}, &api.ResourceQuota{})
return obj.(*api.ResourceQuota), err
}
func (c *FakeResourceQuotas) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,28 +31,28 @@ type FakeSecrets struct {
}
func (c *FakeSecrets) List(labels labels.Selector, field fields.Selector) (*api.SecretList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-secrets"})
return &c.Fake.SecretList, c.Fake.Err
obj, err := c.Fake.Invokes(FakeAction{Action: "list-secrets"}, &api.SecretList{})
return obj.(*api.SecretList), err
}
func (c *FakeSecrets) Get(name string) (*api.Secret, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-secret", Value: name})
return api.Scheme.CopyOrDie(&c.Fake.Secret).(*api.Secret), nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-secret", Value: name}, &api.Secret{})
return obj.(*api.Secret), err
}
func (c *FakeSecrets) Create(secret *api.Secret) (*api.Secret, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-secret", Value: secret})
return &api.Secret{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-secret", Value: secret}, &api.Secret{})
return obj.(*api.Secret), err
}
func (c *FakeSecrets) Update(secret *api.Secret) (*api.Secret, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-secret", Value: secret})
return &api.Secret{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-secret", Value: secret}, &api.Secret{})
return obj.(*api.Secret), err
}
func (c *FakeSecrets) Delete(secret string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-secret", Value: secret})
return nil
func (c *FakeSecrets) Delete(name string) error {
_, err := c.Fake.Invokes(FakeAction{Action: "delete-secret", Value: name}, &api.Secret{})
return err
}
func (c *FakeSecrets) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -31,28 +31,28 @@ type FakeServices struct {
}
func (c *FakeServices) List(selector labels.Selector) (*api.ServiceList, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "list-services"})
return &c.Fake.ServiceList, c.Fake.Err
obj, err := c.Fake.Invokes(FakeAction{Action: "list-services"}, &api.ServiceList{})
return obj.(*api.ServiceList), err
}
func (c *FakeServices) Get(name string) (*api.Service, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "get-service", Value: name})
return &api.Service{ObjectMeta: api.ObjectMeta{Name: name, Namespace: c.Namespace}}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "get-service", Value: name}, &api.Service{})
return obj.(*api.Service), err
}
func (c *FakeServices) Create(service *api.Service) (*api.Service, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-service", Value: service})
return &api.Service{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "create-service", Value: service}, &api.Service{})
return obj.(*api.Service), err
}
func (c *FakeServices) Update(service *api.Service) (*api.Service, error) {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "update-service", Value: service})
return &api.Service{}, nil
obj, err := c.Fake.Invokes(FakeAction{Action: "update-service", Value: service}, &api.Service{})
return obj.(*api.Service), err
}
func (c *FakeServices) Delete(service string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-service", Value: service})
return nil
func (c *FakeServices) Delete(name string) error {
_, err := c.Fake.Invokes(FakeAction{Action: "delete-service", Value: name}, &api.Service{})
return err
}
func (c *FakeServices) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {

View File

@@ -14,17 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package client
package testclient
import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
)
// This test file just ensures that Fake and structs it is embedded in
// implement Interface.
func TestFakeImplementsInterface(t *testing.T) {
_ = Interface(&Fake{})
_ = client.Interface(&Fake{})
}
type MyFake struct {
@@ -32,6 +34,6 @@ type MyFake struct {
}
func TestEmbeddedFakeImplementsInterface(t *testing.T) {
_ = Interface(MyFake{&Fake{}})
_ = Interface(&MyFake{&Fake{}})
_ = client.Interface(MyFake{&Fake{}})
_ = client.Interface(&MyFake{&Fake{}})
}

View File

@@ -0,0 +1,218 @@
/*
Copyright 2015 Google Inc. 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 (
"fmt"
"io/ioutil"
"reflect"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/yaml"
)
// ObjectRetriever abstracts the implementation for retrieving or setting generic
// objects. It is intended to be used to fake calls to a server by returning
// objects based on their kind and name.
type ObjectRetriever interface {
// Kind should return a resource or a list of resources (depending on the provided kind and
// name). It should return an error if the caller should communicate an error to the server.
Kind(kind, name string) (runtime.Object, error)
// Add adds a runtime object for test purposes into this object.
Add(runtime.Object) error
}
// ObjectReaction returns a ReactionFunc that takes a generic action string of the form
// <verb>-<resource> or <verb>-<subresource>-<resource> and attempts to return a runtime
// Object or error that matches the requested action. For instance, list-replicationControllers
// should attempt to return a list of replication controllers. This method delegates to the
// ObjectRetriever interface to satisfy retrieval of lists or retrieval of single items.
// TODO: add support for sub resources
func ObjectReaction(o ObjectRetriever, mapper meta.RESTMapper) ReactionFunc {
return func(action FakeAction) (runtime.Object, error) {
segments := strings.Split(action.Action, "-")
var verb, resource string
switch len(segments) {
case 3:
verb, _, resource = segments[0], segments[1], segments[2]
case 2:
verb, resource = segments[0], segments[1]
default:
return nil, fmt.Errorf("unrecognized action, need two or three segments <verb>-<resource> or <verb>-<subresource>-<resource>: %s", action.Action)
}
_, kind, err := mapper.VersionAndKindForResource(resource)
if err != nil {
return nil, fmt.Errorf("unrecognized action %s: %v", resource, err)
}
// TODO: have mapper return a Kind for a subresource?
switch verb {
case "list", "search":
return o.Kind(kind+"List", "")
case "get", "create", "update", "delete":
// TODO: handle sub resources
if s, ok := action.Value.(string); ok && action.Value != nil {
return o.Kind(kind, s)
}
return o.Kind(kind, "unknown")
default:
return nil, fmt.Errorf("no reaction implemented for %s", action.Action)
}
return nil, nil
}
}
// AddObjectsFromPath loads the JSON or YAML file containing Kubernetes API resources
// and adds them to the provided ObjectRetriever.
func AddObjectsFromPath(path string, o ObjectRetriever) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
data, err = yaml.ToJSON(data)
if err != nil {
return err
}
obj, err := api.Codec.Decode(data)
if err != nil {
return err
}
if err := o.Add(obj); err != nil {
return err
}
return nil
}
type objects struct {
types map[string][]runtime.Object
last map[string]int
typer runtime.ObjectTyper
creater runtime.ObjectCreater
copier copier
}
var _ ObjectRetriever = &objects{}
type copier interface {
Copy(obj runtime.Object) (runtime.Object, error)
}
// NewObjects implements the ObjectRetriever interface by introspecting the
// objects provided to Add() and returning them when the Kind method is invoked.
// If an api.List object is provided to Add(), each child item is added. If an
// object is added that is itself a list (PodList, ServiceList) then that is added
// to the "PodList" kind. If no PodList is added, the retriever will take any loaded
// Pods and return them in a list. If an api.Status is added, and the Details.Kind field
// is set, that status will be returned instead (as an error if Status != Success, or
// as a runtime.Object if Status == Success). If multiple PodLists are provided, they
// will be returned in order by the Kind call, and the last PodList will be reused for
// subsequent calls.
func NewObjects(scheme *runtime.Scheme) ObjectRetriever {
return objects{
types: make(map[string][]runtime.Object),
last: make(map[string]int),
typer: scheme,
creater: scheme,
copier: scheme,
}
}
func (o objects) Kind(kind, name string) (runtime.Object, error) {
empty, _ := o.creater.New("", kind)
nilValue := reflect.Zero(reflect.TypeOf(empty)).Interface().(runtime.Object)
arr, ok := o.types[kind]
if !ok {
if strings.HasSuffix(kind, "List") {
itemKind := kind[:len(kind)-4]
arr, ok := o.types[itemKind]
if !ok {
return empty, nil
}
out, err := o.creater.New("", kind)
if err != nil {
return nilValue, err
}
if err := runtime.SetList(out, arr); err != nil {
return nilValue, err
}
if out, err = o.copier.Copy(out); err != nil {
return nilValue, err
}
return out, nil
}
return nilValue, errors.NewNotFound(kind, name)
}
index := o.last[kind]
if index >= len(arr) {
index = len(arr) - 1
}
if index < 0 {
return nilValue, errors.NewNotFound(kind, name)
}
out, err := o.copier.Copy(arr[index])
if err != nil {
return nilValue, err
}
o.last[kind] = index + 1
if status, ok := out.(*api.Status); ok {
if status.Details != nil {
status.Details.Kind = kind
}
if status.Status != api.StatusSuccess {
return nilValue, &errors.StatusError{*status}
}
}
return out, nil
}
func (o objects) Add(obj runtime.Object) error {
_, kind, err := o.typer.ObjectVersionAndKind(obj)
if err != nil {
return err
}
switch {
case runtime.IsListType(obj):
if kind != "List" {
o.types[kind] = append(o.types[kind], obj)
}
list, err := runtime.ExtractList(obj)
if err != nil {
return err
}
for _, obj := range list {
if err := o.Add(obj); err != nil {
return err
}
}
default:
if status, ok := obj.(*api.Status); ok && status.Details != nil {
kind = status.Details.Kind
}
o.types[kind] = append(o.types[kind], obj)
}
return nil
}

View File

@@ -0,0 +1,127 @@
/*
Copyright 2015 Google Inc. 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 (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"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)
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 {
Actions []FakeAction
Watch watch.Interface
Err error
// 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.Actions = append(c.Actions, action)
if c.ReactFn != nil {
return c.ReactFn(action)
}
return obj, 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) Services(namespace string) client.ServiceInterface {
return &FakeServices{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.Actions = append(c.Actions, FakeAction{Action: "get-version", Value: 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})
return &api.APIVersions{Versions: []string{"v1beta1", "v1beta2"}}, nil
}

View File

@@ -0,0 +1,74 @@
/*
Copyright 2015 Google Inc. 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 (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)
func TestNewClient(t *testing.T) {
o := NewObjects(api.Scheme)
if err := AddObjectsFromPath("../../../examples/guestbook/frontend-service.json", o); err != nil {
t.Fatal(err)
}
client := &Fake{ReactFn: ObjectReaction(o, latest.RESTMapper)}
list, err := client.Services("test").List(labels.Everything())
if err != nil {
t.Fatal(err)
}
if len(list.Items) != 1 {
t.Fatalf("unexpected list %#v", list)
}
// When list is invoked a second time, the same results are returned.
list, err = client.Services("test").List(labels.Everything())
if err != nil {
t.Fatal(err)
}
if len(list.Items) != 1 {
t.Fatalf("unexpected list %#v", list)
}
t.Logf("list: %#v", list)
}
func TestErrors(t *testing.T) {
o := NewObjects(api.Scheme)
o.Add(&api.List{
Items: []runtime.Object{
// This first call to List will return this error
&(errors.NewNotFound("ServiceList", "").(*errors.StatusError).ErrStatus),
// The second call to List will return this error
&(errors.NewForbidden("ServiceList", "", nil).(*errors.StatusError).ErrStatus),
},
})
client := &Fake{ReactFn: ObjectReaction(o, latest.RESTMapper)}
_, err := client.Services("test").List(labels.Everything())
if !errors.IsNotFound(err) {
t.Fatalf("unexpected error: %v", err)
}
t.Logf("error: %#v", err.(*errors.StatusError).Status())
_, err = client.Services("test").List(labels.Everything())
if !errors.IsForbidden(err) {
t.Fatalf("unexpected error: %v", err)
}
}