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

     1  package stack
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/internal/test"
     8  	"github.com/docker/cli/internal/test/builders"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/swarm"
    11  	"github.com/pkg/errors"
    12  	"gotest.tools/v3/assert"
    13  	"gotest.tools/v3/golden"
    14  )
    15  
    16  func TestListErrors(t *testing.T) {
    17  	testCases := []struct {
    18  		args            []string
    19  		flags           map[string]string
    20  		serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
    21  		expectedError   string
    22  	}{
    23  		{
    24  			args:          []string{"foo"},
    25  			expectedError: "accepts no argument",
    26  		},
    27  		{
    28  			flags: map[string]string{
    29  				"format": "{{invalid format}}",
    30  			},
    31  			expectedError: "template parsing error",
    32  		},
    33  		{
    34  			serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
    35  				return []swarm.Service{}, errors.Errorf("error getting services")
    36  			},
    37  			expectedError: "error getting services",
    38  		},
    39  		{
    40  			serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
    41  				return []swarm.Service{*builders.Service()}, nil
    42  			},
    43  			expectedError: "cannot get label",
    44  		},
    45  	}
    46  
    47  	for _, tc := range testCases {
    48  		cmd := newListCommand(test.NewFakeCli(&fakeClient{
    49  			serviceListFunc: tc.serviceListFunc,
    50  		}))
    51  		cmd.SetArgs(tc.args)
    52  		cmd.SetOut(io.Discard)
    53  		for key, value := range tc.flags {
    54  			assert.Check(t, cmd.Flags().Set(key, value))
    55  		}
    56  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    57  	}
    58  }
    59  
    60  func TestStackList(t *testing.T) {
    61  	testCases := []struct {
    62  		doc          string
    63  		serviceNames []string
    64  		flags        map[string]string
    65  		golden       string
    66  	}{
    67  		{
    68  			doc:          "WithFormat",
    69  			serviceNames: []string{"service-name-foo"},
    70  			flags: map[string]string{
    71  				"format": "{{ .Name }}",
    72  			},
    73  			golden: "stack-list-with-format.golden",
    74  		},
    75  		{
    76  			doc:          "WithoutFormat",
    77  			serviceNames: []string{"service-name-foo"},
    78  			golden:       "stack-list-without-format.golden",
    79  		},
    80  		{
    81  			doc: "Sort",
    82  			serviceNames: []string{
    83  				"service-name-foo",
    84  				"service-name-bar",
    85  			},
    86  			golden: "stack-list-sort.golden",
    87  		},
    88  		{
    89  			doc: "SortNatural",
    90  			serviceNames: []string{
    91  				"service-name-1-foo",
    92  				"service-name-10-foo",
    93  				"service-name-2-foo",
    94  			},
    95  			golden: "stack-list-sort-natural.golden",
    96  		},
    97  	}
    98  
    99  	for _, tc := range testCases {
   100  		t.Run(tc.doc, func(t *testing.T) {
   101  			var services []swarm.Service
   102  			for _, name := range tc.serviceNames {
   103  				services = append(services,
   104  					*builders.Service(
   105  						builders.ServiceLabels(map[string]string{
   106  							"com.docker.stack.namespace": name,
   107  						}),
   108  					),
   109  				)
   110  			}
   111  			cli := test.NewFakeCli(&fakeClient{
   112  				serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
   113  					return services, nil
   114  				},
   115  			})
   116  			cmd := newListCommand(cli)
   117  			for key, value := range tc.flags {
   118  				assert.Check(t, cmd.Flags().Set(key, value))
   119  			}
   120  			assert.NilError(t, cmd.Execute())
   121  			golden.Assert(t, cli.OutBuffer().String(), tc.golden)
   122  		})
   123  	}
   124  }