From c5613ac03239fe4d038f0c088ae54e408c9a4848 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 1 Dec 2022 10:47:10 +0100 Subject: [PATCH] cli/command/container: TestNewPortCommandOutput improve test Make sure that the container has multiple port-mappings to illustrate that only the given port is matched. Signed-off-by: Sebastiaan van Stijn --- cli/command/container/port_test.go | 21 +++++++++++++++++-- ...ontainer-port-ipv6-and-ipv4-443-udp.golden | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 cli/command/container/testdata/container-port-ipv6-and-ipv4-443-udp.golden diff --git a/cli/command/container/port_test.go b/cli/command/container/port_test.go index 8ae06a5fb7..60a7ebcca3 100644 --- a/cli/command/container/port_test.go +++ b/cli/command/container/port_test.go @@ -15,18 +15,27 @@ func TestNewPortCommandOutput(t *testing.T) { testCases := []struct { name string ips []string + port string }{ { name: "container-port-ipv4", ips: []string{"0.0.0.0"}, + port: "80", }, { name: "container-port-ipv6", ips: []string{"::"}, + port: "80", }, { name: "container-port-ipv6-and-ipv4", ips: []string{"::", "0.0.0.0"}, + port: "80", + }, + { + name: "container-port-ipv6-and-ipv4-443-udp", + ips: []string{"::", "0.0.0.0"}, + port: "443/udp", }, } for _, tc := range testCases { @@ -36,19 +45,27 @@ func TestNewPortCommandOutput(t *testing.T) { inspectFunc: func(string) (types.ContainerJSON, error) { ci := types.ContainerJSON{NetworkSettings: &types.NetworkSettings{}} ci.NetworkSettings.Ports = nat.PortMap{ - "80/tcp": make([]nat.PortBinding, len(tc.ips)), + "80/tcp": make([]nat.PortBinding, len(tc.ips)), + "443/tcp": make([]nat.PortBinding, len(tc.ips)), + "443/udp": make([]nat.PortBinding, len(tc.ips)), } for i, ip := range tc.ips { ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{ HostIP: ip, HostPort: "3456", } + ci.NetworkSettings.Ports["443/tcp"][i] = nat.PortBinding{ + HostIP: ip, HostPort: "4567", + } + ci.NetworkSettings.Ports["443/udp"][i] = nat.PortBinding{ + HostIP: ip, HostPort: "5678", + } } return ci, nil }, }, test.EnableContentTrust) cmd := NewPortCommand(cli) cmd.SetErr(io.Discard) - cmd.SetArgs([]string{"some_container", "80"}) + cmd.SetArgs([]string{"some_container", tc.port}) err := cmd.Execute() assert.NilError(t, err) golden.Assert(t, cli.OutBuffer().String(), tc.name+".golden") diff --git a/cli/command/container/testdata/container-port-ipv6-and-ipv4-443-udp.golden b/cli/command/container/testdata/container-port-ipv6-and-ipv4-443-udp.golden new file mode 100644 index 0000000000..fae92a6034 --- /dev/null +++ b/cli/command/container/testdata/container-port-ipv6-and-ipv4-443-udp.golden @@ -0,0 +1,2 @@ +[::]:5678 +0.0.0.0:5678