2014-06-23 22:03:08 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-18 20:55:40 -04:00
|
|
|
"net"
|
2014-06-23 22:03:08 -04:00
|
|
|
"os/exec"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-06-23 22:03:08 -04:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestPortList(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-06-23 22:03:08 -04:00
|
|
|
// one port
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
firstID := strings.TrimSpace(out)
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
|
|
|
c.Error("Port list is not correct")
|
2014-06-23 22:03:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", firstID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
|
|
|
|
c.Error("Port list is not correct")
|
2014-06-23 22:03:08 -04:00
|
|
|
}
|
|
|
|
runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
|
2014-10-14 15:23:10 -04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
// three port
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "-d",
|
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
|
|
|
"busybox", "top")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
ID := strings.TrimSpace(out)
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", ID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
|
|
|
c.Error("Port list is not correct")
|
2014-06-23 22:03:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", ID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{
|
2014-06-23 22:03:08 -04:00
|
|
|
"80/tcp -> 0.0.0.0:9876",
|
|
|
|
"81/tcp -> 0.0.0.0:9877",
|
|
|
|
"82/tcp -> 0.0.0.0:9878"}) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Error("Port list is not correct")
|
2014-06-23 22:03:08 -04:00
|
|
|
}
|
|
|
|
runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
// more and one port mapped to the same container port
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "-d",
|
|
|
|
"-p", "9876:80",
|
|
|
|
"-p", "9999:80",
|
|
|
|
"-p", "9877:81",
|
|
|
|
"-p", "9878:82",
|
|
|
|
"busybox", "top")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
ID = strings.TrimSpace(out)
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", ID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
|
|
|
|
c.Error("Port list is not correct")
|
2014-06-23 22:03:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", ID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:23:10 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{
|
2014-06-23 22:03:08 -04:00
|
|
|
"80/tcp -> 0.0.0.0:9876",
|
|
|
|
"80/tcp -> 0.0.0.0:9999",
|
|
|
|
"81/tcp -> 0.0.0.0:9877",
|
|
|
|
"82/tcp -> 0.0.0.0:9878"}) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Error("Port list is not correct\n", out)
|
2014-06-23 22:03:08 -04:00
|
|
|
}
|
|
|
|
runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
|
2014-10-14 15:23:10 -04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:23:10 -04:00
|
|
|
}
|
2014-06-23 22:03:08 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func assertPortList(c *check.C, out string, expected []string) bool {
|
2014-06-23 22:03:08 -04:00
|
|
|
//lines := strings.Split(out, "\n")
|
|
|
|
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
|
|
|
if len(lines) != len(expected) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
|
2014-06-23 22:03:08 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
sort.Strings(lines)
|
|
|
|
sort.Strings(expected)
|
|
|
|
|
|
|
|
for i := 0; i < len(expected); i++ {
|
|
|
|
if lines[i] != expected[i] {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Error("|" + lines[i] + "!=" + expected[i] + "|")
|
2014-06-23 22:03:08 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2015-04-18 20:55:40 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
2015-04-18 20:55:40 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
|
|
|
|
"nc", "-l", "-p", "80")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
firstID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
|
|
|
c.Error("Port list is not correct")
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", "9876")
|
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
|
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", "9876")
|
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Error("Port is still bound after the Container is removed")
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
2015-04-18 20:55:40 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
|
|
|
|
"nc", "-l", "-p", "80")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
firstID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "port", firstID, "80")
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, exposedPort, err := net.SplitHostPort(out)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
|
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
|
|
|
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Error("Port is still bound after the Container is removed")
|
2015-04-18 20:55:40 -04:00
|
|
|
}
|
|
|
|
}
|