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

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( golden string
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
}{ }{
{ {
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", {
}), doc: "Sort",
), serviceNames: []string{
*Service( "service-name-foo",
ServiceLabels(map[string]string{ "service-name-bar",
"com.docker.stack.namespace": "service-name-10-foo",
}),
),
*Service(
ServiceLabels(map[string]string{
"com.docker.stack.namespace": "service-name-2-foo",
}),
),
}, },
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 {
cli := test.NewFakeCli(&fakeClient{ t.Run(tc.doc, func(t *testing.T) {
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { var services []swarm.Service
return uc.swarmServices, nil 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)
} }
} }