docker_cli_port_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. "sort"
  7. "strings"
  8. "github.com/go-check/check"
  9. )
  10. func (s *DockerSuite) TestPortList(c *check.C) {
  11. testRequires(c, DaemonIsLinux)
  12. // one port
  13. out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top")
  14. firstID := strings.TrimSpace(out)
  15. out, _ = dockerCmd(c, "port", firstID, "80")
  16. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  17. c.Error("Port list is not correct")
  18. }
  19. out, _ = dockerCmd(c, "port", firstID)
  20. if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  21. c.Error("Port list is not correct")
  22. }
  23. dockerCmd(c, "rm", "-f", firstID)
  24. // three port
  25. out, _ = dockerCmd(c, "run", "-d",
  26. "-p", "9876:80",
  27. "-p", "9877:81",
  28. "-p", "9878:82",
  29. "busybox", "top")
  30. ID := strings.TrimSpace(out)
  31. out, _ = dockerCmd(c, "port", ID, "80")
  32. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  33. c.Error("Port list is not correct")
  34. }
  35. out, _ = dockerCmd(c, "port", ID)
  36. if !assertPortList(c, out, []string{
  37. "80/tcp -> 0.0.0.0:9876",
  38. "81/tcp -> 0.0.0.0:9877",
  39. "82/tcp -> 0.0.0.0:9878"}) {
  40. c.Error("Port list is not correct")
  41. }
  42. dockerCmd(c, "rm", "-f", ID)
  43. // more and one port mapped to the same container port
  44. out, _ = dockerCmd(c, "run", "-d",
  45. "-p", "9876:80",
  46. "-p", "9999:80",
  47. "-p", "9877:81",
  48. "-p", "9878:82",
  49. "busybox", "top")
  50. ID = strings.TrimSpace(out)
  51. out, _ = dockerCmd(c, "port", ID, "80")
  52. if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  53. c.Error("Port list is not correct")
  54. }
  55. out, _ = dockerCmd(c, "port", ID)
  56. if !assertPortList(c, out, []string{
  57. "80/tcp -> 0.0.0.0:9876",
  58. "80/tcp -> 0.0.0.0:9999",
  59. "81/tcp -> 0.0.0.0:9877",
  60. "82/tcp -> 0.0.0.0:9878"}) {
  61. c.Error("Port list is not correct\n", out)
  62. }
  63. dockerCmd(c, "rm", "-f", ID)
  64. testRange := func() {
  65. // host port ranges used
  66. IDs := make([]string, 3)
  67. for i := 0; i < 3; i++ {
  68. out, _ = dockerCmd(c, "run", "-d",
  69. "-p", "9090-9092:80",
  70. "busybox", "top")
  71. IDs[i] = strings.TrimSpace(out)
  72. out, _ = dockerCmd(c, "port", IDs[i])
  73. if !assertPortList(c, out, []string{
  74. fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i)}) {
  75. c.Error("Port list is not correct\n", out)
  76. }
  77. }
  78. // test port range exhaustion
  79. out, _, err := dockerCmdWithError("run", "-d",
  80. "-p", "9090-9092:80",
  81. "busybox", "top")
  82. if err == nil {
  83. c.Errorf("Exhausted port range did not return an error. Out: %s", out)
  84. }
  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. if err == nil {
  98. c.Errorf("Port range should have returned an error. Out: %s", out)
  99. }
  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. if !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. c.Error("Port list is not correct\n", out)
  113. }
  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. if !assertPortList(c, out, []string{
  123. "80/tcp -> 0.0.0.0:8000",
  124. "80/udp -> 0.0.0.0:8000"}) {
  125. c.Error("Port list is not correct\n", out)
  126. }
  127. dockerCmd(c, "rm", "-f", ID)
  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. for i := 0; i < len(expected); i++ {
  139. if lines[i] != expected[i] {
  140. c.Error("|" + lines[i] + "!=" + expected[i] + "|")
  141. return false
  142. }
  143. }
  144. return true
  145. }
  146. func stopRemoveContainer(id string, c *check.C) {
  147. dockerCmd(c, "rm", "-f", id)
  148. }
  149. func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
  150. testRequires(c, DaemonIsLinux)
  151. // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
  152. port1 := 80
  153. port2 := 443
  154. expose1 := fmt.Sprintf("--expose=%d", port1)
  155. expose2 := fmt.Sprintf("--expose=%d", port2)
  156. dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
  157. // Check docker ps o/p for last created container reports the unpublished ports
  158. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  159. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  160. out, _ := dockerCmd(c, "ps", "-n=1")
  161. if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) {
  162. c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out)
  163. }
  164. // Run the container forcing to publish the exposed ports
  165. dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
  166. // Check docker ps o/p for last created container reports the exposed ports in the port bindings
  167. expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
  168. expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
  169. out, _ = dockerCmd(c, "ps", "-n=1")
  170. if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) {
  171. 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",
  172. unpPort1, unpPort2, out)
  173. }
  174. // Run the container specifying explicit port bindings for the exposed ports
  175. offset := 10000
  176. pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
  177. pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
  178. out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
  179. id := strings.TrimSpace(out)
  180. // Check docker ps o/p for last created container reports the specified port mappings
  181. expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
  182. expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
  183. out, _ = dockerCmd(c, "ps", "-n=1")
  184. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  185. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  186. }
  187. // Remove container now otherwise it will interfeer with next test
  188. stopRemoveContainer(id, c)
  189. // Run the container with explicit port bindings and no exposed ports
  190. out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
  191. id = strings.TrimSpace(out)
  192. // Check docker ps o/p for last created container reports the specified port mappings
  193. out, _ = dockerCmd(c, "ps", "-n=1")
  194. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  195. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  196. }
  197. // Remove container now otherwise it will interfeer with next test
  198. stopRemoveContainer(id, c)
  199. // Run the container with one unpublished exposed port and one explicit port binding
  200. dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
  201. // Check docker ps o/p for last created container reports the specified unpublished port and port mapping
  202. out, _ = dockerCmd(c, "ps", "-n=1")
  203. if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) {
  204. c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out)
  205. }
  206. }
  207. func (s *DockerSuite) TestPortHostBinding(c *check.C) {
  208. testRequires(c, DaemonIsLinux)
  209. out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox",
  210. "nc", "-l", "-p", "80")
  211. firstID := strings.TrimSpace(out)
  212. out, _ = dockerCmd(c, "port", firstID, "80")
  213. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  214. c.Error("Port list is not correct")
  215. }
  216. dockerCmd(c, "run", "--net=host", "busybox",
  217. "nc", "localhost", "9876")
  218. dockerCmd(c, "rm", "-f", firstID)
  219. if _, _, err := dockerCmdWithError("run", "--net=host", "busybox",
  220. "nc", "localhost", "9876"); err == nil {
  221. c.Error("Port is still bound after the Container is removed")
  222. }
  223. }
  224. func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
  225. testRequires(c, DaemonIsLinux)
  226. out, _ := dockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox",
  227. "nc", "-l", "-p", "80")
  228. firstID := strings.TrimSpace(out)
  229. out, _ = dockerCmd(c, "port", firstID, "80")
  230. _, exposedPort, err := net.SplitHostPort(out)
  231. if err != nil {
  232. c.Fatal(out, err)
  233. }
  234. dockerCmd(c, "run", "--net=host", "busybox",
  235. "nc", "localhost", strings.TrimSpace(exposedPort))
  236. dockerCmd(c, "rm", "-f", firstID)
  237. if _, _, err = dockerCmdWithError("run", "--net=host", "busybox",
  238. "nc", "localhost", strings.TrimSpace(exposedPort)); err == nil {
  239. c.Error("Port is still bound after the Container is removed")
  240. }
  241. }