2017-06-20 14:00:01 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-06-20 14:00:01 -04:00
|
|
|
// Import builders to get the builder function as package function
|
2017-08-21 16:30:09 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders"
|
2017-06-20 14:00:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/pkg/errors"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/golden"
|
2017-06-20 14:00:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStackPsErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
args: []string{},
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo", "bar"},
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return nil, errors.Errorf("error getting tasks")
|
|
|
|
},
|
|
|
|
expectedError: "error getting tasks",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := newPsCommand(test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: tc.taskListFunc,
|
2018-06-20 08:48:50 -04:00
|
|
|
}), &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 08:07:26 -04:00
|
|
|
func TestRunPSWithEmptyName(t *testing.T) {
|
|
|
|
cmd := newPsCommand(test.NewFakeCli(&fakeClient{}), &orchestrator)
|
|
|
|
cmd.SetArgs([]string{"' '"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
|
|
|
|
assert.ErrorContains(t, cmd.Execute(), `invalid stack name: "' '"`)
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
func TestStackPsEmptyStack(t *testing.T) {
|
2017-07-05 13:32:54 -04:00
|
|
|
fakeCli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{}, nil
|
|
|
|
},
|
2017-07-05 14:19:52 -04:00
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(fakeCli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2017-09-26 12:33:35 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2017-07-05 13:32:54 -04:00
|
|
|
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, cmd.Execute(), "nothing found in stack: foo")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("", fakeCli.OutBuffer().String()))
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPsWithQuietOption(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{*Task(TaskID("id-foo"))}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(cli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-quiet-option.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPsWithNoTruncOption(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(cli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("no-trunc", "true")
|
|
|
|
cmd.Flags().Set("format", "{{ .ID }}")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-no-trunc-option.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPsWithNoResolveOption(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{*Task(
|
|
|
|
TaskNodeID("id-node-foo"),
|
|
|
|
)}, nil
|
|
|
|
},
|
|
|
|
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
|
|
|
|
return *Node(NodeName("node-name-bar")), nil, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(cli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("no-resolve", "true")
|
|
|
|
cmd.Flags().Set("format", "{{ .Node }}")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-no-resolve-option.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPsWithFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(cli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("format", "{{ .Name }}")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-format.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPsWithConfigFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2017-06-20 14:00:01 -04:00
|
|
|
TasksFormat: "{{ .Name }}",
|
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(cli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-with-config-format.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStackPsWithoutFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-06-20 14:00:01 -04:00
|
|
|
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
return []swarm.Task{*Task(
|
|
|
|
TaskID("id-foo"),
|
|
|
|
TaskServiceID("service-id-foo"),
|
|
|
|
TaskNodeID("id-node"),
|
|
|
|
WithTaskSpec(TaskImage("myimage:mytag")),
|
|
|
|
TaskDesiredState(swarm.TaskStateReady),
|
|
|
|
WithStatus(TaskState(swarm.TaskStateFailed), Timestamp(time.Now().Add(-2*time.Hour))),
|
|
|
|
)}, nil
|
|
|
|
},
|
|
|
|
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
|
|
|
|
return *Node(NodeName("node-name-bar")), nil, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newPsCommand(cli, &orchestrator)
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "stack-ps-without-format.golden")
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|