2021-02-15 10:00:07 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-02-25 07:05:59 -05:00
|
|
|
"io"
|
2021-02-15 10:00:07 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/golden"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPortCommandOutput(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
ips []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "container-port-ipv4",
|
|
|
|
ips: []string{"0.0.0.0"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-port-ipv6",
|
|
|
|
ips: []string{"::"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "container-port-ipv6-and-ipv4",
|
|
|
|
ips: []string{"::", "0.0.0.0"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
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)),
|
|
|
|
}
|
|
|
|
for i, ip := range tc.ips {
|
|
|
|
ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{
|
|
|
|
HostIP: ip, HostPort: "3456",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ci, nil
|
|
|
|
},
|
|
|
|
}, test.EnableContentTrust)
|
|
|
|
cmd := NewPortCommand(cli)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetErr(io.Discard)
|
2021-02-15 10:00:07 -05:00
|
|
|
cmd.SetArgs([]string{"some_container", "80"})
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), tc.name+".golden")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|