mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
update docker_cli_port_test.go with Assert
Part of #16756 Use c.Assert instead of condition judgement Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
This commit is contained in:
parent
d35a1f2868
commit
1ae4eeb813
1 changed files with 76 additions and 78 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/docker/pkg/integration/checker"
|
||||||
"github.com/go-check/check"
|
"github.com/go-check/check"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,15 +19,16 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", firstID, "80")
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
err := assertPortList(c, out, []string{"0.0.0.0:9876"})
|
||||||
c.Error("Port list is not correct")
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", firstID)
|
out, _ = dockerCmd(c, "port", firstID)
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"}) {
|
err = assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876"})
|
||||||
c.Error("Port list is not correct")
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
dockerCmd(c, "rm", "-f", firstID)
|
dockerCmd(c, "rm", "-f", firstID)
|
||||||
|
|
||||||
// three port
|
// three port
|
||||||
|
@ -39,18 +41,19 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", ID, "80")
|
out, _ = dockerCmd(c, "port", ID, "80")
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
err = assertPortList(c, out, []string{"0.0.0.0:9876"})
|
||||||
c.Error("Port list is not correct")
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", ID)
|
out, _ = dockerCmd(c, "port", ID)
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{
|
err = assertPortList(c, out, []string{
|
||||||
"80/tcp -> 0.0.0.0:9876",
|
"80/tcp -> 0.0.0.0:9876",
|
||||||
"81/tcp -> 0.0.0.0:9877",
|
"81/tcp -> 0.0.0.0:9877",
|
||||||
"82/tcp -> 0.0.0.0:9878"}) {
|
"82/tcp -> 0.0.0.0:9878"})
|
||||||
c.Error("Port list is not correct")
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
dockerCmd(c, "rm", "-f", ID)
|
dockerCmd(c, "rm", "-f", ID)
|
||||||
|
|
||||||
// more and one port mapped to the same container port
|
// more and one port mapped to the same container port
|
||||||
|
@ -64,19 +67,19 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", ID, "80")
|
out, _ = dockerCmd(c, "port", ID, "80")
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"}) {
|
err = assertPortList(c, out, []string{"0.0.0.0:9876", "0.0.0.0:9999"})
|
||||||
c.Error("Port list is not correct")
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", ID)
|
out, _ = dockerCmd(c, "port", ID)
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{
|
err = assertPortList(c, out, []string{
|
||||||
"80/tcp -> 0.0.0.0:9876",
|
"80/tcp -> 0.0.0.0:9876",
|
||||||
"80/tcp -> 0.0.0.0:9999",
|
"80/tcp -> 0.0.0.0:9999",
|
||||||
"81/tcp -> 0.0.0.0:9877",
|
"81/tcp -> 0.0.0.0:9877",
|
||||||
"82/tcp -> 0.0.0.0:9878"}) {
|
"82/tcp -> 0.0.0.0:9878"})
|
||||||
c.Error("Port list is not correct\n", out)
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
dockerCmd(c, "rm", "-f", ID)
|
dockerCmd(c, "rm", "-f", ID)
|
||||||
|
|
||||||
testRange := func() {
|
testRange := func() {
|
||||||
|
@ -90,19 +93,17 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", IDs[i])
|
out, _ = dockerCmd(c, "port", IDs[i])
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{
|
err = assertPortList(c, out, []string{fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i)})
|
||||||
fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i)}) {
|
// Port list is not correct
|
||||||
c.Error("Port list is not correct\n", out)
|
c.Assert(err, checker.IsNil)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// test port range exhaustion
|
// test port range exhaustion
|
||||||
out, _, err := dockerCmdWithError("run", "-d",
|
out, _, err = dockerCmdWithError("run", "-d",
|
||||||
"-p", "9090-9092:80",
|
"-p", "9090-9092:80",
|
||||||
"busybox", "top")
|
"busybox", "top")
|
||||||
if err == nil {
|
// Exhausted port range did not return an error
|
||||||
c.Errorf("Exhausted port range did not return an error. Out: %s", out)
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
dockerCmd(c, "rm", "-f", IDs[i])
|
dockerCmd(c, "rm", "-f", IDs[i])
|
||||||
|
@ -114,12 +115,11 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
// test invalid port ranges
|
// test invalid port ranges
|
||||||
for _, invalidRange := range []string{"9090-9089:80", "9090-:80", "-9090:80"} {
|
for _, invalidRange := range []string{"9090-9089:80", "9090-:80", "-9090:80"} {
|
||||||
out, _, err := dockerCmdWithError("run", "-d",
|
out, _, err = dockerCmdWithError("run", "-d",
|
||||||
"-p", invalidRange,
|
"-p", invalidRange,
|
||||||
"busybox", "top")
|
"busybox", "top")
|
||||||
if err == nil {
|
// Port range should have returned an error
|
||||||
c.Errorf("Port range should have returned an error. Out: %s", out)
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// test host range:container range spec.
|
// test host range:container range spec.
|
||||||
|
@ -130,13 +130,13 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", ID)
|
out, _ = dockerCmd(c, "port", ID)
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{
|
err = assertPortList(c, out, []string{
|
||||||
"80/tcp -> 0.0.0.0:9800",
|
"80/tcp -> 0.0.0.0:9800",
|
||||||
"81/tcp -> 0.0.0.0:9801",
|
"81/tcp -> 0.0.0.0:9801",
|
||||||
"82/tcp -> 0.0.0.0:9802",
|
"82/tcp -> 0.0.0.0:9802",
|
||||||
"83/tcp -> 0.0.0.0:9803"}) {
|
"83/tcp -> 0.0.0.0:9803"})
|
||||||
c.Error("Port list is not correct\n", out)
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
dockerCmd(c, "rm", "-f", ID)
|
dockerCmd(c, "rm", "-f", ID)
|
||||||
|
|
||||||
// test mixing protocols in same port range
|
// test mixing protocols in same port range
|
||||||
|
@ -148,32 +148,29 @@ func (s *DockerSuite) TestPortList(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", ID)
|
out, _ = dockerCmd(c, "port", ID)
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{
|
err = assertPortList(c, out, []string{
|
||||||
"80/tcp -> 0.0.0.0:8000",
|
"80/tcp -> 0.0.0.0:8000",
|
||||||
"80/udp -> 0.0.0.0:8000"}) {
|
"80/udp -> 0.0.0.0:8000"})
|
||||||
c.Error("Port list is not correct\n", out)
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
dockerCmd(c, "rm", "-f", ID)
|
dockerCmd(c, "rm", "-f", ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertPortList(c *check.C, out string, expected []string) bool {
|
func assertPortList(c *check.C, out string, expected []string) error {
|
||||||
//lines := strings.Split(out, "\n")
|
|
||||||
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
||||||
if len(lines) != len(expected) {
|
if len(lines) != len(expected) {
|
||||||
c.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
|
return fmt.Errorf("different size lists %s, %d, %d", out, len(lines), len(expected))
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
sort.Strings(lines)
|
sort.Strings(lines)
|
||||||
sort.Strings(expected)
|
sort.Strings(expected)
|
||||||
|
|
||||||
for i := 0; i < len(expected); i++ {
|
for i := 0; i < len(expected); i++ {
|
||||||
if lines[i] != expected[i] {
|
if lines[i] != expected[i] {
|
||||||
c.Error("|" + lines[i] + "!=" + expected[i] + "|")
|
return fmt.Errorf("|" + lines[i] + "!=" + expected[i] + "|")
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopRemoveContainer(id string, c *check.C) {
|
func stopRemoveContainer(id string, c *check.C) {
|
||||||
|
@ -193,9 +190,10 @@ func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
|
||||||
unpPort1 := fmt.Sprintf("%d/tcp", port1)
|
unpPort1 := fmt.Sprintf("%d/tcp", port1)
|
||||||
unpPort2 := fmt.Sprintf("%d/tcp", port2)
|
unpPort2 := fmt.Sprintf("%d/tcp", port2)
|
||||||
out, _ := dockerCmd(c, "ps", "-n=1")
|
out, _ := dockerCmd(c, "ps", "-n=1")
|
||||||
if !strings.Contains(out, unpPort1) || !strings.Contains(out, unpPort2) {
|
// Missing unpublished ports in docker ps output
|
||||||
c.Errorf("Missing unpublished ports(s) (%s, %s) in docker ps output: %s", unpPort1, unpPort2, out)
|
c.Assert(out, checker.Contains, unpPort1)
|
||||||
}
|
// Missing unpublished ports in docker ps output
|
||||||
|
c.Assert(out, checker.Contains, unpPort2)
|
||||||
|
|
||||||
// Run the container forcing to publish the exposed ports
|
// Run the container forcing to publish the exposed ports
|
||||||
dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
|
dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5")
|
||||||
|
@ -204,10 +202,10 @@ func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
|
||||||
expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
|
expBndRegx1 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort1)
|
||||||
expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
|
expBndRegx2 := regexp.MustCompile(`0.0.0.0:\d\d\d\d\d->` + unpPort2)
|
||||||
out, _ = dockerCmd(c, "ps", "-n=1")
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
||||||
if !expBndRegx1.MatchString(out) || !expBndRegx2.MatchString(out) {
|
// Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort1) in docker ps output
|
||||||
c.Errorf("Cannot find expected port binding ports(s) (0.0.0.0:xxxxx->%s, 0.0.0.0:xxxxx->%s) in docker ps output:\n%s",
|
c.Assert(expBndRegx1.MatchString(out), checker.Equals, true, check.Commentf("out: %s; unpPort1: %s", out, unpPort1))
|
||||||
unpPort1, unpPort2, out)
|
// Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort2) in docker ps output
|
||||||
}
|
c.Assert(expBndRegx2.MatchString(out), checker.Equals, true, check.Commentf("out: %s; unpPort2: %s", out, unpPort2))
|
||||||
|
|
||||||
// Run the container specifying explicit port bindings for the exposed ports
|
// Run the container specifying explicit port bindings for the exposed ports
|
||||||
offset := 10000
|
offset := 10000
|
||||||
|
@ -220,9 +218,11 @@ func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
|
||||||
expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
|
expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1)
|
||||||
expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
|
expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2)
|
||||||
out, _ = dockerCmd(c, "ps", "-n=1")
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
||||||
if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
|
// Cannot find expected port binding (expBnd1) in docker ps output
|
||||||
c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
|
c.Assert(out, checker.Contains, expBnd1)
|
||||||
}
|
// Cannot find expected port binding (expBnd2) in docker ps output
|
||||||
|
c.Assert(out, checker.Contains, expBnd2)
|
||||||
|
|
||||||
// Remove container now otherwise it will interfeer with next test
|
// Remove container now otherwise it will interfeer with next test
|
||||||
stopRemoveContainer(id, c)
|
stopRemoveContainer(id, c)
|
||||||
|
|
||||||
|
@ -232,9 +232,10 @@ func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
|
||||||
|
|
||||||
// Check docker ps o/p for last created container reports the specified port mappings
|
// Check docker ps o/p for last created container reports the specified port mappings
|
||||||
out, _ = dockerCmd(c, "ps", "-n=1")
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
||||||
if !strings.Contains(out, expBnd1) || !strings.Contains(out, expBnd2) {
|
// Cannot find expected port binding (expBnd1) in docker ps output
|
||||||
c.Errorf("Cannot find expected port binding(s) (%s, %s) in docker ps output: %s", expBnd1, expBnd2, out)
|
c.Assert(out, checker.Contains, expBnd1)
|
||||||
}
|
// Cannot find expected port binding (expBnd2) in docker ps output
|
||||||
|
c.Assert(out, checker.Contains, expBnd2)
|
||||||
// Remove container now otherwise it will interfeer with next test
|
// Remove container now otherwise it will interfeer with next test
|
||||||
stopRemoveContainer(id, c)
|
stopRemoveContainer(id, c)
|
||||||
|
|
||||||
|
@ -243,9 +244,10 @@ func (s *DockerSuite) TestUnpublishedPortsInPsOutput(c *check.C) {
|
||||||
|
|
||||||
// Check docker ps o/p for last created container reports the specified unpublished port and port mapping
|
// Check docker ps o/p for last created container reports the specified unpublished port and port mapping
|
||||||
out, _ = dockerCmd(c, "ps", "-n=1")
|
out, _ = dockerCmd(c, "ps", "-n=1")
|
||||||
if !strings.Contains(out, unpPort1) || !strings.Contains(out, expBnd2) {
|
// Missing unpublished exposed ports (unpPort1) in docker ps output
|
||||||
c.Errorf("Missing unpublished ports or port binding (%s, %s) in docker ps output: %s", unpPort1, expBnd2, out)
|
c.Assert(out, checker.Contains, unpPort1)
|
||||||
}
|
// Missing port binding (expBnd2) in docker ps output
|
||||||
|
c.Assert(out, checker.Contains, expBnd2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
||||||
|
@ -256,19 +258,18 @@ func (s *DockerSuite) TestPortHostBinding(c *check.C) {
|
||||||
|
|
||||||
out, _ = dockerCmd(c, "port", firstID, "80")
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
||||||
|
|
||||||
if !assertPortList(c, out, []string{"0.0.0.0:9876"}) {
|
err := assertPortList(c, out, []string{"0.0.0.0:9876"})
|
||||||
c.Error("Port list is not correct")
|
// Port list is not correct
|
||||||
}
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
dockerCmd(c, "run", "--net=host", "busybox",
|
dockerCmd(c, "run", "--net=host", "busybox",
|
||||||
"nc", "localhost", "9876")
|
"nc", "localhost", "9876")
|
||||||
|
|
||||||
dockerCmd(c, "rm", "-f", firstID)
|
dockerCmd(c, "rm", "-f", firstID)
|
||||||
|
|
||||||
if _, _, err := dockerCmdWithError("run", "--net=host", "busybox",
|
out, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "9876")
|
||||||
"nc", "localhost", "9876"); err == nil {
|
// Port is still bound after the Container is removed
|
||||||
c.Error("Port is still bound after the Container is removed")
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
||||||
|
@ -280,18 +281,15 @@ func (s *DockerSuite) TestPortExposeHostBinding(c *check.C) {
|
||||||
out, _ = dockerCmd(c, "port", firstID, "80")
|
out, _ = dockerCmd(c, "port", firstID, "80")
|
||||||
|
|
||||||
_, exposedPort, err := net.SplitHostPort(out)
|
_, exposedPort, err := net.SplitHostPort(out)
|
||||||
|
c.Assert(err, checker.IsNil, check.Commentf("out: %s", out))
|
||||||
if err != nil {
|
|
||||||
c.Fatal(out, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dockerCmd(c, "run", "--net=host", "busybox",
|
dockerCmd(c, "run", "--net=host", "busybox",
|
||||||
"nc", "localhost", strings.TrimSpace(exposedPort))
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
||||||
|
|
||||||
dockerCmd(c, "rm", "-f", firstID)
|
dockerCmd(c, "rm", "-f", firstID)
|
||||||
|
|
||||||
if _, _, err = dockerCmdWithError("run", "--net=host", "busybox",
|
out, _, err = dockerCmdWithError("run", "--net=host", "busybox",
|
||||||
"nc", "localhost", strings.TrimSpace(exposedPort)); err == nil {
|
"nc", "localhost", strings.TrimSpace(exposedPort))
|
||||||
c.Error("Port is still bound after the Container is removed")
|
// Port is still bound after the Container is removed
|
||||||
}
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue