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-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
2017-06-20 14:00:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-16 13:04:44 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
2017-06-20 14:00:01 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2017-07-05 14:43:39 -04:00
|
|
|
}))
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
})
|
2017-07-05 13:32:54 -04:00
|
|
|
cmd := newPsCommand(fakeCli)
|
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-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.ErrorContains(cmd.Execute(), ""))
|
|
|
|
assert.Check(t, is.Error(cmd.Execute(), "nothing found in stack: foo"))
|
|
|
|
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
|
|
|
})
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd := newPsCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(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
|
|
|
})
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd := newPsCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("no-trunc", "true")
|
|
|
|
cmd.Flags().Set("format", "{{ .ID }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(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
|
|
|
})
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd := newPsCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("no-resolve", "true")
|
|
|
|
cmd.Flags().Set("format", "{{ .Node }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(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
|
|
|
})
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd := newPsCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"foo"})
|
|
|
|
cmd.Flags().Set("format", "{{ .Name }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(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 }}",
|
|
|
|
})
|
|
|
|
cmd := newPsCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(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
|
|
|
})
|
2017-06-20 14:00:01 -04:00
|
|
|
cmd := newPsCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"foo"})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(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
|
|
|
}
|