diff --git a/pkg/client/fake.go b/pkg/client/fake.go index e6f61a11c8c..7a20ffa2dce 100644 --- a/pkg/client/fake.go +++ b/pkg/client/fake.go @@ -22,84 +22,91 @@ import ( "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. -type FakeClient struct { - // FakeClient by default keeps a simple list of the methods that have been called. - Actions []string +type Fake struct { + // Fake by default keeps a simple list of the methods that have been called. + Actions []FakeAction + Pods api.PodList + Ctrl api.ReplicationController } -func (client *FakeClient) ListPods(selector labels.Selector) (api.PodList, error) { - client.Actions = append(client.Actions, "list-pods") - return api.PodList{}, nil +func (c *Fake) ListPods(selector labels.Selector) (api.PodList, error) { + c.Actions = append(c.Actions, FakeAction{Action: "list-pods"}) + return c.Pods, nil } -func (client *FakeClient) GetPod(name string) (api.Pod, error) { - client.Actions = append(client.Actions, "get-pod") +func (c *Fake) GetPod(name string) (api.Pod, error) { + c.Actions = append(c.Actions, FakeAction{Action: "get-pod", Value: name}) return api.Pod{}, nil } -func (client *FakeClient) DeletePod(name string) error { - client.Actions = append(client.Actions, "delete-pod") +func (c *Fake) DeletePod(name string) error { + c.Actions = append(c.Actions, FakeAction{Action: "delete-pod", Value: name}) return nil } -func (client *FakeClient) CreatePod(pod api.Pod) (api.Pod, error) { - client.Actions = append(client.Actions, "create-pod") +func (c *Fake) CreatePod(pod api.Pod) (api.Pod, error) { + c.Actions = append(c.Actions, FakeAction{Action: "create-pod"}) return api.Pod{}, nil } -func (client *FakeClient) UpdatePod(pod api.Pod) (api.Pod, error) { - client.Actions = append(client.Actions, "update-pod") +func (c *Fake) UpdatePod(pod api.Pod) (api.Pod, error) { + c.Actions = append(c.Actions, FakeAction{Action: "update-pod", Value: pod.ID}) return api.Pod{}, nil } -func (client *FakeClient) ListReplicationControllers(selector labels.Selector) (api.ReplicationControllerList, error) { - client.Actions = append(client.Actions, "list-controllers") +func (c *Fake) ListReplicationControllers(selector labels.Selector) (api.ReplicationControllerList, error) { + c.Actions = append(c.Actions, FakeAction{Action: "list-controllers"}) return api.ReplicationControllerList{}, nil } -func (client *FakeClient) GetReplicationController(name string) (api.ReplicationController, error) { - client.Actions = append(client.Actions, "get-controller") +func (c *Fake) GetReplicationController(name string) (api.ReplicationController, error) { + 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 } -func (client *FakeClient) CreateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) { - client.Actions = append(client.Actions, "create-controller") +func (c *Fake) UpdateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) { + c.Actions = append(c.Actions, FakeAction{Action: "update-controller", Value: controller}) return api.ReplicationController{}, nil } -func (client *FakeClient) UpdateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) { - client.Actions = append(client.Actions, "update-controller") - return api.ReplicationController{}, nil -} - -func (client *FakeClient) DeleteReplicationController(controller string) error { - client.Actions = append(client.Actions, "delete-controller") +func (c *Fake) DeleteReplicationController(controller string) error { + c.Actions = append(c.Actions, FakeAction{Action: "delete-controller", Value: controller}) return nil } -func (client *FakeClient) WatchReplicationControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { - client.Actions = append(client.Actions, "watch-controllers") +func (c *Fake) WatchReplicationControllers(label, field labels.Selector, resourceVersion uint64) (watch.Interface, error) { + c.Actions = append(c.Actions, FakeAction{Action: "watch-controllers"}) return watch.NewFake(), nil } -func (client *FakeClient) GetService(name string) (api.Service, error) { - client.Actions = append(client.Actions, "get-controller") +func (c *Fake) GetService(name string) (api.Service, error) { + c.Actions = append(c.Actions, FakeAction{Action: "get-service", Value: name}) return api.Service{}, nil } -func (client *FakeClient) CreateService(controller api.Service) (api.Service, error) { - client.Actions = append(client.Actions, "create-service") +func (c *Fake) CreateService(service api.Service) (api.Service, error) { + c.Actions = append(c.Actions, FakeAction{Action: "create-service", Value: service}) return api.Service{}, nil } -func (client *FakeClient) UpdateService(controller api.Service) (api.Service, error) { - client.Actions = append(client.Actions, "update-service") +func (c *Fake) UpdateService(service api.Service) (api.Service, error) { + c.Actions = append(c.Actions, FakeAction{Action: "update-service", Value: service}) return api.Service{}, nil } -func (client *FakeClient) DeleteService(controller string) error { - client.Actions = append(client.Actions, "delete-service") +func (c *Fake) DeleteService(service string) error { + c.Actions = append(c.Actions, FakeAction{Action: "delete-service", Value: service}) return nil } diff --git a/pkg/client/fake_test.go b/pkg/client/fake_test.go index 9c739e24142..4c820df1ec5 100644 --- a/pkg/client/fake_test.go +++ b/pkg/client/fake_test.go @@ -20,18 +20,18 @@ import ( "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. func TestFakeImplementsInterface(t *testing.T) { - _ = Interface(&FakeClient{}) + _ = Interface(&Fake{}) } type MyFake struct { - *FakeClient + *Fake } func TestEmbeddedFakeImplementsInterface(t *testing.T) { - _ = Interface(MyFake{&FakeClient{}}) - _ = Interface(&MyFake{&FakeClient{}}) + _ = Interface(MyFake{&Fake{}}) + _ = Interface(&MyFake{&Fake{}}) } diff --git a/pkg/controller/replication_controller_test.go b/pkg/controller/replication_controller_test.go index dcbab8b0af1..19f996f3779 100644 --- a/pkg/controller/replication_controller_test.go +++ b/pkg/controller/replication_controller_test.go @@ -319,7 +319,7 @@ func TestSyncronize(t *testing.T) { type FakeWatcher struct { w *watch.FakeWatcher - *client.FakeClient + *client.Fake } 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) { - client := FakeWatcher{watch.NewFake(), &client.FakeClient{}} + client := FakeWatcher{watch.NewFake(), &client.Fake{}} manager := MakeReplicationManager(client) var testControllerSpec api.ReplicationController received := make(chan struct{}) diff --git a/pkg/kubecfg/kubecfg_test.go b/pkg/kubecfg/kubecfg_test.go index b1bc55c0661..7967c8b9b17 100644 --- a/pkg/kubecfg/kubecfg_test.go +++ b/pkg/kubecfg/kubecfg_test.go @@ -26,142 +26,54 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" - "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" - "github.com/GoogleCloudPlatform/kubernetes/pkg/watch" ) -type Action struct { - 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) { +func validateAction(expectedAction, actualAction client.FakeAction, t *testing.T) { 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) { - client := FakeKubeClient{ - pods: api.PodList{ + fakeClient := client.Fake{ + Pods: api.PodList{ Items: []api.Pod{ {JSONBase: api.JSONBase{ID: "pod-1"}}, {JSONBase: api.JSONBase{ID: "pod-2"}}, }, }, } - Update("foo", &client, 0) - if len(client.actions) != 4 { - t.Errorf("Unexpected action list %#v", client.actions) + Update("foo", &fakeClient, 0) + if len(fakeClient.Actions) != 4 { + t.Errorf("Unexpected action list %#v", fakeClient.Actions) } - validateAction(Action{action: "get-controller", value: "foo"}, client.actions[0], t) - validateAction(Action{action: "list-pods"}, client.actions[1], t) + validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t) + validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[1], t) // 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(Action{action: "delete-pod", value: "pod-2"}, client.actions[3], t) + validateAction(client.FakeAction{Action: "delete-pod", Value: "pod-1"}, fakeClient.Actions[2], t) + validateAction(client.FakeAction{Action: "delete-pod", Value: "pod-2"}, fakeClient.Actions[3], t) } func TestUpdateNoPods(t *testing.T) { - client := FakeKubeClient{} - Update("foo", &client, 0) - if len(client.actions) != 2 { - t.Errorf("Unexpected action list %#v", client.actions) + fakeClient := client.Fake{} + Update("foo", &fakeClient, 0) + if len(fakeClient.Actions) != 2 { + t.Errorf("Unexpected action list %#v", fakeClient.Actions) } - validateAction(Action{action: "get-controller", value: "foo"}, client.actions[0], t) - validateAction(Action{action: "list-pods"}, client.actions[1], t) + validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t) + validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[1], t) } func TestRunController(t *testing.T) { - fakeClient := FakeKubeClient{} + fakeClient := client.Fake{} name := "name" image := "foo/bar" replicas := 3 RunController(image, name, replicas, &fakeClient, "8080:80", -1) - if len(fakeClient.actions) != 1 || fakeClient.actions[0].action != "create-controller" { - t.Errorf("Unexpected actions: %#v", fakeClient.actions) + if len(fakeClient.Actions) != 1 || fakeClient.Actions[0].Action != "create-controller" { + 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 || controller.DesiredState.Replicas != replicas || controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image { @@ -170,17 +82,17 @@ func TestRunController(t *testing.T) { } func TestRunControllerWithService(t *testing.T) { - fakeClient := FakeKubeClient{} + fakeClient := client.Fake{} name := "name" image := "foo/bar" replicas := 3 RunController(image, name, replicas, &fakeClient, "", 8000) - if len(fakeClient.actions) != 2 || - fakeClient.actions[0].action != "create-controller" || - fakeClient.actions[1].action != "create-service" { - t.Errorf("Unexpected actions: %#v", fakeClient.actions) + if len(fakeClient.Actions) != 2 || + fakeClient.Actions[0].Action != "create-controller" || + fakeClient.Actions[1].Action != "create-service" { + 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 || controller.DesiredState.Replicas != replicas || controller.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image != image { @@ -189,65 +101,65 @@ func TestRunControllerWithService(t *testing.T) { } func TestStopController(t *testing.T) { - fakeClient := FakeKubeClient{} + fakeClient := client.Fake{} name := "name" StopController(name, &fakeClient) - if len(fakeClient.actions) != 2 { - t.Errorf("Unexpected actions: %#v", fakeClient.actions) + if len(fakeClient.Actions) != 2 { + t.Errorf("Unexpected actions: %#v", fakeClient.Actions) } - if fakeClient.actions[0].action != "get-controller" || - fakeClient.actions[0].value.(string) != name { - t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) + if fakeClient.Actions[0].Action != "get-controller" || + fakeClient.Actions[0].Value.(string) != name { + t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0]) } - controller := fakeClient.actions[1].value.(api.ReplicationController) - if fakeClient.actions[1].action != "update-controller" || + controller := fakeClient.Actions[1].Value.(api.ReplicationController) + if fakeClient.Actions[1].Action != "update-controller" || 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) { - fakeClient := FakeKubeClient{} + fakeClient := client.Fake{} name := "name" replicas := 17 ResizeController(name, replicas, &fakeClient) - if len(fakeClient.actions) != 2 { - t.Errorf("Unexpected actions: %#v", fakeClient.actions) + if len(fakeClient.Actions) != 2 { + t.Errorf("Unexpected actions: %#v", fakeClient.Actions) } - if fakeClient.actions[0].action != "get-controller" || - fakeClient.actions[0].value.(string) != name { - t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) + if fakeClient.Actions[0].Action != "get-controller" || + fakeClient.Actions[0].Value.(string) != name { + t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0]) } - controller := fakeClient.actions[1].value.(api.ReplicationController) - if fakeClient.actions[1].action != "update-controller" || + controller := fakeClient.Actions[1].Value.(api.ReplicationController) + if fakeClient.Actions[1].Action != "update-controller" || 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) { - fakeClient := FakeKubeClient{} + fakeClient := client.Fake{} name := "name" err := DeleteController(name, &fakeClient) if err != nil { t.Errorf("Unexpected error: %v", err) } - if len(fakeClient.actions) != 2 { - t.Errorf("Unexpected actions: %#v", fakeClient.actions) + if len(fakeClient.Actions) != 2 { + t.Errorf("Unexpected actions: %#v", fakeClient.Actions) } - if fakeClient.actions[0].action != "get-controller" || - fakeClient.actions[0].value.(string) != name { - t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) + if fakeClient.Actions[0].Action != "get-controller" || + fakeClient.Actions[0].Value.(string) != name { + t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0]) } - if fakeClient.actions[1].action != "delete-controller" || - fakeClient.actions[1].value.(string) != name { - t.Errorf("Unexpected action: %#v", fakeClient.actions[1]) + if fakeClient.Actions[1].Action != "delete-controller" || + fakeClient.Actions[1].Value.(string) != name { + t.Errorf("Unexpected Action: %#v", fakeClient.Actions[1]) } } func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) { - fakeClient := FakeKubeClient{ - ctrl: api.ReplicationController{ + fakeClient := client.Fake{ + Ctrl: api.ReplicationController{ DesiredState: api.ReplicationControllerState{ Replicas: 2, }, @@ -255,12 +167,12 @@ func TestCloudCfgDeleteControllerWithReplicas(t *testing.T) { } name := "name" err := DeleteController(name, &fakeClient) - if len(fakeClient.actions) != 1 { - t.Errorf("Unexpected actions: %#v", fakeClient.actions) + if len(fakeClient.Actions) != 1 { + t.Errorf("Unexpected actions: %#v", fakeClient.Actions) } - if fakeClient.actions[0].action != "get-controller" || - fakeClient.actions[0].value.(string) != name { - t.Errorf("Unexpected action: %#v", fakeClient.actions[0]) + if fakeClient.Actions[0].Action != "get-controller" || + fakeClient.Actions[0].Value.(string) != name { + t.Errorf("Unexpected Action: %#v", fakeClient.Actions[0]) } if err == nil { t.Errorf("Unexpected non-error.")