Browse code

Test for updating linked hosts on restart

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2015/01/22 07:34:08
Showing 1 changed files
... ...
@@ -1,14 +1,17 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"github.com/docker/docker/pkg/iptables"
4
+	"fmt"
5 5
 	"io/ioutil"
6 6
 	"os"
7 7
 	"os/exec"
8 8
 	"reflect"
9
+	"regexp"
9 10
 	"strings"
10 11
 	"testing"
11 12
 	"time"
13
+
14
+	"github.com/docker/docker/pkg/iptables"
12 15
 )
13 16
 
14 17
 func TestLinksEtcHostsRegularFile(t *testing.T) {
... ...
@@ -276,3 +279,57 @@ func TestLinksNetworkHostContainer(t *testing.T) {
276 276
 
277 277
 	logDone("link - error thrown when linking to container with --net host")
278 278
 }
279
+
280
+func TestLinksUpdateOnRestart(t *testing.T) {
281
+	defer deleteAllContainers()
282
+
283
+	if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "one", "busybox", "top").CombinedOutput(); err != nil {
284
+		t.Fatal(err, string(out))
285
+	}
286
+	out, err := exec.Command(dockerBinary, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top").CombinedOutput()
287
+	if err != nil {
288
+		t.Fatal(err, string(out))
289
+	}
290
+	id := strings.TrimSpace(string(out))
291
+
292
+	realIP, err := inspectField("one", "NetworkSettings.IPAddress")
293
+	if err != nil {
294
+		t.Fatal(err)
295
+	}
296
+	content, err := readContainerFile(id, "hosts")
297
+	if err != nil {
298
+		t.Fatal(err, string(content))
299
+	}
300
+	getIP := func(hosts []byte, hostname string) string {
301
+		re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
302
+		matches := re.FindSubmatch(hosts)
303
+		if matches == nil {
304
+			t.Fatalf("Hostname %s have no matches in hosts", hostname)
305
+		}
306
+		return string(matches[1])
307
+	}
308
+	if ip := getIP(content, "one"); ip != realIP {
309
+		t.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
310
+	}
311
+	if ip := getIP(content, "onetwo"); ip != realIP {
312
+		t.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
313
+	}
314
+	if out, err := exec.Command(dockerBinary, "restart", "one").CombinedOutput(); err != nil {
315
+		t.Fatal(err, string(out))
316
+	}
317
+	realIP, err = inspectField("one", "NetworkSettings.IPAddress")
318
+	if err != nil {
319
+		t.Fatal(err)
320
+	}
321
+	content, err = readContainerFile(id, "hosts")
322
+	if err != nil {
323
+		t.Fatal(err, string(content))
324
+	}
325
+	if ip := getIP(content, "one"); ip != realIP {
326
+		t.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
327
+	}
328
+	if ip := getIP(content, "onetwo"); ip != realIP {
329
+		t.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
330
+	}
331
+	logDone("link - ensure containers hosts files are updated on restart")
332
+}