moby/daemon/container_unix_test.go
Boaz Shuster 6e78fdb790 Display a warn message when there is binding ports and net mode is host
When a container is created if "--network" is set to "host" all the
ports in the container are bound to the host.
Thus, adding "-p" or "--publish" to the command-line is meaningless.

Unlike "docker run" and "docker create", "docker service create" sends
an error message when network mode is host and port bindings are given

This patch however suggests to send a warning message to the client when
such a case occurs.

The warning message is added to "warnings" which are returned from
"verifyPlatformContainerSettings".

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
2018-02-18 13:28:44 +00:00

44 lines
1.3 KiB
Go

// +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"
"github.com/stretchr/testify/require"
)
// 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"}},
}
for _, tc := range testCases {
hostConfig := &containertypes.HostConfig{
Runtime: "runc",
NetworkMode: "host",
PortBindings: tc.ports,
}
cs := &config.Config{
CommonUnixConfig: config.CommonUnixConfig{
Runtimes: map[string]types.Runtime{"runc": {}},
},
}
d := &Daemon{configStore: cs}
wrns, err := d.verifyContainerSettings("", hostConfig, &containertypes.Config{}, false)
require.NoError(t, err)
require.Equal(t, tc.warnings, wrns)
}
}