2017-05-08 13:36:04 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
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"
|
|
|
|
// Import builders to get the builder function as package function
|
2017-08-21 16:30:09 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders"
|
2017-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-08-16 11:54:23 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/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)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 18:53:52 -05:00
|
|
|
assert.Check(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-05 18:53:52 -05:00
|
|
|
assert.Check(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-05 18:53:52 -05:00
|
|
|
assert.Check(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-05 18:53:52 -05:00
|
|
|
assert.Check(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-05 18:53:52 -05:00
|
|
|
assert.Check(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
|
|
|
}
|