From 8d0ed93aa11ce68a7c48ed71f1d1d4cce61dfa54 Mon Sep 17 00:00:00 2001 From: Dawn Chen Date: Fri, 3 Oct 2014 00:34:18 -0700 Subject: [PATCH] Clean unittests --- pkg/client/podinfo_test.go | 12 +++--------- pkg/kubelet/dockertools/fake_docker_client.go | 12 +++++++++++- pkg/kubelet/kubelet_test.go | 8 ++++---- pkg/kubelet/server_test.go | 5 +---- pkg/master/pod_cache_test.go | 13 +++---------- pkg/registry/pod/rest_test.go | 18 +++++++----------- 6 files changed, 29 insertions(+), 39 deletions(-) diff --git a/pkg/client/podinfo_test.go b/pkg/client/podinfo_test.go index 6282f55170e..b14231d9334 100644 --- a/pkg/client/podinfo_test.go +++ b/pkg/client/podinfo_test.go @@ -27,14 +27,11 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - "github.com/fsouza/go-dockerclient" ) func TestHTTPPodInfoGetter(t *testing.T) { expectObj := api.PodInfo{ - "myID": api.ContainerStatus{ - DetailInfo: docker.Container{ID: "myID"}, - }, + "myID": api.ContainerStatus{}, } body, err := json.Marshal(expectObj) if err != nil { @@ -69,17 +66,14 @@ func TestHTTPPodInfoGetter(t *testing.T) { } // reflect.DeepEqual(expectObj, gotObj) doesn't handle blank times well - if len(gotObj) != len(expectObj) || - expectObj["myID"].DetailInfo.ID != gotObj["myID"].DetailInfo.ID { + if len(gotObj) != len(expectObj) { t.Errorf("Unexpected response. Expected: %#v, received %#v", expectObj, gotObj) } } func TestHTTPPodInfoGetterNotFound(t *testing.T) { expectObj := api.PodInfo{ - "myID": api.ContainerStatus{ - DetailInfo: docker.Container{ID: "myID"}, - }, + "myID": api.ContainerStatus{}, } _, err := json.Marshal(expectObj) if err != nil { diff --git a/pkg/kubelet/dockertools/fake_docker_client.go b/pkg/kubelet/dockertools/fake_docker_client.go index 13d0a47cb34..dd2c601b881 100644 --- a/pkg/kubelet/dockertools/fake_docker_client.go +++ b/pkg/kubelet/dockertools/fake_docker_client.go @@ -29,6 +29,7 @@ type FakeDockerClient struct { sync.Mutex ContainerList []docker.APIContainers Container *docker.Container + Image *docker.Image Err error called []string Stopped []string @@ -67,10 +68,19 @@ func (f *FakeDockerClient) ListContainers(options docker.ListContainersOptions) func (f *FakeDockerClient) InspectContainer(id string) (*docker.Container, error) { f.Lock() defer f.Unlock() - f.called = append(f.called, "inspect") + f.called = append(f.called, "inspect_container") return f.Container, f.Err } +// InspectImage is a test-spy implementation of DockerInterface.InspectImage. +// It adds an entry "inspect" to the internal method call record. +func (f *FakeDockerClient) InspectImage(name string) (*docker.Image, error) { + f.Lock() + defer f.Unlock() + f.called = append(f.called, "inspect_image") + return f.Image, f.Err +} + // CreateContainer is a test-spy implementation of DockerInterface.CreateContainer. // It adds an entry "create" to the internal method call record. func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*docker.Container, error) { diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 0334f57d008..a5159f599ba 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -227,7 +227,7 @@ func TestSyncPodsCreatesNetAndContainer(t *testing.T) { kubelet.drainWorkers() verifyCalls(t, fakeDocker, []string{ - "list", "list", "create", "start", "list", "inspect", "list", "create", "start"}) + "list", "list", "create", "start", "list", "inspect_container", "list", "create", "start"}) fakeDocker.Lock() @@ -316,7 +316,7 @@ func TestSyncPodsWithNetCreatesContainer(t *testing.T) { kubelet.drainWorkers() verifyCalls(t, fakeDocker, []string{ - "list", "list", "list", "inspect", "list", "create", "start"}) + "list", "list", "list", "inspect_container", "list", "create", "start"}) fakeDocker.Lock() if len(fakeDocker.Created) != 1 || @@ -366,7 +366,7 @@ func TestSyncPodsWithNetCreatesContainerCallsHandler(t *testing.T) { kubelet.drainWorkers() verifyCalls(t, fakeDocker, []string{ - "list", "list", "list", "inspect", "list", "create", "start"}) + "list", "list", "list", "inspect_container", "list", "create", "start"}) fakeDocker.Lock() if len(fakeDocker.Created) != 1 || @@ -406,7 +406,7 @@ func TestSyncPodsDeletesWithNoNetContainer(t *testing.T) { kubelet.drainWorkers() verifyCalls(t, fakeDocker, []string{ - "list", "list", "stop", "create", "start", "list", "list", "inspect", "list", "create", "start"}) + "list", "list", "stop", "create", "start", "list", "list", "inspect_container", "list", "create", "start"}) // A map iteration is used to delete containers, so must not depend on // order here. diff --git a/pkg/kubelet/server_test.go b/pkg/kubelet/server_test.go index 6c137c98cc8..4a6aa2fd24d 100644 --- a/pkg/kubelet/server_test.go +++ b/pkg/kubelet/server_test.go @@ -31,7 +31,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - "github.com/fsouza/go-dockerclient" "github.com/google/cadvisor/info" ) @@ -147,9 +146,7 @@ func TestContainers(t *testing.T) { func TestPodInfo(t *testing.T) { fw := newServerTest() expected := api.PodInfo{ - "goodpod": api.ContainerStatus{ - DetailInfo: docker.Container{ID: "myContainerID"}, - }, + "goodpod": api.ContainerStatus{}, } fw.fakeKubelet.infoFunc = func(name string) (api.PodInfo, error) { if name == "goodpod.etcd" { diff --git a/pkg/master/pod_cache_test.go b/pkg/master/pod_cache_test.go index 654b0bef741..0febfb4ce20 100644 --- a/pkg/master/pod_cache_test.go +++ b/pkg/master/pod_cache_test.go @@ -22,7 +22,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest" - "github.com/fsouza/go-dockerclient" ) type FakePodInfoGetter struct { @@ -42,9 +41,7 @@ func TestPodCacheGet(t *testing.T) { cache := NewPodCache(nil, nil) expected := api.PodInfo{ - "foo": api.ContainerStatus{ - DetailInfo: docker.Container{ID: "foo"}, - }, + "foo": api.ContainerStatus{}, } cache.podInfo["foo"] = expected @@ -71,9 +68,7 @@ func TestPodCacheGetMissing(t *testing.T) { func TestPodGetPodInfoGetter(t *testing.T) { expected := api.PodInfo{ - "foo": api.ContainerStatus{ - DetailInfo: docker.Container{ID: "foo"}, - }, + "foo": api.ContainerStatus{}, } fake := FakePodInfoGetter{ data: expected, @@ -107,9 +102,7 @@ func TestPodUpdateAllContainers(t *testing.T) { mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods}) expected := api.PodInfo{ - "foo": api.ContainerStatus{ - DetailInfo: docker.Container{ID: "foo"}, - }, + "foo": api.ContainerStatus{}, } fake := FakePodInfoGetter{ data: expected, diff --git a/pkg/registry/pod/rest_test.go b/pkg/registry/pod/rest_test.go index b7af44aa5f6..c255208c79e 100644 --- a/pkg/registry/pod/rest_test.go +++ b/pkg/registry/pod/rest_test.go @@ -31,8 +31,6 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - - "github.com/fsouza/go-dockerclient" ) func expectApiStatusError(t *testing.T, ch <-chan runtime.Object, msg string) { @@ -605,16 +603,17 @@ func (f *FakePodInfoGetter) GetPodInfo(host, podID string) (api.PodInfo, error) func TestFillPodInfo(t *testing.T) { expectedIP := "1.2.3.4" + expectedTime, _ := time.Parse("2013-Feb-03", "2013-Feb-03") fakeGetter := FakePodInfoGetter{ info: map[string]api.ContainerStatus{ "net": { - DetailInfo: docker.Container{ - ID: "foobar", - Path: "bin/run.sh", - NetworkSettings: &docker.NetworkSettings{ - IPAddress: expectedIP, + State: api.ContainerState{ + Running: &api.ContainerStateRunning{ + StartedAt: expectedTime, }, }, + RestartCount: 1, + PodIP: expectedIP, }, }, } @@ -636,10 +635,7 @@ func TestFillPodInfoNoData(t *testing.T) { fakeGetter := FakePodInfoGetter{ info: map[string]api.ContainerStatus{ "net": { - DetailInfo: docker.Container{ - ID: "foobar", - Path: "bin/run.sh", - }, + State: api.ContainerState{}, }, }, }