docker-cli/cli/command/task/print_test.go
Sebastiaan van Stijn 594aeb390a
cli/command: remove dot-imports and unhandled errors
Please the linters in preparation of updating golangci-lint;

- remove dot-imports
- add some checks for unhandled errors
- replace some fixed-value variables for consts

    cli/command/image/build/context.go:238:17: G107: Potential HTTP request made with variable url (gosec)
        if resp, err = http.Get(url); err != nil {
                       ^
    cli/command/idresolver/idresolver_test.go:7:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/registry_test.go:7:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/cli/command" // Prevents a circular import with "github.com/docker/cli/internal/test"
        ^
    cli/command/task/print_test.go:11:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/swarm/update_test.go:10:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/swarm/unlock_key_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/swarm/join_token_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/node/list_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/node/promote_test.go:8:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/node/demote_test.go:8:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package functions
        ^
    cli/command/node/ps_test.go:11:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/node/update_test.go:8:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/node/inspect_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package functions
        ^
    cli/command/secret/ls_test.go:11:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/secret/inspect_test.go:11:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/volume/inspect_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/volume/list_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/config/inspect_test.go:11:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/config/ls_test.go:11:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/network/list_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders"
        ^
    cli/command/container/list_test.go:10:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/service/list_test.go:12:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders"
        ^
    cli/command/service/client_test.go:6:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/stack/list_test.go:8:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/stack/services_test.go:9:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^
    cli/command/stack/ps_test.go:10:2: dot-imports: should not use dot imports (revive)
        . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
        ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-10-23 15:21:47 +02:00

163 lines
6.1 KiB
Go

package task
import (
"context"
"testing"
"time"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
func TestTaskPrintSorted(t *testing.T) {
apiClient := &fakeClient{
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
if ref == "service-id-one" {
return *builders.Service(builders.ServiceName("service-name-1")), nil, nil
}
return *builders.Service(builders.ServiceName("service-name-10")), nil, nil
},
}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{
*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))),
),
*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))),
),
}
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")
}
func TestTaskPrintWithQuietOption(t *testing.T) {
const quiet = true
const trunc = false
const noResolve = true
apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{*builders.Task(builders.TaskID("id-foo"))}
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-quiet-option.golden")
}
func TestTaskPrintWithNoTruncOption(t *testing.T) {
const quiet = false
const trunc = false
const noResolve = true
apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{
*builders.Task(builders.TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")),
}
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .ID }}")
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-no-trunc-option.golden")
}
func TestTaskPrintWithGlobalService(t *testing.T) {
const quiet = false
const trunc = false
const noResolve = true
apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{
*builders.Task(builders.TaskServiceID("service-id-foo"), builders.TaskNodeID("node-id-bar"), builders.TaskSlot(0)),
}
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-global-service.golden")
}
func TestTaskPrintWithReplicatedService(t *testing.T) {
const quiet = false
const trunc = false
const noResolve = true
apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{
*builders.Task(builders.TaskServiceID("service-id-foo"), builders.TaskSlot(1)),
}
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }}")
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-replicated-service.golden")
}
func TestTaskPrintWithIndentation(t *testing.T) {
const quiet = false
const trunc = false
const noResolve = false
apiClient := &fakeClient{
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
return *builders.Service(builders.ServiceName("service-name-foo")), nil, nil
},
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
return *builders.Node(builders.NodeName("node-name-bar")), nil, nil
},
}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{
*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))),
),
*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))),
),
}
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, formatter.TableFormatKey)
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-indentation.golden")
}
func TestTaskPrintWithResolution(t *testing.T) {
const quiet = false
const trunc = false
const noResolve = false
apiClient := &fakeClient{
serviceInspectWithRaw: func(ref string, options types.ServiceInspectOptions) (swarm.Service, []byte, error) {
return *builders.Service(builders.ServiceName("service-name-foo")), nil, nil
},
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
return *builders.Node(builders.NodeName("node-name-bar")), nil, nil
},
}
cli := test.NewFakeCli(apiClient)
tasks := []swarm.Task{
*builders.Task(builders.TaskServiceID("service-id-foo"), builders.TaskSlot(1)),
}
err := Print(context.Background(), cli, tasks, idresolver.New(apiClient, noResolve), trunc, quiet, "{{ .Name }} {{ .Node }}")
assert.NilError(t, err)
golden.Assert(t, cli.OutBuffer().String(), "task-print-with-resolution.golden")
}