docker_cli_netmode_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. testRequires(c, DaemonIsLinux)
  21. var (
  22. out string
  23. err error
  24. runCmd *exec.Cmd
  25. )
  26. runCmd = exec.Command(dockerBinary, "run", "-h=name", "busybox", "ps")
  27. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  28. c.Fatalf(out, err)
  29. }
  30. checkContains(stringCheckPS, out, c)
  31. runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "ps")
  32. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  33. c.Fatalf(out, err)
  34. }
  35. checkContains(stringCheckPS, out, c)
  36. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=bridge", "busybox", "ps")
  37. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  38. c.Fatalf(out, err)
  39. }
  40. checkContains(stringCheckPS, out, c)
  41. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=none", "busybox", "ps")
  42. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  43. c.Fatalf(out, err)
  44. }
  45. checkContains(stringCheckPS, out, c)
  46. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=host", "busybox", "ps")
  47. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  48. c.Fatalf(out, err)
  49. }
  50. checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
  51. runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=container:other", "busybox", "ps")
  52. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  53. c.Fatalf(out, err, c)
  54. }
  55. checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
  56. runCmd = exec.Command(dockerBinary, "run", "--net=container", "busybox", "ps")
  57. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  58. c.Fatalf(out, err, c)
  59. }
  60. checkContains("--net: invalid net mode: invalid container format container:<name|id>", out, c)
  61. runCmd = exec.Command(dockerBinary, "run", "--net=weird", "busybox", "ps")
  62. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  63. c.Fatalf(out, err)
  64. }
  65. checkContains("network weird not found", out, c)
  66. }
  67. func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
  68. testRequires(c, DaemonIsLinux)
  69. var (
  70. out string
  71. err error
  72. runCmd *exec.Cmd
  73. )
  74. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
  75. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  76. c.Fatalf(out, err)
  77. }
  78. checkContains(runconfig.ErrConflictContainerNetworkAndLinks.Error(), out, c)
  79. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
  80. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  81. c.Fatalf(out, err)
  82. }
  83. checkContains(runconfig.ErrConflictHostNetworkAndLinks.Error(), out, c)
  84. }
  85. func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
  86. testRequires(c, DaemonIsLinux)
  87. var (
  88. out string
  89. err error
  90. runCmd *exec.Cmd
  91. )
  92. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps")
  93. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  94. c.Fatalf(out, err)
  95. }
  96. checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
  97. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
  98. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  99. c.Fatalf(out, err)
  100. }
  101. checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
  102. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps")
  103. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  104. c.Fatalf(out, err)
  105. }
  106. checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
  107. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
  108. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  109. c.Fatalf(out, err)
  110. }
  111. checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
  112. runCmd = exec.Command(dockerBinary, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  113. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  114. c.Fatalf(out, err)
  115. }
  116. checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
  117. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  118. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  119. c.Fatalf(out, err)
  120. }
  121. checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
  122. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-P", "busybox", "ps")
  123. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  124. c.Fatalf(out, err)
  125. }
  126. checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
  127. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
  128. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  129. c.Fatalf(out, err)
  130. }
  131. checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
  132. runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
  133. if out, _, err = runCommandWithOutput(runCmd); err == nil {
  134. c.Fatalf(out, err)
  135. }
  136. checkContains(runconfig.ErrConflictNetworkExposePorts.Error(), out, c)
  137. }