github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/plugin/create_test.go (about) 1 package plugin 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "runtime" 8 "testing" 9 10 "github.com/docker/cli/internal/test" 11 "github.com/docker/docker/api/types" 12 "gotest.tools/assert" 13 is "gotest.tools/assert/cmp" 14 "gotest.tools/fs" 15 ) 16 17 func TestCreateErrors(t *testing.T) { 18 noSuchFile := "no such file or directory" 19 if runtime.GOOS == "windows" { 20 noSuchFile = "The system cannot find the file specified." 21 } 22 testCases := []struct { 23 args []string 24 expectedError string 25 }{ 26 { 27 args: []string{}, 28 expectedError: "requires at least 2 arguments", 29 }, 30 { 31 args: []string{"INVALID_TAG", "context-dir"}, 32 expectedError: "invalid", 33 }, 34 { 35 args: []string{"plugin-foo", "nonexistent_context_dir"}, 36 expectedError: noSuchFile, 37 }, 38 } 39 40 for _, tc := range testCases { 41 cli := test.NewFakeCli(&fakeClient{}) 42 cmd := newCreateCommand(cli) 43 cmd.SetArgs(tc.args) 44 cmd.SetOutput(ioutil.Discard) 45 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 46 } 47 } 48 49 func TestCreateErrorOnFileAsContextDir(t *testing.T) { 50 tmpFile := fs.NewFile(t, "file-as-context-dir") 51 defer tmpFile.Remove() 52 53 cli := test.NewFakeCli(&fakeClient{}) 54 cmd := newCreateCommand(cli) 55 cmd.SetArgs([]string{"plugin-foo", tmpFile.Path()}) 56 cmd.SetOutput(ioutil.Discard) 57 assert.ErrorContains(t, cmd.Execute(), "context must be a directory") 58 } 59 60 func TestCreateErrorOnContextDirWithoutConfig(t *testing.T) { 61 tmpDir := fs.NewDir(t, "plugin-create-test") 62 defer tmpDir.Remove() 63 64 cli := test.NewFakeCli(&fakeClient{}) 65 cmd := newCreateCommand(cli) 66 cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()}) 67 cmd.SetOutput(ioutil.Discard) 68 69 expectedErr := "config.json: no such file or directory" 70 if runtime.GOOS == "windows" { 71 expectedErr = "config.json: The system cannot find the file specified." 72 } 73 assert.ErrorContains(t, cmd.Execute(), expectedErr) 74 } 75 76 func TestCreateErrorOnInvalidConfig(t *testing.T) { 77 tmpDir := fs.NewDir(t, "plugin-create-test", 78 fs.WithDir("rootfs"), 79 fs.WithFile("config.json", "invalid-config-contents")) 80 defer tmpDir.Remove() 81 82 cli := test.NewFakeCli(&fakeClient{}) 83 cmd := newCreateCommand(cli) 84 cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()}) 85 cmd.SetOutput(ioutil.Discard) 86 assert.ErrorContains(t, cmd.Execute(), "invalid") 87 } 88 89 func TestCreateErrorFromDaemon(t *testing.T) { 90 tmpDir := fs.NewDir(t, "plugin-create-test", 91 fs.WithDir("rootfs"), 92 fs.WithFile("config.json", `{ "Name": "plugin-foo" }`)) 93 defer tmpDir.Remove() 94 95 cli := test.NewFakeCli(&fakeClient{ 96 pluginCreateFunc: func(createContext io.Reader, createOptions types.PluginCreateOptions) error { 97 return fmt.Errorf("Error creating plugin") 98 }, 99 }) 100 101 cmd := newCreateCommand(cli) 102 cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()}) 103 cmd.SetOutput(ioutil.Discard) 104 assert.ErrorContains(t, cmd.Execute(), "Error creating plugin") 105 } 106 107 func TestCreatePlugin(t *testing.T) { 108 tmpDir := fs.NewDir(t, "plugin-create-test", 109 fs.WithDir("rootfs"), 110 fs.WithFile("config.json", `{ "Name": "plugin-foo" }`)) 111 defer tmpDir.Remove() 112 113 cli := test.NewFakeCli(&fakeClient{ 114 pluginCreateFunc: func(createContext io.Reader, createOptions types.PluginCreateOptions) error { 115 return nil 116 }, 117 }) 118 119 cmd := newCreateCommand(cli) 120 cmd.SetArgs([]string{"plugin-foo", tmpDir.Path()}) 121 assert.NilError(t, cmd.Execute()) 122 assert.Check(t, is.Equal("plugin-foo\n", cli.OutBuffer().String())) 123 }