docker_cli_netmode_test.go 3.9 KB

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