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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-12-01 10:47:10 +01:00
parent 58487e088a
commit c5613ac032
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 21 additions and 2 deletions

View File

@ -15,18 +15,27 @@ func TestNewPortCommandOutput(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
ips []string ips []string
port string
}{ }{
{ {
name: "container-port-ipv4", name: "container-port-ipv4",
ips: []string{"0.0.0.0"}, ips: []string{"0.0.0.0"},
port: "80",
}, },
{ {
name: "container-port-ipv6", name: "container-port-ipv6",
ips: []string{"::"}, ips: []string{"::"},
port: "80",
}, },
{ {
name: "container-port-ipv6-and-ipv4", name: "container-port-ipv6-and-ipv4",
ips: []string{"::", "0.0.0.0"}, 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 { for _, tc := range testCases {
@ -36,19 +45,27 @@ func TestNewPortCommandOutput(t *testing.T) {
inspectFunc: func(string) (types.ContainerJSON, error) { inspectFunc: func(string) (types.ContainerJSON, error) {
ci := types.ContainerJSON{NetworkSettings: &types.NetworkSettings{}} ci := types.ContainerJSON{NetworkSettings: &types.NetworkSettings{}}
ci.NetworkSettings.Ports = nat.PortMap{ 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 { for i, ip := range tc.ips {
ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{ ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{
HostIP: ip, HostPort: "3456", 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 return ci, nil
}, },
}, test.EnableContentTrust) }, test.EnableContentTrust)
cmd := NewPortCommand(cli) cmd := NewPortCommand(cli)
cmd.SetErr(io.Discard) cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"some_container", "80"}) cmd.SetArgs([]string{"some_container", tc.port})
err := cmd.Execute() err := cmd.Execute()
assert.NilError(t, err) assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), tc.name+".golden") golden.Assert(t, cli.OutBuffer().String(), tc.name+".golden")

View File

@ -0,0 +1,2 @@
[::]:5678
0.0.0.0:5678