2014-04-07 18:34:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-21 22:34:08 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2014-05-21 22:07:40 +00:00
|
|
|
"strings"
|
2015-09-18 17:41:12 +00:00
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-12-01 22:01:51 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-09-18 17:41:12 +00:00
|
|
|
"github.com/go-check/check"
|
2014-04-07 18:34:07 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-27 18:13:25 +00:00
|
|
|
_, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
2014-04-07 18:34:07 +00:00
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
// run ping failed with error
|
|
|
|
c.Assert(exitCode, checker.Equals, 1, check.Commentf("error: %v", err))
|
2014-04-07 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 20:14:38 +00:00
|
|
|
// Test for appropriate error when calling --link with an invalid target container
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
|
2015-02-13 20:14:38 +00:00
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
// 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")
|
2015-02-13 20:14:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:44:22 +00:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
|
2015-02-16 05:00:51 +00:00
|
|
|
|
|
|
|
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"
|
2014-08-28 05:25:57 +00:00
|
|
|
|
2015-02-16 05:00:51 +00:00
|
|
|
// test ping by alias, ping by name, and ping by hostname
|
|
|
|
// 1. Ping by alias
|
2015-04-18 16:46:47 +00:00
|
|
|
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
|
2015-02-16 05:00:51 +00:00
|
|
|
// 2. Ping by container name
|
2015-04-18 16:46:47 +00:00
|
|
|
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
|
2015-02-16 05:00:51 +00:00
|
|
|
// 3. Ping by hostname
|
2015-04-18 16:46:47 +00:00
|
|
|
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
|
2015-02-16 05:00:51 +00:00
|
|
|
|
2014-04-07 18:34:07 +00:00
|
|
|
}
|
2014-05-10 17:27:24 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
|
2015-04-06 13:21:18 +00:00
|
|
|
idA := strings.TrimSpace(out)
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
|
2015-04-06 13:21:18 +00:00
|
|
|
idB := strings.TrimSpace(out)
|
2015-04-18 16:46:47 +00:00
|
|
|
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)
|
2014-10-05 02:47:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-07-08 20:27:58 +00:00
|
|
|
var (
|
|
|
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
|
|
|
result []string
|
|
|
|
)
|
2015-04-18 16:46:47 +00:00
|
|
|
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")
|
2016-01-28 14:19:25 +00:00
|
|
|
links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
|
2014-07-08 20:27:58 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
err := unmarshalJSON([]byte(links), &result)
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-07-08 20:27:58 +00:00
|
|
|
|
|
|
|
output := convertSliceOfStringsToMap(result)
|
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(output, checker.DeepEquals, expected)
|
2014-06-27 08:26:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-07-22 22:13:52 +00:00
|
|
|
var (
|
|
|
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
|
|
|
result []string
|
|
|
|
)
|
2015-04-18 16:46:47 +00:00
|
|
|
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")
|
2016-01-28 14:19:25 +00:00
|
|
|
links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
|
2014-07-22 22:13:52 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
err := unmarshalJSON([]byte(links), &result)
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-07-22 22:13:52 +00:00
|
|
|
|
|
|
|
output := convertSliceOfStringsToMap(result)
|
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(output, checker.DeepEquals, expected)
|
2014-06-27 08:26:02 +00:00
|
|
|
}
|
2014-11-06 23:25:14 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:44:22 +00:00
|
|
|
dockerCmd(c, "create", "--name=first", "busybox", "top")
|
|
|
|
dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
|
|
|
|
dockerCmd(c, "start", "first")
|
|
|
|
|
2014-11-06 23:25:14 +00:00
|
|
|
}
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, SameHostDaemon, ExecSupport)
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2015-07-20 06:44:22 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
|
2014-10-24 21:39:12 +00:00
|
|
|
idOne := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 06:44:22 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
|
2014-10-24 21:39:12 +00:00
|
|
|
idTwo := strings.TrimSpace(out)
|
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(waitRun(idTwo), checker.IsNil)
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2015-02-16 19:32:25 +00:00
|
|
|
contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("contentOne: %s", string(contentOne)))
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2015-02-16 19:32:25 +00:00
|
|
|
contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
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")
|
2014-10-24 21:39:12 +00:00
|
|
|
}
|
2014-12-08 19:33:18 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, SameHostDaemon, ExecSupport)
|
2015-07-20 06:44:22 +00:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
|
2015-01-21 22:34:08 +00:00
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
|
2015-02-16 19:32:25 +00:00
|
|
|
content, err := readContainerFileWithExec(id, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-01-21 22:34:08 +00:00
|
|
|
getIP := func(hosts []byte, hostname string) string {
|
|
|
|
re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
|
|
|
|
matches := re.FindSubmatch(hosts)
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname))
|
2015-01-21 22:34:08 +00:00
|
|
|
return string(matches[1])
|
|
|
|
}
|
2015-10-08 15:56:51 +00:00
|
|
|
ip := getIP(content, "one")
|
|
|
|
c.Assert(ip, checker.Equals, realIP)
|
|
|
|
|
|
|
|
ip = getIP(content, "onetwo")
|
|
|
|
c.Assert(ip, checker.Equals, realIP)
|
|
|
|
|
2015-07-20 06:44:22 +00:00
|
|
|
dockerCmd(c, "restart", "one")
|
2016-01-28 14:19:25 +00:00
|
|
|
realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
|
2015-10-08 15:56:51 +00:00
|
|
|
|
2015-02-16 19:32:25 +00:00
|
|
|
content, err = readContainerFileWithExec(id, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
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)
|
2015-01-21 22:34:08 +00:00
|
|
|
}
|
2015-04-25 11:42:43 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinksEnvs(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:44:22 +00:00
|
|
|
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")
|
2015-10-08 15:56:51 +00:00
|
|
|
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")
|
2015-04-25 11:42:43 +00:00
|
|
|
}
|
2015-05-07 20:02:14 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:44:22 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
|
2015-05-07 20:02:14 +00:00
|
|
|
|
|
|
|
cid := strings.TrimSpace(out)
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(waitRun(cid), checker.IsNil)
|
2015-05-07 20:02:14 +00:00
|
|
|
|
2015-07-20 06:44:22 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
|
2015-05-07 20:02:14 +00:00
|
|
|
|
|
|
|
cid2 := strings.TrimSpace(out)
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(waitRun(cid2), checker.IsNil)
|
2015-05-07 20:02:14 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
links := inspectFieldJSON(c, cid2, "HostConfig.Links")
|
2015-10-08 15:56:51 +00:00
|
|
|
c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
|
2015-05-07 20:02:14 +00:00
|
|
|
}
|
2015-09-01 16:10:24 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2015-09-01 16:10:24 +00:00
|
|
|
dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
|
|
|
|
out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
|
2015-10-08 15:56:51 +00:00
|
|
|
|
|
|
|
// 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
|
2015-12-01 22:01:51 +00:00
|
|
|
c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
|
2015-09-01 16:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2015-09-01 16:10:24 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
// /etc/hosts should be a regular file
|
|
|
|
c.Assert(out, checker.Matches, "^-.+\n")
|
2015-09-01 16:10:24 +00:00
|
|
|
}
|
2016-01-20 21:24:16 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinksMultipleWithSameName(c *check.C) {
|
2016-01-21 17:51:09 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-01-20 21:24:16 +00:00
|
|
|
dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
|
|
|
|
dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
|
|
|
|
dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
|
|
|
|
}
|