DockerCLI/cli/command/plugin/inspect_test.go

151 lines
3.8 KiB
Go
Raw Normal View History

package plugin
import (
"fmt"
"io"
"testing"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
var pluginFoo = &types.Plugin{
ID: "id-foo",
Name: "name-foo",
Config: types.PluginConfig{
Description: "plugin foo description",
DockerVersion: "17.12.1-ce",
Documentation: "plugin foo documentation",
Entrypoint: []string{"/foo"},
Interface: types.PluginConfigInterface{
Socket: "pluginfoo.sock",
},
Linux: types.PluginConfigLinux{
Capabilities: []string{"CAP_SYS_ADMIN"},
},
WorkDir: "workdir-foo",
Rootfs: &types.PluginConfigRootfs{
DiffIds: []string{"sha256:8603eedd4ea52cebb2f22b45405a3dc8f78ba3e31bf18f27b4547a9ff930e0bd"},
Type: "layers",
},
},
}
func TestInspectErrors(t *testing.T) {
testCases := []struct {
description string
args []string
flags map[string]string
expectedError string
inspectFunc func(name string) (*types.Plugin, []byte, error)
}{
{
description: "too few arguments",
args: []string{},
expectedError: "requires at least 1 argument",
},
{
description: "error inspecting plugin",
args: []string{"foo"},
expectedError: "error inspecting plugin",
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
return nil, nil, fmt.Errorf("error inspecting plugin")
},
},
{
description: "invalid format",
args: []string{"foo"},
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",
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{pluginInspectFunc: tc.inspectFunc})
cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
})
}
}
func TestInspect(t *testing.T) {
testCases := []struct {
description string
args []string
flags map[string]string
golden string
inspectFunc func(name string) (*types.Plugin, []byte, error)
}{
{
description: "inspect single plugin with format",
args: []string{"foo"},
flags: map[string]string{
"format": "{{ .Name }}",
},
golden: "plugin-inspect-single-with-format.golden",
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
return &types.Plugin{
ID: "id-foo",
Name: "name-foo",
}, []byte{}, nil
},
},
{
description: "inspect single plugin without format",
args: []string{"foo"},
golden: "plugin-inspect-single-without-format.golden",
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
return pluginFoo, nil, nil
},
},
{
description: "inspect multiple plugins with format",
args: []string{"foo", "bar"},
flags: map[string]string{
"format": "{{ .Name }}",
},
golden: "plugin-inspect-multiple-with-format.golden",
inspectFunc: func(name string) (*types.Plugin, []byte, error) {
switch name {
case "foo":
return &types.Plugin{
ID: "id-foo",
Name: "name-foo",
}, []byte{}, nil
case "bar":
return &types.Plugin{
ID: "id-bar",
Name: "name-bar",
}, []byte{}, nil
default:
return nil, nil, fmt.Errorf("unexpected plugin name: %s", name)
}
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{pluginInspectFunc: tc.inspectFunc})
cmd := newInspectCommand(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)
})
}
}