container_unix_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "testing"
  5. "github.com/docker/docker/api/types"
  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/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. for _, tc := range testCases {
  24. hostConfig := &containertypes.HostConfig{
  25. Runtime: "runc",
  26. NetworkMode: "host",
  27. PortBindings: tc.ports,
  28. }
  29. cs := &config.Config{
  30. CommonUnixConfig: config.CommonUnixConfig{
  31. Runtimes: map[string]types.Runtime{"runc": {}},
  32. },
  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. }