2017-06-29 17:35:26 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2017-06-29 17:35:26 -04:00
|
|
|
"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"
|
2023-10-23 08:51:01 -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"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-06-29 17:35:26 -04:00
|
|
|
)
|
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
func TestTaskPrintSorted(t *testing.T) {
|
|
|
|
apiClient := &fakeClient{
|
|
|
|
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
|
|
|
if ref == "service-id-one" {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Service(builders.ServiceName("service-name-1")), nil, nil
|
2019-09-22 12:22:23 -04:00
|
|
|
}
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Service(builders.ServiceName("service-name-10")), nil, nil
|
2019-09-22 12:22:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(apiClient)
|
|
|
|
tasks := []swarm.Task{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(
|
|
|
|
builders.TaskID("id-foo"),
|
|
|
|
builders.TaskServiceID("service-id-ten"),
|
|
|
|
builders.TaskNodeID("id-node"),
|
|
|
|
builders.WithTaskSpec(builders.TaskImage("myimage:mytag")),
|
|
|
|
builders.TaskDesiredState(swarm.TaskStateReady),
|
|
|
|
builders.WithStatus(builders.TaskState(swarm.TaskStateFailed), builders.Timestamp(time.Now().Add(-2*time.Hour))),
|
2019-09-22 12:22:23 -04:00
|
|
|
),
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(
|
|
|
|
builders.TaskID("id-bar"),
|
|
|
|
builders.TaskServiceID("service-id-one"),
|
|
|
|
builders.TaskNodeID("id-node"),
|
|
|
|
builders.WithTaskSpec(builders.TaskImage("myimage:mytag")),
|
|
|
|
builders.TaskDesiredState(swarm.TaskStateReady),
|
|
|
|
builders.WithStatus(builders.TaskState(swarm.TaskStateFailed), builders.Timestamp(time.Now().Add(-2*time.Hour))),
|
2019-09-22 12:22:23 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, false), false, false, formatter.TableFormatKey)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "task-print-sorted.golden")
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:35:26 -04:00
|
|
|
func TestTaskPrintWithQuietOption(t *testing.T) {
|
2023-10-23 08:51:01 -04:00
|
|
|
const quiet = true
|
|
|
|
const trunc = false
|
|
|
|
const noResolve = true
|
2017-06-29 17:35:26 -04:00
|
|
|
apiClient := &fakeClient{}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2023-10-23 08:51:01 -04:00
|
|
|
tasks := []swarm.Task{*builders.Task(builders.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-06 14:44:13 -05:00
|
|
|
assert.NilError(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) {
|
2023-10-23 08:51:01 -04:00
|
|
|
const quiet = false
|
|
|
|
const trunc = false
|
|
|
|
const noResolve = true
|
2017-06-29 17:35:26 -04:00
|
|
|
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{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(builders.TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")),
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .ID }}")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(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) {
|
2023-10-23 08:51:01 -04:00
|
|
|
const quiet = false
|
|
|
|
const trunc = false
|
|
|
|
const noResolve = true
|
2017-06-29 17:35:26 -04:00
|
|
|
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{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(builders.TaskServiceID("service-id-foo"), builders.TaskNodeID("node-id-bar"), builders.TaskSlot(0)),
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(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) {
|
2023-10-23 08:51:01 -04:00
|
|
|
const quiet = false
|
|
|
|
const trunc = false
|
|
|
|
const noResolve = true
|
2017-06-29 17:35:26 -04:00
|
|
|
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{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(builders.TaskServiceID("service-id-foo"), builders.TaskSlot(1)),
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(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) {
|
2023-10-23 08:51:01 -04:00
|
|
|
const quiet = false
|
|
|
|
const trunc = false
|
|
|
|
const noResolve = false
|
2017-06-29 17:35:26 -04:00
|
|
|
apiClient := &fakeClient{
|
|
|
|
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Service(builders.ServiceName("service-name-foo")), nil, nil
|
2017-06-29 17:35:26 -04:00
|
|
|
},
|
|
|
|
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Node(builders.NodeName("node-name-bar")), nil, nil
|
2017-06-29 17:35:26 -04:00
|
|
|
},
|
|
|
|
}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []swarm.Task{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(
|
|
|
|
builders.TaskID("id-foo"),
|
|
|
|
builders.TaskServiceID("service-id-foo"),
|
|
|
|
builders.TaskNodeID("id-node"),
|
|
|
|
builders.WithTaskSpec(builders.TaskImage("myimage:mytag")),
|
|
|
|
builders.TaskDesiredState(swarm.TaskStateReady),
|
|
|
|
builders.WithStatus(builders.TaskState(swarm.TaskStateFailed), builders.Timestamp(time.Now().Add(-2*time.Hour))),
|
2017-06-29 17:35:26 -04:00
|
|
|
),
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(
|
|
|
|
builders.TaskID("id-bar"),
|
|
|
|
builders.TaskServiceID("service-id-foo"),
|
|
|
|
builders.TaskNodeID("id-node"),
|
|
|
|
builders.WithTaskSpec(builders.TaskImage("myimage:mytag")),
|
|
|
|
builders.TaskDesiredState(swarm.TaskStateReady),
|
|
|
|
builders.WithStatus(builders.TaskState(swarm.TaskStateFailed), builders.Timestamp(time.Now().Add(-2*time.Hour))),
|
2017-06-29 17:35:26 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(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) {
|
2023-10-23 08:51:01 -04:00
|
|
|
const quiet = false
|
|
|
|
const trunc = false
|
|
|
|
const noResolve = false
|
2017-06-29 17:35:26 -04:00
|
|
|
apiClient := &fakeClient{
|
|
|
|
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Service(builders.ServiceName("service-name-foo")), nil, nil
|
2017-06-29 17:35:26 -04:00
|
|
|
},
|
|
|
|
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Node(builders.NodeName("node-name-bar")), nil, nil
|
2017-06-29 17:35:26 -04:00
|
|
|
},
|
|
|
|
}
|
2017-08-16 13:04:44 -04:00
|
|
|
cli := test.NewFakeCli(apiClient)
|
2017-06-29 17:35:26 -04:00
|
|
|
tasks := []swarm.Task{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Task(builders.TaskServiceID("service-id-foo"), builders.TaskSlot(1)),
|
2017-06-29 17:35:26 -04:00
|
|
|
}
|
|
|
|
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }} {{ .Node }}")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(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
|
|
|
}
|