Test for updating linked hosts on restart

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov 2015-01-21 14:34:08 -08:00
parent c2a25058e8
commit 606c71d424
1 changed files with 58 additions and 1 deletions

View File

@ -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")
}