From 5b27fbc0e27358b794946fe9e84a83a6c4497be4 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 28 Aug 2014 00:25:10 +0000 Subject: [PATCH] move TestEntrypoint & TestBindMounts Signed-off-by: Victor Vieux --- integration-cli/docker_cli_run_test.go | 62 ++++++++++++++++++ integration-cli/docker_utils.go | 34 ++++++++++ integration/container_test.go | 91 -------------------------- 3 files changed, 96 insertions(+), 91 deletions(-) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 1021e152ab..7de2d334b1 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "os/exec" + "path" "path/filepath" "reflect" "regexp" @@ -1644,3 +1645,64 @@ func TestRunWithBadDevice(t *testing.T) { } logDone("run - error with bad device") } + +func TestEntrypoint(t *testing.T) { + name := "entrypoint" + cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + expected := "foobar" + if out != expected { + t.Fatalf("Output should be %q, actual out: %q", expected, out) + } + logDone("run - entrypoint") +} + +func TestBindMounts(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "docker-test-container") + if err != nil { + t.Fatal(err) + } + + defer os.RemoveAll(tmpDir) + writeFile(path.Join(tmpDir, "touch-me"), "", t) + + // Test reading from a read-only bind mount + cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + if !strings.Contains(out, "touch-me") { + t.Fatal("Container failed to read from bind mount") + } + + // test writing to bind mount + cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla") + out, _, err = runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist + + // test mounting to an illegal destination directory + cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".") + _, err = runCommand(cmd) + if err == nil { + t.Fatal("Container bind mounted illegal directory") + } + + // test mount a file + cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla") + _, err = runCommand(cmd) + if err != nil { + t.Fatal(err, out) + } + content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist + expected := "yotta" + if content != expected { + t.Fatalf("Output should be %q, actual out: %q", expected, content) + } +} diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index 5561fc24dc..cf1dd203f8 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "io/ioutil" "net/http" "net/http/httptest" @@ -368,3 +369,36 @@ func fakeGIT(name string, files map[string]string) (*FakeGIT, error) { RepoURL: fmt.Sprintf("%s/%s.git", server.URL, name), }, nil } + +// Write `content` to the file at path `dst`, creating it if necessary, +// as well as any missing directories. +// The file is truncated if it already exists. +// Call t.Fatal() at the first error. +func writeFile(dst, content string, t *testing.T) { + // Create subdirectories if necessary + if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) { + t.Fatal(err) + } + f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700) + if err != nil { + t.Fatal(err) + } + // Write content (truncate if it exists) + if _, err := io.Copy(f, strings.NewReader(content)); err != nil { + t.Fatal(err) + } +} + +// Return the contents of file at path `src`. +// Call t.Fatal() at the first error (including if the file doesn't exist) +func readFile(src string, t *testing.T) (content string) { + f, err := os.Open(src) + if err != nil { + t.Fatal(err) + } + data, err := ioutil.ReadAll(f) + if err != nil { + t.Fatal(err) + } + return string(data) +} diff --git a/integration/container_test.go b/integration/container_test.go index 9d55b0b365..60f3e55bc5 100644 --- a/integration/container_test.go +++ b/integration/container_test.go @@ -1,12 +1,8 @@ package docker import ( - "fmt" "io" "io/ioutil" - "os" - "path" - "strings" "testing" "time" @@ -179,53 +175,6 @@ func TestTty(t *testing.T) { } } -func TestEntrypoint(t *testing.T) { - daemon := mkDaemon(t) - defer nuke(daemon) - container, _, err := daemon.Create( - &runconfig.Config{ - Image: GetTestImage(daemon).ID, - Entrypoint: []string{"/bin/echo"}, - Cmd: []string{"-n", "foobar"}, - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err := container.Output() - if err != nil { - t.Fatal(err) - } - if string(output) != "foobar" { - t.Error(string(output)) - } -} - -func TestEntrypointNoCmd(t *testing.T) { - daemon := mkDaemon(t) - defer nuke(daemon) - container, _, err := daemon.Create( - &runconfig.Config{ - Image: GetTestImage(daemon).ID, - Entrypoint: []string{"/bin/echo", "foobar"}, - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer daemon.Destroy(container) - output, err := container.Output() - if err != nil { - t.Fatal(err) - } - if strings.Trim(string(output), "\r\n") != "foobar" { - t.Error(string(output)) - } -} - func BenchmarkRunSequential(b *testing.B) { daemon := mkDaemon(b) defer nuke(daemon) @@ -303,43 +252,3 @@ func BenchmarkRunParallel(b *testing.B) { b.Fatal(errors) } } - -func tempDir(t *testing.T) string { - tmpDir, err := ioutil.TempDir("", "docker-test-container") - if err != nil { - t.Fatal(err) - } - return tmpDir -} - -func TestBindMounts(t *testing.T) { - eng := NewTestEngine(t) - r := mkDaemonFromEngine(eng, t) - defer r.Nuke() - - tmpDir := tempDir(t) - defer os.RemoveAll(tmpDir) - writeFile(path.Join(tmpDir, "touch-me"), "", t) - - // Test reading from a read-only bind mount - stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t) - if !strings.Contains(stdout, "touch-me") { - t.Fatal("Container failed to read from bind mount") - } - - // test writing to bind mount - runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t) - readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist - - // test mounting to an illegal destination directory - if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil { - t.Fatal("Container bind mounted illegal directory") - } - - // test mount a file - runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t) - content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist - if content != "yotta" { - t.Fatal("Container failed to write to bind mount file") - } -}