Merge pull request #916 from smarterclayton/unify_fake_client

Replace pkg/kubecfg#FakeKubeClient with the fake in pkg/client
This commit is contained in:
Daniel Smith 2014-08-15 17:15:19 -07:00
commit 2946e21f80
4 changed files with 113 additions and 194 deletions

View File

@ -22,84 +22,91 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch" "github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
) )
// FakeClient implements Interface. Meant to be embedded into a struct to get a default 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. // implementation. This makes faking out just the method you want to test easier.
type FakeClient struct { type Fake struct {
// FakeClient by default keeps a simple list of the methods that have been called. // Fake by default keeps a simple list of the methods that have been called.
Actions []string Actions []FakeAction
Pods api.PodList
Ctrl api.ReplicationController
} }
func (client *FakeClient) ListPods(selector labels.Selector) (api.PodList, error) { func (c *Fake) ListPods(selector labels.Selector) (api.PodList, error) {
client.Actions = append(client.Actions, "list-pods") c.Actions = append(c.Actions, FakeAction{Action: "list-pods"})
return api.PodList{}, nil return c.Pods, nil
} }
func (client *FakeClient) GetPod(name string) (api.Pod, error) { func (c *Fake) GetPod(name string) (api.Pod, error) {
client.Actions = append(client.Actions, "get-pod") c.Actions = append(c.Actions, FakeAction{Action: "get-pod", Value: name})
return api.Pod{}, nil return api.Pod{}, nil
} }
func (client *FakeClient) DeletePod(name string) error { func (c *Fake) DeletePod(name string) error {
client.Actions = append(client.Actions, "delete-pod") c.Actions = append(c.Actions, FakeAction{Action: "delete-pod", Value: name})
return nil return nil
} }
func (client *FakeClient) CreatePod(pod api.Pod) (api.Pod, error) { func (c *Fake) CreatePod(pod api.Pod) (api.Pod, error) {
client.Actions = append(client.Actions, "create-pod") c.Actions = append(c.Actions, FakeAction{Action: "create-pod"})
return api.Pod{}, nil return api.Pod{}, nil
} }
func (client *FakeClient) UpdatePod(pod api.Pod) (api.Pod, error) { func (c *Fake) UpdatePod(pod api.Pod) (api.Pod, error) {
client.Actions = append(client.Actions, "update-pod") c.Actions = append(c.Actions, FakeAction{Action: "update-pod", Value: pod.ID})
return api.Pod{}, nil return api.Pod{}, nil
} }
func (client *FakeClient) ListReplicationControllers(selector labels.Selector) (api.ReplicationControllerList, error) { func (c *Fake) ListReplicationControllers(selector labels.Selector) (api.ReplicationControllerList, error) {
client.Actions = append(client.Actions, "list-controllers") c.Actions = append(c.Actions, FakeAction{Action: "list-controllers"})
return api.ReplicationControllerList{}, nil return api.ReplicationControllerList{}, nil
} }
func (client *FakeClient) GetReplicationController(name string) (api.ReplicationController, error) { func (c *Fake) GetReplicationController(name string) (api.ReplicationController, error) {
client.Actions = append(client.Actions, "get-controller") c.Actions = append(c.Actions, FakeAction{Action: "get-controller", Value: name})
return c.Ctrl, nil
}
func (c *Fake) CreateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
c.Actions = append(c.Actions, FakeAction{Action: "create-controller", Value: controller})
return api.ReplicationController{}, nil return api.ReplicationController{}, nil
} }
func (client *FakeClient) CreateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) { func (c *Fake) UpdateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
client.Actions = append(client.Actions, "create-controller") c.Actions = append(c.Actions, FakeAction{Action: "update-controller", Value: controller})
return api.ReplicationController{}, nil return api.ReplicationController{}, nil
} }
func (client *FakeClient) UpdateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) { func (c *Fake) DeleteReplicationController(controller string) error {
client.Actions = append(client.Actions, "update-controller") c.Actions = append(c.Actions, FakeAction{Action: "delete-controller", Value: controller})
return api.ReplicationController{}, nil
}
func (client *FakeClient) DeleteReplicationController(controller string) error {
client.Actions = append(client.Actions, "delete-controller")
return nil return nil
} }
func (client *FakeClient) WatchReplicationControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { func (c *Fake) WatchReplicationControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) {
client.Actions = append(client.Actions, "watch-controllers") c.Actions = append(c.Actions, FakeAction{Action: "watch-controllers"})
return watch.NewFake(), nil return watch.NewFake(), nil
} }
func (client *FakeClient) GetService(name string) (api.Service, error) { func (c *Fake) GetService(name string) (api.Service, error) {
client.Actions = append(client.Actions, "get-controller") c.Actions = append(c.Actions, FakeAction{Action: "get-service", Value: name})
return api.Service{}, nil return api.Service{}, nil
} }
func (client *FakeClient) CreateService(controller api.Service) (api.Service, error) { func (c *Fake) CreateService(service api.Service) (api.Service, error) {
client.Actions = append(client.Actions, "create-service") c.Actions = append(c.Actions, FakeAction{Action: "create-service", Value: service})
return api.Service{}, nil return api.Service{}, nil
} }
func (client *FakeClient) UpdateService(controller api.Service) (api.Service, error) { func (c *Fake) UpdateService(service api.Service) (api.Service, error) {
client.Actions = append(client.Actions, "update-service") c.Actions = append(c.Actions, FakeAction{Action: "update-service", Value: service})
return api.Service{}, nil return api.Service{}, nil
} }
func (client *FakeClient) DeleteService(controller string) error { func (c *Fake) DeleteService(service string) error {
client.Actions = append(client.Actions, "delete-service") c.Actions = append(c.Actions, FakeAction{Action: "delete-service", Value: service})
return nil return nil
} }

