docker_cli_port_unix_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // +build !windows
  2. package main
  3. import (
  4. "net"
  5. "os/exec"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestPortHostBinding(c *check.C) {
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
  11. "nc", "-l", "-p", "80")
  12. out, _, err := runCommandWithOutput(runCmd)
  13. if err != nil {
  14. c.Fatal(out, err)
  15. }
  16. firstID := strings.TrimSpace(out)
  17. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  18. out, _, err = runCommandWithOutput(runCmd)
  19. if err != nil {
  20. c.Fatal(out, err)
  21. }
  22. if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
  23. c.Error("Port list is not correct")
  24. }
  25. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  26. "nc", "localhost", "9876")
  27. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  28. c.Fatal(out, err)
  29. }
  30. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  31. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  32. c.Fatal(out, err)
  33. }
  34. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  35. "nc", "localhost", "9876")
  36. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  37. c.Error("Port is still bound after the Container is removed")
  38. }
  39. }
  40. func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
  41. runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
  42. "nc", "-l", "-p", "80")
  43. out, _, err := runCommandWithOutput(runCmd)
  44. if err != nil {
  45. c.Fatal(out, err)
  46. }
  47. firstID := strings.TrimSpace(out)
  48. runCmd = exec.Command(dockerBinary, "port", firstID, "80")
  49. out, _, err = runCommandWithOutput(runCmd)
  50. if err != nil {
  51. c.Fatal(out, err)
  52. }
  53. _, exposedPort, err := net.SplitHostPort(out)
  54. if err != nil {
  55. c.Fatal(out, err)
  56. }
  57. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  58. "nc", "localhost", strings.TrimSpace(exposedPort))
  59. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  60. c.Fatal(out, err)
  61. }
  62. runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
  63. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  64. c.Fatal(out, err)
  65. }
  66. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
  67. "nc", "localhost", strings.TrimSpace(exposedPort))
  68. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  69. c.Error("Port is still bound after the Container is removed")
  70. }
  71. }