Skip to content

Commit

Permalink
make testclient more precise
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Aug 4, 2015
1 parent 4271f28 commit 182885e
Show file tree
Hide file tree
Showing 33 changed files with 1,000 additions and 397 deletions.
279 changes: 279 additions & 0 deletions pkg/client/testclient/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/*
Copyright 2015 The Kubernetes Authors 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 (
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

func NewRootGetAction(resource, name string) GetActionImpl {
action := GetActionImpl{}
action.Verb = "get"
action.Resource = resource
action.Name = name

return action
}

func NewGetAction(resource, namespace, name string) GetActionImpl {
action := GetActionImpl{}
action.Verb = "get"
action.Resource = resource
action.Namespace = namespace
action.Name = name

return action
}

func NewRootListAction(resource string, label labels.Selector, field fields.Selector) ListActionImpl {
action := ListActionImpl{}
action.Verb = "list"
action.Resource = resource
action.ListRestrictions = ListRestrictions{label, field}

return action
}

func NewListAction(resource, namespace string, label labels.Selector, field fields.Selector) ListActionImpl {
action := ListActionImpl{}
action.Verb = "list"
action.Resource = resource
action.Namespace = namespace
action.ListRestrictions = ListRestrictions{label, field}

return action
}

func NewRootCreateAction(resource string, object runtime.Object) CreateActionImpl {
action := CreateActionImpl{}
action.Verb = "create"
action.Resource = resource
action.Object = object

return action
}

func NewCreateAction(resource, namespace string, object runtime.Object) CreateActionImpl {
action := CreateActionImpl{}
action.Verb = "create"
action.Resource = resource
action.Namespace = namespace
action.Object = object

return action
}

func NewRootUpdateAction(resource string, object runtime.Object) UpdateActionImpl {
action := UpdateActionImpl{}
action.Verb = "update"
action.Resource = resource
action.Object = object

return action
}

func NewUpdateAction(resource, namespace string, object runtime.Object) UpdateActionImpl {
action := UpdateActionImpl{}
action.Verb = "update"
action.Resource = resource
action.Namespace = namespace
action.Object = object

return action
}

func NewRootDeleteAction(resource, name string) DeleteActionImpl {
action := DeleteActionImpl{}
action.Verb = "delete"
action.Resource = resource
action.Name = name

return action
}

func NewDeleteAction(resource, namespace, name string) DeleteActionImpl {
action := DeleteActionImpl{}
action.Verb = "delete"
action.Resource = resource
action.Namespace = namespace
action.Name = name

return action
}

func NewRootWatchAction(resource string, label labels.Selector, field fields.Selector, resourceVersion string) WatchActionImpl {
action := WatchActionImpl{}
action.Verb = "watch"
action.Resource = resource
action.WatchRestrictions = WatchRestrictions{label, field, resourceVersion}

return action
}

func NewWatchAction(resource, namespace string, label labels.Selector, field fields.Selector, resourceVersion string) WatchActionImpl {
action := WatchActionImpl{}
action.Verb = "watch"
action.Resource = resource
action.Namespace = namespace
action.WatchRestrictions = WatchRestrictions{label, field, resourceVersion}

return action
}

type ListRestrictions struct {
Labels labels.Selector
Fields fields.Selector
}
type WatchRestrictions struct {
Labels labels.Selector
Fields fields.Selector
ResourceVersion string
}

type Action interface {
GetNamespace() string
GetVerb() string
GetResource() string
GetSubresource() string
Matches(verb, resource string) bool
}

type GenericAction interface {
Action
GetValue() interface{}
}

type GetAction interface {
Action
GetName() string
}

type ListAction interface {
Action
GetListRestrictions() ListRestrictions
}

type CreateAction interface {
Action
GetObject() runtime.Object
}

type UpdateAction interface {
Action
GetObject() runtime.Object
}

type DeleteAction interface {
Action
GetName() string
}

type WatchAction interface {
Action
GetWatchRestrictions() WatchRestrictions
}

type ActionImpl struct {
Namespace string
Verb string
Resource string
Subresource string
}

func (a ActionImpl) GetNamespace() string {
return a.Namespace
}
func (a ActionImpl) GetVerb() string {
return a.Verb
}
func (a ActionImpl) GetResource() string {
return a.Resource
}
func (a ActionImpl) GetSubresource() string {
return a.Subresource
}
func (a ActionImpl) Matches(verb, resource string) bool {
return strings.ToLower(verb) == strings.ToLower(a.Verb) &&
strings.ToLower(resource) == strings.ToLower(a.Resource)
}

type GenericActionImpl struct {
ActionImpl
Value interface{}
}

func (a GenericActionImpl) GetValue() interface{} {
return a.Value
}

type GetActionImpl struct {
ActionImpl
Name string
}

func (a GetActionImpl) GetName() string {
return a.Name
}

type ListActionImpl struct {
ActionImpl
ListRestrictions ListRestrictions
}

func (a ListActionImpl) GetListRestrictions() ListRestrictions {
return a.ListRestrictions
}

type CreateActionImpl struct {
ActionImpl
Object runtime.Object
}

func (a CreateActionImpl) GetObject() runtime.Object {
return a.Object
}

type UpdateActionImpl struct {
ActionImpl
Object runtime.Object
}

func (a UpdateActionImpl) GetObject() runtime.Object {
return a.Object
}

type DeleteActionImpl struct {
ActionImpl
Name string
}

func (a DeleteActionImpl) GetName() string {
return a.Name
}

type WatchActionImpl struct {
ActionImpl
WatchRestrictions WatchRestrictions
}

func (a WatchActionImpl) GetWatchRestrictions() WatchRestrictions {
return a.WatchRestrictions
}
29 changes: 14 additions & 15 deletions pkg/client/testclient/fake_componentstatuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ type FakeComponentStatuses struct {
Fake *Fake
}

func (c *FakeComponentStatuses) List(label labels.Selector, field fields.Selector) (result *api.ComponentStatusList, err error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "list-componentstatuses"}, &api.ComponentStatusList{})
return obj.(*api.ComponentStatusList), err
}

func (c *FakeComponentStatuses) Get(name string) (*api.ComponentStatus, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "get-componentstatus", Value: name}, &api.ComponentStatus{})
// c.Actions = append(c.Actions, FakeAction{Action: "get-componentstatuses", Value: nil})
// testStatus := &api.ComponentStatus{
// Name: "test",
// Health: "ok",
// HealthCode: int(probe.Success),
// Message: "ok",
// Error: "",
// }
// return &api.ComponentStatusList{Items: []api.ComponentStatus{*testStatus}}, nil
obj, err := c.Fake.Invokes(NewRootGetAction("componentstatuses", name), &api.ComponentStatus{})
if obj == nil {
return nil, err
}

return obj.(*api.ComponentStatus), err
}

func (c *FakeComponentStatuses) List(label labels.Selector, field fields.Selector) (result *api.ComponentStatusList, err error) {
obj, err := c.Fake.Invokes(NewRootListAction("componentstatuses", label, field), &api.ComponentStatusList{})
if obj == nil {
return nil, err
}

return obj.(*api.ComponentStatusList), err
}
42 changes: 29 additions & 13 deletions pkg/client/testclient/fake_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,48 @@ type FakeEndpoints struct {
Namespace string
}

func (c *FakeEndpoints) Create(endpoints *api.Endpoints) (*api.Endpoints, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "create-endpoints"}, &api.Endpoints{})
func (c *FakeEndpoints) Get(name string) (*api.Endpoints, error) {
obj, err := c.Fake.Invokes(NewGetAction("endpoints", c.Namespace, name), &api.Endpoints{})
if obj == nil {
return nil, err
}

return obj.(*api.Endpoints), err
}

func (c *FakeEndpoints) List(selector labels.Selector) (*api.EndpointsList, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "list-endpoints"}, &api.EndpointsList{})
func (c *FakeEndpoints) List(label labels.Selector) (*api.EndpointsList, error) {
obj, err := c.Fake.Invokes(NewListAction("endpoints", c.Namespace, label, nil), &api.EndpointsList{})
if obj == nil {
return nil, err
}

return obj.(*api.EndpointsList), err
}

func (c *FakeEndpoints) Get(name string) (*api.Endpoints, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "get-endpoints", Value: name}, &api.Endpoints{})
func (c *FakeEndpoints) Create(endpoints *api.Endpoints) (*api.Endpoints, error) {
obj, err := c.Fake.Invokes(NewCreateAction("endpoints", c.Namespace, endpoints), endpoints)
if obj == nil {
return nil, err
}

return obj.(*api.Endpoints), err
}

func (c *FakeEndpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
obj, err := c.Fake.Invokes(NewUpdateAction("endpoints", c.Namespace, endpoints), endpoints)
if obj == nil {
return nil, err
}

return obj.(*api.Endpoints), err
}

func (c *FakeEndpoints) Delete(name string) error {
_, err := c.Fake.Invokes(FakeAction{Action: "delete-endpoints", Value: name}, &api.Endpoints{})
_, err := c.Fake.Invokes(NewDeleteAction("endpoints", c.Namespace, name), &api.Endpoints{})
return err
}

func (c *FakeEndpoints) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
c.Fake.Invokes(FakeAction{Action: "watch-endpoints", Value: resourceVersion}, nil)
c.Fake.Invokes(NewWatchAction("endpoints", c.Namespace, label, field, resourceVersion), nil)
return c.Fake.Watch, c.Fake.Err()
}

func (c *FakeEndpoints) Update(endpoints *api.Endpoints) (*api.Endpoints, error) {
obj, err := c.Fake.Invokes(FakeAction{Action: "update-endpoints", Value: endpoints.Name}, &api.Endpoints{})
return obj.(*api.Endpoints), err
}
Loading

0 comments on commit 182885e

Please sign in to comment.