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