docker_cli_netmode_test.go 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. out, _, err := dockerCmdWithError(args...)
  27. assert.Assert(c, err != nil, "%v", out)
  28. return out
  29. }
  30. func (s *DockerCLINetmodeSuite) TestNetHostnameWithNetHost(c *testing.T) {
  31. testRequires(c, DaemonIsLinux, NotUserNamespace)
  32. out := cli.DockerCmd(c, "run", "--net=host", "busybox", "ps").Stdout()
  33. assert.Assert(c, strings.Contains(out, stringCheckPS))
  34. }
  35. func (s *DockerCLINetmodeSuite) TestNetHostname(c *testing.T) {
  36. testRequires(c, DaemonIsLinux)
  37. out := cli.DockerCmd(c, "run", "-h=name", "busybox", "ps").Stdout()
  38. assert.Assert(c, strings.Contains(out, stringCheckPS))
  39. out = cli.DockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps").Stdout()
  40. assert.Assert(c, strings.Contains(out, stringCheckPS))
  41. out = cli.DockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps").Stdout()
  42. assert.Assert(c, strings.Contains(out, stringCheckPS))
  43. out = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
  44. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkHostname.Error()))
  45. out = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
  46. assert.Assert(c, strings.Contains(out, "invalid container format container:<name|id>"))
  47. out = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
  48. assert.Assert(c, strings.Contains(strings.ToLower(out), "not found"))
  49. }
  50. func (s *DockerCLINetmodeSuite) TestConflictContainerNetworkAndLinks(c *testing.T) {
  51. testRequires(c, DaemonIsLinux)
  52. out := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
  53. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndLinks.Error()))
  54. }
  55. func (s *DockerCLINetmodeSuite) TestConflictContainerNetworkHostAndLinks(c *testing.T) {
  56. testRequires(c, DaemonIsLinux, NotUserNamespace)
  57. out := dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
  58. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictHostNetworkAndLinks.Error()))
  59. }
  60. func (s *DockerCLINetmodeSuite) TestConflictNetworkModeNetHostAndOptions(c *testing.T) {
  61. testRequires(c, DaemonIsLinux, NotUserNamespace)
  62. out := dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
  63. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndMac.Error()))
  64. }
  65. func (s *DockerCLINetmodeSuite) TestConflictNetworkModeAndOptions(c *testing.T) {
  66. testRequires(c, DaemonIsLinux)
  67. out := dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
  68. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkAndDNS.Error()))
  69. out = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
  70. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkHosts.Error()))
  71. out = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps")
  72. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkPublishPorts.Error()))
  73. out = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
  74. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkPublishPorts.Error()))
  75. out = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
  76. assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkExposePorts.Error()))
  77. }