2014-04-07 22:34:07 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-05-22 02:07:40 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-04-07 22:34:07 +04:00
|
|
|
"os/exec"
|
2014-05-22 02:07:40 +04:00
|
|
|
"strings"
|
2014-04-07 22:34:07 +04:00
|
|
|
"testing"
|
2014-06-27 12:26:02 +04:00
|
|
|
|
2014-07-25 02:19:50 +04: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) {
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAllContainers()
|
|
|
|
|
|
|
|
logDone("link - /etc/hosts is a regular file")
|
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksEtcHostsContentMatch(t *testing.T) {
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteAllContainers()
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-19 21:46:43 +04:00
|
|
|
func TestLinksPingLinkedContainers(t *testing.T) {
|
2014-05-06 06:51:03 +04:00
|
|
|
var out string
|
2014-11-24 18:32:38 +03:00
|
|
|
out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
|
2014-04-07 22:34:07 +04:00
|
|
|
idA := stripTrailingCharacters(out)
|
2014-11-24 18:32:38 +03:00
|
|
|
out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
|
2014-04-07 22:34:07 +04:00
|
|
|
idB := stripTrailingCharacters(out)
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "run", "--rm", "--link", "container1: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)
|
2014-04-07 22:34:07 +04:00
|
|
|
deleteAllContainers()
|
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-09-19 21:46:43 +04:00
|
|
|
func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
|
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
|
|
|
|
2014-10-06 18:26:55 +04:00
|
|
|
sourceRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
|
|
|
|
destinationRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
|
2014-05-10 21:27:24 +04:00
|
|
|
if !iptables.Exists(sourceRule...) || !iptables.Exists(destinationRule...) {
|
|
|
|
t.Fatal("Iptables rules not found")
|
|
|
|
}
|
|
|
|
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "rm", "--link", "parent/http")
|
2014-05-10 21:27:24 +04:00
|
|
|
if iptables.Exists(sourceRule...) || iptables.Exists(destinationRule...) {
|
|
|
|
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
|
|
|
deleteAllContainers()
|
|
|
|
|
|
|
|
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()
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sleep", "10")
|
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)
|
|
|
|
|
|
|
|
equal := deepEqual(expected, output)
|
|
|
|
|
|
|
|
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()
|
2014-11-24 18:32:38 +03:00
|
|
|
dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
|
|
|
|
dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
|
|
|
|
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)
|
|
|
|
|
|
|
|
equal := deepEqual(expected, output)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
logDone("link - container start not failing on updating stopped parent links")
|
|
|
|
}
|