2014-04-07 22:34:07 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-22 01:34:08 +03:00
|
|
|
"fmt"
|
2015-07-20 09:44:22 +03:00
|
|
|
"github.com/go-check/check"
|
2014-12-04 04:52:06 +03:00
|
|
|
"reflect"
|
2015-01-22 01:34:08 +03:00
|
|
|
"regexp"
|
2014-05-22 02:07:40 +04:00
|
|
|
"strings"
|
2014-10-25 01:39:12 +04:00
|
|
|
"time"
|
2014-04-07 22:34:07 +04:00
|
|
|
)
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
|
2015-07-20 09:44:22 +03:00
|
|
|
|
|
|
|
_, exitCode, err := dockerCmdWithError(c, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
2014-04-07 22:34:07 +04:00
|
|
|
|
|
|
|
if exitCode == 0 {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("run ping did not fail")
|
2014-04-07 22:34:07 +04:00
|
|
|
} else if exitCode != 1 {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("run ping failed with errors: %v", err)
|
2014-04-07 22:34:07 +04:00
|
|
|
}
|
2014-08-28 09:25:57 +04:00
|
|
|
|
2014-04-07 22:34:07 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 23:14:38 +03:00
|
|
|
// Test for appropriate error when calling --link with an invalid target container
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
|
2015-02-20 09:56:02 +03:00
|
|
|
|
2015-07-20 09:44:22 +03:00
|
|
|
out, _, err := dockerCmdWithError(c, "run", "--link", "bogus:alias", "busybox", "true")
|
2015-02-13 23:14:38 +03:00
|
|
|
|
|
|
|
if err == nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("an invalid container target should produce an error")
|
2015-02-13 23:14:38 +03:00
|
|
|
}
|
|
|
|
if !strings.Contains(out, "Could not get container") {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
|
2015-02-13 23:14:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
|
2015-02-20 09:56:02 +03:00
|
|
|
|
2015-07-20 09:44:22 +03:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
|
2015-02-16 08:00:51 +03: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 09:25:57 +04:00
|
|
|
|
2015-02-16 08:00:51 +03:00
|
|
|
// test ping by alias, ping by name, and ping by hostname
|
|
|
|
// 1. Ping by alias
|
2015-04-18 19:46:47 +03:00
|
|
|
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
|
2015-02-16 08:00:51 +03:00
|
|
|
// 2. Ping by container name
|
2015-04-18 19:46:47 +03:00
|
|
|
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
|
2015-02-16 08:00:51 +03:00
|
|
|
// 3. Ping by hostname
|
2015-04-18 19:46:47 +03:00
|
|
|
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
|
2015-02-16 08:00:51 +03:00
|
|
|
|
2014-04-07 22:34:07 +04:00
|
|
|
}
|
2014-05-10 21:27:24 +04:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
|
2015-02-20 09:56:02 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
|
2015-04-06 16:21:18 +03:00
|
|
|
idA := strings.TrimSpace(out)
|
2015-04-18 19:46:47 +03:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
|
2015-04-06 16:21:18 +03:00
|
|
|
idB := strings.TrimSpace(out)
|
2015-04-18 19:46:47 +03: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 06:47:54 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
|
2014-07-09 00:27:58 +04:00
|
|
|
var (
|
|
|
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
|
|
|
result []string
|
|
|
|
)
|
2015-04-18 19:46:47 +03: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")
|
2014-07-09 00:27:58 +04:00
|
|
|
links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
|
2014-06-27 12:26:02 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-06-27 12:26:02 +04:00
|
|
|
}
|
2014-07-09 00:27:58 +04:00
|
|
|
|
|
|
|
err = unmarshalJSON([]byte(links), &result)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-07-09 00:27:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
output := convertSliceOfStringsToMap(result)
|
|
|
|
|
2014-12-04 04:52:06 +03:00
|
|
|
equal := reflect.DeepEqual(output, expected)
|
2014-07-09 00:27:58 +04:00
|
|
|
|
|
|
|
if !equal {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Links %s, expected %s", result, expected)
|
2014-06-27 12:26:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
|
2014-07-23 02:13:52 +04:00
|
|
|
var (
|
|
|
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
|
|
|
result []string
|
|
|
|
)
|
2015-04-18 19:46:47 +03: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")
|
2014-07-23 02:13:52 +04:00
|
|
|
links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
|
2014-06-27 12:26:02 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-06-27 12:26:02 +04:00
|
|
|
}
|
2014-07-23 02:13:52 +04:00
|
|
|
|
|
|
|
err = unmarshalJSON([]byte(links), &result)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-06-27 12:26:02 +04:00
|
|
|
}
|
2014-07-23 02:13:52 +04:00
|
|
|
|
|
|
|
output := convertSliceOfStringsToMap(result)
|
|
|
|
|
2014-12-04 04:52:06 +03:00
|
|
|
equal := reflect.DeepEqual(output, expected)
|
2014-07-23 02:13:52 +04:00
|
|
|
|
|
|
|
if !equal {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Links %s, but expected %s", result, expected)
|
2014-07-23 02:13:52 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 12:26:02 +04:00
|
|
|
}
|
2014-11-07 02:25:14 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
|
2015-07-20 09:44:22 +03:00
|
|
|
|
|
|
|
dockerCmd(c, "create", "--name=first", "busybox", "top")
|
|
|
|
dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
|
|
|
|
dockerCmd(c, "start", "first")
|
|
|
|
|
2014-11-07 02:25:14 +03:00
|
|
|
}
|
2014-10-25 01:39:12 +04:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon, ExecSupport)
|
2014-10-25 01:39:12 +04:00
|
|
|
|
2015-07-20 09:44:22 +03:00
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
|
2014-10-25 01:39:12 +04:00
|
|
|
idOne := strings.TrimSpace(out)
|
|
|
|
|
2015-07-20 09:44:22 +03:00
|
|
|
out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
|
2014-10-25 01:39:12 +04:00
|
|
|
idTwo := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2015-02-16 22:32:25 +03:00
|
|
|
contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
|
2014-10-25 01:39:12 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, string(contentOne))
|
2014-10-25 01:39:12 +04:00
|
|
|
}
|
|
|
|
|
2015-02-16 22:32:25 +03:00
|
|
|
contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
|
2014-10-25 01:39:12 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, string(contentTwo))
|
2014-10-25 01:39:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(string(contentTwo), "onetwo") {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("Host is not present in updated hosts file", string(contentTwo))
|
2014-10-25 01:39:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-12-08 22:33:18 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon, ExecSupport)
|
2015-07-20 09:44:22 +03: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-22 01:34:08 +03:00
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
|
|
|
|
realIP, err := inspectField("one", "NetworkSettings.IPAddress")
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
2015-02-16 22:32:25 +03:00
|
|
|
content, err := readContainerFileWithExec(id, "/etc/hosts")
|
2015-01-22 01:34:08 +03:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, string(content))
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
|
|
|
getIP := func(hosts []byte, hostname string) string {
|
|
|
|
re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
|
|
|
|
matches := re.FindSubmatch(hosts)
|
|
|
|
if matches == nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Hostname %s have no matches in hosts", hostname)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
|
|
|
return string(matches[1])
|
|
|
|
}
|
|
|
|
if ip := getIP(content, "one"); ip != realIP {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
|
|
|
if ip := getIP(content, "onetwo"); ip != realIP {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
2015-07-20 09:44:22 +03:00
|
|
|
dockerCmd(c, "restart", "one")
|
2015-01-22 01:34:08 +03:00
|
|
|
realIP, err = inspectField("one", "NetworkSettings.IPAddress")
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
2015-02-16 22:32:25 +03:00
|
|
|
content, err = readContainerFileWithExec(id, "/etc/hosts")
|
2015-01-22 01:34:08 +03:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, string(content))
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
|
|
|
if ip := getIP(content, "one"); ip != realIP {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
|
|
|
if ip := getIP(content, "onetwo"); ip != realIP {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
|
2015-01-22 01:34:08 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 14:42:43 +03:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinksEnvs(c *check.C) {
|
2015-07-20 09:44:22 +03: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-04-25 14:42:43 +03:00
|
|
|
if !strings.Contains(out, "FIRST_ENV_e1=\n") ||
|
|
|
|
!strings.Contains(out, "FIRST_ENV_e2=v2") ||
|
|
|
|
!strings.Contains(out, "FIRST_ENV_e3=v3=v3") {
|
|
|
|
c.Fatalf("Incorrect output: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 23:02:14 +03:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
|
2015-07-20 09:44:22 +03:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
|
2015-05-07 23:02:14 +03:00
|
|
|
|
|
|
|
cid := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid), check.IsNil)
|
|
|
|
|
2015-07-20 09:44:22 +03:00
|
|
|
out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
|
2015-05-07 23:02:14 +03:00
|
|
|
|
|
|
|
cid2 := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(cid2), check.IsNil)
|
|
|
|
|
|
|
|
links, err := inspectFieldJSON(cid2, "HostConfig.Links")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(links, check.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
|
|
|
|
}
|