docker_cli_netmode_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "github.com/docker/docker/runconfig"
  6. "github.com/go-check/check"
  7. )
  8. // GH14530. Validates combinations of --net= with other options
  9. // stringCheckPS is how the output of PS starts in order to validate that
  10. // the command executed in a container did really run PS correctly.
  11. const stringCheckPS = "PID USER"
  12. // checkContains is a helper function that validates a command output did
  13. // contain what was expected.
  14. func checkContains(expected string, out string, c *check.C) {
  15. if !strings.Contains(out, expected) {
  16. c.Fatalf("Expected '%s', got '%s'", expected, out)
  17. }
  18. }
  19. func (s *DockerSuite) TestNetHostname(c *check.C) {
  20. var (
  21. out string
  22. err error
  23. runCmd *exec.Cmd
  24. )
  25. runCmd = exec.Command(dockerBinary, "run", "-h=name", "busybox", "ps")
  26. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  27. c.Fatalf(out, err)
  28. }
  29. checkContains(stringCheckPS, out, c)
  30. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "ps")
  31. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  32. c.Fatalf(out, err)
  33. }
  34. checkContains(stringCheckPS, out, c)
  35. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=bridge", "busybox", "ps")
  36. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  37. c.Fatalf(out, err)
  38. }
  39. checkContains(stringCheckPS, out, c)
  40. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=none", "busybox", "ps")
  41. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  42. c.Fatalf(out, err)
  43. }
  44. checkContains(stringCheckPS, out, c)
  45. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=host", "busybox", "ps")
  46. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  47. c.Fatalf(out, err)
  48. }
  49. checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
  50. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=container:other", "busybox", "ps")
  51. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  52. c.Fatalf(out, err, c)
  53. }
  54. checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
  55. runCmd = exec.Command(dockerBinary, "run", "--net=container", "busybox", "ps")
  56. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  57. c.Fatalf(out, err, c)
  58. }
  59. checkContains("--net: invalid net mode: invalid container format container:<name|id>", out, c)
  60. runCmd = exec.Command(dockerBinary, "run", "--net=weird", "busybox", "ps")
  61. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  62. c.Fatalf(out, err)
  63. }
  64. checkContains("invalid --net: weird", out, c)
  65. }
  66. func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
  67. var (
  68. out string
  69. err error
  70. runCmd *exec.Cmd
  71. )
  72. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
  73. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  74. c.Fatalf(out, err)
  75. }
  76. checkContains(runconfig.ErrConflictContainerNetworkAndLinks.Error(), out, c)
  77. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
  78. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  79. c.Fatalf(out, err)
  80. }
  81. checkContains(runconfig.ErrConflictHostNetworkAndLinks.Error(), out, c)
  82. }
  83. func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
  84. var (
  85. out string
  86. err error
  87. runCmd *exec.Cmd
  88. )
  89. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps")
  90. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  91. c.Fatalf(out, err)
  92. }
  93. checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
  94. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
  95. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  96. c.Fatalf(out, err)
  97. }
  98. checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
  99. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps")
  100. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  101. c.Fatalf(out, err)
  102. }
  103. checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
  104. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
  105. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  106. c.Fatalf(out, err)
  107. }
  108. checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
  109. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  110. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  111. c.Fatalf(out, err)
  112. }
  113. checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
  114. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  115. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  116. c.Fatalf(out, err)
  117. }
  118. checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
  119. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-P", "busybox", "ps")
  120. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  121. c.Fatalf(out, err)
  122. }
  123. checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
  124. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
  125. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  126. c.Fatalf(out, err)
  127. }
  128. checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
  129. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
  130. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  131. c.Fatalf(out, err)
  132. }
  133. checkContains(runconfig.ErrConflictNetworkExposePorts.Error(), out, c)
  134. }