瀏覽代碼

Rewrite TestRunNetHost to compare namespaces

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
Alexander Morozov 10 年之前
父節點
當前提交
e98c08a88f
共有 1 個文件被更改,包括 20 次插入6 次删除
  1. 20 6
      integration-cli/docker_cli_run_test.go

+ 20 - 6
integration-cli/docker_cli_run_test.go

@@ -2708,18 +2708,32 @@ func TestRunNonLocalMacAddress(t *testing.T) {
 
 func TestRunNetHost(t *testing.T) {
 	defer deleteAllContainers()
-	iplinkHost, err := exec.Command("ip", "link", "list").CombinedOutput()
+	hostNet, err := os.Readlink("/proc/1/ns/net")
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	iplinkCont, err := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ip", "link", "list").CombinedOutput()
+	cmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "readlink", "/proc/self/ns/net")
+	out2, _, err := runCommandWithOutput(cmd)
 	if err != nil {
-		t.Fatal(err)
+		t.Fatal(err, out2)
 	}
 
-	if !bytes.Equal(iplinkHost, iplinkCont) {
-		t.Fatalf("Container network:\n%s\nis not equal to host network:\n%s", iplinkCont, iplinkHost)
+	out2 = strings.Trim(out2, "\n")
+	if hostNet != out2 {
+		t.Fatalf("Net namespace different with --net=host %s != %s\n", hostNet, out2)
 	}
-	logDone("run - host network")
+
+	cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/net")
+	out2, _, err = runCommandWithOutput(cmd)
+	if err != nil {
+		t.Fatal(err, out2)
+	}
+
+	out2 = strings.Trim(out2, "\n")
+	if hostNet == out2 {
+		t.Fatalf("Net namespace should be different without --net=host %s == %s\n", hostNet, out2)
+	}
+
+	logDone("run - net host mode")
 }