From 8137d7940d038b13abe61417348b2daf1b704a7d Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Fri, 24 Oct 2014 18:12:54 +0000 Subject: [PATCH 1/2] Break some routines out of the mutable files test for future use Docker-DCO-1.1-Signed-off-by: Erik Hollensbe (github: erikh) Upstream-commit: 20575d20bad60172b9d1f40c9fe357b7c069f466 Component: engine --- .../integration-cli/docker_cli_run_test.go | 22 +++---------- .../integration-cli/docker_test_vars.go | 9 +++--- .../engine/integration-cli/docker_utils.go | 32 +++++++++++++++++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/components/engine/integration-cli/docker_cli_run_test.go b/components/engine/integration-cli/docker_cli_run_test.go index 9546af0014..05915032fb 100644 --- a/components/engine/integration-cli/docker_cli_run_test.go +++ b/components/engine/integration-cli/docker_cli_run_test.go @@ -1897,37 +1897,25 @@ func TestRunMutableNetworkFiles(t *testing.T) { for _, fn := range []string{"resolv.conf", "hosts"} { deleteAllContainers() - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s; while true; do sleep 1; done", fn))) - if err != nil { - t.Fatal(err, out) - } - - time.Sleep(1 * time.Second) - - contID := strings.TrimSpace(out) - - f, err := os.Open(filepath.Join("/var/lib/docker/containers", contID, fn)) + content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s; while true; do sleep 1; done", fn))) if err != nil { t.Fatal(err) } - content, err := ioutil.ReadAll(f) - f.Close() - if strings.TrimSpace(string(content)) != "success" { t.Fatal("Content was not what was modified in the container", string(content)) } - out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "sh", "-c", fmt.Sprintf("while true; do cat /etc/%s; sleep 1; done", fn))) + out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "sh", "-c", fmt.Sprintf("while true; do cat /etc/%s; sleep 1; done", fn))) if err != nil { t.Fatal(err) } - contID = strings.TrimSpace(out) + contID := strings.TrimSpace(out) - resolvConfPath := filepath.Join("/var/lib/docker/containers", contID, fn) + resolvConfPath := containerStorageFile(contID, fn) - f, err = os.OpenFile(resolvConfPath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644) + f, err := os.OpenFile(resolvConfPath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644) if err != nil { t.Fatal(err) } diff --git a/components/engine/integration-cli/docker_test_vars.go b/components/engine/integration-cli/docker_test_vars.go index 23903a39a9..78c481bd23 100644 --- a/components/engine/integration-cli/docker_test_vars.go +++ b/components/engine/integration-cli/docker_test_vars.go @@ -16,10 +16,11 @@ var ( // the private registry to use for tests privateRegistryURL = "127.0.0.1:5000" - dockerBasePath = "/var/lib/docker" - execDriverPath = dockerBasePath + "/execdriver/native" - volumesConfigPath = dockerBasePath + "/volumes" - volumesStoragePath = dockerBasePath + "/vfs/dir" + dockerBasePath = "/var/lib/docker" + execDriverPath = dockerBasePath + "/execdriver/native" + volumesConfigPath = dockerBasePath + "/volumes" + volumesStoragePath = dockerBasePath + "/vfs/dir" + containerStoragePath = dockerBasePath + "/containers" workingDirectory string ) diff --git a/components/engine/integration-cli/docker_utils.go b/components/engine/integration-cli/docker_utils.go index ba1a0b1306..ca33baa2aa 100644 --- a/components/engine/integration-cli/docker_utils.go +++ b/components/engine/integration-cli/docker_utils.go @@ -731,3 +731,35 @@ func readFile(src string, t *testing.T) (content string) { } return string(data) } + +func containerStorageFile(containerId, basename string) string { + return filepath.Join("/var/lib/docker/containers", containerId, basename) +} + +func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) { + out, _, err := runCommandWithOutput(cmd) + if err != nil { + return nil, err + } + + time.Sleep(1 * time.Second) + + contID := strings.TrimSpace(out) + + return readContainerFile(contID, filename) +} + +func readContainerFile(containerId, filename string) ([]byte, error) { + f, err := os.Open(containerStorageFile(containerId, filename)) + if err != nil { + return nil, err + } + defer f.Close() + + content, err := ioutil.ReadAll(f) + if err != nil { + return nil, err + } + + return content, nil +} From 2d3f0f7b70e4c3639d397d61df3b413348b1f965 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Fri, 24 Oct 2014 21:39:12 +0000 Subject: [PATCH 2/2] Test for updating hosts files via links. Docker-DCO-1.1-Signed-off-by: Erik Hollensbe (github: erikh) Upstream-commit: 68bc8de111e4faa2a16583cd45b5c0fd253a3bba Component: engine --- .../integration-cli/docker_cli_links_test.go | 37 +++++++++++++++++++ .../engine/integration-cli/docker_utils.go | 3 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/components/engine/integration-cli/docker_cli_links_test.go b/components/engine/integration-cli/docker_cli_links_test.go index f202ce10a2..d412ef2a1a 100644 --- a/components/engine/integration-cli/docker_cli_links_test.go +++ b/components/engine/integration-cli/docker_cli_links_test.go @@ -6,6 +6,7 @@ import ( "os/exec" "strings" "testing" + "time" "github.com/docker/docker/pkg/iptables" ) @@ -177,3 +178,39 @@ func TestLinksNotStartedParentNotFail(t *testing.T) { } logDone("link - container start not failing on updating stopped parent links") } + +func TestLinksHostsFilesInject(t *testing.T) { + defer deleteAllContainers() + + out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top")) + if err != nil { + t.Fatal(err, out) + } + + idOne := strings.TrimSpace(out) + + out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")) + if err != nil { + t.Fatal(err, out) + } + + idTwo := strings.TrimSpace(out) + + time.Sleep(1 * time.Second) + + contentOne, err := readContainerFile(idOne, "hosts") + if err != nil { + t.Fatal(err, string(contentOne)) + } + + contentTwo, err := readContainerFile(idTwo, "hosts") + if err != nil { + t.Fatal(err, string(contentTwo)) + } + + if !strings.Contains(string(contentTwo), "onetwo") { + t.Fatal("Host is not present in updated hosts file", string(contentTwo)) + } + + logDone("link - ensure containers hosts files are updated with the link alias.") +} diff --git a/components/engine/integration-cli/docker_utils.go b/components/engine/integration-cli/docker_utils.go index ca33baa2aa..9b5fa76c0c 100644 --- a/components/engine/integration-cli/docker_utils.go +++ b/components/engine/integration-cli/docker_utils.go @@ -736,10 +736,11 @@ func containerStorageFile(containerId, basename string) string { return filepath.Join("/var/lib/docker/containers", containerId, basename) } +// docker commands that use this function must be run with the '-d' switch. func runCommandAndReadContainerFile(filename string, cmd *exec.Cmd) ([]byte, error) { out, _, err := runCommandWithOutput(cmd) if err != nil { - return nil, err + return nil, fmt.Errorf("%v: %q", err, out) } time.Sleep(1 * time.Second)