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>
2023-03-30 10:22:06 -04:00
|
|
|
"context"
|
2022-02-25 08:30:27 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-04-01 03:07:22 -04:00
|
|
|
"path/filepath"
|
2017-05-08 13:36:04 -04:00
|
|
|
"reflect"
|
2017-04-01 03:07:22 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
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"
|
2017-04-01 03:07:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const secretDataFile = "secret-create-with-name.golden"
|
|
|
|
|
|
|
|
func TestSecretCreateErrors(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>
2023-03-30 10:22:06 -04:00
|
|
|
secretCreateFunc func(context.Context, swarm.SecretSpec) (types.SecretCreateResponse, error)
|
2017-04-01 03:07:22 -04:00
|
|
|
expectedError string
|
|
|
|
}{
|
2022-09-29 11:21:51 -04:00
|
|
|
{
|
|
|
|
args: []string{"too", "many", "arguments"},
|
2017-07-30 07:01:39 -04:00
|
|
|
expectedError: "requires at least 1 and at most 2 arguments",
|
|
|
|
},
|
2022-09-29 11:21:51 -04:00
|
|
|
{
|
|
|
|
args: []string{"create", "--driver", "driver", "-"},
|
2017-07-30 07:01:39 -04:00
|
|
|
expectedError: "secret data must be empty",
|
2017-04-01 03:07:22 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"name", filepath.Join("testdata", secretDataFile)},
|
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>
2023-03-30 10:22:06 -04:00
|
|
|
secretCreateFunc: func(_ context.Context, secretSpec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("error creating secret")
|
|
|
|
},
|
|
|
|
expectedError: "error creating secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newSecretCreateCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-04-01 03:07:22 -04:00
|
|
|
secretCreateFunc: tc.secretCreateFunc,
|
2017-07-05 14:43:39 -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 TestSecretCreateWithName(t *testing.T) {
|
|
|
|
name := "foo"
|
2022-02-25 08:30:27 -05:00
|
|
|
data, err := os.ReadFile(filepath.Join("testdata", secretDataFile))
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-02-21 13:45:34 -05:00
|
|
|
|
|
|
|
expected := swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: name,
|
|
|
|
Labels: make(map[string]string),
|
|
|
|
},
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
|
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>
2023-03-30 10:22:06 -04:00
|
|
|
secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
2018-02-21 13:45:34 -05:00
|
|
|
if !reflect.DeepEqual(spec, expected) {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected %+v, got %+v", expected, spec)
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
|
2017-07-30 07:01:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretCreateWithDriver(t *testing.T) {
|
|
|
|
expectedDriver := &swarm.Driver{
|
|
|
|
Name: "secret-driver",
|
|
|
|
}
|
|
|
|
name := "foo"
|
|
|
|
|
|
|
|
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>
2023-03-30 10:22:06 -04:00
|
|
|
secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
2017-07-30 07:01:39 -04:00
|
|
|
if spec.Name != name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
|
|
|
|
}
|
|
|
|
|
2018-02-21 13:45:34 -05:00
|
|
|
if spec.Driver.Name != expectedDriver.Name {
|
2017-07-30 07:01:39 -04:00
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name})
|
|
|
|
cmd.Flags().Set("driver", expectedDriver.Name)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
|
2018-02-21 13:45:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretCreateWithTemplatingDriver(t *testing.T) {
|
|
|
|
expectedDriver := &swarm.Driver{
|
|
|
|
Name: "template-driver",
|
|
|
|
}
|
|
|
|
name := "foo"
|
|
|
|
|
|
|
|
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>
2023-03-30 10:22:06 -04:00
|
|
|
secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
2018-02-21 13:45:34 -05:00
|
|
|
if spec.Name != name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if spec.Templating.Name != expectedDriver.Name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected driver %v, got %v", expectedDriver, spec.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name})
|
|
|
|
cmd.Flags().Set("template-driver", expectedDriver.Name)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretCreateWithLabels(t *testing.T) {
|
|
|
|
expectedLabels := map[string]string{
|
|
|
|
"lbl1": "Label-foo",
|
|
|
|
"lbl2": "Label-bar",
|
|
|
|
}
|
|
|
|
name := "foo"
|
|
|
|
|
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>
2023-03-30 10:22:06 -04:00
|
|
|
secretCreateFunc: func(_ context.Context, spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
|
2017-04-01 03:07:22 -04:00
|
|
|
if spec.Name != name {
|
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:36:04 -04:00
|
|
|
if !reflect.DeepEqual(spec.Labels, expectedLabels) {
|
2017-04-01 03:07:22 -04:00
|
|
|
return types.SecretCreateResponse{}, errors.Errorf("expected labels %v, got %v", expectedLabels, spec.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.SecretCreateResponse{
|
|
|
|
ID: "ID-" + spec.Name,
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 12:53:41 -04:00
|
|
|
})
|
2017-04-01 03:07:22 -04:00
|
|
|
|
|
|
|
cmd := newSecretCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
|
|
|
|
cmd.Flags().Set("label", "lbl1=Label-foo")
|
|
|
|
cmd.Flags().Set("label", "lbl2=Label-bar")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("ID-"+name, strings.TrimSpace(cli.OutBuffer().String())))
|
2017-04-01 03:07:22 -04:00
|
|
|
}
|