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>
161 lines
5.6 KiB
Go
161 lines
5.6 KiB
Go
package secret
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
"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"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
"gotest.tools/v3/golden"
|
|
)
|
|
|
|
func TestSecretListErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
args []string
|
|
secretListFunc func(context.Context, types.SecretListOptions) ([]swarm.Secret, error)
|
|
expectedError string
|
|
}{
|
|
{
|
|
args: []string{"foo"},
|
|
expectedError: "accepts no argument",
|
|
},
|
|
{
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
return []swarm.Secret{}, errors.Errorf("error listing secrets")
|
|
},
|
|
expectedError: "error listing secrets",
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
cmd := newSecretListCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
secretListFunc: tc.secretListFunc,
|
|
}),
|
|
)
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOut(io.Discard)
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestSecretList(t *testing.T) {
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
return []swarm.Secret{
|
|
*builders.Secret(builders.SecretID("ID-1-foo"),
|
|
builders.SecretName("1-foo"),
|
|
builders.SecretVersion(swarm.Version{Index: 10}),
|
|
builders.SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
builders.SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
),
|
|
*builders.Secret(builders.SecretID("ID-10-foo"),
|
|
builders.SecretName("10-foo"),
|
|
builders.SecretVersion(swarm.Version{Index: 11}),
|
|
builders.SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
builders.SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
builders.SecretDriver("driver"),
|
|
),
|
|
*builders.Secret(builders.SecretID("ID-2-foo"),
|
|
builders.SecretName("2-foo"),
|
|
builders.SecretVersion(swarm.Version{Index: 11}),
|
|
builders.SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
builders.SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
builders.SecretDriver("driver"),
|
|
),
|
|
}, nil
|
|
},
|
|
})
|
|
cmd := newSecretListCommand(cli)
|
|
assert.NilError(t, cmd.Execute())
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-sort.golden")
|
|
}
|
|
|
|
func TestSecretListWithQuietOption(t *testing.T) {
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
return []swarm.Secret{
|
|
*builders.Secret(builders.SecretID("ID-foo"), builders.SecretName("foo")),
|
|
*builders.Secret(builders.SecretID("ID-bar"), builders.SecretName("bar"), builders.SecretLabels(map[string]string{
|
|
"label": "label-bar",
|
|
})),
|
|
}, nil
|
|
},
|
|
})
|
|
cmd := newSecretListCommand(cli)
|
|
assert.Check(t, cmd.Flags().Set("quiet", "true"))
|
|
assert.NilError(t, cmd.Execute())
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-quiet-option.golden")
|
|
}
|
|
|
|
func TestSecretListWithConfigFormat(t *testing.T) {
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
return []swarm.Secret{
|
|
*builders.Secret(builders.SecretID("ID-foo"), builders.SecretName("foo")),
|
|
*builders.Secret(builders.SecretID("ID-bar"), builders.SecretName("bar"), builders.SecretLabels(map[string]string{
|
|
"label": "label-bar",
|
|
})),
|
|
}, nil
|
|
},
|
|
})
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
|
SecretFormat: "{{ .Name }} {{ .Labels }}",
|
|
})
|
|
cmd := newSecretListCommand(cli)
|
|
assert.NilError(t, cmd.Execute())
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-config-format.golden")
|
|
}
|
|
|
|
func TestSecretListWithFormat(t *testing.T) {
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
return []swarm.Secret{
|
|
*builders.Secret(builders.SecretID("ID-foo"), builders.SecretName("foo")),
|
|
*builders.Secret(builders.SecretID("ID-bar"), builders.SecretName("bar"), builders.SecretLabels(map[string]string{
|
|
"label": "label-bar",
|
|
})),
|
|
}, nil
|
|
},
|
|
})
|
|
cmd := newSecretListCommand(cli)
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}"))
|
|
assert.NilError(t, cmd.Execute())
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-format.golden")
|
|
}
|
|
|
|
func TestSecretListWithFilter(t *testing.T) {
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
|
assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]), "foo")
|
|
assert.Check(t, is.Equal("lbl1=Label-bar", options.Filters.Get("label")[0]))
|
|
return []swarm.Secret{
|
|
*builders.Secret(builders.SecretID("ID-foo"),
|
|
builders.SecretName("foo"),
|
|
builders.SecretVersion(swarm.Version{Index: 10}),
|
|
builders.SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
builders.SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
),
|
|
*builders.Secret(builders.SecretID("ID-bar"),
|
|
builders.SecretName("bar"),
|
|
builders.SecretVersion(swarm.Version{Index: 11}),
|
|
builders.SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
builders.SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
),
|
|
}, nil
|
|
},
|
|
})
|
|
cmd := newSecretListCommand(cli)
|
|
assert.Check(t, cmd.Flags().Set("filter", "name=foo"))
|
|
assert.Check(t, cmd.Flags().Set("filter", "label=lbl1=Label-bar"))
|
|
assert.NilError(t, cmd.Execute())
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-filter.golden")
|
|
}
|