docker_cli_port_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  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. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
  13. out, _, err := runCommandWithOutput(runCmd)
  14. if err != nil {
  15. c.Fatal(out, err)
  16. }
  17. firstID := strings.TrimSpace(out)
  18. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  19. out, _, err = runCommandWithOutput(runCmd)
  20. if err != nil {
  21. c.Fatal(out, err)
  22. }
  23. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  24. c.Error("Port list is not correct")
  25. }
  26. runCmd = exec.Command(dockerBinary, "port", firstID)
  27. out, _, err = runCommandWithOutput(runCmd)
  28. if err != nil {
  29. c.Fatal(out, err)
  30. }
  31. if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  32. c.Error("Port list is not correct")
  33. }
  34. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  35. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  36. c.Fatal(out, err)
  37. }
  38. // three port
  39. runCmd = exec.Command(dockerBinary, "run", "-d",
  40. "-p", "9876:80",
  41. "-p", "9877:81",
  42. "-p", "9878:82",
  43. "busybox", "top")
  44. out, _, err = runCommandWithOutput(runCmd)
  45. if err != nil {
  46. c.Fatal(out, err)
  47. }
  48. ID := strings.TrimSpace(out)
  49. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  50. out, _, err = runCommandWithOutput(runCmd)
  51. if err != nil {
  52. c.Fatal(out, err)
  53. }
  54. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  55. c.Error("Port list is not correct")
  56. }
  57. runCmd = exec.Command(dockerBinary, "port", ID)
  58. out, _, err = runCommandWithOutput(runCmd)
  59. if err != nil {
  60. c.Fatal(out, err)
  61. }
  62. if !assertPortList(c, out, []string{
  63. "80/tcp -> 0.0.0.0:9876",
  64. "81/tcp -> 0.0.0.0:9877",
  65. "82/tcp -> 0.0.0.0:9878"}) {
  66. c.Error("Port list is not correct")
  67. }
  68. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  69. out, _, err = runCommandWithOutput(runCmd)
  70. if err != nil {
  71. c.Fatal(out, err)
  72. }
  73. // more and one port mapped to the same container port
  74. runCmd = exec.Command(dockerBinary, "run", "-d",
  75. "-p", "9876:80",
  76. "-p", "9999:80",
  77. "-p", "9877:81",
  78. "-p", "9878:82",
  79. "busybox", "top")
  80. out, _, err = runCommandWithOutput(runCmd)
  81. if err != nil {
  82. c.Fatal(out, err)
  83. }
  84. ID = strings.TrimSpace(out)
  85. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  86. out, _, err = runCommandWithOutput(runCmd)
  87. if err != nil {
  88. c.Fatal(out, err)
  89. }
  90. if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  91. c.Error("Port list is not correct")
  92. }
  93. runCmd = exec.Command(dockerBinary, "port", ID)
  94. out, _, err = runCommandWithOutput(runCmd)
  95. if err != nil {
  96. c.Fatal(out, err)
  97. }
  98. if !assertPortList(c, out, []string{
  99. "80/tcp -> 0.0.0.0:9876",
  100. "80/tcp -> 0.0.0.0:9999",
  101. "81/tcp -> 0.0.0.0:9877",
  102. "82/tcp -> 0.0.0.0:9878"}) {
  103. c.Error("Port list is not correct\n", out)
  104. }
  105. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  106. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  107. c.Fatal(out, err)
  108. }
  109. }
  110. func assertPortList(c *check.C, out string, expected []string) bool {
  111. //lines := strings.Split(out, "\n")
  112. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  113. if len(lines) != len(expected) {
  114. c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
  115. return false
  116. }
  117. sort.Strings(lines)
  118. sort.Strings(expected)
  119. for i := 0; i < len(expected); i++ {
  120. if lines[i] != expected[i] {
  121. c.Error("|" + lines[i] + "!=" + expected[i] + "|")
  122. return false
  123. }
  124. }
  125. return true
  126. }
  127. func stopRemoveContainer(id string, c *check.C) {
  128. runCmd := exec.Command(dockerBinary, "rm", "-f", id)
  129. _, _, err := runCommandWithOutput(runCmd)
  130. c.Assert(err, check.IsNil)
  131. }
  132. func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
  133. // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
  134. port1 := 80
  135. port2 := 443
  136. expose1 := fmt.Sprintf("--expose=%d", port1)
  137. expose2 := fmt.Sprintf("--expose=%d", port2)
  138. runCmd := exec.Command(dockerBinary, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
  139. out, _, err := runCommandWithOutput(runCmd)
  140. c.Assert(err, check.IsNil)
  141. // Check docker ps o/p for last created container reports the unpublished ports
  142. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  143. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  144. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  145. out, _, err = runCommandWithOutput(runCmd)
  146. c.Assert(err, check.IsNil)
  147. if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) {
  148. c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out)
  149. }
  150. // Run the container forcing to publish the exposed ports
  151. runCmd = exec.Command(dockerBinary, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
  152. out, _, err = runCommandWithOutput(runCmd)
  153. c.Assert(err, check.IsNil)
  154. // Check docker ps o/p for last created container reports the exposed ports in the port bindings
  155. expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
  156. expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
  157. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  158. out, _, err = runCommandWithOutput(runCmd)
  159. c.Assert(err, check.IsNil)
  160. if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) {
  161. 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",
  162. unpPort1, unpPort2, out)
  163. }
  164. // Run the container specifying explicit port bindings for the exposed ports
  165. offset := 10000
  166. pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
  167. pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
  168. runCmd = exec.Command(dockerBinary, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
  169. out, _, err = runCommandWithOutput(runCmd)
  170. c.Assert(err, check.IsNil)
  171. id := strings.TrimSpace(out)
  172. // Check docker ps o/p for last created container reports the specified port mappings
  173. expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
  174. expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
  175. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  176. out, _, err = runCommandWithOutput(runCmd)
  177. c.Assert(err, check.IsNil)
  178. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  179. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  180. }
  181. // Remove container now otherwise it will interfeer with next test
  182. stopRemoveContainer(id, c)
  183. // Run the container with explicit port bindings and no exposed ports
  184. runCmd = exec.Command(dockerBinary, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
  185. out, _, err = runCommandWithOutput(runCmd)
  186. c.Assert(err, check.IsNil)
  187. id = strings.TrimSpace(out)
  188. // Check docker ps o/p for last created container reports the specified port mappings
  189. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  190. out, _, err = runCommandWithOutput(runCmd)
  191. c.Assert(err, check.IsNil)
  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. runCmd = exec.Command(dockerBinary, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
  199. out, _, err = runCommandWithOutput(runCmd)
  200. c.Assert(err, check.IsNil)
  201. // Check docker ps o/p for last created container reports the specified unpublished port and port mapping
  202. runCmd = exec.Command(dockerBinary, "ps", "-n=1")
  203. out, _, err = runCommandWithOutput(runCmd)
  204. c.Assert(err, check.IsNil)
  205. if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) {
  206. c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out)
  207. }
  208. }