View File

@ -20,18 +20,18 @@ import (
"testing" "testing"
) )
// This test file just ensures that FakeClient and structs it is embedded in // This test file just ensures that Fake and structs it is embedded in
// implement Interface. // implement Interface.
func TestFakeImplementsInterface(t *testing.T) { func TestFakeImplementsInterface(t *testing.T) {
_ = Interface(&FakeClient{}) _ = Interface(&Fake{})
} }
type MyFake struct { type MyFake struct {
*FakeClient *Fake
} }
func TestEmbeddedFakeImplementsInterface(t *testing.T) { func TestEmbeddedFakeImplementsInterface(t *testing.T) {
_ = Interface(MyFake{&FakeClient{}}) _ = Interface(MyFake{&Fake{}})
_ = Interface(&MyFake{&FakeClient{}}) _ = Interface(&MyFake{&Fake{}})
} }

View File

@ -319,7 +319,7 @@ func TestSyncronize(t *testing.T) {
type FakeWatcher struct { type FakeWatcher struct {
w *watch.FakeWatcher w *watch.FakeWatcher
*client.FakeClient *client.Fake
} }
func (fw FakeWatcher) WatchReplicationControllers(l, f labels.Selector, rv uint64) (watch.Interface, error) { func (fw FakeWatcher) WatchReplicationControllers(l, f labels.Selector, rv uint64) (watch.Interface, error) {
@ -327,7 +327,7 @@ func (fw FakeWatcher) WatchReplicationControllers(l, f labels.Selector, rv uint6
} }
func TestWatchControllers(t *testing.T) { func TestWatchControllers(t *testing.T) {
client := FakeWatcher{watch.NewFake(), &client.FakeClient{}} client := FakeWatcher{watch.NewFake(), &client.Fake{}}
manager := MakeReplicationManager(client) manager := MakeReplicationManager(client)
var testControllerSpec api.ReplicationController var testControllerSpec api.ReplicationController
received := make(chan struct{}) received := make(chan struct{})

View File

@ -26,142 +26,54 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
) )
type Action struct { func validateAction(expectedAction, actualAction client.FakeAction, t *testing.T) {
action string
value interface{}
}
type FakeKubeClient struct {
actions []Action
pods api.PodList
ctrl api.ReplicationController
}
func (client *FakeKubeClient) ListPods(selector labels.Selector) (api.PodList, error) {
client.actions = append(client.actions, Action{action: "list-pods"})
return client.pods, nil
}
func (client *FakeKubeClient) GetPod(name string) (api.Pod, error) {
client.actions = append(client.actions, Action{action: "get-pod", value: name})
return api.Pod{}, nil
}
func (client *FakeKubeClient) DeletePod(name string) error {
client.actions = append(client.actions, Action{action: "delete-pod", value: name})
return nil
}
func (client *FakeKubeClient) CreatePod(pod api.Pod) (api.Pod, error) {
client.actions = append(client.actions, Action{action: "create-pod"})
return api.Pod{}, nil
}
func (client *FakeKubeClient) UpdatePod(pod api.Pod) (api.Pod, error) {
client.actions = append(client.actions, Action{action: "update-pod", value: pod.ID})
return api.Pod{}, nil
}
func (client *FakeKubeClient) ListReplicationControllers(selector labels.Selector) (api.ReplicationControllerList, error) {
client.actions = append(client.actions, Action{action: "list-controllers"})
return api.ReplicationControllerList{}, nil
}
func (client *FakeKubeClient) GetReplicationController(name string) (api.ReplicationController, error) {
client.actions = append(client.actions, Action{action: "get-controller", value: name})
return client.ctrl, nil
}
func (client *FakeKubeClient) CreateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
client.actions = append(client.actions, Action{action: "create-controller", value: controller})
return api.ReplicationController{}, nil
}
func (client *FakeKubeClient) UpdateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
client.actions = append(client.actions, Action{action: "update-controller", value: controller})
return api.ReplicationController{}, nil
}
func (client *FakeKubeClient) DeleteReplicationController(controller string) error {
client.actions = append(client.actions, Action{action: "delete-controller", value: controller})
return nil
}
func (client *FakeKubeClient) WatchReplicationControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) {
client.actions = append(client.actions, Action{action: "watch-controllers"})
return watch.NewFake(), nil
}
func (client *FakeKubeClient) GetService(name string) (api.Service, error) {
client.actions = append(client.actions, Action{action: "get-service", value: name})
return api.Service{}, nil
}
func (client *FakeKubeClient) CreateService(service api.Service) (api.Service, error) {
client.actions = append(client.actions, Action{action: "create-service", value: service})
return api.Service{}, nil
}
func (client *FakeKubeClient) UpdateService(service api.Service) (api.Service, error) {
client.actions = append(client.actions, Action{action: "update-service", value: service})
return api.Service{}, nil
}
func (client *FakeKubeClient) DeleteService(service string) error {
client.actions = append(client.actions, Action{action: "delete-service", value: service})
return nil
}
func validateAction(expectedAction, actualAction Action, t *testing.T) {
if expectedAction != actualAction { if expectedAction != actualAction {
t.Errorf("Unexpected action: %#v, expected: %#v", actualAction, expectedAction) t.Errorf("Unexpected Action: %#v, expected: %#v", actualAction, expectedAction)
} }
} }
func TestUpdateWithPods(t *testing.T) { func TestUpdateWithPods(t *testing.T) {
client := FakeKubeClient{ fakeClient := client.Fake{
pods: api.PodList{ Pods: api.PodList{
Items: []api.Pod{ Items: []api.Pod{
{JSONBase: api.JSONBase{ID: "pod-1"}}, {JSONBase: api.JSONBase{ID: "pod-1"}},
{JSONBase: api.JSONBase{ID: "pod-2"}}, {JSONBase: api.JSONBase{ID: "pod-2"}},
}, },
}, },
} }
Update("foo", &client, 0) Update("foo", &fakeClient, 0)
if len(client.actions) != 4 { if len(fakeClient.Actions) != 4 {
t.Errorf("Unexpected action list %#v", client.actions) t.Errorf("Unexpected action list %#v", fakeClient.Actions)
} }
validateAction(Action{action: "get-controller", value: "foo"}, client.actions[0], t) validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t)
validateAction(Action{action: "list-pods"}, client.actions[1], t) validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[1], t)
// Update deletes the pods, it relies on the replication controller to replace them. // Update deletes the pods, it relies on the replication controller to replace them.
validateAction(Action{action: "delete-pod", value: "pod-1"}, client.actions[2], t) validateAction(client.FakeAction{Action: "delete-pod", Value: "pod-1"}, fakeClient.Actions[2], t)
validateAction(Action{action: "delete-pod", value: "pod-2"}, client.actions[3], t) validateAction(client.FakeAction{Action: "delete-pod", Value: "pod-2"}, fakeClient.Actions[3], t)
} }
func TestUpdateNoPods(t *testing.T) { func TestUpdateNoPods(t *testing.T) {
client := FakeKubeClient{} fakeClient := client.Fake{}
Update("foo", &client, 0) Update("foo", &fakeClient, 0)
if len(client.actions) != 2 { if len(fakeClient.Actions) != 2 {
t.Errorf("Unexpected action list %#v", client.actions) t.Errorf("Unexpected action list %#v", fakeClient.Actions)
} }
validateAction(Action{action: "get-controller", value: "foo"}, client.actions[0], t) validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t)
validateAction(Action{action: "list-pods"}, client.actions[1], t) validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[1], t)
} }
func TestRunController(t *testing.T) { func TestRunController(t *testing.T) {
fakeClient := FakeKubeClient{} fakeClient := client.Fake{}
name := "name" name := "name"
image := "foo/bar" image := "foo/bar"
replicas := 3 replicas := 3
RunController(image, name, replicas, &fakeClient, "8080:80", -1) RunController(image, name, replicas, &fakeClient, "8080:80", -1)
if len(fakeClient.actions) != 1 || fakeClient.actions[0].action != "create-controller" { if len(fakeClient.Actions) != 1 || fakeClient.Actions[0].Action != "create-controller" {
t.Errorf("Unexpected actions: %#v", fakeClient.actions) t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
} }
controller := fakeClient.actions[0].value.(api.ReplicationController) controller := fakeClient.Actions[0].Value.(api.ReplicationController)
if controller.ID != name || if controller.ID != name ||
controller.DesiredState.Replicas != replicas || controller.DesiredState.Replicas != replicas ||
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image { controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
@ -170,17 +82,17 @@ func TestRunController(t *testing.T) {
} }
func TestRunControllerWithService(t *testing.T) { func TestRunControllerWithService(t *testing.T) {
fakeClient := FakeKubeClient{} fakeClient := client.Fake{}
name := "name" name := "name"
image := "foo/bar" image := "foo/bar"
replicas := 3 replicas := 3
RunController(image, name, replicas, &fakeClient, "", 8000) RunController(image, name, replicas, &fakeClient, "", 8000)
if len(fakeClient.actions) != 2 || if len(fakeClient.Actions) != 2 ||
fakeClient.actions[0].action != "create-controller" || fakeClient.Actions[0].Action != "create-controller" ||
fakeClient.actions[1].action != "create-service" { fakeClient.Actions[1].Action != "create-service" {
t.Errorf("Unexpected actions: %#v", fakeClient.actions) t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
} }
controller := fakeClient.actions[0].value.(api.ReplicationController) controller := fakeClient.Actions[0].Value.(api.ReplicationController)
if controller.ID != name || if controller.ID != name ||
controller.DesiredState.Replicas != replicas || controller.DesiredState.Replicas != replicas ||
controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image { controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image {
@ -189,65 +101,65 @@ func TestRunControllerWithService(t *testing.T) {
} }
func TestStopController(t *testing.T) { func TestStopController(t *testing.T) {
fakeClient := FakeKubeClient{} fakeClient := client.Fake{}
name := "name" name := "name"
StopController(name, &fakeClient) StopController(name, &fakeClient)
if len(fakeClient.actions) != 2 { if len(fakeClient.Actions) != 2 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions) t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
} }
if fakeClient.actions[0].action != "get-controller" || if fakeClient.Actions[0].Action != "get-controller" ||
fakeClient.actions[0].value.(string) != name { fakeClient.Actions[0].Value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0])
} }
controller := fakeClient.actions[1].value.(api.ReplicationController) controller := fakeClient.Actions[1].Value.(api.ReplicationController)
if fakeClient.actions[1].action != "update-controller" || if fakeClient.Actions[1].Action != "update-controller" ||
controller.DesiredState.Replicas != 0 { controller.DesiredState.Replicas != 0 {
t.Errorf("Unexpected action: %#v", fakeClient.actions[1]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[1])
} }
} }
func TestResizeController(t *testing.T) { func TestResizeController(t *testing.T) {
fakeClient := FakeKubeClient{} fakeClient := client.Fake{}
name := "name" name := "name"
replicas := 17 replicas := 17
ResizeController(name, replicas, &fakeClient) ResizeController(name, replicas, &fakeClient)
if len(fakeClient.actions) != 2 { if len(fakeClient.Actions) != 2 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions) t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
} }
if fakeClient.actions[0].action != "get-controller" || if fakeClient.Actions[0].Action != "get-controller" ||
fakeClient.actions[0].value.(string) != name { fakeClient.Actions[0].Value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0])
} }
controller := fakeClient.actions[1].value.(api.ReplicationController) controller := fakeClient.Actions[1].Value.(api.ReplicationController)
if fakeClient.actions[1].action != "update-controller" || if fakeClient.Actions[1].Action != "update-controller" ||
controller.DesiredState.Replicas != 17 { controller.DesiredState.Replicas != 17 {
t.Errorf("Unexpected action: %#v", fakeClient.actions[1]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[1])
} }
} }
func TestCloudCfgDeleteController(t *testing.T) { func TestCloudCfgDeleteController(t *testing.T) {
fakeClient := FakeKubeClient{} fakeClient := client.Fake{}
name := "name" name := "name"
err := DeleteController(name, &fakeClient) err := DeleteController(name, &fakeClient)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
if len(fakeClient.actions) != 2 { if len(fakeClient.Actions) != 2 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions) t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
} }
if fakeClient.actions[0].action != "get-controller" || if fakeClient.Actions[0].Action != "get-controller" ||
fakeClient.actions[0].value.(string) != name { fakeClient.Actions[0].Value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0])
} }
if fakeClient.actions[1].action != "delete-controller" || if fakeClient.Actions[1].Action != "delete-controller" ||
fakeClient.actions[1].value.(string) != name { fakeClient.Actions[1].Value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[1]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[1])
} }
} }
func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) { func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) {
fakeClient := FakeKubeClient{ fakeClient := client.Fake{
ctrl: api.ReplicationController{ Ctrl: api.ReplicationController{
DesiredState: api.ReplicationControllerState{ DesiredState: api.ReplicationControllerState{
Replicas: 2, Replicas: 2,
}, },
@ -255,12 +167,12 @@ func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) {
} }
name := "name" name := "name"
err := DeleteController(name, &fakeClient) err := DeleteController(name, &fakeClient)
if len(fakeClient.actions) != 1 { if len(fakeClient.Actions) != 1 {
t.Errorf("Unexpected actions: %#v", fakeClient.actions) t.Errorf("Unexpected actions: %#v", fakeClient.Actions)
} }
if fakeClient.actions[0].action != "get-controller" || if fakeClient.Actions[0].Action != "get-controller" ||
fakeClient.actions[0].value.(string) != name { fakeClient.Actions[0].Value.(string) != name {
t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0])
} }
if err == nil { if err == nil {
t.Errorf("Unexpected non-error.") t.Errorf("Unexpected non-error.")