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