2014-06-23 22:03:08 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-09-19 13:47:59 -04:00
|
|
|
func TestPortList(t *testing.T) {
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-06-23 22:03:08 -04:00
|
|
|
// one port
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
firstID := strings.TrimSpace(out)
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
// three port
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "-d",
|
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
|
|
|
"busybox", "top")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
ID := strings.TrimSpace(out)
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", ID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
// 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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
ID = strings.TrimSpace(out)
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", ID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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)
|
2014-10-14 15:23:10 -04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|