docker_cli_port_test.go 11 KB

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