From f1102e98bba7246381a90e6bc2dcb6c04770f903 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 27 Jul 2023 22:11:27 +0200 Subject: [PATCH] integration-cli: DockerCLIPortSuite: replace dockerCmd and waitRun Signed-off-by: Sebastiaan van Stijn --- integration-cli/docker_cli_port_test.go | 124 ++++++++++++------------ 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/integration-cli/docker_cli_port_test.go b/integration-cli/docker_cli_port_test.go index e388e8f2fb..96b38e0216 100644 --- a/integration-cli/docker_cli_port_test.go +++ b/integration-cli/docker_cli_port_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" + "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/testutil" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -31,32 +32,33 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) { ctx := testutil.GetContext(c) // one port - out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top") - firstID := strings.TrimSpace(out) + firstID := cli.DockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "top").Stdout() + firstID = strings.TrimSpace(firstID) - out, _ = dockerCmd(c, "port", firstID, "80") + out := cli.DockerCmd(c, "port", firstID, "80").Stdout() assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876"}) - out, _ = dockerCmd(c, "port", firstID) + out = cli.DockerCmd(c, "port", firstID).Stdout() assertPortList(c, out, []string{"80/tcp -> 0.0.0.0:9876", "80/tcp -> [::]:9876"}) - dockerCmd(c, "rm", "-f", firstID) + cli.DockerCmd(c, "rm", "-f", firstID) // three port - out, _ = dockerCmd(c, "run", "-d", + id := cli.DockerCmd(c, "run", "-d", "-p", "9876:80", "-p", "9877:81", "-p", "9878:82", - "busybox", "top") - ID := strings.TrimSpace(out) + "busybox", "top", + ).Stdout() + id = strings.TrimSpace(id) - out, _ = dockerCmd(c, "port", ID, "80") + out = cli.DockerCmd(c, "port", id, "80").Stdout() assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876"}) - out, _ = dockerCmd(c, "port", ID) + out = cli.DockerCmd(c, "port", id).Stdout() assertPortList(c, out, []string{ "80/tcp -> 0.0.0.0:9876", @@ -67,22 +69,23 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) { "82/tcp -> [::]:9878", }) - dockerCmd(c, "rm", "-f", ID) + cli.DockerCmd(c, "rm", "-f", id) // more and one port mapped to the same container port - out, _ = dockerCmd(c, "run", "-d", + id = cli.DockerCmd(c, "run", "-d", "-p", "9876:80", "-p", "9999:80", "-p", "9877:81", "-p", "9878:82", - "busybox", "top") - ID = strings.TrimSpace(out) + "busybox", "top", + ).Stdout() + id = strings.TrimSpace(id) - out, _ = dockerCmd(c, "port", ID, "80") + out = cli.DockerCmd(c, "port", id, "80").Stdout() assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876", "0.0.0.0:9999", "[::]:9999"}) - out, _ = dockerCmd(c, "port", ID) + out = cli.DockerCmd(c, "port", id).Stdout() assertPortList(c, out, []string{ "80/tcp -> 0.0.0.0:9876", @@ -94,16 +97,16 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) { "82/tcp -> 0.0.0.0:9878", "82/tcp -> [::]:9878", }) - dockerCmd(c, "rm", "-f", ID) + cli.DockerCmd(c, "rm", "-f", id) testRange := func() { // host port ranges used IDs := make([]string, 3) for i := 0; i < 3; i++ { - out, _ = dockerCmd(c, "run", "-d", "-p", "9090-9092:80", "busybox", "top") + out = cli.DockerCmd(c, "run", "-d", "-p", "9090-9092:80", "busybox", "top").Stdout() IDs[i] = strings.TrimSpace(out) - out, _ = dockerCmd(c, "port", IDs[i]) + out = cli.DockerCmd(c, "port", IDs[i]).Stdout() assertPortList(c, out, []string{ fmt.Sprintf("80/tcp -> 0.0.0.0:%d", 9090+i), @@ -117,7 +120,7 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) { assert.Assert(c, err != nil, "out: %s", out) for i := 0; i < 3; i++ { - dockerCmd(c, "rm", "-f", IDs[i]) + cli.DockerCmd(c, "rm", "-f", IDs[i]) } } testRange() @@ -132,10 +135,10 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) { } // test host range:container range spec. - out, _ = dockerCmd(c, "run", "-d", "-p", "9800-9803:80-83", "busybox", "top") - ID = strings.TrimSpace(out) + id = cli.DockerCmd(c, "run", "-d", "-p", "9800-9803:80-83", "busybox", "top").Stdout() + id = strings.TrimSpace(id) - out, _ = dockerCmd(c, "port", ID) + out = cli.DockerCmd(c, "port", id).Stdout() assertPortList(c, out, []string{ "80/tcp -> 0.0.0.0:9800", @@ -147,17 +150,17 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) { "83/tcp -> 0.0.0.0:9803", "83/tcp -> [::]:9803", }) - dockerCmd(c, "rm", "-f", ID) + cli.DockerCmd(c, "rm", "-f", id) // test mixing protocols in same port range - out, _ = dockerCmd(c, "run", "-d", "-p", "8000-8080:80", "-p", "8000-8080:80/udp", "busybox", "top") - ID = strings.TrimSpace(out) - - out, _ = dockerCmd(c, "port", ID) + id = cli.DockerCmd(c, "run", "-d", "-p", "8000-8080:80", "-p", "8000-8080:80/udp", "busybox", "top").Stdout() + id = strings.TrimSpace(id) + out = cli.DockerCmd(c, "port", id).Stdout() // Running this test multiple times causes the TCP port to increment. - assertPortRange(ctx, ID, []int{8000, 8080}, []int{8000, 8080}) - dockerCmd(c, "rm", "-f", ID) + err := assertPortRange(ctx, id, []int{8000, 8080}, []int{8000, 8080}) + assert.Check(c, err) + cli.DockerCmd(c, "rm", "-f", id) } func assertPortList(c *testing.T, out string, expected []string) { @@ -231,7 +234,7 @@ func assertPortRange(ctx context.Context, id string, expectedTCP, expectedUDP [] } func stopRemoveContainer(id string, c *testing.T) { - dockerCmd(c, "rm", "-f", id) + cli.DockerCmd(c, "rm", "-f", id) } func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) { @@ -241,23 +244,23 @@ func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) { port2 := 443 expose1 := fmt.Sprintf("--expose=%d", port1) expose2 := fmt.Sprintf("--expose=%d", port2) - dockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5") + cli.DockerCmd(c, "run", "-d", expose1, expose2, "busybox", "sleep", "5") // Check docker ps o/p for last created container reports the unpublished ports unpPort1 := fmt.Sprintf("%d/tcp", port1) unpPort2 := fmt.Sprintf("%d/tcp", port2) - out, _ := dockerCmd(c, "ps", "-n=1") + out := cli.DockerCmd(c, "ps", "-n=1").Stdout() // Missing unpublished ports in docker ps output assert.Assert(c, strings.Contains(out, unpPort1)) // Missing unpublished ports in docker ps output assert.Assert(c, strings.Contains(out, unpPort2)) // Run the container forcing to publish the exposed ports - dockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5") + cli.DockerCmd(c, "run", "-d", "-P", expose1, expose2, "busybox", "sleep", "5") // Check docker ps o/p for last created container reports the exposed ports in the port bindings 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) - out, _ = dockerCmd(c, "ps", "-n=1") + out = cli.DockerCmd(c, "ps", "-n=1").Stdout() // Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort1) in docker ps output assert.Equal(c, expBndRegx1.MatchString(out), true, fmt.Sprintf("out: %s; unpPort1: %s", out, unpPort1)) // Cannot find expected port binding port (0.0.0.0:xxxxx->unpPort2) in docker ps output @@ -267,13 +270,14 @@ func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) { offset := 10000 pFlag1 := fmt.Sprintf("%d:%d", offset+port1, port1) pFlag2 := fmt.Sprintf("%d:%d", offset+port2, port2) - out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5") - id := strings.TrimSpace(out) + + id := cli.DockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, expose1, expose2, "busybox", "sleep", "5").Stdout() + id = strings.TrimSpace(id) // Check docker ps o/p for last created container reports the specified port mappings expBnd1 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port1, unpPort1) expBnd2 := fmt.Sprintf("0.0.0.0:%d->%s", offset+port2, unpPort2) - out, _ = dockerCmd(c, "ps", "-n=1") + out = cli.DockerCmd(c, "ps", "-n=1").Stdout() // Cannot find expected port binding (expBnd1) in docker ps output assert.Assert(c, strings.Contains(out, expBnd1)) // Cannot find expected port binding (expBnd2) in docker ps output @@ -282,11 +286,11 @@ func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) { stopRemoveContainer(id, c) // Run the container with explicit port bindings and no exposed ports - out, _ = dockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5") - id = strings.TrimSpace(out) + id = cli.DockerCmd(c, "run", "-d", "-p", pFlag1, "-p", pFlag2, "busybox", "sleep", "5").Stdout() + id = strings.TrimSpace(id) // Check docker ps o/p for last created container reports the specified port mappings - out, _ = dockerCmd(c, "ps", "-n=1") + out = cli.DockerCmd(c, "ps", "-n=1").Stdout() // Cannot find expected port binding (expBnd1) in docker ps output assert.Assert(c, strings.Contains(out, expBnd1)) // Cannot find expected port binding (expBnd2) in docker ps output @@ -295,10 +299,10 @@ func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) { stopRemoveContainer(id, c) // Run the container with one unpublished exposed port and one explicit port binding - dockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5") + cli.DockerCmd(c, "run", "-d", expose1, "-p", pFlag2, "busybox", "sleep", "5") // Check docker ps o/p for last created container reports the specified unpublished port and port mapping - out, _ = dockerCmd(c, "ps", "-n=1") + out = cli.DockerCmd(c, "ps", "-n=1").Stdout() // Missing unpublished exposed ports (unpPort1) in docker ps output assert.Assert(c, strings.Contains(out, unpPort1)) // Missing port binding (expBnd2) in docker ps output @@ -307,16 +311,16 @@ func (s *DockerCLIPortSuite) TestUnpublishedPortsInPsOutput(c *testing.T) { func (s *DockerCLIPortSuite) TestPortHostBinding(c *testing.T) { testRequires(c, DaemonIsLinux, NotUserNamespace) - out, _ := dockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "nc", "-l", "-p", "80") - firstID := strings.TrimSpace(out) + firstID := cli.DockerCmd(c, "run", "-d", "-p", "9876:80", "busybox", "nc", "-l", "-p", "80").Stdout() + firstID = strings.TrimSpace(firstID) - out, _ = dockerCmd(c, "port", firstID, "80") + out := cli.DockerCmd(c, "port", firstID, "80").Stdout() assertPortList(c, out, []string{"0.0.0.0:9876", "[::]:9876"}) - dockerCmd(c, "run", "--net=host", "busybox", "nc", "localhost", "9876") + cli.DockerCmd(c, "run", "--net=host", "busybox", "nc", "localhost", "9876") - dockerCmd(c, "rm", "-f", firstID) + cli.DockerCmd(c, "rm", "-f", firstID) out, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "9876") // Port is still bound after the Container is removed @@ -325,15 +329,14 @@ func (s *DockerCLIPortSuite) TestPortHostBinding(c *testing.T) { func (s *DockerCLIPortSuite) TestPortExposeHostBinding(c *testing.T) { testRequires(c, DaemonIsLinux, NotUserNamespace) - out, _ := dockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox", "nc", "-l", "-p", "80") - firstID := strings.TrimSpace(out) + firstID := cli.DockerCmd(c, "run", "-d", "-P", "--expose", "80", "busybox", "nc", "-l", "-p", "80").Stdout() + firstID = strings.TrimSpace(firstID) - out, _ = dockerCmd(c, "inspect", "--format", `{{index .NetworkSettings.Ports "80/tcp" 0 "HostPort" }}`, firstID) + exposedPort := cli.DockerCmd(c, "inspect", "--format", `{{index .NetworkSettings.Ports "80/tcp" 0 "HostPort" }}`, firstID).Stdout() + exposedPort = strings.TrimSpace(exposedPort) + cli.DockerCmd(c, "run", "--net=host", "busybox", "nc", "127.0.0.1", exposedPort) - exposedPort := strings.TrimSpace(out) - dockerCmd(c, "run", "--net=host", "busybox", "nc", "127.0.0.1", exposedPort) - - dockerCmd(c, "rm", "-f", firstID) + cli.DockerCmd(c, "rm", "-f", firstID) out, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "127.0.0.1", exposedPort) // Port is still bound after the Container is removed @@ -342,19 +345,18 @@ func (s *DockerCLIPortSuite) TestPortExposeHostBinding(c *testing.T) { func (s *DockerCLIPortSuite) TestPortBindingOnSandbox(c *testing.T) { testRequires(c, DaemonIsLinux, NotUserNamespace) - dockerCmd(c, "network", "create", "--internal", "-d", "bridge", "internal-net") + cli.DockerCmd(c, "network", "create", "--internal", "-d", "bridge", "internal-net") nr := getNetworkResource(c, "internal-net") assert.Equal(c, nr.Internal, true) - dockerCmd(c, "run", "--net", "internal-net", "-d", "--name", "c1", - "-p", "8080:8080", "busybox", "nc", "-l", "-p", "8080") - assert.Assert(c, waitRun("c1") == nil) + cli.DockerCmd(c, "run", "--net", "internal-net", "-d", "--name", "c1", "-p", "8080:8080", "busybox", "nc", "-l", "-p", "8080") + cli.WaitRun(c, "c1") _, _, err := dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080") assert.Assert(c, err != nil, "Port mapping on internal network is expected to fail") // Connect container to another normal bridge network - dockerCmd(c, "network", "create", "-d", "bridge", "foo-net") - dockerCmd(c, "network", "connect", "foo-net", "c1") + cli.DockerCmd(c, "network", "create", "-d", "bridge", "foo-net") + cli.DockerCmd(c, "network", "connect", "foo-net", "c1") _, _, err = dockerCmdWithError("run", "--net=host", "busybox", "nc", "localhost", "8080") assert.Assert(c, err == nil, "Port mapping on the new network is expected to succeed")