diff --git a/cli/command/stack/list_test.go b/cli/command/stack/list_test.go index e646fdbea2..5fdc04bed3 100644 --- a/cli/command/stack/list_test.go +++ b/cli/command/stack/list_test.go @@ -63,90 +63,68 @@ func TestListErrors(t *testing.T) { } } -func TestListWithFormat(t *testing.T) { - cli := test.NewFakeCli( - &fakeClient{ - serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { - return []swarm.Service{ - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-foo", - }), - )}, nil - }, - }) - cmd := newListCommand(cli, &orchestrator) - cmd.Flags().Set("format", "{{ .Name }}") - assert.NilError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), "stack-list-with-format.golden") -} - -func TestListWithoutFormat(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { - return []swarm.Service{ - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-foo", - }), - )}, nil - }, - }) - cmd := newListCommand(cli, &orchestrator) - assert.NilError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), "stack-list-without-format.golden") -} - -func TestListOrder(t *testing.T) { - usecases := []struct { - golden string - swarmServices []swarm.Service +func TestStackList(t *testing.T) { + testCases := []struct { + doc string + serviceNames []string + flags map[string]string + golden string }{ { - golden: "stack-list-sort.golden", - swarmServices: []swarm.Service{ - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-foo", - }), - ), - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-bar", - }), - ), + doc: "WithFormat", + serviceNames: []string{"service-name-foo"}, + flags: map[string]string{ + "format": "{{ .Name }}", }, + golden: "stack-list-with-format.golden", }, { - golden: "stack-list-sort-natural.golden", - swarmServices: []swarm.Service{ - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-1-foo", - }), - ), - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-10-foo", - }), - ), - *Service( - ServiceLabels(map[string]string{ - "com.docker.stack.namespace": "service-name-2-foo", - }), - ), + doc: "WithoutFormat", + serviceNames: []string{"service-name-foo"}, + golden: "stack-list-without-format.golden", + }, + { + doc: "Sort", + serviceNames: []string{ + "service-name-foo", + "service-name-bar", }, + golden: "stack-list-sort.golden", + }, + { + doc: "SortNatural", + serviceNames: []string{ + "service-name-1-foo", + "service-name-10-foo", + "service-name-2-foo", + }, + golden: "stack-list-sort-natural.golden", }, } - for _, uc := range usecases { - cli := test.NewFakeCli(&fakeClient{ - serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { - return uc.swarmServices, nil - }, + for _, tc := range testCases { + t.Run(tc.doc, func(t *testing.T) { + var services []swarm.Service + for _, name := range tc.serviceNames { + services = append(services, + *Service( + ServiceLabels(map[string]string{ + "com.docker.stack.namespace": name, + }), + ), + ) + } + cli := test.NewFakeCli(&fakeClient{ + serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { + return services, nil + }, + }) + cmd := newListCommand(cli, &orchestrator) + for key, value := range tc.flags { + cmd.Flags().Set(key, value) + } + assert.NilError(t, cmd.Execute()) + golden.Assert(t, cli.OutBuffer().String(), tc.golden) }) - cmd := newListCommand(cli, &orchestrator) - assert.NilError(t, cmd.Execute()) - golden.Assert(t, cli.OutBuffer().String(), uc.golden) } }