summaryrefslogtreecommitdiff
path: root/integration-cli/docker_cli_port_test.go
diff options
context:
space:
mode:
authorSvenDowideit <SvenDowideit@home.org.au>2014-06-24 12:03:08 +1000
committerSvenDowideit <SvenDowideit@home.org.au>2014-08-29 18:31:51 +1000
commit4e340cedeccabcae8875dd35f8c984246d6c3bc1 (patch)
treea824aa2fd12f9312ddde8b7c61464f217aa0f023 /integration-cli/docker_cli_port_test.go
parent6eaac7d5710016d9717cb79b48c1bfe5f3ac92af (diff)
downloaddocker-4e340cedeccabcae8875dd35f8c984246d6c3bc1.tar.gz
List all ports when calling `docker port container`
Docker-DCO-1.1-Signed-off-by: SvenDowideit <SvenDowideit@home.org.au> (github: SvenDowideit)
Diffstat (limited to 'integration-cli/docker_cli_port_test.go')
-rw-r--r--integration-cli/docker_cli_port_test.go125
1 files changed, 125 insertions, 0 deletions
diff --git a/integration-cli/docker_cli_port_test.go b/integration-cli/docker_cli_port_test.go
new file mode 100644
index 0000000000..667889227f
--- /dev/null
+++ b/integration-cli/docker_cli_port_test.go
@@ -0,0 +1,125 @@
+package main
+
+import (
+ "os/exec"
+ "sort"
+ "strings"
+ "testing"
+)
+
+func TestListPorts(t *testing.T) {
+ // one port
+ runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
+ out, _, err := runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+ firstID := stripTrailingCharacters(out)
+
+ runCmd = exec.Command(dockerBinary, "port", firstID, "80")
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
+ t.Error("Port list is not correct")
+ }
+
+ runCmd = exec.Command(dockerBinary, "port", firstID)
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
+ t.Error("Port list is not correct")
+ }
+ runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ // three port
+ runCmd = exec.Command(dockerBinary, "run", "-d",
+ "-p", "9876:80",
+ "-p", "9877:81",
+ "-p", "9878:82",
+ "busybox", "top")
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+ ID := stripTrailingCharacters(out)
+
+ runCmd = exec.Command(dockerBinary, "port", ID, "80")
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
+ t.Error("Port list is not correct")
+ }
+
+ runCmd = exec.Command(dockerBinary, "port", ID)
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ if !assertPortList(t, out, []string{
+ "80/tcp -> 0.0.0.0:9876",
+ "81/tcp -> 0.0.0.0:9877",
+ "82/tcp -> 0.0.0.0:9878"}) {
+ t.Error("Port list is not correct")
+ }
+ runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ // more and one port mapped to the same container port
+ runCmd = exec.Command(dockerBinary, "run", "-d",
+ "-p", "9876:80",
+ "-p", "9999:80",
+ "-p", "9877:81",
+ "-p", "9878:82",
+ "busybox", "top")
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+ ID = stripTrailingCharacters(out)
+
+ runCmd = exec.Command(dockerBinary, "port", ID, "80")
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
+ t.Error("Port list is not correct")
+ }
+
+ runCmd = exec.Command(dockerBinary, "port", ID)
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ if !assertPortList(t, out, []string{
+ "80/tcp -> 0.0.0.0:9876",
+ "80/tcp -> 0.0.0.0:9999",
+ "81/tcp -> 0.0.0.0:9877",
+ "82/tcp -> 0.0.0.0:9878"}) {
+ t.Error("Port list is not correct\n", out)
+ }
+ runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
+ out, _, err = runCommandWithOutput(runCmd)
+ errorOut(err, t, out)
+
+ deleteAllContainers()
+
+ logDone("port - test port list")
+}
+
+func assertPortList(t *testing.T, out string, expected []string) bool {
+ //lines := strings.Split(out, "\n")
+ lines := strings.Split(strings.Trim(out, "\n "), "\n")
+ if len(lines) != len(expected) {
+ t.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
+ return false
+ }
+ sort.Strings(lines)
+ sort.Strings(expected)
+
+ for i := 0; i < len(expected); i++ {
+ if lines[i] != expected[i] {
+ t.Error("|" + lines[i] + "!=" + expected[i] + "|")
+ return false
+ }
+ }
+
+ return true
+}