container_unix_test.go 1.3 KB

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