docker_cli_netmode_test.go 4.3 KB

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