refactor stack ps tests to table-driven

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
Arash Deshmeh 2018-08-10 14:55:51 -04:00
parent 3a3e720f91
commit 340e4ee8e5
1 changed files with 129 additions and 116 deletions

View File

@ -51,58 +51,55 @@ func TestStackPsErrors(t *testing.T) {
} }
} }
func TestRunPSWithEmptyName(t *testing.T) { func TestStackPs(t *testing.T) {
cmd := newPsCommand(test.NewFakeCli(&fakeClient{}), &orchestrator) testCases := []struct {
cmd.SetArgs([]string{"' '"}) doc string
cmd.SetOutput(ioutil.Discard) taskListFunc func(types.TaskListOptions) ([]swarm.Task, error)
nodeInspectWithRaw func(string) (swarm.Node, []byte, error)
assert.ErrorContains(t, cmd.Execute(), `invalid stack name: "' '"`) config configfile.ConfigFile
} args []string
flags map[string]string
func TestStackPsEmptyStack(t *testing.T) { expectedErr string
fakeCli := test.NewFakeCli(&fakeClient{ golden string
}{
{
doc: "WithEmptyName",
args: []string{"' '"},
expectedErr: `invalid stack name: "' '"`,
},
{
doc: "WithEmptyStack",
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, nil return []swarm.Task{}, nil
}, },
}) args: []string{"foo"},
cmd := newPsCommand(fakeCli, &orchestrator) expectedErr: "nothing found in stack: foo",
cmd.SetArgs([]string{"foo"}) },
cmd.SetOutput(ioutil.Discard) {
doc: "WithQuietOption",
assert.Error(t, cmd.Execute(), "nothing found in stack: foo")
assert.Check(t, is.Equal("", fakeCli.OutBuffer().String()))
}
func TestStackPsWithQuietOption(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskID("id-foo"))}, nil return []swarm.Task{*Task(TaskID("id-foo"))}, nil
}, },
}) args: []string{"foo"},
cmd := newPsCommand(cli, &orchestrator) flags: map[string]string{
cmd.SetArgs([]string{"foo"}) "quiet": "true",
cmd.Flags().Set("quiet", "true") },
assert.NilError(t, cmd.Execute()) golden: "stack-ps-with-quiet-option.golden",
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-quiet-option.golden") },
{
} doc: "WithNoTruncOption",
func TestStackPsWithNoTruncOption(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil
}, },
}) args: []string{"foo"},
cmd := newPsCommand(cli, &orchestrator) flags: map[string]string{
cmd.SetArgs([]string{"foo"}) "no-trunc": "true",
cmd.Flags().Set("no-trunc", "true") "format": "{{ .ID }}",
cmd.Flags().Set("format", "{{ .ID }}") },
assert.NilError(t, cmd.Execute()) golden: "stack-ps-with-no-trunc-option.golden",
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-no-trunc-option.golden") },
} {
doc: "WithNoResolveOption",
func TestStackPsWithNoResolveOption(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task( return []swarm.Task{*Task(
TaskNodeID("id-node-foo"), TaskNodeID("id-node-foo"),
@ -111,45 +108,37 @@ func TestStackPsWithNoResolveOption(t *testing.T) {
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
return *Node(NodeName("node-name-bar")), nil, nil return *Node(NodeName("node-name-bar")), nil, nil
}, },
}) args: []string{"foo"},
cmd := newPsCommand(cli, &orchestrator) flags: map[string]string{
cmd.SetArgs([]string{"foo"}) "no-resolve": "true",
cmd.Flags().Set("no-resolve", "true") "format": "{{ .Node }}",
cmd.Flags().Set("format", "{{ .Node }}") },
assert.NilError(t, cmd.Execute()) golden: "stack-ps-with-no-resolve-option.golden",
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-no-resolve-option.golden") },
} {
doc: "WithFormat",
func TestStackPsWithFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil
}, },
}) args: []string{"foo"},
cmd := newPsCommand(cli, &orchestrator) flags: map[string]string{
cmd.SetArgs([]string{"foo"}) "format": "{{ .Name }}",
cmd.Flags().Set("format", "{{ .Name }}") },
assert.NilError(t, cmd.Execute()) golden: "stack-ps-with-format.golden",
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-format.golden") },
} {
doc: "WithConfigFormat",
func TestStackPsWithConfigFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil
}, },
}) config: configfile.ConfigFile{
cli.SetConfigFile(&configfile.ConfigFile{
TasksFormat: "{{ .Name }}", TasksFormat: "{{ .Name }}",
}) },
cmd := newPsCommand(cli, &orchestrator) args: []string{"foo"},
cmd.SetArgs([]string{"foo"}) golden: "stack-ps-with-config-format.golden",
assert.NilError(t, cmd.Execute()) },
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-config-format.golden") {
} doc: "WithoutFormat",
func TestStackPsWithoutFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task( return []swarm.Task{*Task(
TaskID("id-foo"), TaskID("id-foo"),
@ -163,9 +152,33 @@ func TestStackPsWithoutFormat(t *testing.T) {
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
return *Node(NodeName("node-name-bar")), nil, nil return *Node(NodeName("node-name-bar")), nil, nil
}, },
args: []string{"foo"},
golden: "stack-ps-without-format.golden",
},
}
for _, tc := range testCases {
t.Run(tc.doc, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
taskListFunc: tc.taskListFunc,
nodeInspectWithRaw: tc.nodeInspectWithRaw,
}) })
cli.SetConfigFile(&tc.config)
cmd := newPsCommand(cli, &orchestrator) cmd := newPsCommand(cli, &orchestrator)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
cmd.SetOutput(ioutil.Discard)
if tc.expectedErr != "" {
assert.Error(t, cmd.Execute(), tc.expectedErr)
assert.Check(t, is.Equal("", cli.OutBuffer().String()))
return
}
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-without-format.golden") golden.Assert(t, cli.OutBuffer().String(), tc.golden)
})
}
} }