2017-05-08 13:36:04 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
"context"
|
2022-02-25 07:04:43 -05:00
|
|
|
"io"
|
2017-05-08 13:36:04 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2023-10-23 08:51:01 -04:00
|
|
|
"github.com/docker/cli/internal/test/builders"
|
2017-05-08 13:36:04 -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-05-08 13:36:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc func(context.Context, types.ConfigListOptions) ([]swarm.Config, error)
|
2017-05-08 13:36:04 -04:00
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no argument",
|
|
|
|
},
|
|
|
|
{
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
2017-05-08 13:36:04 -04:00
|
|
|
return []swarm.Config{}, errors.Errorf("error listing configs")
|
|
|
|
},
|
|
|
|
expectedError: "error listing configs",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newConfigListCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-05-08 13:36:04 -04:00
|
|
|
configListFunc: tc.configListFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}),
|
2017-05-08 13:36:04 -04:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
2022-02-25 07:04:43 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigList(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
2017-05-08 13:36:04 -04:00
|
|
|
return []swarm.Config{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-1-foo"),
|
|
|
|
builders.ConfigName("1-foo"),
|
|
|
|
builders.ConfigVersion(swarm.Version{Index: 10}),
|
|
|
|
builders.ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
builders.ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-05-08 13:36:04 -04:00
|
|
|
),
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-10-foo"),
|
|
|
|
builders.ConfigName("10-foo"),
|
|
|
|
builders.ConfigVersion(swarm.Version{Index: 11}),
|
|
|
|
builders.ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
builders.ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-07-07 04:22:52 -04:00
|
|
|
),
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-2-foo"),
|
|
|
|
builders.ConfigName("2-foo"),
|
|
|
|
builders.ConfigVersion(swarm.Version{Index: 11}),
|
|
|
|
builders.ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
builders.ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-05-08 13:36:04 -04:00
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(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(), "config-list-sort.golden")
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigListWithQuietOption(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
2017-05-08 13:36:04 -04:00
|
|
|
return []swarm.Config{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-foo"), builders.ConfigName("foo")),
|
|
|
|
*builders.Config(builders.ConfigID("ID-bar"), builders.ConfigName("bar"), builders.ConfigLabels(map[string]string{
|
2017-05-08 13:36:04 -04:00
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("quiet", "true"))
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 11:54:23 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "config-list-with-quiet-option.golden")
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigListWithConfigFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
2017-05-08 13:36:04 -04:00
|
|
|
return []swarm.Config{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-foo"), builders.ConfigName("foo")),
|
|
|
|
*builders.Config(builders.ConfigID("ID-bar"), builders.ConfigName("bar"), builders.ConfigLabels(map[string]string{
|
2017-05-08 13:36:04 -04:00
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2017-05-08 13:36:04 -04:00
|
|
|
ConfigFormat: "{{ .Name }} {{ .Labels }}",
|
|
|
|
})
|
|
|
|
cmd := newConfigListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 11:54:23 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "config-list-with-config-format.golden")
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigListWithFormat(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
2017-05-08 13:36:04 -04:00
|
|
|
return []swarm.Config{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-foo"), builders.ConfigName("foo")),
|
|
|
|
*builders.Config(builders.ConfigID("ID-bar"), builders.ConfigName("bar"), builders.ConfigLabels(map[string]string{
|
2017-05-08 13:36:04 -04:00
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}"))
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 11:54:23 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "config-list-with-format.golden")
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigListWithFilter(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
cli/command/config: 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/config/client_test.go:19:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigCreate(ctx context.Context, spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
^
cli/command/config/client_test.go:26:43: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error) {
^
cli/command/config/client_test.go:33:33: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
^
cli/command/config/client_test.go:40:35: unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
func (c *fakeClient) ConfigRemove(ctx context.Context, name string) error {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-30 10:14:47 -04:00
|
|
|
configListFunc: func(_ context.Context, options types.ConfigListOptions) ([]swarm.Config, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("foo", options.Filters.Get("name")[0]))
|
|
|
|
assert.Check(t, is.Equal("lbl1=Label-bar", options.Filters.Get("label")[0]))
|
2017-05-08 13:36:04 -04:00
|
|
|
return []swarm.Config{
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-foo"),
|
|
|
|
builders.ConfigName("foo"),
|
|
|
|
builders.ConfigVersion(swarm.Version{Index: 10}),
|
|
|
|
builders.ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
builders.ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-05-08 13:36:04 -04:00
|
|
|
),
|
2023-10-23 08:51:01 -04:00
|
|
|
*builders.Config(builders.ConfigID("ID-bar"),
|
|
|
|
builders.ConfigName("bar"),
|
|
|
|
builders.ConfigVersion(swarm.Version{Index: 11}),
|
|
|
|
builders.ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
builders.ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
2017-05-08 13:36:04 -04:00
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("filter", "name=foo"))
|
|
|
|
assert.Check(t, cmd.Flags().Set("filter", "label=lbl1=Label-bar"))
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 11:54:23 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "config-list-with-filter.golden")
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|