Merge pull request #1287 from adshmh/refactor-stack-list-unit-tests

refactor stack list command unit tests to table-driven
This commit is contained in:
Silvin Lubecki 2018-09-20 14:26:01 +02:00 committed by GitHub
commit b23272f34d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) { func TestStackList(t *testing.T) {
cli := test.NewFakeCli( testCases := []struct {
&fakeClient{ doc string
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceNames []string
return []swarm.Service{ flags map[string]string
*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 golden string
swarmServices []swarm.Service
}{ }{
{ {
golden: "stack-list-sort.golden", doc: "WithFormat",
swarmServices: []swarm.Service{ serviceNames: []string{"service-name-foo"},
*Service( flags: map[string]string{
ServiceLabels(map[string]string{ "format": "{{ .Name }}",
"com.docker.stack.namespace": "service-name-foo",
}),
),
*Service(
ServiceLabels(map[string]string{
"com.docker.stack.namespace": "service-name-bar",
}),
),
}, },
golden: "stack-list-with-format.golden",
}, },
{ {
golden: "stack-list-sort-natural.golden", doc: "WithoutFormat",
swarmServices: []swarm.Service{ serviceNames: []string{"service-name-foo"},
*Service( golden: "stack-list-without-format.golden",
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: "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{ cli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return uc.swarmServices, nil return services, nil
}, },
}) })
cmd := newListCommand(cli, &orchestrator) cmd := newListCommand(cli, &orchestrator)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), uc.golden) golden.Assert(t, cli.OutBuffer().String(), tc.golden)
})
} }
} }