docker_cli_port_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  63. func assertPortList(c *check.C, out string, expected []string) bool {
  64. //lines := strings.Split(out, "\n")
  65. lines := strings.Split(strings.Trim(out, "\n "), "\n")
  66. if len(lines) != len(expected) {
  67. c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
  68. return false
  69. }
  70. sort.Strings(lines)
  71. sort.Strings(expected)
  72. for i := 0; i < len(expected); i++ {
  73. if lines[i] != expected[i] {
  74. c.Error("|" + lines[i] + "!=" + expected[i] + "|")
  75. return false
  76. }
  77. }
  78. return true
  79. }
  80. func stopRemoveContainer(id string, c *check.C) {
  81. dockerCmd(c, "rm", "-f", id)
  82. }
  83. func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
  84. // Run busybox with command line expose (equivalent to EXPOSE in image's Dockerfile) for the following ports
  85. port1 := 80
  86. port2 := 443
  87. expose1 := fmt.Sprintf("--expose=%d", port1)
  88. expose2 := fmt.Sprintf("--expose=%d", port2)
  89. dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5")
  90. // Check docker ps o/p for last created container reports the unpublished ports
  91. unpPort1 := fmt.Sprintf("%d/tcp", port1)
  92. unpPort2 := fmt.Sprintf("%d/tcp", port2)
  93. out, _ := dockerCmd(c, "ps", "-n=1")
  94. if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) {
  95. c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out)
  96. }
  97. // Run the container forcing to publish the exposed ports
  98. dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
  99. // Check docker ps o/p for last created container reports the exposed ports in the port bindings
  100. expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
  101. expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
  102. out, _ = dockerCmd(c, "ps", "-n=1")
  103. if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) {
  104. 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",
  105. unpPort1, unpPort2, out)
  106. }
  107. // Run the container specifying explicit port bindings for the exposed ports
  108. offset := 10000
  109. pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1)
  110. pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2)
  111. out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5")
  112. id := strings.TrimSpace(out)
  113. // Check docker ps o/p for last created container reports the specified port mappings
  114. expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
  115. expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
  116. out, _ = dockerCmd(c, "ps", "-n=1")
  117. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  118. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  119. }
  120. // Remove container now otherwise it will interfeer with next test
  121. stopRemoveContainer(id, c)
  122. // Run the container with explicit port bindings and no exposed ports
  123. out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5")
  124. id = strings.TrimSpace(out)
  125. // Check docker ps o/p for last created container reports the specified port mappings
  126. out, _ = dockerCmd(c, "ps", "-n=1")
  127. if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
  128. c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
  129. }
  130. // Remove container now otherwise it will interfeer with next test
  131. stopRemoveContainer(id, c)
  132. // Run the container with one unpublished exposed port and one explicit port binding
  133. dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5")
  134. // Check docker ps o/p for last created container reports the specified unpublished port and port mapping
  135. out, _ = dockerCmd(c, "ps", "-n=1")
  136. if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) {
  137. c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out)
  138. }
  139. }