github.com/scorpionis/docker@v1.6.0-rc7/integration-cli/docker_cli_port_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestPortList(t *testing.T) {
    11  	defer deleteAllContainers()
    12  
    13  	// one port
    14  	runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
    15  	out, _, err := runCommandWithOutput(runCmd)
    16  	if err != nil {
    17  		t.Fatal(out, err)
    18  	}
    19  	firstID := stripTrailingCharacters(out)
    20  
    21  	runCmd = exec.Command(dockerBinary, "port", firstID, "80")
    22  	out, _, err = runCommandWithOutput(runCmd)
    23  	if err != nil {
    24  		t.Fatal(out, err)
    25  	}
    26  
    27  	if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
    28  		t.Error("Port list is not correct")
    29  	}
    30  
    31  	runCmd = exec.Command(dockerBinary, "port", firstID)
    32  	out, _, err = runCommandWithOutput(runCmd)
    33  	if err != nil {
    34  		t.Fatal(out, err)
    35  	}
    36  
    37  	if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
    38  		t.Error("Port list is not correct")
    39  	}
    40  	runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
    41  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    42  		t.Fatal(out, err)
    43  	}
    44  
    45  	// three port
    46  	runCmd = exec.Command(dockerBinary, "run", "-d",
    47  		"-p", "9876:80",
    48  		"-p", "9877:81",
    49  		"-p", "9878:82",
    50  		"busybox", "top")
    51  	out, _, err = runCommandWithOutput(runCmd)
    52  	if err != nil {
    53  		t.Fatal(out, err)
    54  	}
    55  	ID := stripTrailingCharacters(out)
    56  
    57  	runCmd = exec.Command(dockerBinary, "port", ID, "80")
    58  	out, _, err = runCommandWithOutput(runCmd)
    59  	if err != nil {
    60  		t.Fatal(out, err)
    61  	}
    62  
    63  	if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
    64  		t.Error("Port list is not correct")
    65  	}
    66  
    67  	runCmd = exec.Command(dockerBinary, "port", ID)
    68  	out, _, err = runCommandWithOutput(runCmd)
    69  	if err != nil {
    70  		t.Fatal(out, err)
    71  	}
    72  
    73  	if !assertPortList(t, out, []string{
    74  		"80/tcp -> 0.0.0.0:9876",
    75  		"81/tcp -> 0.0.0.0:9877",
    76  		"82/tcp -> 0.0.0.0:9878"}) {
    77  		t.Error("Port list is not correct")
    78  	}
    79  	runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
    80  	out, _, err = runCommandWithOutput(runCmd)
    81  	if err != nil {
    82  		t.Fatal(out, err)
    83  	}
    84  
    85  	// more and one port mapped to the same container port
    86  	runCmd = exec.Command(dockerBinary, "run", "-d",
    87  		"-p", "9876:80",
    88  		"-p", "9999:80",
    89  		"-p", "9877:81",
    90  		"-p", "9878:82",
    91  		"busybox", "top")
    92  	out, _, err = runCommandWithOutput(runCmd)
    93  	if err != nil {
    94  		t.Fatal(out, err)
    95  	}
    96  	ID = stripTrailingCharacters(out)
    97  
    98  	runCmd = exec.Command(dockerBinary, "port", ID, "80")
    99  	out, _, err = runCommandWithOutput(runCmd)
   100  	if err != nil {
   101  		t.Fatal(out, err)
   102  	}
   103  
   104  	if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
   105  		t.Error("Port list is not correct")
   106  	}
   107  
   108  	runCmd = exec.Command(dockerBinary, "port", ID)
   109  	out, _, err = runCommandWithOutput(runCmd)
   110  	if err != nil {
   111  		t.Fatal(out, err)
   112  	}
   113  
   114  	if !assertPortList(t, out, []string{
   115  		"80/tcp -> 0.0.0.0:9876",
   116  		"80/tcp -> 0.0.0.0:9999",
   117  		"81/tcp -> 0.0.0.0:9877",
   118  		"82/tcp -> 0.0.0.0:9878"}) {
   119  		t.Error("Port list is not correct\n", out)
   120  	}
   121  	runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
   122  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   123  		t.Fatal(out, err)
   124  	}
   125  
   126  	logDone("port - test port list")
   127  }
   128  
   129  func assertPortList(t *testing.T, out string, expected []string) bool {
   130  	//lines := strings.Split(out, "\n")
   131  	lines := strings.Split(strings.Trim(out, "\n "), "\n")
   132  	if len(lines) != len(expected) {
   133  		t.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
   134  		return false
   135  	}
   136  	sort.Strings(lines)
   137  	sort.Strings(expected)
   138  
   139  	for i := 0; i < len(expected); i++ {
   140  		if lines[i] != expected[i] {
   141  			t.Error("|" + lines[i] + "!=" + expected[i] + "|")
   142  			return false
   143  		}
   144  	}
   145  
   146  	return true
   147  }