container_unix_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //go:build linux || freebsd
  2. package daemon
  3. import (
  4. "testing"
  5. containertypes "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/daemon/config"
  7. "github.com/docker/go-connections/nat"
  8. "gotest.tools/v3/assert"
  9. )
  10. // TestContainerWarningHostAndPublishPorts that a warning is returned when setting network mode to host and specifying published ports.
  11. // This should not be tested on Windows because Windows doesn't support "host" network mode.
  12. func TestContainerWarningHostAndPublishPorts(t *testing.T) {
  13. testCases := []struct {
  14. ports nat.PortMap
  15. warnings []string
  16. }{
  17. {ports: nat.PortMap{}},
  18. {ports: nat.PortMap{
  19. "8080": []nat.PortBinding{{HostPort: "8989"}},
  20. }, warnings: []string{"Published ports are discarded when using host network mode"}},
  21. }
  22. muteLogs()
  23. for _, tc := range testCases {
  24. hostConfig := &containertypes.HostConfig{
  25. Runtime: "runc",
  26. NetworkMode: "host",
  27. PortBindings: tc.ports,
  28. }
  29. cs := &config.Config{}
  30. configureRuntimes(cs)
  31. d := &Daemon{}
  32. d.configStore.Store(cs)
  33. wrns, err := d.verifyContainerSettings(cs, hostConfig, &containertypes.Config{}, false)
  34. assert.NilError(t, err)
  35. assert.DeepEqual(t, tc.warnings, wrns)
  36. }
  37. }