github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/integration-cli/docker_cli_port_test.go (about) 1 package main 2 3 import ( 4 "net" 5 "os/exec" 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 runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top") 16 out, _, err := runCommandWithOutput(runCmd) 17 if err != nil { 18 c.Fatal(out, err) 19 } 20 firstID := strings.TrimSpace(out) 21 22 runCmd = exec.Command(dockerBinary, "port", firstID, "80") 23 out, _, err = runCommandWithOutput(runCmd) 24 if err != nil { 25 c.Fatal(out, err) 26 } 27 28 if !assertPortList(c, out, []string{"0.0.0.0:9876"}) { 29 c.Error("Port list is not correct") 30 } 31 32 runCmd = exec.Command(dockerBinary, "port", firstID) 33 out, _, err = runCommandWithOutput(runCmd) 34 if err != nil { 35 c.Fatal(out, err) 36 } 37 38 if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) { 39 c.Error("Port list is not correct") 40 } 41 runCmd = exec.Command(dockerBinary, "rm", "-f", firstID) 42 if out, _, err = runCommandWithOutput(runCmd); err != nil { 43 c.Fatal(out, err) 44 } 45 46 // three port 47 runCmd = exec.Command(dockerBinary, "run", "-d", 48 "-p", "9876:80", 49 "-p", "9877:81", 50 "-p", "9878:82", 51 "busybox", "top") 52 out, _, err = runCommandWithOutput(runCmd) 53 if err != nil { 54 c.Fatal(out, err) 55 } 56 ID := strings.TrimSpace(out) 57 58 runCmd = exec.Command(dockerBinary, "port", ID, "80") 59 out, _, err = runCommandWithOutput(runCmd) 60 if err != nil { 61 c.Fatal(out, err) 62 } 63 64 if !assertPortList(c, out, []string{"0.0.0.0:9876"}) { 65 c.Error("Port list is not correct") 66 } 67 68 runCmd = exec.Command(dockerBinary, "port", ID) 69 out, _, err = runCommandWithOutput(runCmd) 70 if err != nil { 71 c.Fatal(out, err) 72 } 73 74 if !assertPortList(c, out, []string{ 75 "80/tcp -> 0.0.0.0:9876", 76 "81/tcp -> 0.0.0.0:9877", 77 "82/tcp -> 0.0.0.0:9878"}) { 78 c.Error("Port list is not correct") 79 } 80 runCmd = exec.Command(dockerBinary, "rm", "-f", ID) 81 out, _, err = runCommandWithOutput(runCmd) 82 if err != nil { 83 c.Fatal(out, err) 84 } 85 86 // more and one port mapped to the same container port 87 runCmd = exec.Command(dockerBinary, "run", "-d", 88 "-p", "9876:80", 89 "-p", "9999:80", 90 "-p", "9877:81", 91 "-p", "9878:82", 92 "busybox", "top") 93 out, _, err = runCommandWithOutput(runCmd) 94 if err != nil { 95 c.Fatal(out, err) 96 } 97 ID = strings.TrimSpace(out) 98 99 runCmd = exec.Command(dockerBinary, "port", ID, "80") 100 out, _, err = runCommandWithOutput(runCmd) 101 if err != nil { 102 c.Fatal(out, err) 103 } 104 105 if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) { 106 c.Error("Port list is not correct") 107 } 108 109 runCmd = exec.Command(dockerBinary, "port", ID) 110 out, _, err = runCommandWithOutput(runCmd) 111 if err != nil { 112 c.Fatal(out, err) 113 } 114 115 if !assertPortList(c, out, []string{ 116 "80/tcp -> 0.0.0.0:9876", 117 "80/tcp -> 0.0.0.0:9999", 118 "81/tcp -> 0.0.0.0:9877", 119 "82/tcp -> 0.0.0.0:9878"}) { 120 c.Error("Port list is not correct\n", out) 121 } 122 runCmd = exec.Command(dockerBinary, "rm", "-f", ID) 123 if out, _, err = runCommandWithOutput(runCmd); err != nil { 124 c.Fatal(out, err) 125 } 126 127 } 128 129 func assertPortList(c *check.C, 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 c.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 c.Error("|" + lines[i] + "!=" + expected[i] + "|") 142 return false 143 } 144 } 145 146 return true 147 } 148 149 func (s *DockerSuite) TestPortHostBinding(c *check.C) { 150 runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", 151 "nc", "-l", "-p", "80") 152 out, _, err := runCommandWithOutput(runCmd) 153 if err != nil { 154 c.Fatal(out, err) 155 } 156 firstID := strings.TrimSpace(out) 157 158 runCmd = exec.Command(dockerBinary, "port", firstID, "80") 159 out, _, err = runCommandWithOutput(runCmd) 160 if err != nil { 161 c.Fatal(out, err) 162 } 163 164 if !assertPortList(c, out, []string{"0.0.0.0:9876"}) { 165 c.Error("Port list is not correct") 166 } 167 168 runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", 169 "nc", "localhost", "9876") 170 if out, _, err = runCommandWithOutput(runCmd); err != nil { 171 c.Fatal(out, err) 172 } 173 174 runCmd = exec.Command(dockerBinary, "rm", "-f", firstID) 175 if out, _, err = runCommandWithOutput(runCmd); err != nil { 176 c.Fatal(out, err) 177 } 178 179 runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", 180 "nc", "localhost", "9876") 181 if out, _, err = runCommandWithOutput(runCmd); err == nil { 182 c.Error("Port is still bound after the Container is removed") 183 } 184 } 185 186 func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) { 187 runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox", 188 "nc", "-l", "-p", "80") 189 out, _, err := runCommandWithOutput(runCmd) 190 if err != nil { 191 c.Fatal(out, err) 192 } 193 firstID := strings.TrimSpace(out) 194 195 runCmd = exec.Command(dockerBinary, "port", firstID, "80") 196 out, _, err = runCommandWithOutput(runCmd) 197 if err != nil { 198 c.Fatal(out, err) 199 } 200 201 _, exposedPort, err := net.SplitHostPort(out) 202 203 if err != nil { 204 c.Fatal(out, err) 205 } 206 207 runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", 208 "nc", "localhost", strings.TrimSpace(exposedPort)) 209 if out, _, err = runCommandWithOutput(runCmd); err != nil { 210 c.Fatal(out, err) 211 } 212 213 runCmd = exec.Command(dockerBinary, "rm", "-f", firstID) 214 if out, _, err = runCommandWithOutput(runCmd); err != nil { 215 c.Fatal(out, err) 216 } 217 218 runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", 219 "nc", "localhost", strings.TrimSpace(exposedPort)) 220 if out, _, err = runCommandWithOutput(runCmd); err == nil { 221 c.Error("Port is still bound after the Container is removed") 222 } 223 }