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