refactor stack list command unit tests to table-driven

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
Arash Deshmeh 2018-08-13 15:40:26 -04:00
parent 3993346fc6
commit e9dc2293b1
1 changed files with 53 additions and 75 deletions

View File

@ -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 {
func TestStackList(t *testing.T) {
testCases := []struct {
doc string
serviceNames []string
flags map[string]string
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: "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 {
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 uc.swarmServices, nil
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(), uc.golden)
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
})
}
}