docker_cli_port_test.go 7.5 KB

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