docker_cli_port_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package main
  2. import (
  3. "net"
  4. "os/exec"
  5. "sort"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestPortList(c *check.C) {
  10. // one port
  11. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
  12. out, _, err := runCommandWithOutput(runCmd)
  13. if err != nil {
  14. c.Fatal(out, err)
  15. }
  16. firstID := strings.TrimSpace(out)
  17. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  18. out, _, err = runCommandWithOutput(runCmd)
  19. if err != nil {
  20. c.Fatal(out, err)
  21. }
  22. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  23. c.Error("Port list is not correct")
  24. }
  25. runCmd = exec.Command(dockerBinary, "port", firstID)
  26. out, _, err = runCommandWithOutput(runCmd)
  27. if err != nil {
  28. c.Fatal(out, err)
  29. }
  30. if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  31. c.Error("Port list is not correct")
  32. }
  33. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  34. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  35. c.Fatal(out, err)
  36. }
  37. // three port
  38. runCmd = exec.Command(dockerBinary, "run", "-d",
  39. "-p", "9876:80",
  40. "-p", "9877:81",
  41. "-p", "9878:82",
  42. "busybox", "top")
  43. out, _, err = runCommandWithOutput(runCmd)
  44. if err != nil {
  45. c.Fatal(out, err)
  46. }
  47. ID := strings.TrimSpace(out)
  48. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  49. out, _, err = runCommandWithOutput(runCmd)
  50. if err != nil {
  51. c.Fatal(out, err)
  52. }
  53. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  54. c.Error("Port list is not correct")
  55. }
  56. runCmd = exec.Command(dockerBinary, "port", ID)
  57. out, _, err = runCommandWithOutput(runCmd)
  58. if err != nil {
  59. c.Fatal(out, err)
  60. }
  61. if !assertPortList(c, out, []string{
  62. "80/tcp -> 0.0.0.0:9876",
  63. "81/tcp -> 0.0.0.0:9877",
  64. "82/tcp -> 0.0.0.0:9878"}) {
  65. c.Error("Port list is not correct")
  66. }
  67. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  68. out, _, err = runCommandWithOutput(runCmd)
  69. if err != nil {
  70. c.Fatal(out, err)
  71. }
  72. // more and one port mapped to the same container port
  73. runCmd = exec.Command(dockerBinary, "run", "-d",
  74. "-p", "9876:80",
  75. "-p", "9999:80",
  76. "-p", "9877:81",
  77. "-p", "9878:82",
  78. "busybox", "top")
  79. out, _, err = runCommandWithOutput(runCmd)
  80. if err != nil {
  81. c.Fatal(out, err)
  82. }
  83. ID = strings.TrimSpace(out)
  84. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  85. out, _, err = runCommandWithOutput(runCmd)
  86. if err != nil {
  87. c.Fatal(out, err)
  88. }
  89. if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  90. c.Error("Port list is not correct")
  91. }
  92. runCmd = exec.Command(dockerBinary, "port", ID)
  93. out, _, err = runCommandWithOutput(runCmd)
  94. if err != nil {
  95. c.Fatal(out, err)
  96. }
  97. if !assertPortList(c, out, []string{
  98. "80/tcp -> 0.0.0.0:9876",
  99. "80/tcp -> 0.0.0.0:9999",
  100. "81/tcp -> 0.0.0.0:9877",
  101. "82/tcp -> 0.0.0.0:9878"}) {
  102. c.Error("Port list is not correct\n", out)
  103. }
  104. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  105. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  106. c.Fatal(out, err)
  107. }
  108. }
  109. func assertPortList(c *check.C, out string, expected []string) bool {
  110. //lines := strings.Split(out, "\n")
  111. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  112. if len(lines) != len(expected) {
  113. c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
  114. return false
  115. }
  116. sort.Strings(lines)
  117. sort.Strings(expected)
  118. for i := 0; i < len(expected); i++ {
  119. if lines[i] != expected[i] {
  120. c.Error("|" + lines[i] + "!=" + expected[i] + "|")
  121. return false
  122. }
  123. }
  124. return true
  125. }
  126. func (s *DockerSuite) TestPortHostBinding(c *check.C) {
  127. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
  128. "nc", "-l", "-p", "80")
  129. out, _, err := runCommandWithOutput(runCmd)
  130. if err != nil {
  131. c.Fatal(out, err)
  132. }
  133. firstID := strings.TrimSpace(out)
  134. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  135. out, _, err = runCommandWithOutput(runCmd)
  136. if err != nil {
  137. c.Fatal(out, err)
  138. }
  139. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  140. c.Error("Port list is not correct")
  141. }
  142. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  143. "nc", "localhost", "9876")
  144. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  145. c.Fatal(out, err)
  146. }
  147. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  148. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  149. c.Fatal(out, err)
  150. }
  151. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  152. "nc", "localhost", "9876")
  153. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  154. c.Error("Port is still bound after the Container is removed")
  155. }
  156. }
  157. func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
  158. runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
  159. "nc", "-l", "-p", "80")
  160. out, _, err := runCommandWithOutput(runCmd)
  161. if err != nil {
  162. c.Fatal(out, err)
  163. }
  164. firstID := strings.TrimSpace(out)
  165. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  166. out, _, err = runCommandWithOutput(runCmd)
  167. if err != nil {
  168. c.Fatal(out, err)
  169. }
  170. _, exposedPort, err := net.SplitHostPort(out)
  171. if err != nil {
  172. c.Fatal(out, err)
  173. }
  174. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  175. "nc", "localhost", strings.TrimSpace(exposedPort))
  176. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  177. c.Fatal(out, err)
  178. }
  179. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  180. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  181. c.Fatal(out, err)
  182. }
  183. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  184. "nc", "localhost", strings.TrimSpace(exposedPort))
  185. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  186. c.Error("Port is still bound after the Container is removed")
  187. }
  188. }