2017-06-20 14:00:01 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
|
|
// Import builders to get the builder function as package function
|
|
|
|
. "github.com/docker/cli/cli/internal/test/builders"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/pkg/testutil"
|
|
|
|
"github.com/docker/docker/pkg/testutil/golden"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-07-05 13:32:54 -04:00
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:19:52 -04:00
|
|
|
assert.Equal(t, "", fakeCli.OutBuffer().String())
|
|
|
|
assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().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")
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-06-20 14:00:01 -04:00
|
|
|
expected := golden.Get(t, []byte(actual), "stack-ps-with-quiet-option.golden")
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }}")
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-06-20 14:00:01 -04:00
|
|
|
expected := golden.Get(t, []byte(actual), "stack-ps-with-no-trunc-option.golden")
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }}")
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-06-20 14:00:01 -04:00
|
|
|
expected := golden.Get(t, []byte(actual), "stack-ps-with-no-resolve-option.golden")
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }}")
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-06-20 14:00:01 -04:00
|
|
|
expected := golden.Get(t, []byte(actual), "stack-ps-with-format.golden")
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
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"})
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-06-20 14:00:01 -04:00
|
|
|
expected := golden.Get(t, []byte(actual), "stack-ps-with-config-format.golden")
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
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"})
|
|
|
|
assert.NoError(t, cmd.Execute())
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-06-20 14:00:01 -04:00
|
|
|
expected := golden.Get(t, []byte(actual), "stack-ps-without-format.golden")
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
|
|
|
|
}
|