func TestRemoveImage(t *testing.T) { fakeDocker := test.NewFakeDockerClient() dh := getDocker(fakeDocker) image := dockertypes.ImageInspect{ID: "test-abcd"} fakeDocker.Images = map[string]dockertypes.ImageInspect{image.ID: image} err := dh.RemoveImage("test-abcd") if err != nil { t.Errorf("Unexpected error removing image: %s", err) } }
func TestRemoveContainer(t *testing.T) { fakeDocker := test.NewFakeDockerClient() dh := getDocker(fakeDocker) containerID := "testContainerId" fakeDocker.Containers[containerID] = dockercontainer.Config{} err := dh.RemoveContainer(containerID) if err != nil { t.Errorf("%+v", err) } expectedCalls := []string{"remove"} if !reflect.DeepEqual(fakeDocker.Calls, expectedCalls) { t.Errorf("Expected fakeDocker.Calls %v, got %v", expectedCalls, fakeDocker.Calls) } }
func TestGetImageID(t *testing.T) { fakeDocker := test.NewFakeDockerClient() dh := getDocker(fakeDocker) image := dockertypes.ImageInspect{ID: "test-abcd:latest"} fakeDocker.Images = map[string]dockertypes.ImageInspect{image.ID: image} id, err := dh.GetImageID("test-abcd") expectedCalls := []string{"inspect_image"} if !reflect.DeepEqual(fakeDocker.Calls, expectedCalls) { t.Errorf("Expected fakeDocker.Calls %v, got %v", expectedCalls, fakeDocker.Calls) } if err != nil { t.Errorf("Unexpected error returned: %v", err) } else if id != image.ID { t.Errorf("Unexpected image id returned: %s", id) } }
func TestRunContainer(t *testing.T) { type runtest struct { calls []string image dockertypes.ImageInspect cmd string externalScripts bool paramScriptsURL string paramDestination string cmdExpected []string } tests := map[string]runtest{ "default": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: true, cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /tmp -xf - && /tmp/scripts/%s", api.Assemble)}, }, "paramDestination": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: true, paramDestination: "/opt/test", cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /opt/test -xf - && /opt/test/scripts/%s", api.Assemble)}, }, "paramDestination¶mScripts": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: true, paramDestination: "/opt/test", paramScriptsURL: "http://my.test.url/test?param=one", cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /opt/test -xf - && /opt/test/scripts/%s", api.Assemble)}, }, "scriptsInsideImageEnvironment": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Env: []string{ScriptsURLEnvironment + "=image:///opt/bin/"}, }, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: false, cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /tmp -xf - && /opt/bin/%s", api.Assemble)}, }, "scriptsInsideImageLabel": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Labels: map[string]string{ScriptsURLLabel: "image:///opt/bin/"}, }, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: false, cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /tmp -xf - && /opt/bin/%s", api.Assemble)}, }, "scriptsInsideImageEnvironmentWithParamDestination": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Env: []string{ScriptsURLEnvironment + "=image:///opt/bin"}, }, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: false, paramDestination: "/opt/sti", cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /opt/sti -xf - && /opt/bin/%s", api.Assemble)}, }, "scriptsInsideImageLabelWithParamDestination": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Labels: map[string]string{ScriptsURLLabel: "image:///opt/bin"}, }, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: false, paramDestination: "/opt/sti", cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /opt/sti -xf - && /opt/bin/%s", api.Assemble)}, }, "paramDestinationFromImageEnvironment": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Env: []string{LocationEnvironment + "=/opt", ScriptsURLEnvironment + "=http://my.test.url/test?param=one"}, }, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: true, cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /opt -xf - && /opt/scripts/%s", api.Assemble)}, }, "paramDestinationFromImageLabel": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Labels: map[string]string{DestinationLabel: "/opt", ScriptsURLLabel: "http://my.test.url/test?param=one"}, }, Config: &dockercontainer.Config{}, }, cmd: api.Assemble, externalScripts: true, cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /opt -xf - && /opt/scripts/%s", api.Assemble)}, }, "usageCommand": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{}, }, cmd: api.Usage, externalScripts: true, cmdExpected: []string{"/bin/sh", "-c", fmt.Sprintf("tar -C /tmp -xf - && /tmp/scripts/%s", api.Usage)}, }, "otherCommand": { calls: []string{"inspect_image", "inspect_image", "inspect_image", "create", "attach", "start", "remove"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{}, }, cmd: api.Run, externalScripts: true, cmdExpected: []string{fmt.Sprintf("/tmp/scripts/%s", api.Run)}, }, } for desc, tst := range tests { fakeDocker := test.NewFakeDockerClient() dh := getDocker(fakeDocker) tst.image.ID = "test/image:latest" fakeDocker.Images = map[string]dockertypes.ImageInspect{tst.image.ID: tst.image} if len(fakeDocker.Containers) > 0 { t.Errorf("newly created fake client should have empty container map: %+v", fakeDocker.Containers) } //NOTE: the combo of the fake k8s client, go 1.6, and using os.Stderr/os.Stdout caused what appeared to be go test crashes // when we tried to call their closers in RunContainer err := dh.RunContainer(RunContainerOptions{ Image: "test/image", PullImage: true, ExternalScripts: tst.externalScripts, ScriptsURL: tst.paramScriptsURL, Destination: tst.paramDestination, Command: tst.cmd, Env: []string{"Key1=Value1", "Key2=Value2"}, Stdin: os.Stdin, //Stdout: os.Stdout, //Stderr: os.Stdout, }) if err != nil { t.Errorf("%s: Unexpected error: %v", desc, err) } // container ID will be random, so don't look up directly ... just get the 1 entry which should be there if len(fakeDocker.Containers) != 1 { t.Errorf("fake container map should only have 1 entry: %+v", fakeDocker.Containers) } for _, container := range fakeDocker.Containers { // Validate the Container parameters if container.Image != "test/image:latest" { t.Errorf("%s: Unexpected create config image: %s", desc, container.Image) } if !reflect.DeepEqual(container.Cmd, dockerstrslice.StrSlice(tst.cmdExpected)) { t.Errorf("%s: Unexpected create config command: %#v instead of %q", desc, container.Cmd, strings.Join(tst.cmdExpected, " ")) } if !reflect.DeepEqual(container.Env, []string{"Key1=Value1", "Key2=Value2"}) { t.Errorf("%s: Unexpected create config env: %#v", desc, container.Env) } if !reflect.DeepEqual(fakeDocker.Calls, tst.calls) { t.Errorf("%s: Expected fakeDocker.Calls %v, got %v", desc, tst.calls, fakeDocker.Calls) } } } }
func TestGetScriptsURL(t *testing.T) { type urltest struct { image dockertypes.ImageInspect result string calls []string inspectErr error } tests := map[string]urltest{ "not present": { calls: []string{"inspect_image"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Env: []string{"Env1=value1"}, Labels: map[string]string{}, }, Config: &dockercontainer.Config{ Env: []string{"Env2=value2"}, Labels: map[string]string{}, }, }, result: "", }, "env in containerConfig": { calls: []string{"inspect_image"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Env: []string{"Env1=value1", ScriptsURLEnvironment + "=test_url_value"}, }, Config: &dockercontainer.Config{}, }, result: "test_url_value", }, "env in image config": { calls: []string{"inspect_image"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{ Env: []string{ "Env1=value1", ScriptsURLEnvironment + "=test_url_value_2", "Env2=value2", }, }, }, result: "test_url_value_2", }, "label in containerConfig": { calls: []string{"inspect_image"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{ Labels: map[string]string{ScriptsURLLabel: "test_url_value"}, }, Config: &dockercontainer.Config{}, }, result: "test_url_value", }, "label in image config": { calls: []string{"inspect_image"}, image: dockertypes.ImageInspect{ ContainerConfig: &dockercontainer.Config{}, Config: &dockercontainer.Config{ Labels: map[string]string{ScriptsURLLabel: "test_url_value_2"}, }, }, result: "test_url_value_2", }, "inspect error": { calls: []string{"inspect_image", "pull"}, image: dockertypes.ImageInspect{}, inspectErr: fmt.Errorf("Inspect error"), }, } for desc, tst := range tests { fakeDocker := test.NewFakeDockerClient() dh := getDocker(fakeDocker) tst.image.ID = "test/image:latest" if tst.inspectErr != nil { fakeDocker.PullFail = tst.inspectErr } else { fakeDocker.Images = map[string]dockertypes.ImageInspect{tst.image.ID: tst.image} } url, err := dh.GetScriptsURL(tst.image.ID) if !reflect.DeepEqual(fakeDocker.Calls, tst.calls) { t.Errorf("%s: Expected fakeDocker.Calls %v, got %v", desc, tst.calls, fakeDocker.Calls) } if err != nil && tst.inspectErr == nil { t.Errorf("%s: Unexpected error returned: %v", desc, err) } if tst.inspectErr == nil && url != tst.result { t.Errorf("%s: Unexpected result. Expected: %s Actual: %s", desc, tst.result, url) } } }