container_unix_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. Runtimes: map[string]types.Runtime{"runc": {}},
  33. }
  34. d := &Daemon{configStore: cs}
  35. wrns, err := d.verifyContainerSettings(hostConfig, &containertypes.Config{}, false)
  36. assert.NilError(t, err)
  37. assert.DeepEqual(t, tc.warnings, wrns)
  38. }
  39. }