github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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 testRange := func() { 82 // host port ranges used 83 IDs := make([]string, 3) 84 for i := 0; i < 3; i++ { 85 out, _ = dockerCmd(c, "run", "-d", 86 "-p", "9090-9092:80", 87 "busybox", "top") 88 IDs[i] = strings.TrimSpace(out) 89 90 out, _ = dockerCmd(c, "port", IDs[i]) 91 92 if !assertPortList(c, out, []string{ 93 fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i)}) { 94 c.Error("Port list is not correct\n", out) 95 } 96 } 97 98 // test port range exhaustion 99 out, _, err := dockerCmdWithError("run", "-d", 100 "-p", "9090-9092:80", 101 "busybox", "top") 102 if err == nil { 103 c.Errorf("Exhausted port range did not return an error. Out: %s", out) 104 } 105 106 for i := 0; i < 3; i++ { 107 dockerCmd(c, "rm", "-f", IDs[i]) 108 } 109 } 110 testRange() 111 // Verify we ran re-use port ranges after they are no longer in use. 112 testRange() 113 114 // test invalid port ranges 115 for _, invalidRange := range []string{"9090-9089:80", "9090-:80", "-9090:80"} { 116 out, _, err := dockerCmdWithError("run", "-d", 117 "-p", invalidRange, 118 "busybox", "top") 119 if err == nil { 120 c.Errorf("Port range should have returned an error. Out: %s", out) 121 } 122 } 123 124 // test host range:container range spec. 125 out, _ = dockerCmd(c, "run", "-d", 126 "-p", "9800-9803:80-83", 127 "busybox", "top") 128 ID = strings.TrimSpace(out) 129 130 out, _ = dockerCmd(c, "port", ID) 131 132 if !assertPortList(c, out, []string{ 133 "80/tcp -> 0.0.0.0:9800", 134 "81/tcp -> 0.0.0.0:9801", 135 "82/tcp -> 0.0.0.0:9802", 136 "83/tcp -> 0.0.0.0:9803"}) { 137 c.Error("Port list is not correct\n", out) 138 } 139 dockerCmd(c, "rm", "-f", ID) 140 141 // test mixing protocols in same port range 142 out, _ = dockerCmd(c, "run", "-d", 143 "-p", "8000-8080:80", 144 "-p", "8000-8080:80/udp", 145 "busybox", "top") 146 ID = strings.TrimSpace(out) 147 148 out, _ = dockerCmd(c, "port", ID) 149 150 if !assertPortList(c, out, []string{ 151 "80/tcp -> 0.0.0.0:8000", 152 "80/udp -> 0.0.0.0:8000"}) { 153 c.Error("Port list is not correct\n", out) 154 } 155 dockerCmd(c, "rm", "-f", ID) 156 } 157 158 func assertPortList(c *check.C, out string, expected []string) bool { 159 //lines := strings.Split(out, "\n") 160 lines := strings.Split(strings.Trim(out, "\n "), "\n") 161 if len(lines) != len(expected) { 162 c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected)) 163 return false 164 } 165 sort.Strings(lines) 166 sort.Strings(expected) 167 168 for i := 0; i < len(expected); i++ { 169 if lines[i] != expected[i] { 170 c.Error("|" + lines[i] + "!=" + expected[i] + "|") 171 return false 172 } 173 } 174 175 return true 176 } 177 178 func stopRemoveContainer(id string, c *check.C) { 179 dockerCmd(c, "rm", "-f", id) 180 } 181 182 func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) { 183 // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports 184 port1 := 80 185 port2 := 443 186 expose1 := fmt.Sprintf("--expose=%d", port1) 187 expose2 := fmt.Sprintf("--expose=%d", port2) 188 dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5") 189 190 // Check docker ps o/p for last created container reports the unpublished ports 191 unpPort1 := fmt.Sprintf("%d/tcp", port1) 192 unpPort2 := fmt.Sprintf("%d/tcp", port2) 193 out, _ := dockerCmd(c, "ps", "-n=1") 194 if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) { 195 c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out) 196 } 197 198 // Run the container forcing to publish the exposed ports 199 dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5") 200 201 // Check docker ps o/p for last created container reports the exposed ports in the port bindings 202 expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1) 203 expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2) 204 out, _ = dockerCmd(c, "ps", "-n=1") 205 if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) { 206 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", 207 unpPort1, unpPort2, out) 208 } 209 210 // Run the container specifying explicit port bindings for the exposed ports 211 offset := 10000 212 pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1) 213 pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2) 214 out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5") 215 id := strings.TrimSpace(out) 216 217 // Check docker ps o/p for last created container reports the specified port mappings 218 expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1) 219 expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2) 220 out, _ = dockerCmd(c, "ps", "-n=1") 221 if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) { 222 c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out) 223 } 224 // Remove container now otherwise it will interfeer with next test 225 stopRemoveContainer(id, c) 226 227 // Run the container with explicit port bindings and no exposed ports 228 out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5") 229 id = strings.TrimSpace(out) 230 231 // Check docker ps o/p for last created container reports the specified port mappings 232 out, _ = dockerCmd(c, "ps", "-n=1") 233 if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) { 234 c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out) 235 } 236 // Remove container now otherwise it will interfeer with next test 237 stopRemoveContainer(id, c) 238 239 // Run the container with one unpublished exposed port and one explicit port binding 240 dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5") 241 242 // Check docker ps o/p for last created container reports the specified unpublished port and port mapping 243 out, _ = dockerCmd(c, "ps", "-n=1") 244 if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) { 245 c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out) 246 } 247 }