github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/install_test.go (about) 1 package plugin 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 "testing" 8 9 "github.com/docker/cli/internal/test" 10 "github.com/docker/cli/internal/test/notary" 11 "github.com/docker/docker/api/types" 12 13 "gotest.tools/v3/assert" 14 ) 15 16 func TestInstallErrors(t *testing.T) { 17 testCases := []struct { 18 description string 19 args []string 20 expectedError string 21 installFunc func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) 22 }{ 23 { 24 description: "insufficient number of arguments", 25 args: []string{}, 26 expectedError: "requires at least 1 argument", 27 }, 28 { 29 description: "invalid alias", 30 args: []string{"foo", "--alias", "UPPERCASE_ALIAS"}, 31 expectedError: "invalid", 32 }, 33 { 34 description: "invalid plugin name", 35 args: []string{"UPPERCASE_REPONAME"}, 36 expectedError: "invalid", 37 }, 38 { 39 description: "installation error", 40 args: []string{"foo"}, 41 expectedError: "Error installing plugin", 42 installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) { 43 return nil, fmt.Errorf("Error installing plugin") 44 }, 45 }, 46 { 47 description: "installation error due to missing image", 48 args: []string{"foo"}, 49 expectedError: "docker image pull", 50 installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) { 51 return nil, fmt.Errorf("(image) when fetching") 52 }, 53 }, 54 } 55 56 for _, tc := range testCases { 57 cli := test.NewFakeCli(&fakeClient{pluginInstallFunc: tc.installFunc}) 58 cmd := newInstallCommand(cli) 59 cmd.SetArgs(tc.args) 60 cmd.SetOut(io.Discard) 61 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 62 } 63 } 64 65 func TestInstallContentTrustErrors(t *testing.T) { 66 testCases := []struct { 67 description string 68 args []string 69 expectedError string 70 notaryFunc test.NotaryClientFuncType 71 }{ 72 { 73 description: "install plugin, offline notary server", 74 args: []string{"plugin:tag"}, 75 expectedError: "client is offline", 76 notaryFunc: notary.GetOfflineNotaryRepository, 77 }, 78 { 79 description: "install plugin, uninitialized notary server", 80 args: []string{"plugin:tag"}, 81 expectedError: "remote trust data does not exist", 82 notaryFunc: notary.GetUninitializedNotaryRepository, 83 }, 84 { 85 description: "install plugin, empty notary server", 86 args: []string{"plugin:tag"}, 87 expectedError: "No valid trust data for tag", 88 notaryFunc: notary.GetEmptyTargetsNotaryRepository, 89 }, 90 } 91 92 for _, tc := range testCases { 93 cli := test.NewFakeCli(&fakeClient{ 94 pluginInstallFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) { 95 return nil, fmt.Errorf("should not try to install plugin") 96 }, 97 }, test.EnableContentTrust) 98 cli.SetNotaryClient(tc.notaryFunc) 99 cmd := newInstallCommand(cli) 100 cmd.SetArgs(tc.args) 101 cmd.SetOut(io.Discard) 102 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 103 } 104 } 105 106 func TestInstall(t *testing.T) { 107 testCases := []struct { 108 description string 109 args []string 110 expectedOutput string 111 installFunc func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) 112 }{ 113 { 114 description: "install with no additional flags", 115 args: []string{"foo"}, 116 expectedOutput: "Installed plugin foo\n", 117 installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) { 118 return io.NopCloser(strings.NewReader("")), nil 119 }, 120 }, 121 { 122 description: "install with disable flag", 123 args: []string{"--disable", "foo"}, 124 expectedOutput: "Installed plugin foo\n", 125 installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) { 126 assert.Check(t, options.Disabled) 127 return io.NopCloser(strings.NewReader("")), nil 128 }, 129 }, 130 } 131 132 for _, tc := range testCases { 133 cli := test.NewFakeCli(&fakeClient{pluginInstallFunc: tc.installFunc}) 134 cmd := newInstallCommand(cli) 135 cmd.SetArgs(tc.args) 136 assert.NilError(t, cmd.Execute()) 137 assert.Check(t, strings.Contains(cli.OutBuffer().String(), tc.expectedOutput)) 138 } 139 }