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>
125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package stack
|
|
|
|
import (
|
|
"io"
|
|
"testing"
|
|
|
|
"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"
|
|
"github.com/pkg/errors"
|
|
"gotest.tools/v3/assert"
|
|
"gotest.tools/v3/golden"
|
|
)
|
|
|
|
func TestListErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
args []string
|
|
flags map[string]string
|
|
serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
|
|
expectedError string
|
|
}{
|
|
{
|
|
args: []string{"foo"},
|
|
expectedError: "accepts no argument",
|
|
},
|
|
{
|
|
flags: map[string]string{
|
|
"format": "{{invalid format}}",
|
|
},
|
|
expectedError: "template parsing error",
|
|
},
|
|
{
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
return []swarm.Service{}, errors.Errorf("error getting services")
|
|
},
|
|
expectedError: "error getting services",
|
|
},
|
|
{
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
return []swarm.Service{*builders.Service()}, nil
|
|
},
|
|
expectedError: "cannot get label",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
cmd := newListCommand(test.NewFakeCli(&fakeClient{
|
|
serviceListFunc: tc.serviceListFunc,
|
|
}))
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOut(io.Discard)
|
|
for key, value := range tc.flags {
|
|
assert.Check(t, cmd.Flags().Set(key, value))
|
|
}
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestStackList(t *testing.T) {
|
|
testCases := []struct {
|
|
doc string
|
|
serviceNames []string
|
|
flags map[string]string
|
|
golden string
|
|
}{
|
|
{
|
|
doc: "WithFormat",
|
|
serviceNames: []string{"service-name-foo"},
|
|
flags: map[string]string{
|
|
"format": "{{ .Name }}",
|
|
},
|
|
golden: "stack-list-with-format.golden",
|
|
},
|
|
{
|
|
doc: "WithoutFormat",
|
|
serviceNames: []string{"service-name-foo"},
|
|
golden: "stack-list-without-format.golden",
|
|
},
|
|
{
|
|
doc: "Sort",
|
|
serviceNames: []string{
|
|
"service-name-foo",
|
|
"service-name-bar",
|
|
},
|
|
golden: "stack-list-sort.golden",
|
|
},
|
|
{
|
|
doc: "SortNatural",
|
|
serviceNames: []string{
|
|
"service-name-1-foo",
|
|
"service-name-10-foo",
|
|
"service-name-2-foo",
|
|
},
|
|
golden: "stack-list-sort-natural.golden",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
var services []swarm.Service
|
|
for _, name := range tc.serviceNames {
|
|
services = append(services,
|
|
*builders.Service(
|
|
builders.ServiceLabels(map[string]string{
|
|
"com.docker.stack.namespace": name,
|
|
}),
|
|
),
|
|
)
|
|
}
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
return services, nil
|
|
},
|
|
})
|
|
cmd := newListCommand(cli)
|
|
for key, value := range tc.flags {
|
|
assert.Check(t, cmd.Flags().Set(key, value))
|
|
}
|
|
assert.NilError(t, cmd.Execute())
|
|
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
|
|
})
|
|
}
|
|
}
|