docker_cli_port_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if err != nil {
  13. t.Fatal(out, err)
  14. }
  15. firstID := stripTrailingCharacters(out)
  16. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  17. out, _, err = runCommandWithOutput(runCmd)
  18. if err != nil {
  19. t.Fatal(out, err)
  20. }
  21. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  22. t.Error("Port list is not correct")
  23. }
  24. runCmd = exec.Command(dockerBinary, "port", firstID)
  25. out, _, err = runCommandWithOutput(runCmd)
  26. if err != nil {
  27. t.Fatal(out, err)
  28. }
  29. if !assertPortList(t, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
  30. t.Error("Port list is not correct")
  31. }
  32. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  33. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  34. t.Fatal(out, err)
  35. }
  36. // three port
  37. runCmd = exec.Command(dockerBinary, "run", "-d",
  38. "-p", "9876:80",
  39. "-p", "9877:81",
  40. "-p", "9878:82",
  41. "busybox", "top")
  42. out, _, err = runCommandWithOutput(runCmd)
  43. if err != nil {
  44. t.Fatal(out, err)
  45. }
  46. ID := stripTrailingCharacters(out)
  47. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  48. out, _, err = runCommandWithOutput(runCmd)
  49. if err != nil {
  50. t.Fatal(out, err)
  51. }
  52. if !assertPortList(t, out, []string{"0.0.0.0:9876"}) {
  53. t.Error("Port list is not correct")
  54. }
  55. runCmd = exec.Command(dockerBinary, "port", ID)
  56. out, _, err = runCommandWithOutput(runCmd)
  57. if err != nil {
  58. t.Fatal(out, err)
  59. }
  60. if !assertPortList(t, out, []string{
  61. "80/tcp -> 0.0.0.0:9876",
  62. "81/tcp -> 0.0.0.0:9877",
  63. "82/tcp -> 0.0.0.0:9878"}) {
  64. t.Error("Port list is not correct")
  65. }
  66. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  67. out, _, err = runCommandWithOutput(runCmd)
  68. if err != nil {
  69. t.Fatal(out, err)
  70. }
  71. // more and one port mapped to the same container port
  72. runCmd = exec.Command(dockerBinary, "run", "-d",
  73. "-p", "9876:80",
  74. "-p", "9999:80",
  75. "-p", "9877:81",
  76. "-p", "9878:82",
  77. "busybox", "top")
  78. out, _, err = runCommandWithOutput(runCmd)
  79. if err != nil {
  80. t.Fatal(out, err)
  81. }
  82. ID = stripTrailingCharacters(out)
  83. runCmd = exec.Command(dockerBinary, "port", ID, "80")
  84. out, _, err = runCommandWithOutput(runCmd)
  85. if err != nil {
  86. t.Fatal(out, err)
  87. }
  88. if !assertPortList(t, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
  89. t.Error("Port list is not correct")
  90. }
  91. runCmd = exec.Command(dockerBinary, "port", ID)
  92. out, _, err = runCommandWithOutput(runCmd)
  93. if err != nil {
  94. t.Fatal(out, err)
  95. }
  96. if !assertPortList(t, out, []string{
  97. "80/tcp -> 0.0.0.0:9876",
  98. "80/tcp -> 0.0.0.0:9999",
  99. "81/tcp -> 0.0.0.0:9877",
  100. "82/tcp -> 0.0.0.0:9878"}) {
  101. t.Error("Port list is not correct\n", out)
  102. }
  103. runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
  104. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  105. t.Fatal(out, err)
  106. }
  107. deleteAllContainers()
  108. logDone("port - test port list")
  109. }
  110. func assertPortList(t *testing.T, 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. t.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. t.Error("|" + lines[i] + "!=" + expected[i] + "|")
  122. return false
  123. }
  124. }
  125. return true
  126. }