docker_cli_port_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os/exec"
  6. "regexp"
  7. "sort"
  8. "strings"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestPortList(c *check.C) {
  12. // one port
  13. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
  14. out, _, err := runCommandWithOutput(runCmd)
  15. if err != nil {
  16. c.Fatal(out, err)
  17. }
  18. firstID := strings.TrimSpace(out)
  19. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  20. out, _, err = runCommandWithOutput(runCmd)
  21. if err != nil {
  22. c.Fatal(out, err)
  23. }
  24. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  25. c.Error("Port list is not correct")
  26. }
  27. runCmd = exec.Command(dockerBinary, "port", firstID)
  28. out, _, err = runCommandWithOutput(runCmd)
  29. if err != nil {
  30. c.Fatal(out, err)
  31. }
  32. if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  33. c.Error("Port list is not correct")
  34. }
  35. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  36. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  37. c.Fatal(out, err)
  38. }
  39. // three port
  40. runCmd = exec.Command(dockerBinary, "run", "-d",
  41. "-p", "9876:80",
  42. "-p", "9877:81",
  43. "-p", "9878:82",
  44. "busybox", "top")
  45. out, _, err = runCommandWithOutput(runCmd)
  46. if err != nil {
  47. c.Fatal(out, err)
  48. }
  49. ID := strings.TrimSpace(out)
  50. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  51. out, _, err = runCommandWithOutput(runCmd)
  52. if err != nil {
  53. c.Fatal(out, err)
  54. }
  55. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  56. c.Error("Port list is not correct")
  57. }
  58. runCmd = exec.Command(dockerBinary, "port", ID)
  59. out, _, err = runCommandWithOutput(runCmd)
  60. if err != nil {
  61. c.Fatal(out, err)
  62. }
  63. if !assertPortList(c, out, []string{
  64. "80/tcp -> 0.0.0.0:9876",
  65. "81/tcp -> 0.0.0.0:9877",
  66. "82/tcp -> 0.0.0.0:9878"}) {
  67. c.Error("Port list is not correct")
  68. }
  69. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  70. out, _, err = runCommandWithOutput(runCmd)
  71. if err != nil {
  72. c.Fatal(out, err)
  73. }
  74. // more and one port mapped to the same container port
  75. runCmd = exec.Command(dockerBinary, "run", "-d",
  76. "-p", "9876:80",
  77. "-p", "9999:80",
  78. "-p", "9877:81",
  79. "-p", "9878:82",
  80. "busybox", "top")
  81. out, _, err = runCommandWithOutput(runCmd)
  82. if err != nil {
  83. c.Fatal(out, err)
  84. }
  85. ID = strings.TrimSpace(out)
  86. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  87. out, _, err = runCommandWithOutput(runCmd)
  88. if err != nil {
  89. c.Fatal(out, err)
  90. }
  91. if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  92. c.Error("Port list is not correct")
  93. }
  94. runCmd = exec.Command(dockerBinary, "port", ID)
  95. out, _, err = runCommandWithOutput(runCmd)
  96. if err != nil {
  97. c.Fatal(out, err)
  98. }
  99. if !assertPortList(c, out, []string{
  100. "80/tcp -> 0.0.0.0:9876",
  101. "80/tcp -> 0.0.0.0:9999",
  102. "81/tcp -> 0.0.0.0:9877",
  103. "82/tcp -> 0.0.0.0:9878"}) {
  104. c.Error("Port list is not correct\n", out)
  105. }
  106. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  107. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  108. c.Fatal(out, err)
  109. }
  110. }
  111. func assertPortList(c *check.C, out string, expected []string) bool {
  112. //lines := strings.Split(out, "\n")
  113. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  114. if len(lines) != len(expected) {
  115. c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
  116. return false
  117. }
  118. sort.Strings(lines)
  119. sort.Strings(expected)
  120. for i := 0; i < len(expected); i++ {
  121. if lines[i] != expected[i] {
  122. c.Error("|" + lines[i] + "!=" + expected[i] + "|")
  123. return false
  124. }
  125. }
  126. return true
  127. }
  128. func (s *DockerSuite) TestPortHostBinding(c *check.C) {
  129. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
  130. "nc", "-l", "-p", "80")
  131. out, _, err := runCommandWithOutput(runCmd)
  132. if err != nil {
  133. c.Fatal(out, err)
  134. }
  135. firstID := strings.TrimSpace(out)
  136. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  137. out, _, err = runCommandWithOutput(runCmd)
  138. if err != nil {
  139. c.Fatal(out, err)
  140. }
  141. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  142. c.Error("Port list is not correct")
  143. }
  144. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  145. "nc", "localhost", "9876")
  146. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  147. c.Fatal(out, err)
  148. }
  149. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  150. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  151. c.Fatal(out, err)
  152. }
  153. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  154. "nc", "localhost", "9876")
  155. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  156. c.Error("Port is still bound after the Container is removed")
  157. }
  158. }
  159. func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
  160. runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
  161. "nc", "-l", "-p", "80")
  162. out, _, err := runCommandWithOutput(runCmd)
  163. if err != nil {
  164. c.Fatal(out, err)
  165. }
  166. firstID := strings.TrimSpace(out)
  167. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  168. out, _, err = runCommandWithOutput(runCmd)
  169. if err != nil {
  170. c.Fatal(out, err)
  171. }
  172. _, exposedPort, err := net.SplitHostPort(out)
  173. if err != nil {
  174. c.Fatal(out, err)
  175. }
  176. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  177. "nc", "localhost", strings.TrimSpace(exposedPort))
  178. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  179. c.Fatal(out, err)
  180. }
  181. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  182. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  183. c.Fatal(out, err)
  184. }
  185. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  186. "nc", "localhost", strings.TrimSpace(exposedPort))
  187. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  188. c.Error("Port is still bound after the Container is removed")
  189. }
  190. }
  191. func stopRemoveContainer(id string, c *check.C) {
  192. runCmd := exec.Command(dockerBinary, "rm", "-f", id)
  193. _, _, err := runCommandWithOutput(runCmd)
  194. c.Assert(err, check.IsNil)
  195. }
  196. func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
  197. // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
  198. port1 := 80
  199. port2 := 443
  200. expose1 := fmt.Sprintf("--expose=%d", port1)
  201. expose2 := fmt.Sprintf("--expose=%d", port2)
  202. runCmd := exec.Command(dockerBinary, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
  203. out, _, err := runCommandWithOutput(runCmd)
  204. c.Assert(err, check.IsNil)
  205. // Check docker ps o/p for last created container reports the unpublished ports
  206. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  207. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  208. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  209. out, _, err = runCommandWithOutput(runCmd)
  210. c.Assert(err, check.IsNil)
  211. if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) {
  212. c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out)
  213. }
  214. // Run the container forcing to publish the exposed ports
  215. runCmd = exec.Command(dockerBinary, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
  216. out, _, err = runCommandWithOutput(runCmd)
  217. c.Assert(err, check.IsNil)
  218. // Check docker ps o/p for last created container reports the exposed ports in the port bindings
  219. expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
  220. expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
  221. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  222. out, _, err = runCommandWithOutput(runCmd)
  223. c.Assert(err, check.IsNil)
  224. if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) {
  225. 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",
  226. unpPort1, unpPort2, out)
  227. }
  228. // Run the container specifying explicit port bindings for the exposed ports
  229. offset := 10000
  230. pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
  231. pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
  232. runCmd = exec.Command(dockerBinary, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
  233. out, _, err = runCommandWithOutput(runCmd)
  234. c.Assert(err, check.IsNil)
  235. id := strings.TrimSpace(out)
  236. // Check docker ps o/p for last created container reports the specified port mappings
  237. expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
  238. expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
  239. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  240. out, _, err = runCommandWithOutput(runCmd)
  241. c.Assert(err, check.IsNil)
  242. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  243. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  244. }
  245. // Remove container now otherwise it will interfeer with next test
  246. stopRemoveContainer(id, c)
  247. // Run the container with explicit port bindings and no exposed ports
  248. runCmd = exec.Command(dockerBinary, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
  249. out, _, err = runCommandWithOutput(runCmd)
  250. c.Assert(err, check.IsNil)
  251. id = strings.TrimSpace(out)
  252. // Check docker ps o/p for last created container reports the specified port mappings
  253. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  254. out, _, err = runCommandWithOutput(runCmd)
  255. c.Assert(err, check.IsNil)
  256. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  257. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  258. }
  259. // Remove container now otherwise it will interfeer with next test
  260. stopRemoveContainer(id, c)
  261. // Run the container with one unpublished exposed port and one explicit port binding
  262. runCmd = exec.Command(dockerBinary, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
  263. out, _, err = runCommandWithOutput(runCmd)
  264. c.Assert(err, check.IsNil)
  265. // Check docker ps o/p for last created container reports the specified unpublished port and port mapping
  266. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  267. out, _, err = runCommandWithOutput(runCmd)
  268. c.Assert(err, check.IsNil)
  269. if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) {
  270. c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out)
  271. }
  272. }