123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package main
- import (
- "net"
- "os/exec"
- "sort"
- "strings"
- "github.com/go-check/check"
- )
- func (s *DockerSuite) TestPortList(c *check.C) {
- // one port
- runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
- out, _, err := runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- firstID := strings.TrimSpace(out)
- runCmd = exec.Command(dockerBinary, "port", firstID, "80")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
- c.Error("Port list is not correct")
- }
- runCmd = exec.Command(dockerBinary, "port", firstID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
- c.Error("Port list is not correct")
- }
- runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- c.Fatal(out, err)
- }
- // three port
- runCmd = exec.Command(dockerBinary, "run", "-d",
- "-p", "9876:80",
- "-p", "9877:81",
- "-p", "9878:82",
- "busybox", "top")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- ID := strings.TrimSpace(out)
- runCmd = exec.Command(dockerBinary, "port", ID, "80")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
- c.Error("Port list is not correct")
- }
- runCmd = exec.Command(dockerBinary, "port", ID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{
- "80/tcp -> 0.0.0.0:9876",
- "81/tcp -> 0.0.0.0:9877",
- "82/tcp -> 0.0.0.0:9878"}) {
- c.Error("Port list is not correct")
- }
- runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- // 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)
- if err != nil {
- c.Fatal(out, err)
- }
- ID = strings.TrimSpace(out)
- runCmd = exec.Command(dockerBinary, "port", ID, "80")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
- c.Error("Port list is not correct")
- }
- runCmd = exec.Command(dockerBinary, "port", ID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{
- "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"}) {
- c.Error("Port list is not correct\n", out)
- }
- runCmd = exec.Command(dockerBinary, "rm", "-f", ID)
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- c.Fatal(out, err)
- }
- }
- func assertPortList(c *check.C, out string, expected []string) bool {
- //lines := strings.Split(out, "\n")
- lines := strings.Split(strings.Trim(out, "\n "), "\n")
- if len(lines) != len(expected) {
- c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
- return false
- }
- sort.Strings(lines)
- sort.Strings(expected)
- for i := 0; i < len(expected); i++ {
- if lines[i] != expected[i] {
- c.Error("|" + lines[i] + "!=" + expected[i] + "|")
- return false
- }
- }
- return true
- }
- func (s *DockerSuite) TestPortHostBinding(c *check.C) {
- runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox",
- "nc", "-l", "-p", "80")
- out, _, err := runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- firstID := strings.TrimSpace(out)
- runCmd = exec.Command(dockerBinary, "port", firstID, "80")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
- c.Error("Port list is not correct")
- }
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
- "nc", "localhost", "9876")
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- c.Fatal(out, err)
- }
- runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- c.Fatal(out, err)
- }
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
- "nc", "localhost", "9876")
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
- c.Error("Port is still bound after the Container is removed")
- }
- }
- func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
- runCmd := exec.Command(dockerBinary, "run", "-d", "-P", "--expose", "80", "busybox",
- "nc", "-l", "-p", "80")
- out, _, err := runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- firstID := strings.TrimSpace(out)
- runCmd = exec.Command(dockerBinary, "port", firstID, "80")
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- _, exposedPort, err := net.SplitHostPort(out)
- if err != nil {
- c.Fatal(out, err)
- }
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
- "nc", "localhost", strings.TrimSpace(exposedPort))
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- c.Fatal(out, err)
- }
- runCmd = exec.Command(dockerBinary, "rm", "-f", firstID)
- if out, _, err = runCommandWithOutput(runCmd); err != nil {
- c.Fatal(out, err)
- }
- runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox",
- "nc", "localhost", strings.TrimSpace(exposedPort))
- if out, _, err = runCommandWithOutput(runCmd); err == nil {
- c.Error("Port is still bound after the Container is removed")
- }
- }
|