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)
}
}

View File

@ -30,6 +30,7 @@ import (
apierrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
fake_cloud "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/fake"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
@ -49,7 +50,7 @@ const (
// PodsInterface and PodInterface to test list & delet pods, which is implemented in
// the embeded client.Fake field.
type FakeNodeHandler struct {
client.Fake
*testclient.Fake
// Input: Hooks determine if request is valid or not
CreateHook func(*FakeNodeHandler, *api.Node) bool
@ -508,6 +509,9 @@ func TestSyncCloudNodes(t *testing.T) {
}
for _, item := range table {
if item.fakeNodeHandler.Fake == nil {
item.fakeNodeHandler.Fake = testclient.NewSimpleFake()
}
nodeController := NewNodeController(item.fakeCloud, item.matchRE, nil, &api.NodeResources{}, item.fakeNodeHandler, nil, 10, time.Minute,
util.NewFakeRateLimiter(), testNodeMonitorGracePeriod, testNodeStartupGracePeriod, testNodeMonitorPeriod)
if err := nodeController.SyncCloudNodes(); err != nil {
@ -542,15 +546,13 @@ func TestSyncCloudNodesEvictPods(t *testing.T) {
matchRE string
expectedRequestCount int
expectedDeleted []string
expectedActions []client.FakeAction
expectedActions []testclient.FakeAction
}{
{
// No node to delete: do nothing.
fakeNodeHandler: &FakeNodeHandler{
Existing: []*api.Node{newNode("node0"), newNode("node1")},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0"), *newPod("pod1", "node1")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0"), *newPod("pod1", "node1")}}),
},
fakeCloud: &fake_cloud.FakeCloud{
Machines: []string{"node0", "node1"},
@ -564,9 +566,7 @@ func TestSyncCloudNodesEvictPods(t *testing.T) {
// Delete node1, and pod0 is running on it.
fakeNodeHandler: &FakeNodeHandler{
Existing: []*api.Node{newNode("node0"), newNode("node1")},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node1")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node1")}}),
},
fakeCloud: &fake_cloud.FakeCloud{
Machines: []string{"node0"},
@ -574,15 +574,13 @@ func TestSyncCloudNodesEvictPods(t *testing.T) {
matchRE: ".*",
expectedRequestCount: 2, // List + Delete
expectedDeleted: []string{"node1"},
expectedActions: []client.FakeAction{{Action: "list-pods"}, {Action: "delete-pod", Value: "pod0"}},
expectedActions: []testclient.FakeAction{{Action: "list-pods"}, {Action: "delete-pod", Value: "pod0"}},
},
{
// Delete node1, but pod0 is running on node0.
fakeNodeHandler: &FakeNodeHandler{
Existing: []*api.Node{newNode("node0"), newNode("node1")},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
fakeCloud: &fake_cloud.FakeCloud{
Machines: []string{"node0"},
@ -590,11 +588,14 @@ func TestSyncCloudNodesEvictPods(t *testing.T) {
matchRE: ".*",
expectedRequestCount: 2, // List + Delete
expectedDeleted: []string{"node1"},
expectedActions: []client.FakeAction{{Action: "list-pods"}},
expectedActions: []testclient.FakeAction{{Action: "list-pods"}},
},
}
for _, item := range table {
if item.fakeNodeHandler.Fake == nil {
item.fakeNodeHandler.Fake = testclient.NewSimpleFake()
}
nodeController := NewNodeController(item.fakeCloud, item.matchRE, nil, &api.NodeResources{}, item.fakeNodeHandler, nil, 10, time.Minute,
util.NewFakeRateLimiter(), testNodeMonitorGracePeriod, testNodeStartupGracePeriod, testNodeMonitorPeriod)
if err := nodeController.SyncCloudNodes(); err != nil {
@ -915,7 +916,7 @@ func TestSyncProbedNodeStatusEvictPods(t *testing.T) {
fakeNodeHandler *FakeNodeHandler
fakeKubeletClient *FakeKubeletClient
expectedRequestCount int
expectedActions []client.FakeAction
expectedActions []testclient.FakeAction
}{
{
// Existing node is healthy, current probe is healthy too.
@ -935,9 +936,7 @@ func TestSyncProbedNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node1")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node1")}}),
},
fakeKubeletClient: &FakeKubeletClient{
Status: probe.Success,
@ -965,9 +964,7 @@ func TestSyncProbedNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
fakeKubeletClient: &FakeKubeletClient{
Status: probe.Failure,
@ -997,9 +994,7 @@ func TestSyncProbedNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
fakeKubeletClient: &FakeKubeletClient{
Status: probe.Failure,
@ -1030,16 +1025,14 @@ func TestSyncProbedNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
fakeKubeletClient: &FakeKubeletClient{
Status: probe.Failure,
Err: nil,
},
expectedRequestCount: 2, // List+Update
expectedActions: []client.FakeAction{{Action: "list-pods"}, {Action: "delete-pod", Value: "pod0"}},
expectedActions: []testclient.FakeAction{{Action: "list-pods"}, {Action: "delete-pod", Value: "pod0"}},
},
}
@ -1081,9 +1074,7 @@ func TestMonitorNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
timeToPass: 0,
newNodeStatus: api.NodeStatus{},
@ -1111,9 +1102,7 @@ func TestMonitorNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
timeToPass: evictionTimeout,
newNodeStatus: api.NodeStatus{
@ -1151,9 +1140,7 @@ func TestMonitorNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
timeToPass: time.Hour,
newNodeStatus: api.NodeStatus{
@ -1191,9 +1178,7 @@ func TestMonitorNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
timeToPass: evictionTimeout - testNodeMonitorGracePeriod,
newNodeStatus: api.NodeStatus{
@ -1231,9 +1216,7 @@ func TestMonitorNodeStatusEvictPods(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
timeToPass: 60 * time.Minute,
newNodeStatus: api.NodeStatus{
@ -1302,9 +1285,7 @@ func TestMonitorNodeStatusUpdateStatus(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
expectedRequestCount: 2, // List+Update
expectedNodes: []*api.Node{
@ -1339,9 +1320,7 @@ func TestMonitorNodeStatusUpdateStatus(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
expectedRequestCount: 1, // List
expectedNodes: nil,
@ -1376,9 +1355,7 @@ func TestMonitorNodeStatusUpdateStatus(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
expectedRequestCount: 3, // (List+)List+Update
timeToPass: time.Hour,
@ -1454,9 +1431,7 @@ func TestMonitorNodeStatusUpdateStatus(t *testing.T) {
},
},
},
Fake: client.Fake{
PodsList: api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}},
},
Fake: testclient.NewSimpleFake(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
},
expectedRequestCount: 1, // List
expectedNodes: nil,

View File

@ -28,6 +28,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
@ -339,12 +340,12 @@ func TestControllerUpdateReplicas(t *testing.T) {
type FakeWatcher struct {
w *watch.FakeWatcher
*client.Fake
*testclient.Fake
}
func TestWatchControllers(t *testing.T) {
fakeWatch := watch.NewFake()
client := &client.Fake{Watch: fakeWatch}
client := &testclient.Fake{Watch: fakeWatch}
manager := NewReplicationManager(client)
var testControllerSpec api.ReplicationController
received := make(chan struct{})

View File

@ -26,6 +26,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
@ -33,7 +34,7 @@ type describeClient struct {
T *testing.T
Namespace string
Err error
*client.Fake
client.Interface
}
func init() {
@ -41,8 +42,13 @@ func init() {
}
func TestDescribePod(t *testing.T) {
fake := &client.Fake{}
c := &describeClient{T: t, Namespace: "foo", Fake: fake}
fake := testclient.NewSimpleFake(&api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "bar",
Namespace: "foo",
},
})
c := &describeClient{T: t, Namespace: "foo", Interface: fake}
d := PodDescriber{c}
out, err := d.Describe("foo", "bar")
if err != nil {
@ -54,8 +60,13 @@ func TestDescribePod(t *testing.T) {
}
func TestDescribeService(t *testing.T) {
fake := &client.Fake{}
c := &describeClient{T: t, Namespace: "foo", Fake: fake}
fake := testclient.NewSimpleFake(&api.Service{
ObjectMeta: api.ObjectMeta{
Name: "bar",
Namespace: "foo",
},
})
c := &describeClient{T: t, Namespace: "foo", Interface: fake}
d := ServiceDescriber{c}
out, err := d.Describe("foo", "bar")
if err != nil {
@ -68,34 +79,32 @@ func TestDescribeService(t *testing.T) {
func TestPodDescribeResultsSorted(t *testing.T) {
// Arrange
fake := &client.Fake{
EventsList: api.EventList{
Items: []api.Event{
{
Source: api.EventSource{Component: "kubelet"},
Message: "Item 1",
FirstTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
Count: 1,
},
{
Source: api.EventSource{Component: "scheduler"},
Message: "Item 2",
FirstTimestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
LastTimestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
Count: 1,
},
{
Source: api.EventSource{Component: "kubelet"},
Message: "Item 3",
FirstTimestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
LastTimestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
Count: 1,
},
fake := testclient.NewSimpleFake(&api.EventList{
Items: []api.Event{
{
Source: api.EventSource{Component: "kubelet"},
Message: "Item 1",
FirstTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
Count: 1,
},
{
Source: api.EventSource{Component: "scheduler"},
Message: "Item 2",
FirstTimestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
LastTimestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
Count: 1,
},
{
Source: api.EventSource{Component: "kubelet"},
Message: "Item 3",
FirstTimestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
LastTimestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
Count: 1,
},
},
}
c := &describeClient{T: t, Namespace: "foo", Fake: fake}
})
c := &describeClient{T: t, Namespace: "foo", Interface: fake}
d := PodDescriber{c}
// Act

View File

@ -22,10 +22,11 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
type ErrorReplicationControllers struct {
client.FakeReplicationControllers
testclient.FakeReplicationControllers
}
func (c *ErrorReplicationControllers) Update(controller *api.ReplicationController) (*api.ReplicationController, error) {
@ -33,15 +34,15 @@ func (c *ErrorReplicationControllers) Update(controller *api.ReplicationControll
}
type ErrorReplicationControllerClient struct {
client.Fake
testclient.Fake
}
func (c *ErrorReplicationControllerClient) ReplicationControllers(namespace string) client.ReplicationControllerInterface {
return &ErrorReplicationControllers{client.FakeReplicationControllers{Fake: &c.Fake, Namespace: namespace}}
return &ErrorReplicationControllers{testclient.FakeReplicationControllers{Fake: &c.Fake, Namespace: namespace}}
}
func TestReplicationControllerResizeRetry(t *testing.T) {
fake := &ErrorReplicationControllerClient{Fake: client.Fake{}}
fake := &ErrorReplicationControllerClient{Fake: testclient.Fake{}}
resizer := ReplicationControllerResizer{fake}
preconditions := ResizePrecondition{-1, ""}
count := uint(3)
@ -65,7 +66,7 @@ func TestReplicationControllerResizeRetry(t *testing.T) {
}
func TestReplicationControllerResize(t *testing.T) {
fake := &client.Fake{}
fake := &testclient.Fake{}
resizer := ReplicationControllerResizer{fake}
preconditions := ResizePrecondition{-1, ""}
count := uint(3)
@ -75,22 +76,20 @@ func TestReplicationControllerResize(t *testing.T) {
if len(fake.Actions) != 2 {
t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", fake.Actions)
}
if fake.Actions[0].Action != "get-controller" || fake.Actions[0].Value != name {
t.Errorf("unexpected action: %v, expected get-controller %s", fake.Actions[0], name)
if fake.Actions[0].Action != "get-replicationController" || fake.Actions[0].Value != name {
t.Errorf("unexpected action: %v, expected get-replicationController %s", fake.Actions[0], name)
}
if fake.Actions[1].Action != "update-controller" || fake.Actions[1].Value.(*api.ReplicationController).Spec.Replicas != int(count) {
t.Errorf("unexpected action %v, expected update-controller with replicas = %d", fake.Actions[1], count)
if fake.Actions[1].Action != "update-replicationController" || fake.Actions[1].Value.(*api.ReplicationController).Spec.Replicas != int(count) {
t.Errorf("unexpected action %v, expected update-replicationController with replicas = %d", fake.Actions[1], count)
}
}
func TestReplicationControllerResizeFailsPreconditions(t *testing.T) {
fake := &client.Fake{
Ctrl: api.ReplicationController{
Spec: api.ReplicationControllerSpec{
Replicas: 10,
},
fake := testclient.NewSimpleFake(&api.ReplicationController{
Spec: api.ReplicationControllerSpec{
Replicas: 10,
},
}
})
resizer := ReplicationControllerResizer{fake}
preconditions := ResizePrecondition{2, ""}
count := uint(3)
@ -100,8 +99,8 @@ func TestReplicationControllerResizeFailsPreconditions(t *testing.T) {
if len(fake.Actions) != 1 {
t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", fake.Actions)
}
if fake.Actions[0].Action != "get-controller" || fake.Actions[0].Value != name {
t.Errorf("unexpected action: %v, expected get-controller %s", fake.Actions[0], name)
if fake.Actions[0].Action != "get-replicationController" || fake.Actions[0].Value != name {
t.Errorf("unexpected action: %v, expected get-replicationController %s", fake.Actions[0], name)
}
}

View File

@ -24,10 +24,11 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
type updaterFake struct {
*client.Fake
*testclient.Fake
ctrl client.ReplicationControllerInterface
}
@ -36,11 +37,11 @@ func (c *updaterFake) ReplicationControllers(namespace string) client.Replicatio
}
func fakeClientFor(namespace string, responses []fakeResponse) client.Interface {
fake := client.Fake{}
fake := testclient.Fake{}
return &updaterFake{
&fake,
&fakeRc{
&client.FakeReplicationControllers{
&testclient.FakeReplicationControllers{
Fake: &fake,
Namespace: namespace,
},
@ -55,12 +56,12 @@ type fakeResponse struct {
}
type fakeRc struct {
*client.FakeReplicationControllers
*testclient.FakeReplicationControllers
responses []fakeResponse
}
func (c *fakeRc) Get(name string) (*api.ReplicationController, error) {
action := client.FakeAction{Action: "get-controller", Value: name}
action := testclient.FakeAction{Action: "get-controller", Value: name}
if len(c.responses) == 0 {
return nil, fmt.Errorf("Unexpected Action: %s", action)
}
@ -71,12 +72,12 @@ func (c *fakeRc) Get(name string) (*api.ReplicationController, error) {
}
func (c *fakeRc) Create(controller *api.ReplicationController) (*api.ReplicationController, error) {
c.Fake.Actions = append(c.Fake.Actions, client.FakeAction{Action: "create-controller", Value: controller.ObjectMeta.Name})
c.Fake.Actions = append(c.Fake.Actions, testclient.FakeAction{Action: "create-controller", Value: controller.ObjectMeta.Name})
return controller, nil
}
func (c *fakeRc) Update(controller *api.ReplicationController) (*api.ReplicationController, error) {
c.Fake.Actions = append(c.Fake.Actions, client.FakeAction{Action: "update-controller", Value: controller.ObjectMeta.Name})
c.Fake.Actions = append(c.Fake.Actions, testclient.FakeAction{Action: "update-controller", Value: controller.ObjectMeta.Name})
return controller, nil
}

View File

@ -23,16 +23,15 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
func TestReplicationControllerStop(t *testing.T) {
fake := &client.Fake{
Ctrl: api.ReplicationController{
Spec: api.ReplicationControllerSpec{
Replicas: 0,
},
fake := testclient.NewSimpleFake(&api.ReplicationController{
Spec: api.ReplicationControllerSpec{
Replicas: 0,
},
}
})
reaper := ReplicationControllerReaper{fake, time.Millisecond, time.Millisecond}
name := "foo"
s, err := reaper.Stop("default", name)
@ -47,14 +46,14 @@ func TestReplicationControllerStop(t *testing.T) {
t.Errorf("unexpected actions: %v, expected 4 actions (get, update, get, delete)", fake.Actions)
}
for i, action := range []string{"get", "update", "get", "delete"} {
if fake.Actions[i].Action != action+"-controller" {
t.Errorf("unexpected action: %v, expected %s-controller", fake.Actions[i], action)
if fake.Actions[i].Action != action+"-replicationController" {
t.Errorf("unexpected action: %v, expected %s-replicationController", fake.Actions[i], action)
}
}
}
type noSuchPod struct {
*client.FakePods
*testclient.FakePods
}
func (c *noSuchPod) Get(name string) (*api.Pod, error) {
@ -62,7 +61,7 @@ func (c *noSuchPod) Get(name string) (*api.Pod, error) {
}
type noDeleteService struct {
*client.FakeServices
*testclient.FakeServices
}
func (c *noDeleteService) Delete(service string) error {
@ -70,12 +69,12 @@ func (c *noDeleteService) Delete(service string) error {
}
type reaperFake struct {
*client.Fake
*testclient.Fake
noSuchPod, noDeleteService bool
}
func (c *reaperFake) Pods(namespace string) client.PodInterface {
pods := &client.FakePods{c.Fake, namespace}
pods := &testclient.FakePods{c.Fake, namespace}
if c.noSuchPod {
return &noSuchPod{pods}
}
@ -83,7 +82,7 @@ func (c *reaperFake) Pods(namespace string) client.PodInterface {
}
func (c *reaperFake) Services(namespace string) client.ServiceInterface {
services := &client.FakeServices{c.Fake, namespace}
services := &testclient.FakeServices{c.Fake, namespace}
if c.noDeleteService {
return &noDeleteService{services}
}
@ -100,7 +99,7 @@ func TestSimpleStop(t *testing.T) {
}{
{
fake: &reaperFake{
Fake: &client.Fake{},
Fake: &testclient.Fake{},
},
kind: "Pod",
actions: []string{"get-pod", "delete-pod"},
@ -109,7 +108,7 @@ func TestSimpleStop(t *testing.T) {
},
{
fake: &reaperFake{
Fake: &client.Fake{},
Fake: &testclient.Fake{},
},
kind: "Service",
actions: []string{"get-service", "delete-service"},
@ -118,7 +117,7 @@ func TestSimpleStop(t *testing.T) {
},
{
fake: &reaperFake{
Fake: &client.Fake{},
Fake: &testclient.Fake{},
noSuchPod: true,
},
kind: "Pod",
@ -128,7 +127,7 @@ func TestSimpleStop(t *testing.T) {
},
{
fake: &reaperFake{
Fake: &client.Fake{},
Fake: &testclient.Fake{},
noDeleteService: true,
},
kind: "Service",

View File

@ -37,8 +37,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/cadvisor"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
@ -63,7 +63,7 @@ type TestKubelet struct {
kubelet *Kubelet
fakeDocker *dockertools.FakeDockerClient
fakeCadvisor *cadvisor.Mock
fakeKubeClient *client.Fake
fakeKubeClient *testclient.Fake
waitGroup *sync.WaitGroup
fakeMirrorClient *fakeMirrorClient
}
@ -72,7 +72,7 @@ func newTestKubelet(t *testing.T) *TestKubelet {
fakeDocker := &dockertools.FakeDockerClient{RemovedImages: util.StringSet{}}
fakeDockerCache := dockertools.NewFakeDockerCache(fakeDocker)
fakeRecorder := &record.FakeRecorder{}
fakeKubeClient := &client.Fake{}
fakeKubeClient := &testclient.Fake{}
kubelet := &Kubelet{}
kubelet.dockerClient = fakeDocker
@ -3079,10 +3079,9 @@ func TestUpdateNewNodeStatus(t *testing.T) {
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
kubeClient := testKubelet.fakeKubeClient
mockCadvisor := testKubelet.fakeCadvisor
kubeClient.MinionsList = api.NodeList{Items: []api.Node{
kubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{
{ObjectMeta: api.ObjectMeta{Name: "testnode"}},
}}
}}).ReactFn
machineInfo := &cadvisorApi.MachineInfo{
MachineID: "123",
SystemUUID: "abc",
@ -3090,6 +3089,7 @@ func TestUpdateNewNodeStatus(t *testing.T) {
NumCores: 2,
MemoryCapacity: 1024,
}
mockCadvisor := testKubelet.fakeCadvisor
mockCadvisor.On("MachineInfo").Return(machineInfo, nil)
versionInfo := &cadvisorApi.VersionInfo{
KernelVersion: "3.16.0-0.bpo.4-amd64",
@ -3130,8 +3130,8 @@ func TestUpdateNewNodeStatus(t *testing.T) {
if err := kubelet.updateNodeStatus(); err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(kubeClient.Actions) != 2 {
t.Errorf("unexpected actions: %v", kubeClient.Actions)
if len(kubeClient.Actions) != 2 || kubeClient.Actions[1].Action != "update-node" {
t.Fatalf("unexpected actions: %v", kubeClient.Actions)
}
updatedNode, ok := kubeClient.Actions[1].Value.(*api.Node)
if !ok {
@ -3146,7 +3146,7 @@ func TestUpdateNewNodeStatus(t *testing.T) {
updatedNode.Status.Conditions[0].LastProbeTime = util.Time{}
updatedNode.Status.Conditions[0].LastTransitionTime = util.Time{}
if !reflect.DeepEqual(expectedNode, updatedNode) {
t.Errorf("expected \n%v\n, got \n%v", expectedNode, updatedNode)
t.Errorf("unexpected objects: %s", util.ObjectDiff(expectedNode, updatedNode))
}
}
@ -3154,8 +3154,7 @@ func TestUpdateExistingNodeStatus(t *testing.T) {
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
kubeClient := testKubelet.fakeKubeClient
mockCadvisor := testKubelet.fakeCadvisor
kubeClient.MinionsList = api.NodeList{Items: []api.Node{
kubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{
{
ObjectMeta: api.ObjectMeta{Name: "testnode"},
Spec: api.NodeSpec{},
@ -3175,7 +3174,8 @@ func TestUpdateExistingNodeStatus(t *testing.T) {
},
},
},
}}
}}).ReactFn
mockCadvisor := testKubelet.fakeCadvisor
machineInfo := &cadvisorApi.MachineInfo{
MachineID: "123",
SystemUUID: "abc",
@ -3231,11 +3231,11 @@ func TestUpdateExistingNodeStatus(t *testing.T) {
t.Errorf("unexpected object type")
}
// Expect LastProbeTime to be updated to Now, while LastTransitionTime to be the same.
if reflect.DeepEqual(updatedNode.Status.Conditions[0].LastProbeTime, util.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC)) {
if reflect.DeepEqual(updatedNode.Status.Conditions[0].LastProbeTime.Rfc3339Copy().UTC(), util.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC).Time) {
t.Errorf("expected \n%v\n, got \n%v", util.Now(), util.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC))
}
if !reflect.DeepEqual(updatedNode.Status.Conditions[0].LastTransitionTime, util.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC)) {
t.Errorf("expected \n%v\n, got \n%v", updatedNode.Status.Conditions[0].LastTransitionTime,
if !reflect.DeepEqual(updatedNode.Status.Conditions[0].LastTransitionTime.Rfc3339Copy().UTC(), util.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC).Time) {
t.Errorf("expected \n%#v\n, got \n%#v", updatedNode.Status.Conditions[0].LastTransitionTime.Rfc3339Copy(),
util.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC))
}
updatedNode.Status.Conditions[0].LastProbeTime = util.Time{}
@ -3248,15 +3248,14 @@ func TestUpdateExistingNodeStatus(t *testing.T) {
func TestUpdateNodeStatusError(t *testing.T) {
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
kubeClient := testKubelet.fakeKubeClient
// No matching node for the kubelet
kubeClient.MinionsList = api.NodeList{Items: []api.Node{}}
testKubelet.fakeKubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{}}).ReactFn
if err := kubelet.updateNodeStatus(); err == nil {
t.Errorf("unexpected non error: %v", err)
}
if len(kubeClient.Actions) != nodeStatusUpdateRetry {
t.Errorf("unexpected actions: %v", kubeClient.Actions)
if len(testKubelet.fakeKubeClient.Actions) != nodeStatusUpdateRetry {
t.Errorf("unexpected actions: %v", testKubelet.fakeKubeClient.Actions)
}
}

View File

@ -23,6 +23,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
var testPod *api.Pod = &api.Pod{
@ -34,7 +35,7 @@ var testPod *api.Pod = &api.Pod{
}
func newTestStatusManager() *statusManager {
return newStatusManager(&client.Fake{})
return newStatusManager(&testclient.Fake{})
}
func generateRandomMessage() string {
@ -48,7 +49,7 @@ func getRandomPodStatus() api.PodStatus {
}
func verifyActions(t *testing.T, kubeClient client.Interface, expectedActions []string) {
actions := kubeClient.(*client.Fake).Actions
actions := kubeClient.(*testclient.Fake).Actions
if len(actions) != len(expectedActions) {
t.Errorf("unexpected actions, got: %s expected: %s", actions, expectedActions)
return

View File

@ -20,8 +20,8 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
@ -41,7 +41,7 @@ func TestFinalized(t *testing.T) {
}
func TestFinalize(t *testing.T) {
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
testNamespace := api.Namespace{
ObjectMeta: api.ObjectMeta{
Name: "test",
@ -58,7 +58,7 @@ func TestFinalize(t *testing.T) {
if mockClient.Actions[0].Action != "finalize-namespace" {
t.Errorf("Expected finalize-namespace action %v", mockClient.Actions[0].Action)
}
finalizers := mockClient.Namespace.Spec.Finalizers
finalizers := mockClient.Actions[0].Value.(*api.Namespace).Spec.Finalizers
if len(finalizers) != 1 {
t.Errorf("There should be a single finalizer remaining")
}
@ -68,7 +68,7 @@ func TestFinalize(t *testing.T) {
}
func TestSyncNamespaceThatIsTerminating(t *testing.T) {
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
nm := NamespaceManager{kubeClient: mockClient, store: cache.NewStore(cache.MetaNamespaceKeyFunc)}
now := util.Now()
testNamespace := api.Namespace{
@ -92,7 +92,7 @@ func TestSyncNamespaceThatIsTerminating(t *testing.T) {
"list-services",
"list-pods",
"list-resourceQuotas",
"list-controllers",
"list-replicationControllers",
"list-secrets",
"list-limitRanges",
"list-events",
@ -108,7 +108,7 @@ func TestSyncNamespaceThatIsTerminating(t *testing.T) {
}
func TestSyncNamespaceThatIsActive(t *testing.T) {
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
nm := NamespaceManager{kubeClient: mockClient, store: cache.NewStore(cache.MetaNamespaceKeyFunc)}
testNamespace := api.Namespace{
ObjectMeta: api.ObjectMeta{

View File

@ -22,7 +22,7 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
@ -30,7 +30,7 @@ func TestServices(t *testing.T) {
service := api.Service{ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"}}
fakeWatch := watch.NewFake()
fakeClient := &client.Fake{Watch: fakeWatch}
fakeClient := &testclient.Fake{Watch: fakeWatch}
services := make(chan ServiceUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
@ -44,7 +44,7 @@ func TestServices(t *testing.T) {
// test adding a service to the watch
fakeWatch.Add(&service)
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-services", "1"}}) {
t.Errorf("expected call to watch-services, got %#v", fakeClient)
}
@ -68,7 +68,7 @@ func TestServices(t *testing.T) {
fakeWatch.Stop()
newFakeWatch.Add(&service)
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}, {"watch-services", "2"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-services", "1"}, {"watch-services", "2"}}) {
t.Errorf("expected call to watch-endpoints, got %#v", fakeClient)
}
}
@ -78,13 +78,13 @@ func TestServicesFromZero(t *testing.T) {
fakeWatch := watch.NewFake()
fakeWatch.Stop()
fakeClient := &client.Fake{Watch: fakeWatch}
fakeClient.ServiceList = api.ServiceList{
fakeClient := testclient.NewSimpleFake(&api.ServiceList{
ListMeta: api.ListMeta{ResourceVersion: "2"},
Items: []api.Service{
service,
},
}
})
fakeClient.Watch = fakeWatch
services := make(chan ServiceUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
@ -108,13 +108,13 @@ func TestServicesFromZero(t *testing.T) {
if resourceVersion != "2" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-services", nil}, {"watch-services", "2"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"list-services", nil}, {"watch-services", "2"}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
func TestServicesError(t *testing.T) {
fakeClient := &client.Fake{Err: errors.New("test")}
fakeClient := &testclient.Fake{Err: errors.New("test")}
services := make(chan ServiceUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
@ -131,13 +131,13 @@ func TestServicesError(t *testing.T) {
if resourceVersion != "" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-services", "1"}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
func TestServicesErrorTimeout(t *testing.T) {
fakeClient := &client.Fake{Err: errors.New("use of closed network connection")}
fakeClient := &testclient.Fake{Err: errors.New("use of closed network connection")}
services := make(chan ServiceUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
@ -154,13 +154,13 @@ func TestServicesErrorTimeout(t *testing.T) {
if resourceVersion != "1" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-services", "1"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-services", "1"}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
func TestServicesFromZeroError(t *testing.T) {
fakeClient := &client.Fake{Err: errors.New("test")}
fakeClient := &testclient.Fake{Err: errors.New("test")}
services := make(chan ServiceUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll), services: services},
@ -177,7 +177,7 @@ func TestServicesFromZeroError(t *testing.T) {
if resourceVersion != "" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-services", nil}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"list-services", nil}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
@ -192,7 +192,7 @@ func TestEndpoints(t *testing.T) {
}
fakeWatch := watch.NewFake()
fakeClient := &client.Fake{Watch: fakeWatch}
fakeClient := &testclient.Fake{Watch: fakeWatch}
endpoints := make(chan EndpointsUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
@ -206,7 +206,7 @@ func TestEndpoints(t *testing.T) {
// test adding an endpoint to the watch
fakeWatch.Add(&endpoint)
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-endpoints", "1"}}) {
t.Errorf("expected call to watch-endpoints, got %#v", fakeClient)
}
@ -230,7 +230,7 @@ func TestEndpoints(t *testing.T) {
fakeWatch.Stop()
newFakeWatch.Add(&endpoint)
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}, {"watch-endpoints", "2"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-endpoints", "1"}, {"watch-endpoints", "2"}}) {
t.Errorf("expected call to watch-endpoints, got %#v", fakeClient)
}
}
@ -246,13 +246,13 @@ func TestEndpointsFromZero(t *testing.T) {
fakeWatch := watch.NewFake()
fakeWatch.Stop()
fakeClient := &client.Fake{Watch: fakeWatch}
fakeClient.EndpointsList = api.EndpointsList{
fakeClient := testclient.NewSimpleFake(&api.EndpointsList{
ListMeta: api.ListMeta{ResourceVersion: "2"},
Items: []api.Endpoints{
endpoint,
},
}
})
fakeClient.Watch = fakeWatch
endpoints := make(chan EndpointsUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
@ -276,13 +276,13 @@ func TestEndpointsFromZero(t *testing.T) {
if resourceVersion != "2" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-endpoints", nil}, {"watch-endpoints", "2"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"list-endpoints", nil}, {"watch-endpoints", "2"}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
func TestEndpointsError(t *testing.T) {
fakeClient := &client.Fake{Err: errors.New("test")}
fakeClient := &testclient.Fake{Err: errors.New("test")}
endpoints := make(chan EndpointsUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
@ -299,13 +299,13 @@ func TestEndpointsError(t *testing.T) {
if resourceVersion != "" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-endpoints", "1"}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
func TestEndpointsErrorTimeout(t *testing.T) {
fakeClient := &client.Fake{Err: errors.New("use of closed network connection")}
fakeClient := &testclient.Fake{Err: errors.New("use of closed network connection")}
endpoints := make(chan EndpointsUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
@ -322,13 +322,13 @@ func TestEndpointsErrorTimeout(t *testing.T) {
if resourceVersion != "1" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"watch-endpoints", "1"}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"watch-endpoints", "1"}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}
func TestEndpointsFromZeroError(t *testing.T) {
fakeClient := &client.Fake{Err: errors.New("test")}
fakeClient := &testclient.Fake{Err: errors.New("test")}
endpoints := make(chan EndpointsUpdate)
source := SourceAPI{
s: servicesReflector{watcher: fakeClient.Services(api.NamespaceAll)},
@ -345,7 +345,7 @@ func TestEndpointsFromZeroError(t *testing.T) {
if resourceVersion != "" {
t.Errorf("unexpected resource version, got %#v", resourceVersion)
}
if !reflect.DeepEqual(fakeClient.Actions, []client.FakeAction{{"list-endpoints", nil}}) {
if !reflect.DeepEqual(fakeClient.Actions, []testclient.FakeAction{{"list-endpoints", nil}}) {
t.Errorf("unexpected actions, got %#v", fakeClient)
}
}

View File

@ -18,6 +18,7 @@ package controller
import (
"fmt"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"

View File

@ -21,7 +21,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
@ -150,17 +150,15 @@ func TestSyncResourceQuota(t *testing.T) {
},
}
kubeClient := &client.Fake{
PodsList: podList,
}
kubeClient := testclient.NewSimpleFake(&podList, &quota)
resourceQuotaManager := NewResourceQuotaManager(kubeClient)
err := resourceQuotaManager.syncResourceQuota(quota)
if err != nil {
t.Errorf("Unexpected error %v", err)
t.Fatalf("Unexpected error %v", err)
}
usage := kubeClient.ResourceQuotaStatus
usage := kubeClient.Actions[1].Value.(*api.ResourceQuota)
// ensure hard and used limits are what we expected
for k, v := range expectedUsage.Status.Hard {

View File

@ -26,6 +26,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
@ -86,9 +87,7 @@ func TestPlugin(t *testing.T) {
},
}
client := &client.Fake{
Secret: secret,
}
client := testclient.NewSimpleFake(&secret)
pluginMgr := volume.VolumePluginMgr{}
pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, client))

View File

@ -22,14 +22,14 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
// TestAdmission verifies a namespace is created on create requests for namespace managed resources
func TestAdmission(t *testing.T) {
namespace := "test"
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
handler := &provision{
client: mockClient,
store: cache.NewStore(cache.MetaNamespaceKeyFunc),
@ -56,7 +56,7 @@ func TestAdmission(t *testing.T) {
// TestAdmissionNamespaceExists verifies that no client call is made when a namespace already exists
func TestAdmissionNamespaceExists(t *testing.T) {
namespace := "test"
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
store := cache.NewStore(cache.MetaNamespaceKeyFunc)
store.Add(&api.Namespace{
ObjectMeta: api.ObjectMeta{Name: namespace},
@ -84,7 +84,7 @@ func TestAdmissionNamespaceExists(t *testing.T) {
// TestIgnoreAdmission validates that a request is ignored if its not a create
func TestIgnoreAdmission(t *testing.T) {
namespace := "test"
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
handler := &provision{
client: mockClient,
store: cache.NewStore(cache.MetaNamespaceKeyFunc),
@ -108,7 +108,7 @@ func TestIgnoreAdmission(t *testing.T) {
// TestAdmissionNamespaceExistsUnknownToHandler
func TestAdmissionNamespaceExistsUnknownToHandler(t *testing.T) {
namespace := "test"
mockClient := &client.Fake{
mockClient := &testclient.Fake{
Err: errors.NewAlreadyExists("namespaces", namespace),
}
store := cache.NewStore(cache.MetaNamespaceKeyFunc)

View File

@ -21,8 +21,8 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
// TestAdmission
@ -38,7 +38,7 @@ func TestAdmission(t *testing.T) {
}
store := cache.NewStore(cache.MetaNamespaceIndexFunc)
store.Add(namespaceObj)
mockClient := &client.Fake{}
mockClient := &testclient.Fake{}
handler := &lifecycle{
client: mockClient,
store: store,

View File

@ -22,7 +22,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
)
func getResourceRequirements(cpu, memory string) api.ResourceRequirements {
@ -40,7 +40,7 @@ func getResourceRequirements(cpu, memory string) api.ResourceRequirements {
func TestAdmissionIgnoresDelete(t *testing.T) {
namespace := "default"
handler := NewResourceQuota(&client.Fake{})
handler := NewResourceQuota(&testclient.Fake{})
err := handler.Admit(admission.NewAttributesRecord(nil, namespace, "pods", "DELETE"))
if err != nil {
t.Errorf("ResourceQuota should admit all deletes", err)
@ -49,19 +49,17 @@ func TestAdmissionIgnoresDelete(t *testing.T) {
func TestIncrementUsagePods(t *testing.T) {
namespace := "default"
client := &client.Fake{
PodsList: api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
client := testclient.NewSimpleFake(&api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -84,19 +82,17 @@ func TestIncrementUsagePods(t *testing.T) {
func TestIncrementUsageMemory(t *testing.T) {
namespace := "default"
client := &client.Fake{
PodsList: api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
client := testclient.NewSimpleFake(&api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -127,19 +123,17 @@ func TestIncrementUsageMemory(t *testing.T) {
func TestExceedUsageMemory(t *testing.T) {
namespace := "default"
client := &client.Fake{
PodsList: api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
client := testclient.NewSimpleFake(&api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -162,19 +156,17 @@ func TestExceedUsageMemory(t *testing.T) {
func TestIncrementUsageCPU(t *testing.T) {
namespace := "default"
client := &client.Fake{
PodsList: api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
client := testclient.NewSimpleFake(&api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -205,19 +197,17 @@ func TestIncrementUsageCPU(t *testing.T) {
func TestExceedUsageCPU(t *testing.T) {
namespace := "default"
client := &client.Fake{
PodsList: api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
client := testclient.NewSimpleFake(&api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -240,19 +230,17 @@ func TestExceedUsageCPU(t *testing.T) {
func TestExceedUsagePods(t *testing.T) {
namespace := "default"
client := &client.Fake{
PodsList: api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
client := testclient.NewSimpleFake(&api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
Spec: api.PodSpec{
Volumes: []api.Volume{{Name: "vol"}},
Containers: []api.Container{{Name: "ctr", Image: "image", Resources: getResourceRequirements("100m", "1Gi")}},
},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -268,15 +256,13 @@ func TestExceedUsagePods(t *testing.T) {
func TestIncrementUsageServices(t *testing.T) {
namespace := "default"
client := &client.Fake{
ServiceList: api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
client := testclient.NewSimpleFake(&api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -299,15 +285,13 @@ func TestIncrementUsageServices(t *testing.T) {
func TestExceedUsageServices(t *testing.T) {
namespace := "default"
client := &client.Fake{
ServiceList: api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
client := testclient.NewSimpleFake(&api.ServiceList{
Items: []api.Service{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -323,15 +307,13 @@ func TestExceedUsageServices(t *testing.T) {
func TestIncrementUsageReplicationControllers(t *testing.T) {
namespace := "default"
client := &client.Fake{
CtrlList: api.ReplicationControllerList{
Items: []api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
client := testclient.NewSimpleFake(&api.ReplicationControllerList{
Items: []api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},
@ -354,15 +336,13 @@ func TestIncrementUsageReplicationControllers(t *testing.T) {
func TestExceedUsageReplicationControllers(t *testing.T) {
namespace := "default"
client := &client.Fake{
CtrlList: api.ReplicationControllerList{
Items: []api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
client := testclient.NewSimpleFake(&api.ReplicationControllerList{
Items: []api.ReplicationController{
{
ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace},
},
},
}
})
status := &api.ResourceQuotaStatus{
Hard: api.ResourceList{},
Used: api.ResourceList{},