2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux || freebsd
|
2017-11-15 14:44:49 +00:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/daemon/config"
|
|
|
|
"github.com/docker/go-connections/nat"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2017-11-15 14:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestContainerWarningHostAndPublishPorts that a warning is returned when setting network mode to host and specifying published ports.
|
|
|
|
// This should not be tested on Windows because Windows doesn't support "host" network mode.
|
|
|
|
func TestContainerWarningHostAndPublishPorts(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
ports nat.PortMap
|
|
|
|
warnings []string
|
|
|
|
}{
|
|
|
|
{ports: nat.PortMap{}},
|
|
|
|
{ports: nat.PortMap{
|
|
|
|
"8080": []nat.PortBinding{{HostPort: "8989"}},
|
|
|
|
}, warnings: []string{"Published ports are discarded when using host network mode"}},
|
|
|
|
}
|
2019-10-17 00:47:37 +00:00
|
|
|
muteLogs()
|
2017-11-15 14:44:49 +00:00
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
hostConfig := &containertypes.HostConfig{
|
|
|
|
Runtime: "runc",
|
|
|
|
NetworkMode: "host",
|
|
|
|
PortBindings: tc.ports,
|
|
|
|
}
|
|
|
|
cs := &config.Config{
|
2021-07-10 12:50:32 +00:00
|
|
|
Runtimes: map[string]types.Runtime{"runc": {}},
|
2017-11-15 14:44:49 +00:00
|
|
|
}
|
|
|
|
d := &Daemon{configStore: cs}
|
2021-06-11 19:01:18 +00:00
|
|
|
wrns, err := d.verifyContainerSettings(hostConfig, &containertypes.Config{}, false)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, tc.warnings, wrns)
|
2017-11-15 14:44:49 +00:00
|
|
|
}
|
|
|
|
}
|