|
@@ -1,14 +1,17 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "github.com/docker/docker/pkg/iptables"
|
|
|
+ "fmt"
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
"reflect"
|
|
|
+ "regexp"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
"time"
|
|
|
+
|
|
|
+ "github.com/docker/docker/pkg/iptables"
|
|
|
)
|
|
|
|
|
|
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")
|
|
|
}
|
|
|
+
|
|
|
+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")
|
|
|
+}
|