From d4034f9c436d652f2b20676e2c6722db506df4ff Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Mon, 1 Jun 2015 12:30:51 -0700 Subject: [PATCH 1/4] Move more docker-specific tests to dockertools/manager_test.go --- pkg/kubelet/dockertools/manager_test.go | 137 ++++++++++++++++- pkg/kubelet/kubelet_test.go | 193 ++---------------------- 2 files changed, 145 insertions(+), 185 deletions(-) diff --git a/pkg/kubelet/dockertools/manager_test.go b/pkg/kubelet/dockertools/manager_test.go index 38b89301915..617faad5283 100644 --- a/pkg/kubelet/dockertools/manager_test.go +++ b/pkg/kubelet/dockertools/manager_test.go @@ -88,7 +88,7 @@ func (*fakeOptionGenerator) GenerateRunContainerOptions(pod *api.Pod, container return &kubecontainer.RunContainerOptions{}, nil } -func newTestDockerManager() (*DockerManager, *FakeDockerClient) { +func newTestDockerManagerWithHTTPClient(fakeHTTPClient *fakeHTTP) (*DockerManager, *FakeDockerClient) { fakeDocker := &FakeDockerClient{VersionInfo: docker.Env{"Version=1.1.3", "ApiVersion=1.15"}, Errors: make(map[string]error), RemovedImages: util.StringSet{}} fakeRecorder := &record.FakeRecorder{} readinessManager := kubecontainer.NewReadinessManager() @@ -106,12 +106,16 @@ func newTestDockerManager() (*DockerManager, *FakeDockerClient) { kubecontainer.FakeOS{}, networkPlugin, optionGenerator, - &fakeHTTP{}, + fakeHTTPClient, runtimeHooks) return dockerManager, fakeDocker } +func newTestDockerManager() (*DockerManager, *FakeDockerClient) { + return newTestDockerManagerWithHTTPClient(&fakeHTTP{}) +} + func matchString(t *testing.T, pattern, str string) bool { match, err := regexp.MatchString(pattern, str) if err != nil { @@ -1779,3 +1783,132 @@ func TestGetRestartCount(t *testing.T) { fakeDocker.ExitedContainerList = []docker.APIContainers{} verifyRestartCount(&pod, 2) } + +func TestSyncPodWithPodInfraCreatesContainerCallsHandler(t *testing.T) { + fakeHTTPClient := &fakeHTTP{} + dm, fakeDocker := newTestDockerManagerWithHTTPClient(fakeHTTPClient) + + pod := &api.Pod{ + ObjectMeta: api.ObjectMeta{ + UID: "12345678", + Name: "foo", + Namespace: "new", + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + { + Name: "bar", + Lifecycle: &api.Lifecycle{ + PostStart: &api.Handler{ + HTTPGet: &api.HTTPGetAction{ + Host: "foo", + Port: util.IntOrString{IntVal: 8080, Kind: util.IntstrInt}, + Path: "bar", + }, + }, + }, + }, + }, + }, + } + fakeDocker.ContainerList = []docker.APIContainers{ + { + // pod infra container + Names: []string{"/k8s_POD." + strconv.FormatUint(generatePodInfraContainerHash(pod), 16) + "_foo_new_12345678_0"}, + ID: "9876", + }, + } + fakeDocker.ContainerMap = map[string]*docker.Container{ + "9876": { + ID: "9876", + Config: &docker.Config{}, + HostConfig: &docker.HostConfig{}, + }, + } + + runSyncPod(t, dm, fakeDocker, pod) + + verifyCalls(t, fakeDocker, []string{ + // Check the pod infra container. + "inspect_container", + // Create container. + "create", "start", + }) + + fakeDocker.Lock() + if len(fakeDocker.Created) != 1 || + !matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) { + t.Errorf("Unexpected containers created %v", fakeDocker.Created) + } + fakeDocker.Unlock() + if fakeHTTPClient.url != "http://foo:8080/bar" { + t.Errorf("Unexpected handler: %q", fakeHTTPClient.url) + } +} + +func TestSyncPodEventHandlerFails(t *testing.T) { + // Simulate HTTP failure. + fakeHTTPClient := &fakeHTTP{err: fmt.Errorf("test error")} + dm, fakeDocker := newTestDockerManagerWithHTTPClient(fakeHTTPClient) + + pod := &api.Pod{ + ObjectMeta: api.ObjectMeta{ + UID: "12345678", + Name: "foo", + Namespace: "new", + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + {Name: "bar", + Lifecycle: &api.Lifecycle{ + PostStart: &api.Handler{ + HTTPGet: &api.HTTPGetAction{ + Host: "does.no.exist", + Port: util.IntOrString{IntVal: 8080, Kind: util.IntstrInt}, + Path: "bar", + }, + }, + }, + }, + }, + }, + } + + fakeDocker.ContainerList = []docker.APIContainers{ + { + // pod infra container + Names: []string{"/k8s_POD." + strconv.FormatUint(generatePodInfraContainerHash(pod), 16) + "_foo_new_12345678_42"}, + ID: "9876", + }, + } + fakeDocker.ContainerMap = map[string]*docker.Container{ + "9876": { + ID: "9876", + Config: &docker.Config{}, + HostConfig: &docker.HostConfig{}, + }, + } + + runSyncPod(t, dm, fakeDocker, pod) + + verifyCalls(t, fakeDocker, []string{ + // Check the pod infra container. + "inspect_container", + // Create the container. + "create", "start", + // Kill the container since event handler fails. + "inspect_container", "stop", + }) + + // TODO(yifan): Check the stopped container's name. + if len(fakeDocker.Stopped) != 1 { + t.Fatalf("Wrong containers were stopped: %v", fakeDocker.Stopped) + } + dockerName, _, err := ParseDockerName(fakeDocker.Stopped[0]) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + if dockerName.ContainerName != "bar" { + t.Errorf("Wrong stopped container, expected: bar, get: %q", dockerName.ContainerName) + } +} diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index f55f69e453b..bfd7cb10898 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -73,6 +73,16 @@ type TestKubelet struct { const testKubeletHostname = "127.0.0.1" +type fakeHTTP struct { + url string + err error +} + +func (f *fakeHTTP) Get(url string) (*http.Response, error) { + f.url = url + return nil, f.err +} + func newTestKubelet(t *testing.T) *TestKubelet { fakeDocker := &dockertools.FakeDockerClient{Errors: make(map[string]error), RemovedImages: util.StringSet{}} fakeDocker.VersionInfo = []string{"ApiVersion=1.15"} @@ -530,100 +540,6 @@ func TestSyncPodsStartPod(t *testing.T) { fakeRuntime.AssertStartedPods([]string{string(pods[0].UID)}) } -type fakeHTTP struct { - url string - err error -} - -func (f *fakeHTTP) Get(url string) (*http.Response, error) { - f.url = url - return nil, f.err -} - -func TestSyncPodsWithPodInfraCreatesContainerCallsHandler(t *testing.T) { - testKubelet := newTestKubelet(t) - testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) - testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) - testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) - kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker - fakeHttp := fakeHTTP{} - - // Simulate HTTP failure. Re-create the containerRuntime to inject the failure. - kubelet.httpClient = &fakeHttp - runtimeHooks := newKubeletRuntimeHooks(kubelet.recorder) - kubelet.containerRuntime = dockertools.NewFakeDockerManager(kubelet.dockerClient, kubelet.recorder, kubelet.readinessManager, kubelet.containerRefManager, dockertools.PodInfraContainerImage, 0, 0, "", kubelet.os, kubelet.networkPlugin, kubelet, kubelet.httpClient, runtimeHooks) - - pods := []*api.Pod{ - { - ObjectMeta: api.ObjectMeta{ - UID: "12345678", - Name: "foo", - Namespace: "new", - }, - Spec: api.PodSpec{ - Containers: []api.Container{ - { - Name: "bar", - Lifecycle: &api.Lifecycle{ - PostStart: &api.Handler{ - HTTPGet: &api.HTTPGetAction{ - Host: "foo", - Port: util.IntOrString{IntVal: 8080, Kind: util.IntstrInt}, - Path: "bar", - }, - }, - }, - }, - }, - }, - }, - } - fakeDocker.ContainerList = []docker.APIContainers{ - { - // pod infra container - Names: []string{"/k8s_POD." + strconv.FormatUint(generatePodInfraContainerHash(pods[0]), 16) + "_foo_new_12345678_0"}, - ID: "9876", - }, - } - fakeDocker.ContainerMap = map[string]*docker.Container{ - "9876": { - ID: "9876", - Config: &docker.Config{}, - HostConfig: &docker.HostConfig{}, - }, - } - kubelet.podManager.SetPods(pods) - err := kubelet.SyncPods(pods, emptyPodUIDs, map[string]*api.Pod{}, time.Now()) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - - verifyCalls(t, fakeDocker, []string{ - "list", "list", - // Get pod status. - "list", "inspect_container", "inspect_image", - // Check the pod infra container. - "inspect_container", - // Create container. - "create", "start", - // Get pod status. - "list", "inspect_container", "inspect_container", - // Get pods for deleting orphaned volumes. - "list", - }) - - fakeDocker.Lock() - if len(fakeDocker.Created) != 1 || - !matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) { - t.Errorf("Unexpected containers created %v", fakeDocker.Created) - } - fakeDocker.Unlock() - if fakeHttp.url != "http://foo:8080/bar" { - t.Errorf("Unexpected handler: %q", fakeHttp.url) - } -} - func TestSyncPodsDeletesWhenSourcesAreReady(t *testing.T) { ready := false @@ -1086,95 +1002,6 @@ func TestRunInContainer(t *testing.T) { } } -func TestSyncPodEventHandlerFails(t *testing.T) { - testKubelet := newTestKubelet(t) - testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) - testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) - testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) - kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker - - // Simulate HTTP failure. Re-create the containerRuntime to inject the failure. - kubelet.httpClient = &fakeHTTP{ - err: fmt.Errorf("test error"), - } - runtimeHooks := newKubeletRuntimeHooks(kubelet.recorder) - kubelet.containerRuntime = dockertools.NewFakeDockerManager(kubelet.dockerClient, kubelet.recorder, kubelet.readinessManager, kubelet.containerRefManager, dockertools.PodInfraContainerImage, 0, 0, "", kubelet.os, kubelet.networkPlugin, kubelet, kubelet.httpClient, runtimeHooks) - - pods := []*api.Pod{ - { - ObjectMeta: api.ObjectMeta{ - UID: "12345678", - Name: "foo", - Namespace: "new", - }, - Spec: api.PodSpec{ - Containers: []api.Container{ - {Name: "bar", - Lifecycle: &api.Lifecycle{ - PostStart: &api.Handler{ - HTTPGet: &api.HTTPGetAction{ - Host: "does.no.exist", - Port: util.IntOrString{IntVal: 8080, Kind: util.IntstrInt}, - Path: "bar", - }, - }, - }, - }, - }, - }, - }, - } - - fakeDocker.ContainerList = []docker.APIContainers{ - { - // pod infra container - Names: []string{"/k8s_POD." + strconv.FormatUint(generatePodInfraContainerHash(pods[0]), 16) + "_foo_new_12345678_42"}, - ID: "9876", - }, - } - fakeDocker.ContainerMap = map[string]*docker.Container{ - "9876": { - ID: "9876", - Config: &docker.Config{}, - HostConfig: &docker.HostConfig{}, - }, - } - kubelet.podManager.SetPods(pods) - err := kubelet.SyncPods(pods, emptyPodUIDs, map[string]*api.Pod{}, time.Now()) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - - verifyCalls(t, fakeDocker, []string{ - "list", "list", - // Get pod status. - "list", "inspect_container", "inspect_image", - // Check the pod infra container. - "inspect_container", - // Create the container. - "create", "start", - // Kill the container since event handler fails. - "inspect_container", "stop", - // Get pod status. - "list", "inspect_container", "inspect_container", - // Get pods for deleting orphaned volumes. - "list", - }) - - // TODO(yifan): Check the stopped container's name. - if len(fakeDocker.Stopped) != 1 { - t.Fatalf("Wrong containers were stopped: %v", fakeDocker.Stopped) - } - dockerName, _, err := dockertools.ParseDockerName(fakeDocker.Stopped[0]) - if err != nil { - t.Errorf("Unexpected error: %v", err) - } - if dockerName.ContainerName != "bar" { - t.Errorf("Wrong stopped container, expected: bar, get: %q", dockerName.ContainerName) - } -} - func TestParseResolvConf(t *testing.T) { testCases := []struct { data string From b3197bb49b92ee437059799a441a9ced48135217 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Mon, 1 Jun 2015 12:38:07 -0700 Subject: [PATCH 2/4] Switch more tests from newTestKubelet to newTestKubeletWithFakeRuntime --- pkg/kubelet/kubelet_test.go | 62 ++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index bfd7cb10898..2582acd3e50 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -28,7 +28,6 @@ import ( "reflect" "regexp" "sort" - "strconv" "strings" "testing" "time" @@ -83,6 +82,7 @@ func (f *fakeHTTP) Get(url string) (*http.Response, error) { return nil, f.err } +// TODO: Depcreated. Please use newTestKubeletWithFakeRuntime instead. func newTestKubelet(t *testing.T) *TestKubelet { fakeDocker := &dockertools.FakeDockerClient{Errors: make(map[string]error), RemovedImages: util.StringSet{}} fakeDocker.VersionInfo = []string{"ApiVersion=1.15"} @@ -261,7 +261,7 @@ func newTestPods(count int) []*api.Pod { } func TestKubeletDirs(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet root := kubelet.rootDirectory @@ -323,7 +323,7 @@ func TestKubeletDirs(t *testing.T) { } func TestKubeletDirsCompat(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet root := kubelet.rootDirectory if err := os.MkdirAll(root, 0750); err != nil { @@ -570,7 +570,7 @@ func TestSyncPodsDeletesWhenSourcesAreReady(t *testing.T) { } func TestMountExternalVolumes(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet kubelet.volumePluginMgr.InitPlugins([]volume.VolumePlugin{&volume.FakeVolumePlugin{"fake", nil}}, &volumeHost{kubelet}) @@ -605,7 +605,7 @@ func TestMountExternalVolumes(t *testing.T) { } func TestGetPodVolumesFromDisk(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet plug := &volume.FakeVolumePlugin{"fake", nil} kubelet.volumePluginMgr.InitPlugins([]volume.VolumePlugin{plug}, &volumeHost{kubelet}) @@ -1499,7 +1499,7 @@ func TestMakeEnvironmentVariables(t *testing.T) { } for i, tc := range testCases { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kl := testKubelet.kubelet kl.masterServiceNamespace = tc.masterServiceNs if tc.nilLister { @@ -2208,7 +2208,7 @@ func TestGetHostPortConflicts(t *testing.T) { // Tests that we handle port conflicts correctly by setting the failed status in status map. func TestHandlePortConflicts(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kl := testKubelet.kubelet testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -2252,7 +2252,7 @@ func TestHandlePortConflicts(t *testing.T) { // Tests that we handle not matching labels selector correctly by setting the failed status in status map. func TestHandleNodeSelector(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kl := testKubelet.kubelet kl.nodeLister = testNodeLister{nodes: []api.Node{ {ObjectMeta: api.ObjectMeta{Name: testKubeletHostname, Labels: map[string]string{"key": "B"}}}, @@ -2294,7 +2294,7 @@ func TestHandleNodeSelector(t *testing.T) { // Tests that we handle exceeded resources correctly by setting the failed status in status map. func TestHandleMemExceeded(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kl := testKubelet.kubelet testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{MemoryCapacity: 100}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -2342,7 +2342,7 @@ func TestHandleMemExceeded(t *testing.T) { // TODO(filipg): This test should be removed once StatusSyncer can do garbage collection without external signal. func TestPurgingObsoleteStatusMapEntries(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -2365,7 +2365,7 @@ func TestPurgingObsoleteStatusMapEntries(t *testing.T) { } func TestValidatePodStatus(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet testCases := []struct { podPhase api.PodPhase @@ -2391,7 +2391,7 @@ func TestValidatePodStatus(t *testing.T) { } func TestValidateContainerStatus(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet containerName := "x" testCases := []struct { @@ -2466,7 +2466,7 @@ func TestValidateContainerStatus(t *testing.T) { } func TestUpdateNewNodeStatus(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet kubeClient := testKubelet.fakeKubeClient kubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{ @@ -2544,7 +2544,7 @@ func TestUpdateNewNodeStatus(t *testing.T) { } func TestUpdateExistingNodeStatus(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet kubeClient := testKubelet.fakeKubeClient kubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{ @@ -2643,13 +2643,13 @@ func TestUpdateExistingNodeStatus(t *testing.T) { } func TestUpdateNodeStatusWithoutContainerRuntime(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet kubeClient := testKubelet.fakeKubeClient - fakeDocker := testKubelet.fakeDocker + fakeRuntime := testKubelet.fakeRuntime // This causes returning an error from GetContainerRuntimeVersion() which // simulates that container runtime is down. - fakeDocker.VersionInfo = []string{} + fakeRuntime.VersionInfo = "" kubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{ {ObjectMeta: api.ObjectMeta{Name: testKubeletHostname}}, @@ -2729,7 +2729,7 @@ func TestUpdateNodeStatusWithoutContainerRuntime(t *testing.T) { } func TestUpdateNodeStatusError(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet // No matching node for the kubelet testKubelet.fakeKubeClient.ReactFn = testclient.NewSimpleFake(&api.NodeList{Items: []api.Node{}}).ReactFn @@ -2743,7 +2743,7 @@ func TestUpdateNodeStatusError(t *testing.T) { } func TestCreateMirrorPod(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kl := testKubelet.kubelet manager := testKubelet.fakeMirrorClient pod := &api.Pod{ @@ -2772,7 +2772,7 @@ func TestCreateMirrorPod(t *testing.T) { } func TestDeleteOutdatedMirrorPod(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -2825,7 +2825,7 @@ func TestDeleteOutdatedMirrorPod(t *testing.T) { } func TestDeleteOrphanedMirrorPods(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -2945,7 +2945,7 @@ func TestGetContainerInfoForMirrorPods(t *testing.T) { } func TestDoNotCacheStatusForStaticPods(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -2981,7 +2981,7 @@ func TestDoNotCacheStatusForStaticPods(t *testing.T) { } func TestHostNetworkAllowed(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet capabilities.SetForTests(capabilities.Capabilities{ @@ -3011,7 +3011,7 @@ func TestHostNetworkAllowed(t *testing.T) { } func TestHostNetworkDisallowed(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet capabilities.SetForTests(capabilities.Capabilities{ @@ -3040,7 +3040,7 @@ func TestHostNetworkDisallowed(t *testing.T) { } func TestPrivilegeContainerAllowed(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet capabilities.SetForTests(capabilities.Capabilities{ @@ -3067,7 +3067,7 @@ func TestPrivilegeContainerAllowed(t *testing.T) { } func TestPrivilegeContainerDisallowed(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet capabilities.SetForTests(capabilities.Capabilities{ @@ -3093,7 +3093,7 @@ func TestPrivilegeContainerDisallowed(t *testing.T) { } func TestFilterOutTerminatedPods(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet pods := newTestPods(5) pods[0].Status.Phase = api.PodFailed @@ -3110,7 +3110,7 @@ func TestFilterOutTerminatedPods(t *testing.T) { } func TestRegisterExistingNodeWithApiserver(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet kubeClient := testKubelet.fakeKubeClient kubeClient.ReactFn = func(action testclient.FakeAction) (runtime.Object, error) { @@ -3239,7 +3239,7 @@ func TestMakePortMappings(t *testing.T) { } func TestIsPodPastActiveDeadline(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet pods := newTestPods(5) @@ -3362,7 +3362,7 @@ func TestSyncPodsDoesNotSetPodsThatDidNotRunTooLongToFailed(t *testing.T) { } func TestDeletePodDirsForDeletedPods(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) @@ -3410,7 +3410,7 @@ func TestDeletePodDirsForDeletedPods(t *testing.T) { } func TestDoesNotDeletePodDirsForTerminatedPods(t *testing.T) { - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil) testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil) From 55f7b9b9b16c6c607584a6533bfc71779bd37768 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Wed, 27 May 2015 10:32:53 -0700 Subject: [PATCH 3/4] Adapt most CommandRunner tests to use FakeRuntime --- pkg/kubelet/kubelet_test.go | 104 +++++++++++++----------------------- 1 file changed, 37 insertions(+), 67 deletions(-) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 2582acd3e50..4604f8deac0 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -937,12 +937,10 @@ func (f *fakeContainerCommandRunner) PortForward(pod *kubecontainer.Pod, port ui } func TestRunInContainerNoSuchPod(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker - fakeDocker.ContainerList = []docker.APIContainers{} - kubelet.runner = &fakeCommandRunner + fakeRuntime := testKubelet.fakeRuntime + fakeRuntime.PodList = []*kubecontainer.Pod{} podName := "podFoo" podNamespace := "nsFoo" @@ -961,36 +959,18 @@ func TestRunInContainerNoSuchPod(t *testing.T) { } func TestRunInContainer(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker + fakeRuntime := testKubelet.fakeRuntime + fakeCommandRunner := fakeContainerCommandRunner{} kubelet.runner = &fakeCommandRunner containerID := "abc1234" - podName := "podFoo" - podNamespace := "nsFoo" - containerName := "containerFoo" - - fakeDocker.ContainerList = []docker.APIContainers{ - { - ID: containerID, - Names: []string{"/k8s_" + containerName + "_" + podName + "_" + podNamespace + "_12345678_42"}, - }, + fakeRuntime.PodList = []*kubecontainer.Pod{ + {ID: "12345678", Name: "podFoo", Namespace: "nsFoo", Containers: []*kubecontainer.Container{{Name: "containerFoo", ID: types.UID(containerID)}}}, } - cmd := []string{"ls"} - _, err := kubelet.RunInContainer( - kubecontainer.GetPodFullName(&api.Pod{ - ObjectMeta: api.ObjectMeta{ - UID: "12345678", - Name: podName, - Namespace: podNamespace, - }, - }), - "", - containerName, - cmd) + _, err := kubelet.RunInContainer("podFoo_nsFoo", "", "containerFoo", cmd) if fakeCommandRunner.ID != containerID { t.Errorf("unexpected Name: %s", fakeCommandRunner.ID) } @@ -1923,20 +1903,20 @@ func TestGetPodReadyCondition(t *testing.T) { } func TestExecInContainerNoSuchPod(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker - fakeDocker.ContainerList = []docker.APIContainers{} + fakeRuntime := testKubelet.fakeRuntime + fakeCommandRunner := fakeContainerCommandRunner{} kubelet.runner = &fakeCommandRunner + fakeRuntime.PodList = []*kubecontainer.Pod{} podName := "podFoo" podNamespace := "nsFoo" - containerName := "containerFoo" + containerID := "containerFoo" err := kubelet.ExecInContainer( kubecontainer.GetPodFullName(&api.Pod{ObjectMeta: api.ObjectMeta{Name: podName, Namespace: podNamespace}}), "", - containerName, + containerID, []string{"ls"}, nil, nil, @@ -1952,21 +1932,17 @@ func TestExecInContainerNoSuchPod(t *testing.T) { } func TestExecInContainerNoSuchContainer(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker + fakeRuntime := testKubelet.fakeRuntime + fakeCommandRunner := fakeContainerCommandRunner{} kubelet.runner = &fakeCommandRunner podName := "podFoo" podNamespace := "nsFoo" containerID := "containerFoo" - - fakeDocker.ContainerList = []docker.APIContainers{ - { - ID: "notfound", - Names: []string{"/k8s_notfound_" + podName + "_" + podNamespace + "_12345678_42"}, - }, + fakeRuntime.PodList = []*kubecontainer.Pod{ + {ID: "12345678", Name: podName, Namespace: podNamespace, Containers: []*kubecontainer.Container{{Name: "bar", ID: "barID"}}}, } err := kubelet.ExecInContainer( @@ -2006,10 +1982,10 @@ func (f *fakeReadWriteCloser) Close() error { } func TestExecInContainer(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker + fakeRuntime := testKubelet.fakeRuntime + fakeCommandRunner := fakeContainerCommandRunner{} kubelet.runner = &fakeCommandRunner podName := "podFoo" @@ -2020,12 +1996,9 @@ func TestExecInContainer(t *testing.T) { stdout := &fakeReadWriteCloser{} stderr := &fakeReadWriteCloser{} tty := true - - fakeDocker.ContainerList = []docker.APIContainers{ - { - ID: containerID, - Names: []string{"/k8s_" + containerID + "_" + podName + "_" + podNamespace + "_12345678_42"}, - }, + fakeRuntime.PodList = []*kubecontainer.Pod{ + {ID: "12345678", Name: podName, Namespace: podNamespace, Containers: []*kubecontainer.Container{ + {Name: containerID, ID: types.UID(containerID)}}}, } err := kubelet.ExecInContainer( @@ -2046,7 +2019,7 @@ func TestExecInContainer(t *testing.T) { t.Fatalf("unexpected error: %s", err) } if e, a := containerID, fakeCommandRunner.ID; e != a { - t.Fatalf("container id: expected %s, got %s", e, a) + t.Fatalf("container name: expected %q, got %q", e, a) } if e, a := command, fakeCommandRunner.Cmd; !reflect.DeepEqual(e, a) { t.Fatalf("command: expected '%v', got '%v'", e, a) @@ -2066,11 +2039,11 @@ func TestExecInContainer(t *testing.T) { } func TestPortForwardNoSuchPod(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker - fakeDocker.ContainerList = []docker.APIContainers{} + fakeRuntime := testKubelet.fakeRuntime + fakeRuntime.PodList = []*kubecontainer.Pod{} + fakeCommandRunner := fakeContainerCommandRunner{} kubelet.runner = &fakeCommandRunner podName := "podFoo" @@ -2092,21 +2065,18 @@ func TestPortForwardNoSuchPod(t *testing.T) { } func TestPortForwardNoSuchContainer(t *testing.T) { - fakeCommandRunner := fakeContainerCommandRunner{} - testKubelet := newTestKubelet(t) + testKubelet := newTestKubeletWithFakeRuntime(t) kubelet := testKubelet.kubelet - fakeDocker := testKubelet.fakeDocker + fakeRuntime := testKubelet.fakeRuntime + fakeCommandRunner := fakeContainerCommandRunner{} kubelet.runner = &fakeCommandRunner podName := "podFoo" podNamespace := "nsFoo" var port uint16 = 5000 - fakeDocker.ContainerList = []docker.APIContainers{ - { - ID: "notfound", - Names: []string{"/k8s_notfound_" + podName + "_" + podNamespace + "_12345678_42"}, - }, + fakeRuntime.PodList = []*kubecontainer.Pod{ + {ID: "12345678", Name: podName, Namespace: podNamespace, Containers: []*kubecontainer.Container{{Name: "bar", ID: "barID"}}}, } err := kubelet.PortForward( @@ -2172,7 +2142,7 @@ func TestPortForward(t *testing.T) { t.Fatalf("unexpected error: %s", err) } if e, a := infraContainerID, fakeCommandRunner.ID; e != a { - t.Fatalf("container id: expected %s, got %s", e, a) + t.Fatalf("container id: expected %q, got %q", e, a) } if e, a := port, fakeCommandRunner.Port; e != a { t.Fatalf("port: expected %v, got %v", e, a) From 64aa958efbe4353bf56774839eb093876e2aa8d3 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Mon, 1 Jun 2015 18:14:08 -0700 Subject: [PATCH 4/4] Make PodList span multiple lines for readability --- pkg/kubelet/kubelet_test.go | 115 ++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 13 deletions(-) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 4604f8deac0..e530fce78bb 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -552,7 +552,13 @@ func TestSyncPodsDeletesWhenSourcesAreReady(t *testing.T) { kubelet.sourcesReady = func() bool { return ready } fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: "foo", Namespace: "new", Containers: []*kubecontainer.Container{{Name: "bar"}}}, + { + ID: "12345678", + Name: "foo", Namespace: "new", + Containers: []*kubecontainer.Container{ + {Name: "bar"}, + }, + }, } if err := kubelet.SyncPods([]*api.Pod{}, emptyPodUIDs, map[string]*api.Pod{}, time.Now()); err != nil { t.Errorf("unexpected error: %v", err) @@ -733,7 +739,17 @@ func TestGetContainerInfo(t *testing.T) { mockCadvisor := testKubelet.fakeCadvisor mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, nil) fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{{Name: "foo", ID: types.UID(containerID)}}}, + { + ID: "12345678", + Name: "qux", + Namespace: "ns", + Containers: []*kubecontainer.Container{ + { + Name: "foo", + ID: types.UID(containerID), + }, + }, + }, } stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", cadvisorReq) if err != nil { @@ -806,7 +822,16 @@ func TestGetContainerInfoWhenCadvisorFailed(t *testing.T) { cadvisorReq := &cadvisorApi.ContainerInfoRequest{} mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, cadvisorApiFailure) fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "uuid", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{{Name: "foo", ID: types.UID(containerID)}}}, + { + ID: "uuid", + Name: "qux", + Namespace: "ns", + Containers: []*kubecontainer.Container{ + {Name: "foo", + ID: types.UID(containerID), + }, + }, + }, } stats, err := kubelet.GetContainerInfo("qux_ns", "uuid", "foo", cadvisorReq) if stats != nil { @@ -881,7 +906,15 @@ func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) { kubelet := testKubelet.kubelet mockCadvisor := testKubelet.fakeCadvisor fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{{Name: "bar", ID: types.UID("fakeID")}}}, + { + ID: "12345678", + Name: "qux", + Namespace: "ns", + Containers: []*kubecontainer.Container{ + {Name: "bar", + ID: types.UID("fakeID"), + }, + }}, } stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil) @@ -967,7 +1000,16 @@ func TestRunInContainer(t *testing.T) { containerID := "abc1234" fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: "podFoo", Namespace: "nsFoo", Containers: []*kubecontainer.Container{{Name: "containerFoo", ID: types.UID(containerID)}}}, + { + ID: "12345678", + Name: "podFoo", + Namespace: "nsFoo", + Containers: []*kubecontainer.Container{ + {Name: "containerFoo", + ID: types.UID(containerID), + }, + }, + }, } cmd := []string{"ls"} _, err := kubelet.RunInContainer("podFoo_nsFoo", "", "containerFoo", cmd) @@ -1942,7 +1984,15 @@ func TestExecInContainerNoSuchContainer(t *testing.T) { podNamespace := "nsFoo" containerID := "containerFoo" fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: podName, Namespace: podNamespace, Containers: []*kubecontainer.Container{{Name: "bar", ID: "barID"}}}, + { + ID: "12345678", + Name: podName, + Namespace: podNamespace, + Containers: []*kubecontainer.Container{ + {Name: "bar", + ID: "barID"}, + }, + }, } err := kubelet.ExecInContainer( @@ -1997,8 +2047,16 @@ func TestExecInContainer(t *testing.T) { stderr := &fakeReadWriteCloser{} tty := true fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: podName, Namespace: podNamespace, Containers: []*kubecontainer.Container{ - {Name: containerID, ID: types.UID(containerID)}}}, + { + ID: "12345678", + Name: podName, + Namespace: podNamespace, + Containers: []*kubecontainer.Container{ + {Name: containerID, + ID: types.UID(containerID), + }, + }, + }, } err := kubelet.ExecInContainer( @@ -2076,7 +2134,15 @@ func TestPortForwardNoSuchContainer(t *testing.T) { var port uint16 = 5000 fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: podName, Namespace: podNamespace, Containers: []*kubecontainer.Container{{Name: "bar", ID: "barID"}}}, + { + ID: "12345678", + Name: podName, + Namespace: podNamespace, + Containers: []*kubecontainer.Container{ + {Name: "bar", + ID: "barID"}, + }, + }, } err := kubelet.PortForward( @@ -2898,8 +2964,17 @@ func TestGetContainerInfoForMirrorPods(t *testing.T) { kubelet := testKubelet.kubelet fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "1234", Name: "qux", Namespace: "ns", Containers: []*kubecontainer.Container{ - {Name: "foo", ID: types.UID(containerID)}}}, + { + ID: "1234", + Name: "qux", + Namespace: "ns", + Containers: []*kubecontainer.Container{ + { + Name: "foo", + ID: types.UID(containerID), + }, + }, + }, } kubelet.podManager.SetPods(pods) @@ -3265,7 +3340,14 @@ func TestSyncPodsSetStatusToFailedForPodsThatRunTooLong(t *testing.T) { } fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: "bar", Namespace: "new", Containers: []*kubecontainer.Container{{Name: "foo"}}}, + { + ID: "12345678", + Name: "bar", + Namespace: "new", + Containers: []*kubecontainer.Container{ + {Name: "foo"}, + }, + }, } // Let the pod worker sets the status to fail after this sync. @@ -3313,7 +3395,14 @@ func TestSyncPodsDoesNotSetPodsThatDidNotRunTooLongToFailed(t *testing.T) { } fakeRuntime.PodList = []*kubecontainer.Pod{ - {ID: "12345678", Name: "bar", Namespace: "new", Containers: []*kubecontainer.Container{{Name: "foo"}}}, + { + ID: "12345678", + Name: "bar", + Namespace: "new", + Containers: []*kubecontainer.Container{ + {Name: "foo"}, + }, + }, } kubelet.podManager.SetPods(pods)