2015-07-08 16:30:03 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox",
|
2015-07-08 16:30:03 -04:00
|
|
|
"nc", "-l", "-p", "80")
|
|
|
|
firstID := strings.TrimSpace(out)
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
2015-07-08 16:30:03 -04:00
|
|
|
|
|
|
|
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
|
|
|
c.Error("Port list is not correct")
|
|
|
|
}
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "run", "--net=host", "busybox",
|
2015-07-08 16:30:03 -04:00
|
|
|
"nc", "localhost", "9876")
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "rm", "-f", firstID)
|
2015-07-08 16:30:03 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
if _, _, err := dockerCmdWithError(c, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", "9876"); err == nil {
|
2015-07-08 16:30:03 -04:00
|
|
|
c.Error("Port is still bound after the Container is removed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox",
|
2015-07-08 16:30:03 -04:00
|
|
|
"nc", "-l", "-p", "80")
|
|
|
|
firstID := strings.TrimSpace(out)
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
2015-07-08 16:30:03 -04:00
|
|
|
|
|
|
|
_, exposedPort, err := net.SplitHostPort(out)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(out, err)
|
|
|
|
}
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "run", "--net=host", "busybox",
|
2015-07-08 16:30:03 -04:00
|
|
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "rm", "-f", firstID)
|
2015-07-08 16:30:03 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
if _, _, err = dockerCmdWithError(c, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", strings.TrimSpace(exposedPort)); err == nil {
|
2015-07-08 16:30:03 -04:00
|
|
|
c.Error("Port is still bound after the Container is removed")
|
|
|
|
}
|
|
|
|
}
|