github.com/rentongzhang/docker@v1.8.2-rc1/integration-cli/docker_cli_port_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "regexp" 6 "sort" 7 "strings" 8 9 "github.com/go-check/check" 10 ) 11 12 func (s *DockerSuite) TestPortList(c *check.C) { 13 14 // one port 15 out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top") 16 firstID := strings.TrimSpace(out) 17 18 out, _ = dockerCmd(c, "port", firstID, "80") 19 20 if !assertPortList(c, out, []string{"0.0.0.0:9876"}) { 21 c.Error("Port list is not correct") 22 } 23 24 out, _ = dockerCmd(c, "port", firstID) 25 26 if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) { 27 c.Error("Port list is not correct") 28 } 29 dockerCmd(c, "rm", "-f", firstID) 30 31 // three port 32 out, _ = dockerCmd(c, "run", "-d", 33 "-p", "9876:80", 34 "-p", "9877:81", 35 "-p", "9878:82", 36 "busybox", "top") 37 ID := strings.TrimSpace(out) 38 39 out, _ = dockerCmd(c, "port", ID, "80") 40 41 if !assertPortList(c, out, []string{"0.0.0.0:9876"}) { 42 c.Error("Port list is not correct") 43 } 44 45 out, _ = dockerCmd(c, "port", ID) 46 47 if !assertPortList(c, out, []string{ 48 "80/tcp -> 0.0.0.0:9876", 49 "81/tcp -> 0.0.0.0:9877", 50 "82/tcp -> 0.0.0.0:9878"}) { 51 c.Error("Port list is not correct") 52 } 53 dockerCmd(c, "rm", "-f", ID) 54 55 // more and one port mapped to the same container port 56 out, _ = dockerCmd(c, "run", "-d", 57 "-p", "9876:80", 58 "-p", "9999:80", 59 "-p", "9877:81", 60 "-p", "9878:82", 61 "busybox", "top") 62 ID = strings.TrimSpace(out) 63 64 out, _ = dockerCmd(c, "port", ID, "80") 65 66 if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) { 67 c.Error("Port list is not correct") 68 } 69 70 out, _ = dockerCmd(c, "port", ID) 71 72 if !assertPortList(c, out, []string{ 73 "80/tcp -> 0.0.0.0:9876", 74 "80/tcp -> 0.0.0.0:9999", 75 "81/tcp -> 0.0.0.0:9877", 76 "82/tcp -> 0.0.0.0:9878"}) { 77 c.Error("Port list is not correct\n", out) 78 } 79 dockerCmd(c, "rm", "-f", ID) 80 81 } 82 83 func assertPortList(c *check.C, out string, expected []string) bool { 84 //lines := strings.Split(out, "\n") 85 lines := strings.Split(strings.Trim(out, "\n "), "\n") 86 if len(lines) != len(expected) { 87 c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected)) 88 return false 89 } 90 sort.Strings(lines) 91 sort.Strings(expected) 92 93 for i := 0; i < len(expected); i++ { 94 if lines[i] != expected[i] { 95 c.Error("|" + lines[i] + "!=" + expected[i] + "|") 96 return false 97 } 98 } 99 100 return true 101 } 102 103 func stopRemoveContainer(id string, c *check.C) { 104 dockerCmd(c, "rm", "-f", id) 105 } 106 107 func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) { 108 // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports 109 port1 := 80 110 port2 := 443 111 expose1 := fmt.Sprintf("--expose=%d", port1) 112 expose2 := fmt.Sprintf("--expose=%d", port2) 113 dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5") 114 115 // Check docker ps o/p for last created container reports the unpublished ports 116 unpPort1 := fmt.Sprintf("%d/tcp", port1) 117 unpPort2 := fmt.Sprintf("%d/tcp", port2) 118 out, _ := dockerCmd(c, "ps", "-n=1") 119 if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) { 120 c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out) 121 } 122 123 // Run the container forcing to publish the exposed ports 124 dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5") 125 126 // Check docker ps o/p for last created container reports the exposed ports in the port bindings 127 expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1) 128 expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2) 129 out, _ = dockerCmd(c, "ps", "-n=1") 130 if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) { 131 c.Errorf("Cannot find expected port binding ports(s) (0.0.0.0:xxxxx->%s, 0.0.0.0:xxxxx->%s) in docker ps output:\n%s", 132 unpPort1, unpPort2, out) 133 } 134 135 // Run the container specifying explicit port bindings for the exposed ports 136 offset := 10000 137 pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1) 138 pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2) 139 out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5") 140 id := strings.TrimSpace(out) 141 142 // Check docker ps o/p for last created container reports the specified port mappings 143 expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1) 144 expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2) 145 out, _ = dockerCmd(c, "ps", "-n=1") 146 if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) { 147 c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out) 148 } 149 // Remove container now otherwise it will interfeer with next test 150 stopRemoveContainer(id, c) 151 152 // Run the container with explicit port bindings and no exposed ports 153 out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5") 154 id = strings.TrimSpace(out) 155 156 // Check docker ps o/p for last created container reports the specified port mappings 157 out, _ = dockerCmd(c, "ps", "-n=1") 158 if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) { 159 c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out) 160 } 161 // Remove container now otherwise it will interfeer with next test 162 stopRemoveContainer(id, c) 163 164 // Run the container with one unpublished exposed port and one explicit port binding 165 dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5") 166 167 // Check docker ps o/p for last created container reports the specified unpublished port and port mapping 168 out, _ = dockerCmd(c, "ps", "-n=1") 169 if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) { 170 c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out) 171 } 172 }