github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/stack/list_test.go (about)

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