123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package main
- import (
- "os/exec"
- "sort"
- "strings"
- "testing"
- )
- func TestPortList(t *testing.T) {
- // one port
- runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
- out, _, err := runCommandWithOutput(runCmd)
- if err != nil {
- t.Fatal(out, err)
- }
- firstID := stripTrailingCharacters(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, "port", firstID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- t.Fatal(out, err)
- }
- 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)
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- t.Fatal(out, err)
- }
- // three port
- runCmd = exec.Command(dockerBinary, "run", "-d",
- "-p", "9876:80",
- "-p", "9877:81",
- "-p", "9878:82",
- "busybox", "top")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- t.Fatal(out, err)
- }
- ID := stripTrailingCharacters(out)
- runCmd = exec.Command(dockerBinary, "port", ID, "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, "port", ID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- t.Fatal(out, err)
- }
- 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)
- if err != nil {
- t.Fatal(out, err)
- }
- // 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)
- if err != nil {
- t.Fatal(out, err)
- }
- ID = stripTrailingCharacters(out)
- runCmd = exec.Command(dockerBinary, "port", ID, "80")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- t.Fatal(out, err)
- }
- 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)
- if err != nil {
- t.Fatal(out, err)
- }
- 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)
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- t.Fatal(out, err)
- }
- 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
- }
|