2014-04-07 22:34:07 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-01-22 01:34:08 +03:00
|
|
|
"fmt"
|
2014-05-22 02:07:40 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-04-07 22:34:07 +04:00
|
|
|
"os/exec"
|
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-04-07 22:34:07 +04:00
|
|
|
"testing"
|
2014-10-25 01:39:12 +04:00
|
|
|
"time"
|
2015-01-22 01:34:08 +03:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/iptables"
|
2014-04-07 22:34:07 +04:00
|
|
|
)
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksEtcHostsRegularFile(t *testing.T) {
|
2015-02-20 09:56:02 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-05-22 02:07:40 +04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 23:50:35 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-05-22 02:07:40 +04:00
|
|
|
|
|
|
|
if !strings.HasPrefix(out, "-") {
|
|
|
|
t.Errorf("/etc/hosts should be a regular file")
|
|
|
|
}
|
|
|
|
logDone("link - /etc/hosts is a regular file")
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksEtcHostsContentMatch(t *testing.T) {
|
2015-02-14 01:53:39 +03:00
|
|
|
testRequires(t, SameHostDaemon)
|
2015-02-26 07:16:44 +03:00
|
|
|
defer deleteAllContainers()
|
2015-02-14 01:53:39 +03:00
|
|
|
|
2014-05-22 02:07:40 +04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 23:50:35 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-05-22 02:07:40 +04:00
|
|
|
|
|
|
|
hosts, err := ioutil.ReadFile("/etc/hosts")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
t.Skip("/etc/hosts does not exist, skip this test")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out != string(hosts) {
|
|
|
|
t.Errorf("container")
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("link - /etc/hosts matches hosts copy")
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksPingUnlinkedContainers(t *testing.T) {
|
2014-04-07 22:34:07 +04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
|
|
|
exitCode, err := runCommand(runCmd)
|
|
|
|
|
|
|
|
if exitCode == 0 {
|
|
|
|
t.Fatal("run ping did not fail")
|
|
|
|
} else if exitCode != 1 {
|
2014-10-14 23:50:35 +04:00
|
|
|
t.Fatalf("run ping failed with errors: %v", err)
|
2014-04-07 22:34:07 +04:00
|
|
|
}
|
2014-08-28 09:25:57 +04:00
|
|
|
|
|
|
|
logDone("links - ping unlinked container")
|
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
|
|
|
|
func TestLinksInvalidContainerTarget(t *testing.T) {
|
2015-02-20 09:56:02 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2015-02-13 23:14:38 +03:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--link", "bogus:alias", "busybox", "true")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("an invalid container target should produce an error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, "Could not get container") {
|
2015-03-26 06:18:42 +03:00
|
|
|
t.Fatalf("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
|
2015-02-13 23:14:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("links - linking to non-existent container should not work")
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksPingLinkedContainers(t *testing.T) {
|
2015-02-20 09:56:02 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2015-02-16 08:00:51 +03:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
|
|
|
|
if _, err := runCommand(runCmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
|
|
|
|
if _, err := runCommand(runCmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
|
|
|
|
// 2. Ping by container name
|
|
|
|
dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
|
|
|
|
// 3. Ping by hostname
|
|
|
|
dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
|
|
|
|
|
2014-08-28 09:25:57 +04:00
|
|
|
logDone("links - ping linked container")
|
2014-04-07 22:34:07 +04:00
|
|
|
}
|
2014-05-10 21:27:24 +04:00
|
|
|
|
2014-10-05 06:47:54 +04:00
|
|
|
func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
|
2015-02-20 09:56:02 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2015-04-14 08:16:19 +03:00
|
|
|
out, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "top")
|
2015-04-06 16:21:18 +03:00
|
|
|
idA := strings.TrimSpace(out)
|
2015-04-14 08:16:19 +03:00
|
|
|
out, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "top")
|
2015-04-06 16:21:18 +03:00
|
|
|
idB := strings.TrimSpace(out)
|
2014-10-05 06:47:54 +04:00
|
|
|
dockerCmd(t, "rename", "container1", "container_new")
|
|
|
|
dockerCmd(t, "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(t, "kill", idA)
|
|
|
|
dockerCmd(t, "kill", idB)
|
|
|
|
|
|
|
|
logDone("links - ping linked container after rename")
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
|
2015-02-14 01:53:39 +03:00
|
|
|
testRequires(t, SameHostDaemon)
|
2015-02-26 07:16:44 +03:00
|
|
|
defer deleteAllContainers()
|
2015-02-14 01:53:39 +03:00
|
|
|
|
2015-04-09 02:32:45 +03:00
|
|
|
dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
|
2014-05-10 21:27:24 +04:00
|
|
|
|
2014-10-06 18:26:55 +04:00
|
|
|
childIP := findContainerIP(t, "child")
|
|
|
|
parentIP := findContainerIP(t, "parent")
|
2014-05-10 21:27:24 +04:00
|
|
|
|
2015-02-14 06:10:14 +03:00
|
|
|
sourceRule := []string{"-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
|
|
|
|
destinationRule := []string{"-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
|
|
|
|
if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
|
2014-05-10 21:27:24 +04:00
|
|
|
t.Fatal("Iptables rules not found")
|
|
|
|
}
|
|
|
|
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "rm", "--link", "parent/http")
|
2015-02-14 06:10:14 +03:00
|
|
|
if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
|
2014-05-10 21:27:24 +04:00
|
|
|
t.Fatal("Iptables rules should be removed when unlink")
|
|
|
|
}
|
|
|
|
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "kill", "child")
|
|
|
|
dockerCmd(t, "kill", "parent")
|
2014-05-10 21:27:24 +04:00
|
|
|
|
|
|
|
logDone("link - verify iptables when link and unlink")
|
|
|
|
}
|
2014-06-27 12:26:02 +04:00
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksInspectLinksStarted(t *testing.T) {
|
2014-07-09 00:27:58 +04:00
|
|
|
var (
|
|
|
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
|
|
|
result []string
|
|
|
|
)
|
2014-06-27 12:26:02 +04:00
|
|
|
defer deleteAllContainers()
|
2015-04-09 02:32:45 +03:00
|
|
|
dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "top")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "top")
|
|
|
|
dockerCmd(t, "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 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-09 00:27:58 +04:00
|
|
|
|
|
|
|
err = unmarshalJSON([]byte(links), &result)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
t.Fatalf("Links %s, expected %s", result, expected)
|
2014-06-27 12:26:02 +04:00
|
|
|
}
|
|
|
|
logDone("link - links in started container inspect")
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksInspectLinksStopped(t *testing.T) {
|
2014-07-23 02:13:52 +04:00
|
|
|
var (
|
|
|
|
expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
|
|
|
|
result []string
|
|
|
|
)
|
2014-06-27 12:26:02 +04:00
|
|
|
defer deleteAllContainers()
|
2015-04-09 02:32:45 +03:00
|
|
|
dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "top")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "top")
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "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 {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-23 02:13:52 +04:00
|
|
|
|
|
|
|
err = unmarshalJSON([]byte(links), &result)
|
|
|
|
if err != nil {
|
|
|
|
t.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 {
|
|
|
|
t.Fatalf("Links %s, but expected %s", result, expected)
|
|
|
|
}
|
|
|
|
|
2014-06-27 12:26:02 +04:00
|
|
|
logDone("link - links in stopped container inspect")
|
|
|
|
}
|
2014-11-07 02:25:14 +03:00
|
|
|
|
|
|
|
func TestLinksNotStartedParentNotFail(t *testing.T) {
|
|
|
|
defer deleteAllContainers()
|
|
|
|
runCmd := exec.Command(dockerBinary, "create", "--name=first", "busybox", "top")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
|
|
|
runCmd = exec.Command(dockerBinary, "create", "--name=second", "--link=first:first", "busybox", "top")
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
|
|
|
runCmd = exec.Command(dockerBinary, "start", "first")
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-02-10 02:01:52 +03:00
|
|
|
logDone("link - container start successfully updating stopped parent links")
|
2014-11-07 02:25:14 +03:00
|
|
|
}
|
2014-10-25 01:39:12 +04:00
|
|
|
|
|
|
|
func TestLinksHostsFilesInject(t *testing.T) {
|
2015-02-21 10:24:30 +03:00
|
|
|
testRequires(t, SameHostDaemon, ExecSupport)
|
2015-02-14 01:53:39 +03:00
|
|
|
|
2014-10-25 01:39:12 +04:00
|
|
|
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)
|
|
|
|
|
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 {
|
|
|
|
t.Fatal(err, string(contentOne))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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.")
|
|
|
|
}
|
2014-12-08 22:33:18 +03:00
|
|
|
|
|
|
|
func TestLinksNetworkHostContainer(t *testing.T) {
|
|
|
|
defer deleteAllContainers()
|
|
|
|
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
|
|
|
|
if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior.") {
|
|
|
|
t.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
logDone("link - error thrown when linking to container with --net host")
|
|
|
|
}
|
2015-01-22 01:34:08 +03:00
|
|
|
|
|
|
|
func TestLinksUpdateOnRestart(t *testing.T) {
|
2015-02-21 10:24:30 +03:00
|
|
|
testRequires(t, SameHostDaemon, ExecSupport)
|
2015-02-14 01:53:39 +03:00
|
|
|
|
2015-01-22 01:34:08 +03:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
|
|
|
if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "one", "busybox", "top").CombinedOutput(); err != nil {
|
|
|
|
t.Fatal(err, string(out))
|
|
|
|
}
|
|
|
|
out, err := exec.Command(dockerBinary, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err, string(out))
|
|
|
|
}
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
|
|
|
|
realIP, err := inspectField("one", "NetworkSettings.IPAddress")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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 {
|
|
|
|
t.Fatal(err, string(content))
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
t.Fatalf("Hostname %s have no matches in hosts", hostname)
|
|
|
|
}
|
|
|
|
return string(matches[1])
|
|
|
|
}
|
|
|
|
if ip := getIP(content, "one"); ip != realIP {
|
|
|
|
t.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
|
|
|
|
}
|
|
|
|
if ip := getIP(content, "onetwo"); ip != realIP {
|
|
|
|
t.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
|
|
|
|
}
|
|
|
|
if out, err := exec.Command(dockerBinary, "restart", "one").CombinedOutput(); err != nil {
|
|
|
|
t.Fatal(err, string(out))
|
|
|
|
}
|
|
|
|
realIP, err = inspectField("one", "NetworkSettings.IPAddress")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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 {
|
|
|
|
t.Fatal(err, string(content))
|
|
|
|
}
|
|
|
|
if ip := getIP(content, "one"); ip != realIP {
|
|
|
|
t.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
|
|
|
|
}
|
|
|
|
if ip := getIP(content, "onetwo"); ip != realIP {
|
|
|
|
t.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
|
|
|
|
}
|
|
|
|
logDone("link - ensure containers hosts files are updated on restart")
|
|
|
|
}
|