docker_cli_netmode_test.go 4.1 KB

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