container_unix_test.go 1.2 KB

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