github.com/gunjan5/docker@v1.8.2/integration-cli/docker_cli_port_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"net"
     7  	"strings"
     8  
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func (s *DockerSuite) TestPortHostBinding(c *check.C) {
    13  	out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox",
    14  		"nc", "-l", "-p", "80")
    15  	firstID := strings.TrimSpace(out)
    16  
    17  	out, _ = dockerCmd(c, "port", firstID, "80")
    18  
    19  	if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
    20  		c.Error("Port list is not correct")
    21  	}
    22  
    23  	dockerCmd(c, "run", "--net=host", "busybox",
    24  		"nc", "localhost", "9876")
    25  
    26  	dockerCmd(c, "rm", "-f", firstID)
    27  
    28  	if _, _, err := dockerCmdWithError(c, "run", "--net=host", "busybox",
    29  		"nc", "localhost", "9876"); err == nil {
    30  		c.Error("Port is still bound after the Container is removed")
    31  	}
    32  }
    33  
    34  func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
    35  	out, _ := dockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox",
    36  		"nc", "-l", "-p", "80")
    37  	firstID := strings.TrimSpace(out)
    38  
    39  	out, _ = dockerCmd(c, "port", firstID, "80")
    40  
    41  	_, exposedPort, err := net.SplitHostPort(out)
    42  
    43  	if err != nil {
    44  		c.Fatal(out, err)
    45  	}
    46  
    47  	dockerCmd(c, "run", "--net=host", "busybox",
    48  		"nc", "localhost", strings.TrimSpace(exposedPort))
    49  
    50  	dockerCmd(c, "rm", "-f", firstID)
    51  
    52  	if _, _, err = dockerCmdWithError(c, "run", "--net=host", "busybox",
    53  		"nc", "localhost", strings.TrimSpace(exposedPort)); err == nil {
    54  		c.Error("Port is still bound after the Container is removed")
    55  	}
    56  }