github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/port_test.go (about)

     1  package container
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/internal/test"
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/go-connections/nat"
    10  	"gotest.tools/v3/assert"
    11  	"gotest.tools/v3/golden"
    12  )
    13  
    14  func TestNewPortCommandOutput(t *testing.T) {
    15  	testCases := []struct {
    16  		name string
    17  		ips  []string
    18  		port string
    19  	}{
    20  		{
    21  			name: "container-port-ipv4",
    22  			ips:  []string{"0.0.0.0"},
    23  			port: "80",
    24  		},
    25  		{
    26  			name: "container-port-ipv6",
    27  			ips:  []string{"::"},
    28  			port: "80",
    29  		},
    30  		{
    31  			name: "container-port-ipv6-and-ipv4",
    32  			ips:  []string{"::", "0.0.0.0"},
    33  			port: "80",
    34  		},
    35  		{
    36  			name: "container-port-ipv6-and-ipv4-443-udp",
    37  			ips:  []string{"::", "0.0.0.0"},
    38  			port: "443/udp",
    39  		},
    40  		{
    41  			name: "container-port-all-ports",
    42  			ips:  []string{"::", "0.0.0.0"},
    43  		},
    44  	}
    45  	for _, tc := range testCases {
    46  		tc := tc
    47  		t.Run(tc.name, func(t *testing.T) {
    48  			cli := test.NewFakeCli(&fakeClient{
    49  				inspectFunc: func(string) (types.ContainerJSON, error) {
    50  					ci := types.ContainerJSON{NetworkSettings: &types.NetworkSettings{}}
    51  					ci.NetworkSettings.Ports = nat.PortMap{
    52  						"80/tcp":  make([]nat.PortBinding, len(tc.ips)),
    53  						"443/tcp": make([]nat.PortBinding, len(tc.ips)),
    54  						"443/udp": make([]nat.PortBinding, len(tc.ips)),
    55  					}
    56  					for i, ip := range tc.ips {
    57  						ci.NetworkSettings.Ports["80/tcp"][i] = nat.PortBinding{
    58  							HostIP: ip, HostPort: "3456",
    59  						}
    60  						ci.NetworkSettings.Ports["443/tcp"][i] = nat.PortBinding{
    61  							HostIP: ip, HostPort: "4567",
    62  						}
    63  						ci.NetworkSettings.Ports["443/udp"][i] = nat.PortBinding{
    64  							HostIP: ip, HostPort: "5678",
    65  						}
    66  					}
    67  					return ci, nil
    68  				},
    69  			}, test.EnableContentTrust)
    70  			cmd := NewPortCommand(cli)
    71  			cmd.SetErr(io.Discard)
    72  			cmd.SetArgs([]string{"some_container", tc.port})
    73  			err := cmd.Execute()
    74  			assert.NilError(t, err)
    75  			golden.Assert(t, cli.OutBuffer().String(), tc.name+".golden")
    76  		})
    77  	}
    78  }