github.com/rish1988/moby@v25.0.2+incompatible/daemon/container_unix_test.go (about)

     1  //go:build linux || freebsd
     2  
     3  package daemon
     4  
     5  import (
     6  	"testing"
     7  
     8  	containertypes "github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/daemon/config"
    10  	"github.com/docker/go-connections/nat"
    11  	"gotest.tools/v3/assert"
    12  )
    13  
    14  // TestContainerWarningHostAndPublishPorts that a warning is returned when setting network mode to host and specifying published ports.
    15  // This should not be tested on Windows because Windows doesn't support "host" network mode.
    16  func TestContainerWarningHostAndPublishPorts(t *testing.T) {
    17  	testCases := []struct {
    18  		ports    nat.PortMap
    19  		warnings []string
    20  	}{
    21  		{ports: nat.PortMap{}},
    22  		{ports: nat.PortMap{
    23  			"8080": []nat.PortBinding{{HostPort: "8989"}},
    24  		}, warnings: []string{"Published ports are discarded when using host network mode"}},
    25  	}
    26  	muteLogs(t)
    27  
    28  	for _, tc := range testCases {
    29  		hostConfig := &containertypes.HostConfig{
    30  			Runtime:      "runc",
    31  			NetworkMode:  "host",
    32  			PortBindings: tc.ports,
    33  		}
    34  		d := &Daemon{}
    35  		cfg, err := config.New()
    36  		assert.NilError(t, err)
    37  		runtimes, err := setupRuntimes(cfg)
    38  		assert.NilError(t, err)
    39  		daemonCfg := &configStore{Config: *cfg, Runtimes: runtimes}
    40  		wrns, err := d.verifyContainerSettings(daemonCfg, hostConfig, &containertypes.Config{}, false)
    41  		assert.NilError(t, err)
    42  		assert.DeepEqual(t, tc.warnings, wrns)
    43  	}
    44  }