2017-05-08 13:36:04 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
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"
|
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-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
|
|
|
|
configListFunc func(types.ConfigListOptions) ([]swarm.Config, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
|
|
|
|
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{
|
2017-05-08 13:36:04 -04:00
|
|
|
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
|
|
|
|
return []swarm.Config{
|
2017-07-07 04:22:52 -04:00
|
|
|
*Config(ConfigID("ID-1-foo"),
|
|
|
|
ConfigName("1-foo"),
|
2017-05-08 13:36:04 -04:00
|
|
|
ConfigVersion(swarm.Version{Index: 10}),
|
|
|
|
ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
2017-07-07 04:22:52 -04:00
|
|
|
*Config(ConfigID("ID-10-foo"),
|
|
|
|
ConfigName("10-foo"),
|
|
|
|
ConfigVersion(swarm.Version{Index: 11}),
|
|
|
|
ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
*Config(ConfigID("ID-2-foo"),
|
|
|
|
ConfigName("2-foo"),
|
2017-05-08 13:36:04 -04:00
|
|
|
ConfigVersion(swarm.Version{Index: 11}),
|
|
|
|
ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
}, 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{
|
2017-05-08 13:36:04 -04:00
|
|
|
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
|
|
|
|
return []swarm.Config{
|
|
|
|
*Config(ConfigID("ID-foo"), ConfigName("foo")),
|
|
|
|
*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(cli)
|
|
|
|
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{
|
2017-05-08 13:36:04 -04:00
|
|
|
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
|
|
|
|
return []swarm.Config{
|
|
|
|
*Config(ConfigID("ID-foo"), ConfigName("foo")),
|
|
|
|
*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
|
|
|
|
"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{
|
2017-05-08 13:36:04 -04:00
|
|
|
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
|
|
|
|
return []swarm.Config{
|
|
|
|
*Config(ConfigID("ID-foo"), ConfigName("foo")),
|
|
|
|
*Config(ConfigID("ID-bar"), ConfigName("bar"), ConfigLabels(map[string]string{
|
|
|
|
"label": "label-bar",
|
|
|
|
})),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(cli)
|
|
|
|
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{
|
2017-05-08 13:36:04 -04:00
|
|
|
configListFunc: func(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{
|
|
|
|
*Config(ConfigID("ID-foo"),
|
|
|
|
ConfigName("foo"),
|
|
|
|
ConfigVersion(swarm.Version{Index: 10}),
|
|
|
|
ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
*Config(ConfigID("ID-bar"),
|
|
|
|
ConfigName("bar"),
|
|
|
|
ConfigVersion(swarm.Version{Index: 11}),
|
|
|
|
ConfigCreatedAt(time.Now().Add(-2*time.Hour)),
|
|
|
|
ConfigUpdatedAt(time.Now().Add(-1*time.Hour)),
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigListCommand(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 11:54:23 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "config-list-with-filter.golden")
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|