Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 52 additions & 74 deletions cli/command/stack/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
func TestStackList(t *testing.T) {
testCases := []struct {
doc string
serviceNames []string
flags map[string]string
golden string
}{
{
doc: "WithFormat",
serviceNames: []string{"service-name-foo"},
flags: map[string]string{
"format": "{{ .Name }}",
},
})
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
golden: "stack-list-with-format.golden",
},
})
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
}{
{
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: "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",
},
{
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: "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)
}
}