Ver Fonte

Test for updating linked hosts on restart

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
Alexander Morozov há 10 anos atrás
pai
commit
606c71d424
1 ficheiros alterados com 58 adições e 1 exclusões
  1. 58 1
      integration-cli/docker_cli_links_test.go

+ 58 - 1
integration-cli/docker_cli_links_test.go

@@ -1,14 +1,17 @@
 package main
 package main
 
 
 import (
 import (
-	"github.com/docker/docker/pkg/iptables"
+	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"reflect"
 	"reflect"
+	"regexp"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
+
+	"github.com/docker/docker/pkg/iptables"
 )
 )
 
 
 func TestLinksEtcHostsRegularFile(t *testing.T) {
 func TestLinksEtcHostsRegularFile(t *testing.T) {
@@ -276,3 +279,57 @@ func TestLinksNetworkHostContainer(t *testing.T) {
 
 
 	logDone("link - error thrown when linking to container with --net host")
 	logDone("link - error thrown when linking to container with --net host")
 }
 }
+
+func TestLinksUpdateOnRestart(t *testing.T) {
+	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)
+	}
+	content, err := readContainerFile(id, "hosts")
+	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)
+	}
+	content, err = readContainerFile(id, "hosts")
+	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")
+}