1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_links_test.go
David Calavera 25682577c9 Extract network settings types for inspect.
Keeping backwards compatibility.

Signed-off-by: David Calavera <david.calavera@gmail.com>
Signed-off-by: Tibor Vass <tibor@docker.com>
2015-10-27 19:16:21 -04:00

213 lines
8.3 KiB
Go

package main
import (
"fmt"
"regexp"
"strings"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
testRequires(c, DaemonIsLinux)
_, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
// run ping failed with error
c.Assert(exitCode, checker.Equals, 1, check.Commentf("error: %v", err))
}
// Test for appropriate error when calling --link with an invalid target container
func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
// an invalid container target should produce an error
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
// an invalid container target should produce an error
c.Assert(out, checker.Contains, "Could not get container")
}
func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
// test ping by alias, ping by name, and ping by hostname
// 1. Ping by alias
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
// 2. Ping by container name
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
// 3. Ping by hostname
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
}
func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
idA := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
idB := strings.TrimSpace(out)
dockerCmd(c, "rename", "container1", "container_new")
dockerCmd(c, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
dockerCmd(c, "kill", idA)
dockerCmd(c, "kill", idB)
}
func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
testRequires(c, DaemonIsLinux)
var (
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
result []string
)
dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
c.Assert(err, checker.IsNil)
err = unmarshalJSON([]byte(links), &result)
c.Assert(err, checker.IsNil)
output := convertSliceOfStringsToMap(result)
c.Assert(output, checker.DeepEquals, expected)
}
func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
testRequires(c, DaemonIsLinux)
var (
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
result []string
)
dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
c.Assert(err, checker.IsNil)
err = unmarshalJSON([]byte(links), &result)
c.Assert(err, checker.IsNil)
output := convertSliceOfStringsToMap(result)
c.Assert(output, checker.DeepEquals, expected)
}
func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "create", "--name=first", "busybox", "top")
dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
dockerCmd(c, "start", "first")
}
func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
testRequires(c, DaemonIsLinux)
testRequires(c, SameHostDaemon, ExecSupport)
out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
idOne := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
idTwo := strings.TrimSpace(out)
c.Assert(waitRun(idTwo), checker.IsNil)
contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
c.Assert(err, checker.IsNil, check.Commentf("contentOne: %s", string(contentOne)))
contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
c.Assert(err, checker.IsNil, check.Commentf("contentTwo: %s", string(contentTwo)))
// Host is not present in updated hosts file
c.Assert(string(contentTwo), checker.Contains, "onetwo")
}
func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
testRequires(c, DaemonIsLinux)
testRequires(c, SameHostDaemon, ExecSupport)
dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
id := strings.TrimSpace(string(out))
realIP, err := inspectField("one", "NetworkSettings.Networks.bridge.IPAddress")
if err != nil {
c.Fatal(err)
}
c.Assert(err, checker.IsNil)
content, err := readContainerFileWithExec(id, "/etc/hosts")
c.Assert(err, checker.IsNil)
getIP := func(hosts []byte, hostname string) string {
re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
matches := re.FindSubmatch(hosts)
c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname))
return string(matches[1])
}
ip := getIP(content, "one")
c.Assert(ip, checker.Equals, realIP)
ip = getIP(content, "onetwo")
c.Assert(ip, checker.Equals, realIP)
dockerCmd(c, "restart", "one")
realIP, err = inspectField("one", "NetworkSettings.Networks.bridge.IPAddress")
c.Assert(err, checker.IsNil)
content, err = readContainerFileWithExec(id, "/etc/hosts")
c.Assert(err, checker.IsNil, check.Commentf("content: %s", string(content)))
ip = getIP(content, "one")
c.Assert(ip, checker.Equals, realIP)
ip = getIP(content, "onetwo")
c.Assert(ip, checker.Equals, realIP)
}
func (s *DockerSuite) TestLinksEnvs(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
c.Assert(out, checker.Contains, "FIRST_ENV_e1=\n")
c.Assert(out, checker.Contains, "FIRST_ENV_e2=v2")
c.Assert(out, checker.Contains, "FIRST_ENV_e3=v3=v3")
}
func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
cid := strings.TrimSpace(out)
c.Assert(waitRun(cid), checker.IsNil)
out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
cid2 := strings.TrimSpace(out)
c.Assert(waitRun(cid2), checker.IsNil)
links, err := inspectFieldJSON(cid2, "HostConfig.Links")
c.Assert(err, checker.IsNil)
c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
}
func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
testRequires(c, DaemonIsLinux, NotUserNamespace)
dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
// Running container linking to a container with --net host should have failed
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
// Running container linking to a container with --net host should have failed
c.Assert(out, checker.Contains, "--net=host can't be used with links. This would result in undefined behavior")
}
func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
testRequires(c, DaemonIsLinux, NotUserNamespace)
out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
// /etc/hosts should be a regular file
c.Assert(out, checker.Matches, "^-.+\n")
}