2018-06-28 16:51:54 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2018-06-28 16:51:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
expectedError string
|
|
|
|
listFunc func(filter filters.Args) (types.PluginsListResponse, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "too many arguments",
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no arguments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "error listing plugins",
|
|
|
|
args: []string{},
|
|
|
|
expectedError: "error listing plugins",
|
|
|
|
listFunc: func(filter filters.Args) (types.PluginsListResponse, error) {
|
|
|
|
return types.PluginsListResponse{}, fmt.Errorf("error listing plugins")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "invalid format",
|
|
|
|
args: []string{},
|
|
|
|
flags: map[string]string{
|
|
|
|
"format": "{{invalid format}}",
|
|
|
|
},
|
linting: fix incorrectly formatted errors (revive)
cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
^
cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n",
^
cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Template parsing error: %v\n", err)
^
cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("error checking context: '%s'.", err)
^
cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n")
^
cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)
return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 4ab70bf61e87271ca56511172352296a202ced41)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-03-27 15:13:03 -04:00
|
|
|
expectedError: "template parsing error",
|
2018-06-28 16:51:54 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{pluginListFunc: tc.listFunc})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
2020-05-07 08:25:59 -04:00
|
|
|
cmd.SetOut(ioutil.Discard)
|
2018-06-28 16:51:54 -04:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
2019-04-02 07:25:33 -04:00
|
|
|
singlePluginListFunc := func(_ filters.Args) (types.PluginsListResponse, error) {
|
2018-06-28 16:51:54 -04:00
|
|
|
return types.PluginsListResponse{
|
|
|
|
{
|
|
|
|
ID: "id-foo",
|
|
|
|
Name: "name-foo",
|
|
|
|
Enabled: true,
|
|
|
|
Config: types.PluginConfig{
|
|
|
|
Description: "desc-bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
description string
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
golden string
|
|
|
|
listFunc func(filter filters.Args) (types.PluginsListResponse, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "list with no additional flags",
|
|
|
|
args: []string{},
|
|
|
|
golden: "plugin-list-without-format.golden",
|
|
|
|
listFunc: singlePluginListFunc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "list with filters",
|
|
|
|
args: []string{},
|
|
|
|
flags: map[string]string{
|
|
|
|
"filter": "foo=bar",
|
|
|
|
},
|
|
|
|
golden: "plugin-list-without-format.golden",
|
|
|
|
listFunc: func(filter filters.Args) (types.PluginsListResponse, error) {
|
|
|
|
assert.Check(t, is.Equal("bar", filter.Get("foo")[0]))
|
|
|
|
return singlePluginListFunc(filter)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "list with quiet option",
|
|
|
|
args: []string{},
|
|
|
|
flags: map[string]string{
|
|
|
|
"quiet": "true",
|
|
|
|
},
|
|
|
|
golden: "plugin-list-with-quiet-option.golden",
|
|
|
|
listFunc: singlePluginListFunc,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "list with no-trunc option",
|
|
|
|
args: []string{},
|
|
|
|
flags: map[string]string{
|
|
|
|
"no-trunc": "true",
|
|
|
|
"format": "{{ .ID }}",
|
|
|
|
},
|
|
|
|
golden: "plugin-list-with-no-trunc-option.golden",
|
2019-04-02 07:25:33 -04:00
|
|
|
listFunc: func(_ filters.Args) (types.PluginsListResponse, error) {
|
2018-06-28 16:51:54 -04:00
|
|
|
return types.PluginsListResponse{
|
|
|
|
{
|
|
|
|
ID: "xyg4z2hiSLO5yTnBJfg4OYia9gKA6Qjd",
|
|
|
|
Name: "name-foo",
|
|
|
|
Enabled: true,
|
|
|
|
Config: types.PluginConfig{
|
|
|
|
Description: "desc-bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "list with format",
|
|
|
|
args: []string{},
|
|
|
|
flags: map[string]string{
|
|
|
|
"format": "{{ .Name }}",
|
|
|
|
},
|
|
|
|
golden: "plugin-list-with-format.golden",
|
|
|
|
listFunc: singlePluginListFunc,
|
|
|
|
},
|
2018-06-29 12:15:43 -04:00
|
|
|
{
|
|
|
|
description: "list output is sorted based on plugin name",
|
|
|
|
args: []string{},
|
|
|
|
flags: map[string]string{
|
|
|
|
"format": "{{ .Name }}",
|
|
|
|
},
|
|
|
|
golden: "plugin-list-sort.golden",
|
2019-04-02 07:25:33 -04:00
|
|
|
listFunc: func(_ filters.Args) (types.PluginsListResponse, error) {
|
2018-06-29 12:15:43 -04:00
|
|
|
return types.PluginsListResponse{
|
|
|
|
{
|
|
|
|
ID: "id-1",
|
|
|
|
Name: "plugin-1-foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "id-2",
|
|
|
|
Name: "plugin-10-foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "id-3",
|
|
|
|
Name: "plugin-2-foo",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2018-06-28 16:51:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{pluginListFunc: tc.listFunc})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), tc.golden)
|
|
|
|
}
|
|
|
|
}
|