docker_cli_port_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package main
  2. import (
  3. "net"
  4. "os/exec"
  5. "sort"
  6. "strings"
  7. "testing"
  8. )
  9. func TestPortList(t *testing.T) {
  10. defer deleteAllContainers()
  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. t.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. t.Fatal(out, err)
  22. }
  23. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  24. t.Error("Port list is not correct")
  25. }
  26. runCmd = exec.Command(dockerBinary, "port", firstID)
  27. out, _, err = runCommandWithOutput(runCmd)
  28. if err != nil {
  29. t.Fatal(out, err)
  30. }
  31. if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  32. t.Error("Port list is not correct")
  33. }
  34. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  35. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  36. t.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. t.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. t.Fatal(out, err)
  53. }
  54. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  55. t.Error("Port list is not correct")
  56. }
  57. runCmd = exec.Command(dockerBinary, "port", ID)
  58. out, _, err = runCommandWithOutput(runCmd)
  59. if err != nil {
  60. t.Fatal(out, err)
  61. }
  62. if !assertPortList(t, 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. t.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. t.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. t.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. t.Fatal(out, err)
  89. }
  90. if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  91. t.Error("Port list is not correct")
  92. }
  93. runCmd = exec.Command(dockerBinary, "port", ID)
  94. out, _, err = runCommandWithOutput(runCmd)
  95. if err != nil {
  96. t.Fatal(out, err)
  97. }
  98. if !assertPortList(t, 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. t.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. t.Fatal(out, err)
  108. }
  109. logDone("port - test port list")
  110. }
  111. func assertPortList(t *testing.T, 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. t.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. t.Error("|" + lines[i] + "!=" + expected[i] + "|")
  123. return false
  124. }
  125. }
  126. return true
  127. }
  128. func TestPortHostBinding(t *testing.T) {
  129. defer deleteAllContainers()
  130. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
  131. "nc", "-l", "-p", "80")
  132. out, _, err := runCommandWithOutput(runCmd)
  133. if err != nil {
  134. t.Fatal(out, err)
  135. }
  136. firstID := strings.TrimSpace(out)
  137. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  138. out, _, err = runCommandWithOutput(runCmd)
  139. if err != nil {
  140. t.Fatal(out, err)
  141. }
  142. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  143. t.Error("Port list is not correct")
  144. }
  145. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  146. "nc", "localhost", "9876")
  147. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  148. t.Fatal(out, err)
  149. }
  150. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  151. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  152. t.Fatal(out, err)
  153. }
  154. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  155. "nc", "localhost", "9876")
  156. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  157. t.Error("Port is still bound after the Container is removed")
  158. }
  159. logDone("port - test host binding done")
  160. }
  161. func TestPortExposeHostBinding(t *testing.T) {
  162. defer deleteAllContainers()
  163. runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
  164. "nc", "-l", "-p", "80")
  165. out, _, err := runCommandWithOutput(runCmd)
  166. if err != nil {
  167. t.Fatal(out, err)
  168. }
  169. firstID := strings.TrimSpace(out)
  170. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  171. out, _, err = runCommandWithOutput(runCmd)
  172. if err != nil {
  173. t.Fatal(out, err)
  174. }
  175. _, exposedPort, err := net.SplitHostPort(out)
  176. if err != nil {
  177. t.Fatal(out, err)
  178. }
  179. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  180. "nc", "localhost", strings.TrimSpace(exposedPort))
  181. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  182. t.Fatal(out, err)
  183. }
  184. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  185. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  186. t.Fatal(out, err)
  187. }
  188. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  189. "nc", "localhost", strings.TrimSpace(exposedPort))
  190. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  191. t.Error("Port is still bound after the Container is removed")
  192. }
  193. logDone("port - test port expose done")
  194. }