浏览代码

Enhanced port integration-cli tests

THe port tests in integration-cli tests just for the port-mapping as
seen by Docker daemon. But it doesn't perform a more indepth testing by
checking for the exposed port on the host.

This change helps to fill that gap.

Signed-off-by: Madhu Venugopal <madhu@docker.com>
Madhu Venugopal 10 年之前
父节点
当前提交
448a1a7139
共有 1 个文件被更改,包括 83 次插入0 次删除
  1. 83 0
      integration-cli/docker_cli_port_test.go

+ 83 - 0
integration-cli/docker_cli_port_test.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"net"
 	"os/exec"
 	"sort"
 	"strings"
@@ -145,3 +146,85 @@ func assertPortList(t *testing.T, out string, expected []string) bool {
 
 	return true
 }
+
+func TestPortHostBinding(t *testing.T) {
+	defer deleteAllContainers()
+
+	runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
+		"nc", "-l", "-p", "80")
+	out, _, err := runCommandWithOutput(runCmd)
+	if err != nil {
+		t.Fatal(out, err)
+	}
+	firstID := strings.TrimSpace(out)
+
+	runCmd = exec.Command(dockerBinary, "port", firstID, "80")
+	out, _, err = runCommandWithOutput(runCmd)
+	if err != nil {
+		t.Fatal(out, err)
+	}
+
+	if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
+		t.Error("Port list is not correct")
+	}
+
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
+		"nc", "localhost", "9876")
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
+		t.Fatal(out, err)
+	}
+
+	runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
+		t.Fatal(out, err)
+	}
+
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
+		"nc", "localhost", "9876")
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
+		t.Error("Port is still bound after the Container is removed")
+	}
+	logDone("port - test host binding done")
+}
+
+func TestPortExposeHostBinding(t *testing.T) {
+	defer deleteAllContainers()
+
+	runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
+		"nc", "-l", "-p", "80")
+	out, _, err := runCommandWithOutput(runCmd)
+	if err != nil {
+		t.Fatal(out, err)
+	}
+	firstID := strings.TrimSpace(out)
+
+	runCmd = exec.Command(dockerBinary, "port", firstID, "80")
+	out, _, err = runCommandWithOutput(runCmd)
+	if err != nil {
+		t.Fatal(out, err)
+	}
+
+	_, exposedPort, err := net.SplitHostPort(out)
+
+	if err != nil {
+		t.Fatal(out, err)
+	}
+
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
+		"nc", "localhost", strings.TrimSpace(exposedPort))
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
+		t.Fatal(out, err)
+	}
+
+	runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
+		t.Fatal(out, err)
+	}
+
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
+		"nc", "localhost", strings.TrimSpace(exposedPort))
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
+		t.Error("Port is still bound after the Container is removed")
+	}
+	logDone("port - test port expose done")
+}