2017-06-29 17:35:26 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-06-29 17:35:26 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
// 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-29 17:35:26 -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"
|
2017-08-16 13:04:44 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
2017-06-29 17:35:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTaskPrintWithQuietOption(t *testing.T) {
|
|
|
|
quiet := true
|
|
|
|
trunc := false
|
|
|
|
noResolve := true
|
|
|
|
apiClient := &fakeClient{}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
|
|
|
tasks := []swarm.Task{*Task(TaskID("id-foo"))}
|
2017-06-29 17:35:26 -04:00
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err)
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-quiet-option.golden")
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskPrintWithNoTruncOption(t *testing.T) {
|
|
|
|
quiet := false
|
|
|
|
trunc := false
|
|
|
|
noResolve := true
|
|
|
|
apiClient := &fakeClient{}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []swarm.Task{
|
|
|
|
*Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")),
|
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .ID }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err)
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-no-trunc-option.golden")
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskPrintWithGlobalService(t *testing.T) {
|
|
|
|
quiet := false
|
|
|
|
trunc := false
|
|
|
|
noResolve := true
|
|
|
|
apiClient := &fakeClient{}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []swarm.Task{
|
|
|
|
*Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)),
|
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err)
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-global-service.golden")
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskPrintWithReplicatedService(t *testing.T) {
|
|
|
|
quiet := false
|
|
|
|
trunc := false
|
|
|
|
noResolve := true
|
|
|
|
apiClient := &fakeClient{}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []swarm.Task{
|
|
|
|
*Task(TaskServiceID("service-id-foo"), TaskSlot(1)),
|
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err)
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-replicated-service.golden")
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskPrintWithIndentation(t *testing.T) {
|
|
|
|
quiet := false
|
|
|
|
trunc := false
|
|
|
|
noResolve := false
|
|
|
|
apiClient := &fakeClient{
|
|
|
|
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
|
|
|
return *Service(ServiceName("service-name-foo")), nil, nil
|
|
|
|
},
|
|
|
|
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
|
|
|
|
return *Node(NodeName("node-name-bar")), nil, nil
|
|
|
|
},
|
|
|
|
}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []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))),
|
|
|
|
),
|
|
|
|
*Task(
|
|
|
|
TaskID("id-bar"),
|
|
|
|
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))),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err)
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-indentation.golden")
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskPrintWithResolution(t *testing.T) {
|
|
|
|
quiet := false
|
|
|
|
trunc := false
|
|
|
|
noResolve := false
|
|
|
|
apiClient := &fakeClient{
|
|
|
|
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
|
|
|
return *Service(ServiceName("service-name-foo")), nil, nil
|
|
|
|
},
|
|
|
|
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
|
|
|
|
return *Node(NodeName("node-name-bar")), nil, nil
|
|
|
|
},
|
|
|
|
}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []swarm.Task{
|
|
|
|
*Task(TaskServiceID("service-id-foo"), TaskSlot(1)),
|
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }} {{ .Node }}")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, err)
|
2017-08-16 13:04:44 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-resolution.golden")
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|