docker_cli_netmode_test.go 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/docker/docker/integration-cli/checker"
  6. "github.com/docker/docker/runconfig"
  7. "github.com/go-check/check"
  8. "gotest.tools/assert"
  9. )
  10. // GH14530. Validates combinations of --net= with other options
  11. // stringCheckPS is how the output of PS starts in order to validate that
  12. // the command executed in a container did really run PS correctly.
  13. const stringCheckPS = "PID USER"
  14. // DockerCmdWithFail executes a docker command that is supposed to fail and returns
  15. // the output, the exit code. If the command returns a Nil error, it will fail and
  16. // stop the tests.
  17. func dockerCmdWithFail(c *testing.T, args ...string) (string, int) {
  18. out, status, err := dockerCmdWithError(args...)
  19. assert.Assert(c, err != nil, check.Commentf("%v", out))
  20. return out, status
  21. }
  22. func (s *DockerSuite) TestNetHostnameWithNetHost(c *testing.T) {
  23. testRequires(c, DaemonIsLinux, NotUserNamespace)
  24. out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ps")
  25. assert.Assert(c, out, checker.Contains, stringCheckPS)
  26. }
  27. func (s *DockerSuite) TestNetHostname(c *testing.T) {
  28. testRequires(c, DaemonIsLinux)
  29. out, _ := dockerCmd(c, "run", "-h=name", "busybox", "ps")
  30. assert.Assert(c, out, checker.Contains, stringCheckPS)
  31. out, _ = dockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps")
  32. assert.Assert(c, out, checker.Contains, stringCheckPS)
  33. out, _ = dockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps")
  34. assert.Assert(c, out, checker.Contains, stringCheckPS)
  35. out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
  36. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error())
  37. out, _ = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
  38. assert.Assert(c, out, checker.Contains, "invalid container format container:<name|id>")
  39. out, _ = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
  40. assert.Assert(c, strings.ToLower(out), checker.Contains, "not found")
  41. }
  42. func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *testing.T) {
  43. testRequires(c, DaemonIsLinux)
  44. out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
  45. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictContainerNetworkAndLinks.Error())
  46. }
  47. func (s *DockerSuite) TestConflictContainerNetworkHostAndLinks(c *testing.T) {
  48. testRequires(c, DaemonIsLinux, NotUserNamespace)
  49. out, _ := dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
  50. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
  51. }
  52. func (s *DockerSuite) TestConflictNetworkModeNetHostAndOptions(c *testing.T) {
  53. testRequires(c, DaemonIsLinux, NotUserNamespace)
  54. out, _ := dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  55. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error())
  56. }
  57. func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *testing.T) {
  58. testRequires(c, DaemonIsLinux)
  59. out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
  60. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error())
  61. out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
  62. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictNetworkHosts.Error())
  63. out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  64. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error())
  65. out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps")
  66. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
  67. out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
  68. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
  69. out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
  70. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictNetworkExposePorts.Error())
  71. }