docker_cli_port_test.go 10 KB

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