2017-04-01 03:07:22 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
"context"
|
2022-02-25 08:30:27 -05:00
|
|
|
"io"
|
2017-04-01 03:07:22 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2019-04-02 11:20:21 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
|
2017-04-01 03:07:22 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc func(context.Context, types.SecretListOptions) ([]swarm.Secret, error)
|
2017-04-01 03:07:22 -04:00
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no argument",
|
|
|
|
},
|
|
|
|
{
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{}, errors.Errorf("error listing secrets")
|
|
|
|
},
|
|
|
|
expectedError: "error listing secrets",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newSecretListCommand(
|
2017-08-16 12:53:41 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretListFunc: tc.secretListFunc,
|
2017-08-16 12:53:41 -04:00
|
|
|
}),
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
2022-02-25 08:30:27 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretList(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{
|
2017-07-07 04:22:52 -04:00
|
|
|
*Secret(SecretID("ID-1-foo"),
|
|
|
|
SecretName("1-foo"),
|
2017-04-01 03:07:22 -04:00
|
|
|
SecretVersion(swarm.Version{Index: 10}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
2017-07-07 04:22:52 -04:00
|
|
|
*Secret(SecretID("ID-10-foo"),
|
|
|
|
SecretName("10-foo"),
|
|
|
|
SecretVersion(swarm.Version{Index: 11}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
SecretDriver("driver"),
|
|
|
|
),
|
|
|
|
*Secret(SecretID("ID-2-foo"),
|
|
|
|
SecretName("2-foo"),
|
2017-04-01 03:07:22 -04:00
|
|
|
SecretVersion(swarm.Version{Index: 11}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-07-30 07:01:39 -04:00
|
|
|
SecretDriver("driver"),
|
2017-04-01 03:07:22 -04:00
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-07-07 04:22:52 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-sort.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithQuietOption(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"), SecretName("foo")),
|
|
|
|
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-quiet-option.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithConfigFormat(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"), SecretName("foo")),
|
|
|
|
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2017-04-01 03:07:22 -04:00
|
|
|
SecretFormat: "{{ .Name }} {{ .Labels }}",
|
|
|
|
})
|
|
|
|
cmd := newSecretListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-config-format.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithFormat(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"), SecretName("foo")),
|
|
|
|
*Secret(SecretID("ID-bar"), SecretName("bar"), SecretLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
|
|
|
cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-format.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretListWithFilter(t *testing.T) {
|
2017-08-16 12:53:41 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/secret: fakeClient: include context in fake client (revive)
I could either remove the name for these contexts, or make the fake functions
more accurately reflect the actual implementation (decided to go for the latter
one)
cli/command/secret/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretCreate(ctx context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
^
cli/command/secret/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretInspectWithRaw(ctx context.Context, id string) (swarm.Secret, []byte, error) {
^
cli/command/secret/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
^
cli/command/secret/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) SecretRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9dd012aa5df1c7b076f53d6cd2c328de0ea3b0f4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:22:06 -04:00
|
|
|
secretListFunc: func(_ context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
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]))
|
2017-04-01 03:07:22 -04:00
|
|
|
return []swarm.Secret{
|
|
|
|
*Secret(SecretID("ID-foo"),
|
|
|
|
SecretName("foo"),
|
|
|
|
SecretVersion(swarm.Version{Index: 10}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
*Secret(SecretID("ID-bar"),
|
|
|
|
SecretName("bar"),
|
|
|
|
SecretVersion(swarm.Version{Index: 11}),
|
|
|
|
SecretCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
SecretUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
cmd := newSecretListCommand(cli)
|
|
|
|
cmd.Flags().Set("filter", "name=foo")
|
|
|
|
cmd.Flags().Set("filter", "label=lbl1=Label-bar")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 12:53:41 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-filter.golden")
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|