2017-11-15 09:44:49 -05: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 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2017-11-15 09:44:49 -05: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-16 20:47:37 -04:00
|
|
|
muteLogs()
|
2017-11-15 09:44:49 -05:00
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
hostConfig := &containertypes.HostConfig{
|
|
|
|
Runtime: "runc",
|
|
|
|
NetworkMode: "host",
|
|
|
|
PortBindings: tc.ports,
|
|
|
|
}
|
|
|
|
cs := &config.Config{
|
2021-07-10 08:50:32 -04:00
|
|
|
Runtimes: map[string]types.Runtime{"runc": {}},
|
2017-11-15 09:44:49 -05:00
|
|
|
}
|
|
|
|
d := &Daemon{configStore: cs}
|
2021-06-11 15:01:18 -04:00
|
|
|
wrns, err := d.verifyContainerSettings(hostConfig, &containertypes.Config{}, false)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, tc.warnings, wrns)
|
2017-11-15 09:44:49 -05:00
|
|
|
}
|
|
|
|
}
|