docker_cli_port_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "github.com/docker/docker/integration-cli/checker"
  10. "github.com/go-check/check"
  11. )
  12. func (s *DockerSuite) TestPortList(c *check.C) {
  13. testRequires(c, DaemonIsLinux)
  14. // one port
  15. out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top")
  16. firstID := strings.TrimSpace(out)
  17. out, _ = dockerCmd(c, "port", firstID, "80")
  18. err := assertPortList(c, out, []string{"0.0.0.0:9876"})
  19. // Port list is not correct
  20. c.Assert(err, checker.IsNil)
  21. out, _ = dockerCmd(c, "port", firstID)
  22. err = assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"})
  23. // Port list is not correct
  24. c.Assert(err, checker.IsNil)
  25. dockerCmd(c, "rm", "-f", firstID)
  26. // three port
  27. out, _ = dockerCmd(c, "run", "-d",
  28. "-p", "9876:80",
  29. "-p", "9877:81",
  30. "-p", "9878:82",
  31. "busybox", "top")
  32. ID := strings.TrimSpace(out)
  33. out, _ = dockerCmd(c, "port", ID, "80")
  34. err = assertPortList(c, out, []string{"0.0.0.0:9876"})
  35. // Port list is not correct
  36. c.Assert(err, checker.IsNil)
  37. out, _ = dockerCmd(c, "port", ID)
  38. err = assertPortList(c, out, []string{
  39. "80/tcp -> 0.0.0.0:9876",
  40. "81/tcp -> 0.0.0.0:9877",
  41. "82/tcp -> 0.0.0.0:9878"})
  42. // Port list is not correct
  43. c.Assert(err, checker.IsNil)
  44. dockerCmd(c, "rm", "-f", ID)
  45. // more and one port mapped to the same container port
  46. out, _ = dockerCmd(c, "run", "-d",
  47. "-p", "9876:80",
  48. "-p", "9999:80",
  49. "-p", "9877:81",
  50. "-p", "9878:82",
  51. "busybox", "top")
  52. ID = strings.TrimSpace(out)
  53. out, _ = dockerCmd(c, "port", ID, "80")
  54. err = assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"})
  55. // Port list is not correct
  56. c.Assert(err, checker.IsNil)
  57. out, _ = dockerCmd(c, "port", ID)
  58. err = assertPortList(c, out, []string{
  59. "80/tcp -> 0.0.0.0:9876",
  60. "80/tcp -> 0.0.0.0:9999",
  61. "81/tcp -> 0.0.0.0:9877",
  62. "82/tcp -> 0.0.0.0:9878"})
  63. // Port list is not correct
  64. c.Assert(err, checker.IsNil)
  65. dockerCmd(c, "rm", "-f", ID)
  66. testRange := func() {
  67. // host port ranges used
  68. IDs := make([]string, 3)
  69. for i := 0; i < 3; i++ {
  70. out, _ = dockerCmd(c, "run", "-d",
  71. "-p", "9090-9092:80",
  72. "busybox", "top")
  73. IDs[i] = strings.TrimSpace(out)
  74. out, _ = dockerCmd(c, "port", IDs[i])
  75. err = assertPortList(c, out, []string{fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i)})
  76. // Port list is not correct
  77. c.Assert(err, checker.IsNil)
  78. }
  79. // test port range exhaustion
  80. out, _, err = dockerCmdWithError("run", "-d",
  81. "-p", "9090-9092:80",
  82. "busybox", "top")
  83. // Exhausted port range did not return an error
  84. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  85. for i := 0; i < 3; i++ {
  86. dockerCmd(c, "rm", "-f", IDs[i])
  87. }
  88. }
  89. testRange()
  90. // Verify we ran re-use port ranges after they are no longer in use.
  91. testRange()
  92. // test invalid port ranges
  93. for _, invalidRange := range []string{"9090-9089:80", "9090-:80", "-9090:80"} {
  94. out, _, err = dockerCmdWithError("run", "-d",
  95. "-p", invalidRange,
  96. "busybox", "top")
  97. // Port range should have returned an error
  98. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  99. }
  100. // test host range:container range spec.
  101. out, _ = dockerCmd(c, "run", "-d",
  102. "-p", "9800-9803:80-83",
  103. "busybox", "top")
  104. ID = strings.TrimSpace(out)
  105. out, _ = dockerCmd(c, "port", ID)
  106. err = assertPortList(c, out, []string{
  107. "80/tcp -> 0.0.0.0:9800",
  108. "81/tcp -> 0.0.0.0:9801",
  109. "82/tcp -> 0.0.0.0:9802",
  110. "83/tcp -> 0.0.0.0:9803"})
  111. // Port list is not correct
  112. c.Assert(err, checker.IsNil)
  113. dockerCmd(c, "rm", "-f", ID)
  114. // test mixing protocols in same port range
  115. out, _ = dockerCmd(c, "run", "-d",
  116. "-p", "8000-8080:80",
  117. "-p", "8000-8080:80/udp",
  118. "busybox", "top")
  119. ID = strings.TrimSpace(out)
  120. out, _ = dockerCmd(c, "port", ID)
  121. // Running this test multiple times causes the TCP port to increment.
  122. err = assertPortRange(c, out, []int{8000, 8080}, []int{8000, 8080})
  123. // Port list is not correct
  124. c.Assert(err, checker.IsNil)
  125. dockerCmd(c, "rm", "-f", ID)
  126. }
  127. func assertPortList(c *check.C, out string, expected []string) error {
  128. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  129. if len(lines) != len(expected) {
  130. return fmt.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
  131. }
  132. sort.Strings(lines)
  133. sort.Strings(expected)
  134. for i := 0; i < len(expected); i++ {
  135. if lines[i] != expected[i] {
  136. return fmt.Errorf("|" + lines[i] + "!=" + expected[i] + "|")
  137. }
  138. }
  139. return nil
  140. }
  141. func assertPortRange(c *check.C, out string, expectedTcp, expectedUdp []int) error {
  142. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  143. var validTcp, validUdp bool
  144. for _, l := range lines {
  145. // 80/tcp -> 0.0.0.0:8015
  146. port, err := strconv.Atoi(strings.Split(l, ":")[1])
  147. if err != nil {
  148. return err
  149. }
  150. if strings.Contains(l, "tcp") && expectedTcp != nil {
  151. if port < expectedTcp[0] || port > expectedTcp[1] {
  152. return fmt.Errorf("tcp port (%d) not in range expected range %d-%d", port, expectedTcp[0], expectedTcp[1])
  153. }
  154. validTcp = true
  155. }
  156. if strings.Contains(l, "udp") && expectedUdp != nil {
  157. if port < expectedUdp[0] || port > expectedUdp[1] {
  158. return fmt.Errorf("udp port (%d) not in range expected range %d-%d", port, expectedUdp[0], expectedUdp[1])
  159. }
  160. validUdp = true
  161. }
  162. }
  163. if !validTcp {
  164. return fmt.Errorf("tcp port not found")
  165. }
  166. if !validUdp {
  167. return fmt.Errorf("udp port not found")
  168. }
  169. return nil
  170. }
  171. func stopRemoveContainer(id string, c *check.C) {
  172. dockerCmd(c, "rm", "-f", id)
  173. }
  174. func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
  175. testRequires(c, DaemonIsLinux)
  176. // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
  177. port1 := 80
  178. port2 := 443
  179. expose1 := fmt.Sprintf("--expose=%d", port1)
  180. expose2 := fmt.Sprintf("--expose=%d", port2)
  181. dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
  182. // Check docker ps o/p for last created container reports the unpublished ports
  183. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  184. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  185. out, _ := dockerCmd(c, "ps", "-n=1")
  186. // Missing unpublished ports in docker ps output
  187. c.Assert(out, checker.Contains, unpPort1)
  188. // Missing unpublished ports in docker ps output
  189. c.Assert(out, checker.Contains, unpPort2)
  190. // Run the container forcing to publish the exposed ports
  191. dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
  192. // Check docker ps o/p for last created container reports the exposed ports in the port bindings
  193. expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
  194. expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
  195. out, _ = dockerCmd(c, "ps", "-n=1")
  196. // Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort1) in docker ps output
  197. c.Assert(expBndRegx1.MatchString(out), checker.Equals, true, check.Commentf("out: %s; unpPort1: %s", out, unpPort1))
  198. // Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort2) in docker ps output
  199. c.Assert(expBndRegx2.MatchString(out), checker.Equals, true, check.Commentf("out: %s; unpPort2: %s", out, unpPort2))
  200. // Run the container specifying explicit port bindings for the exposed ports
  201. offset := 10000
  202. pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
  203. pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
  204. out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
  205. id := strings.TrimSpace(out)
  206. // Check docker ps o/p for last created container reports the specified port mappings
  207. expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
  208. expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
  209. out, _ = dockerCmd(c, "ps", "-n=1")
  210. // Cannot find expected port binding (expBnd1) in docker ps output
  211. c.Assert(out, checker.Contains, expBnd1)
  212. // Cannot find expected port binding (expBnd2) in docker ps output
  213. c.Assert(out, checker.Contains, expBnd2)
  214. // Remove container now otherwise it will interfere with next test
  215. stopRemoveContainer(id, c)
  216. // Run the container with explicit port bindings and no exposed ports
  217. out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
  218. id = strings.TrimSpace(out)
  219. // Check docker ps o/p for last created container reports the specified port mappings
  220. out, _ = dockerCmd(c, "ps", "-n=1")
  221. // Cannot find expected port binding (expBnd1) in docker ps output
  222. c.Assert(out, checker.Contains, expBnd1)
  223. // Cannot find expected port binding (expBnd2) in docker ps output
  224. c.Assert(out, checker.Contains, expBnd2)
  225. // Remove container now otherwise it will interfere with next test
  226. stopRemoveContainer(id, c)
  227. // Run the container with one unpublished exposed port and one explicit port binding
  228. dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
  229. // Check docker ps o/p for last created container reports the specified unpublished port and port mapping
  230. out, _ = dockerCmd(c, "ps", "-n=1")
  231. // Missing unpublished exposed ports (unpPort1) in docker ps output
  232. c.Assert(out, checker.Contains, unpPort1)
  233. // Missing port binding (expBnd2) in docker ps output
  234. c.Assert(out, checker.Contains, expBnd2)
  235. }
  236. func (s *DockerSuite) TestPortHostBinding(c *check.C) {
  237. testRequires(c, DaemonIsLinux, NotUserNamespace)
  238. out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox",
  239. "nc", "-l", "-p", "80")
  240. firstID := strings.TrimSpace(out)
  241. out, _ = dockerCmd(c, "port", firstID, "80")
  242. err := assertPortList(c, out, []string{"0.0.0.0:9876"})
  243. // Port list is not correct
  244. c.Assert(err, checker.IsNil)
  245. dockerCmd(c, "run", "--net=host", "busybox",
  246. "nc", "localhost", "9876")
  247. dockerCmd(c, "rm", "-f", firstID)
  248. out, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "9876")
  249. // Port is still bound after the Container is removed
  250. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  251. }
  252. func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
  253. testRequires(c, DaemonIsLinux, NotUserNamespace)
  254. out, _ := dockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox",
  255. "nc", "-l", "-p", "80")
  256. firstID := strings.TrimSpace(out)
  257. out, _ = dockerCmd(c, "port", firstID, "80")
  258. _, exposedPort, err := net.SplitHostPort(out)
  259. c.Assert(err, checker.IsNil, check.Commentf("out: %s", out))
  260. dockerCmd(c, "run", "--net=host", "busybox",
  261. "nc", "localhost", strings.TrimSpace(exposedPort))
  262. dockerCmd(c, "rm", "-f", firstID)
  263. out, _, err = dockerCmdWithError("run", "--net=host", "busybox",
  264. "nc", "localhost", strings.TrimSpace(exposedPort))
  265. // Port is still bound after the Container is removed
  266. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  267. }
  268. func (s *DockerSuite) TestPortBindingOnSandbox(c *check.C) {
  269. testRequires(c, DaemonIsLinux, NotUserNamespace)
  270. dockerCmd(c, "network", "create", "--internal", "-d", "bridge", "internal-net")
  271. nr := getNetworkResource(c, "internal-net")
  272. c.Assert(nr.Internal, checker.Equals, true)
  273. dockerCmd(c, "run", "--net", "internal-net", "-d", "--name", "c1",
  274. "-p", "8080:8080", "busybox", "nc", "-l", "-p", "8080")
  275. c.Assert(waitRun("c1"), check.IsNil)
  276. _, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080")
  277. c.Assert(err, check.NotNil,
  278. check.Commentf("Port mapping on internal network is expected to fail"))
  279. // Connect container to another normal bridge network
  280. dockerCmd(c, "network", "create", "-d", "bridge", "foo-net")
  281. dockerCmd(c, "network", "connect", "foo-net", "c1")
  282. _, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080")
  283. c.Assert(err, check.IsNil,
  284. check.Commentf("Port mapping on the new network is expected to succeed"))
  285. }