2018-02-13 05:38:14 -05:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-02-27 10:54:36 -05:00
|
|
|
"runtime"
|
2018-02-13 05:38:14 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
2018-02-13 05:38:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateErrors(t *testing.T) {
|
2018-02-27 10:54:36 -05:00
|
|
|
noSuchFile := "no such file or directory"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
noSuchFile = "The system cannot find the file specified."
|
|
|
|
}
|
2018-02-13 05:38:14 -05:00
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{},
|
|
|
|
expectedError: "requires at least 2 arguments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"INVALID_TAG", "context-dir"},
|
|
|
|
expectedError: "invalid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"plugin-foo", "nonexistent_context_dir"},
|
2018-02-27 10:54:36 -05:00
|
|
|
expectedError: noSuchFile,
|
2018-02-13 05:38:14 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2018-02-13 05:38:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateErrorOnFileAsContextDir(t *testing.T) {
|
|
|
|
tmpFile := fs.NewFile(t, "file-as-context-dir")
|
|
|
|
defer tmpFile.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"plugin-foo", tmpFile.Path()})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "context must be a directory")
|
2018-02-13 05:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateErrorOnContextDirWithoutConfig(t *testing.T) {
|
|
|
|
tmpDir := fs.NewDir(t, "plugin-create-test")
|
|
|
|
defer tmpDir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-02-27 10:54:36 -05:00
|
|
|
|
|
|
|
expectedErr := "config.json: no such file or directory"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedErr = "config.json: The system cannot find the file specified."
|
|
|
|
}
|
|
|
|
assert.ErrorContains(t, cmd.Execute(), expectedErr)
|
2018-02-13 05:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateErrorOnInvalidConfig(t *testing.T) {
|
|
|
|
tmpDir := fs.NewDir(t, "plugin-create-test",
|
|
|
|
fs.WithDir("rootfs"),
|
|
|
|
fs.WithFile("config.json", "invalid-config-contents"))
|
|
|
|
defer tmpDir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "invalid")
|
2018-02-13 05:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateErrorFromDaemon(t *testing.T) {
|
|
|
|
tmpDir := fs.NewDir(t, "plugin-create-test",
|
|
|
|
fs.WithDir("rootfs"),
|
|
|
|
fs.WithFile("config.json", `{ "Name": "plugin-foo" }`))
|
|
|
|
defer tmpDir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
pluginCreateFunc: func(createContext io.Reader, createOptions types.PluginCreateOptions) error {
|
|
|
|
return fmt.Errorf("Error creating plugin")
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "Error creating plugin")
|
2018-02-13 05:38:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreatePlugin(t *testing.T) {
|
|
|
|
tmpDir := fs.NewDir(t, "plugin-create-test",
|
|
|
|
fs.WithDir("rootfs"),
|
|
|
|
fs.WithFile("config.json", `{ "Name": "plugin-foo" }`))
|
|
|
|
defer tmpDir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
pluginCreateFunc: func(createContext io.Reader, createOptions types.PluginCreateOptions) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newCreateCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("plugin-foo\n", cli.OutBuffer().String()))
|
2018-02-13 05:38:14 -05:00
|
|
|
}
|