From 8892320835ad2b21b0efd4848d2af0d8a791a8e9 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Mon, 1 Sep 2014 19:55:39 +0400 Subject: [PATCH 1/2] Move TestRunCidFileCleanupIfEmpty to integration-cli Signed-off-by: Alexandr Morozov --- integration-cli/docker_cli_run_test.go | 23 ++++++++++++++++++++ integration/commands_test.go | 29 -------------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index b50d580163..189ac917ba 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -1772,3 +1772,26 @@ func TestHostsLinkedContainerUpdate(t *testing.T) { logDone("run - /etc/hosts updated in parent when restart") } + +// Ensure that CIDFile gets deleted if it's empty +// Perform this test by making `docker run` fail +func TestRunCidFileCleanupIfEmpty(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "TestRunCidFile") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + tmpCidFile := path.Join(tmpDir, "cid") + cmd := exec.Command(dockerBinary, "run", "--cidfile", tmpCidFile, "scratch") + out, _, err := runCommandWithOutput(cmd) + t.Log(out) + if err == nil { + t.Fatal("Run without command must fail") + } + + if _, err := os.Stat(tmpCidFile); err == nil { + t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile) + } + deleteAllContainers() + logDone("run - cleanup empty cidfile on fail") +} diff --git a/integration/commands_test.go b/integration/commands_test.go index 10735750dc..60deac5b62 100644 --- a/integration/commands_test.go +++ b/integration/commands_test.go @@ -583,32 +583,3 @@ func TestRunCidFileCheckIDLength(t *testing.T) { }) } - -// Ensure that CIDFile gets deleted if it's empty -// Perform this test by making `docker run` fail -func TestRunCidFileCleanupIfEmpty(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "TestRunCidFile") - if err != nil { - t.Fatal(err) - } - tmpCidFile := path.Join(tmpDir, "cid") - - cli := client.NewDockerCli(nil, ioutil.Discard, ioutil.Discard, testDaemonProto, testDaemonAddr, nil) - defer cleanup(globalEngine, t) - - c := make(chan struct{}) - go func() { - defer close(c) - if err := cli.CmdRun("--cidfile", tmpCidFile, unitTestImageID); err == nil { - t.Fatal("running without a command should haveve failed") - } - if _, err := os.Stat(tmpCidFile); err == nil { - t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile) - } - }() - defer os.RemoveAll(tmpDir) - - setTimeout(t, "CmdRun timed out", 5*time.Second, func() { - <-c - }) -} From 195cee99839a77afec7aaf7fe43cb5a9ceae4f57 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Mon, 1 Sep 2014 20:15:20 +0400 Subject: [PATCH 2/2] Move TestRunCidFileCheckIDLength to integration-cli Signed-off-by: Alexandr Morozov --- integration-cli/docker_cli_run_test.go | 31 +++++++++++++++ integration/commands_test.go | 54 -------------------------- 2 files changed, 31 insertions(+), 54 deletions(-) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 189ac917ba..81198ce213 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -1795,3 +1795,34 @@ func TestRunCidFileCleanupIfEmpty(t *testing.T) { deleteAllContainers() logDone("run - cleanup empty cidfile on fail") } + +// #2098 - Docker cidFiles only contain short version of the containerId +//sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test" +// TestRunCidFile tests that run --cidfile returns the longid +func TestRunCidFileCheckIDLength(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "TestRunCidFile") + if err != nil { + t.Fatal(err) + } + tmpCidFile := path.Join(tmpDir, "cid") + defer os.RemoveAll(tmpDir) + cmd := exec.Command(dockerBinary, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err) + } + id := strings.TrimSpace(out) + buffer, err := ioutil.ReadFile(tmpCidFile) + if err != nil { + t.Fatal(err) + } + cid := string(buffer) + if len(cid) != 64 { + t.Fatalf("--cidfile should be a long id, not '%s'", id) + } + if cid != id { + t.Fatalf("cid must be equal to %s, got %s", id, cid) + } + deleteAllContainers() + logDone("run - cidfile contains long id") +} diff --git a/integration/commands_test.go b/integration/commands_test.go index 60deac5b62..a4422a4c46 100644 --- a/integration/commands_test.go +++ b/integration/commands_test.go @@ -5,8 +5,6 @@ import ( "fmt" "io" "io/ioutil" - "os" - "path" "strings" "testing" "time" @@ -531,55 +529,3 @@ func TestRunErrorBindNonExistingSource(t *testing.T) { <-c }) } - -// #2098 - Docker cidFiles only contain short version of the containerId -//sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test" -// TestRunCidFile tests that run --cidfile returns the longid -func TestRunCidFileCheckIDLength(t *testing.T) { - stdout, stdoutPipe := io.Pipe() - - tmpDir, err := ioutil.TempDir("", "TestRunCidFile") - if err != nil { - t.Fatal(err) - } - tmpCidFile := path.Join(tmpDir, "cid") - - cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil) - defer cleanup(globalEngine, t) - - c := make(chan struct{}) - go func() { - defer close(c) - if err := cli.CmdRun("--cidfile", tmpCidFile, unitTestImageID, "ls"); err != nil { - t.Fatal(err) - } - }() - - defer os.RemoveAll(tmpDir) - setTimeout(t, "Reading command output time out", 2*time.Second, func() { - cmdOutput, err := bufio.NewReader(stdout).ReadString('\n') - if err != nil { - t.Fatal(err) - } - if len(cmdOutput) < 1 { - t.Fatalf("'ls' should return something , not '%s'", cmdOutput) - } - //read the tmpCidFile - buffer, err := ioutil.ReadFile(tmpCidFile) - if err != nil { - t.Fatal(err) - } - id := string(buffer) - - if len(id) != len("2bf44ea18873287bd9ace8a4cb536a7cbe134bed67e805fdf2f58a57f69b320c") { - t.Fatalf("--cidfile should be a long id, not '%s'", id) - } - //test that its a valid cid? (though the container is gone..) - //remove the file and dir. - }) - - setTimeout(t, "CmdRun timed out", 5*time.Second, func() { - <-c - }) - -}