docker_cli_port_test.go 8.7 KB

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