docker_cli_port_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "os/exec"
  4. "sort"
  5. "strings"
  6. "testing"
  7. )
  8. func TestPortList(t *testing.T) {
  9. // one port
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. errorOut(err, t, out)
  13. firstID := stripTrailingCharacters(out)
  14. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  15. out, _, err = runCommandWithOutput(runCmd)
  16. errorOut(err, t, out)
  17. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  18. t.Error("Port list is not correct")
  19. }
  20. runCmd = exec.Command(dockerBinary, "port", firstID)
  21. out, _, err = runCommandWithOutput(runCmd)
  22. errorOut(err, t, out)
  23. if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  24. t.Error("Port list is not correct")
  25. }
  26. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  27. out, _, err = runCommandWithOutput(runCmd)
  28. errorOut(err, t, out)
  29. // three port
  30. runCmd = exec.Command(dockerBinary, "run", "-d",
  31. "-p", "9876:80",
  32. "-p", "9877:81",
  33. "-p", "9878:82",
  34. "busybox", "top")
  35. out, _, err = runCommandWithOutput(runCmd)
  36. errorOut(err, t, out)
  37. ID := stripTrailingCharacters(out)
  38. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  39. out, _, err = runCommandWithOutput(runCmd)
  40. errorOut(err, t, out)
  41. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  42. t.Error("Port list is not correct")
  43. }
  44. runCmd = exec.Command(dockerBinary, "port", ID)
  45. out, _, err = runCommandWithOutput(runCmd)
  46. errorOut(err, t, out)
  47. if !assertPortList(t, out, []string{
  48. "80/tcp -> 0.0.0.0:9876",
  49. "81/tcp -> 0.0.0.0:9877",
  50. "82/tcp -> 0.0.0.0:9878"}) {
  51. t.Error("Port list is not correct")
  52. }
  53. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  54. out, _, err = runCommandWithOutput(runCmd)
  55. errorOut(err, t, out)
  56. // more and one port mapped to the same container port
  57. runCmd = exec.Command(dockerBinary, "run", "-d",
  58. "-p", "9876:80",
  59. "-p", "9999:80",
  60. "-p", "9877:81",
  61. "-p", "9878:82",
  62. "busybox", "top")
  63. out, _, err = runCommandWithOutput(runCmd)
  64. errorOut(err, t, out)
  65. ID = stripTrailingCharacters(out)
  66. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  67. out, _, err = runCommandWithOutput(runCmd)
  68. errorOut(err, t, out)
  69. if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  70. t.Error("Port list is not correct")
  71. }
  72. runCmd = exec.Command(dockerBinary, "port", ID)
  73. out, _, err = runCommandWithOutput(runCmd)
  74. errorOut(err, t, out)
  75. if !assertPortList(t, out, []string{
  76. "80/tcp -> 0.0.0.0:9876",
  77. "80/tcp -> 0.0.0.0:9999",
  78. "81/tcp -> 0.0.0.0:9877",
  79. "82/tcp -> 0.0.0.0:9878"}) {
  80. t.Error("Port list is not correct\n", out)
  81. }
  82. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  83. out, _, err = runCommandWithOutput(runCmd)
  84. errorOut(err, t, out)
  85. deleteAllContainers()
  86. logDone("port - test port list")
  87. }
  88. func assertPortList(t *testing.T, out string, expected []string) bool {
  89. //lines := strings.Split(out, "\n")
  90. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  91. if len(lines) != len(expected) {
  92. t.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
  93. return false
  94. }
  95. sort.Strings(lines)
  96. sort.Strings(expected)
  97. for i := 0; i < len(expected); i++ {
  98. if lines[i] != expected[i] {
  99. t.Error("|" + lines[i] + "!=" + expected[i] + "|")
  100. return false
  101. }
  102. }
  103. return true
  104. }