github.com/pritambaral/docker@v1.4.2-0.20150120174542-b2fe1b3dd952/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  	// one port
    12  	runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
    13  	out, _, err := runCommandWithOutput(runCmd)
    14  	if err != nil {
    15  		t.Fatal(out, err)
    16  	}
    17  	firstID := stripTrailingCharacters(out)
    18  
    19  	runCmd = exec.Command(dockerBinary, "port", firstID, "80")
    20  	out, _, err = runCommandWithOutput(runCmd)
    21  	if err != nil {
    22  		t.Fatal(out, err)
    23  	}
    24  
    25  	if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
    26  		t.Error("Port list is not correct")
    27  	}
    28  
    29  	runCmd = exec.Command(dockerBinary, "port", firstID)
    30  	out, _, err = runCommandWithOutput(runCmd)
    31  	if err != nil {
    32  		t.Fatal(out, err)
    33  	}
    34  
    35  	if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
    36  		t.Error("Port list is not correct")
    37  	}
    38  	runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
    39  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    40  		t.Fatal(out, err)
    41  	}
    42  
    43  	// three port
    44  	runCmd = exec.Command(dockerBinary, "run", "-d",
    45  		"-p", "9876:80",
    46  		"-p", "9877:81",
    47  		"-p", "9878:82",
    48  		"busybox", "top")
    49  	out, _, err = runCommandWithOutput(runCmd)
    50  	if err != nil {
    51  		t.Fatal(out, err)
    52  	}
    53  	ID := stripTrailingCharacters(out)
    54  
    55  	runCmd = exec.Command(dockerBinary, "port", ID, "80")
    56  	out, _, err = runCommandWithOutput(runCmd)
    57  	if err != nil {
    58  		t.Fatal(out, err)
    59  	}
    60  
    61  	if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
    62  		t.Error("Port list is not correct")
    63  	}
    64  
    65  	runCmd = exec.Command(dockerBinary, "port", ID)
    66  	out, _, err = runCommandWithOutput(runCmd)
    67  	if err != nil {
    68  		t.Fatal(out, err)
    69  	}
    70  
    71  	if !assertPortList(t, out, []string{
    72  		"80/tcp -> 0.0.0.0:9876",
    73  		"81/tcp -> 0.0.0.0:9877",
    74  		"82/tcp -> 0.0.0.0:9878"}) {
    75  		t.Error("Port list is not correct")
    76  	}
    77  	runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
    78  	out, _, err = runCommandWithOutput(runCmd)
    79  	if err != nil {
    80  		t.Fatal(out, err)
    81  	}
    82  
    83  	// more and one port mapped to the same container port
    84  	runCmd = exec.Command(dockerBinary, "run", "-d",
    85  		"-p", "9876:80",
    86  		"-p", "9999:80",
    87  		"-p", "9877:81",
    88  		"-p", "9878:82",
    89  		"busybox", "top")
    90  	out, _, err = runCommandWithOutput(runCmd)
    91  	if err != nil {
    92  		t.Fatal(out, err)
    93  	}
    94  	ID = stripTrailingCharacters(out)
    95  
    96  	runCmd = exec.Command(dockerBinary, "port", ID, "80")
    97  	out, _, err = runCommandWithOutput(runCmd)
    98  	if err != nil {
    99  		t.Fatal(out, err)
   100  	}
   101  
   102  	if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
   103  		t.Error("Port list is not correct")
   104  	}
   105  
   106  	runCmd = exec.Command(dockerBinary, "port", ID)
   107  	out, _, err = runCommandWithOutput(runCmd)
   108  	if err != nil {
   109  		t.Fatal(out, err)
   110  	}
   111  
   112  	if !assertPortList(t, out, []string{
   113  		"80/tcp -> 0.0.0.0:9876",
   114  		"80/tcp -> 0.0.0.0:9999",
   115  		"81/tcp -> 0.0.0.0:9877",
   116  		"82/tcp -> 0.0.0.0:9878"}) {
   117  		t.Error("Port list is not correct\n", out)
   118  	}
   119  	runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
   120  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   121  		t.Fatal(out, err)
   122  	}
   123  
   124  	deleteAllContainers()
   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  }