github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/pluginaction/uninstall_test.go (about) 1 package pluginaction_test 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "path/filepath" 9 10 "code.cloudfoundry.org/cli/actor/actionerror" 11 . "code.cloudfoundry.org/cli/actor/pluginaction" 12 "code.cloudfoundry.org/cli/actor/pluginaction/pluginactionfakes" 13 "code.cloudfoundry.org/cli/util/configv3" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Plugin actor", func() { 19 var ( 20 actor *Actor 21 fakeConfig *pluginactionfakes.FakeConfig 22 ) 23 24 BeforeEach(func() { 25 fakeConfig = new(pluginactionfakes.FakeConfig) 26 actor = NewActor(fakeConfig, nil) 27 }) 28 29 Describe("UninstallPlugin", func() { 30 var ( 31 binaryPath string 32 fakePluginUninstaller *pluginactionfakes.FakePluginUninstaller 33 pluginHome string 34 ) 35 36 BeforeEach(func() { 37 var err error 38 pluginHome, err = ioutil.TempDir("", "") 39 Expect(err).ToNot(HaveOccurred()) 40 41 binaryPath = filepath.Join(pluginHome, "banana-faceman") 42 err = ioutil.WriteFile(binaryPath, nil, 0600) 43 Expect(err).ToNot(HaveOccurred()) 44 45 fakePluginUninstaller = new(pluginactionfakes.FakePluginUninstaller) 46 }) 47 48 AfterEach(func() { 49 os.RemoveAll(pluginHome) 50 }) 51 52 When("the plugin does not exist", func() { 53 BeforeEach(func() { 54 fakeConfig.GetPluginReturns(configv3.Plugin{}, false) 55 }) 56 57 It("returns a PluginNotFoundError", func() { 58 err := actor.UninstallPlugin(fakePluginUninstaller, "some-non-existent-plugin") 59 Expect(err).To(MatchError(actionerror.PluginNotFoundError{PluginName: "some-non-existent-plugin"})) 60 }) 61 }) 62 63 When("the plugin exists", func() { 64 BeforeEach(func() { 65 fakeConfig.GetPluginReturns(configv3.Plugin{ 66 Name: "some-plugin", 67 Location: binaryPath, 68 }, true) 69 }) 70 71 When("no errors are encountered", func() { 72 It("runs the plugin cleanup, deletes the binary and removes the plugin config", func() { 73 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 74 Expect(err).ToNot(HaveOccurred()) 75 76 Expect(fakeConfig.GetPluginCallCount()).To(Equal(1)) 77 Expect(fakeConfig.GetPluginArgsForCall(0)).To(Equal("some-plugin")) 78 79 Expect(fakePluginUninstaller.RunCallCount()).To(Equal(1)) 80 path, command := fakePluginUninstaller.RunArgsForCall(0) 81 Expect(path).To(Equal(binaryPath)) 82 Expect(command).To(Equal("CLI-MESSAGE-UNINSTALL")) 83 84 _, err = os.Stat(binaryPath) 85 Expect(os.IsNotExist(err)).To(BeTrue()) 86 87 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(1)) 88 Expect(fakeConfig.RemovePluginArgsForCall(0)).To(Equal("some-plugin")) 89 90 Expect(fakeConfig.WritePluginConfigCallCount()).To(Equal(1)) 91 }) 92 }) 93 94 When("the plugin binary does not exist", func() { 95 BeforeEach(func() { 96 Expect(os.Remove(binaryPath)).ToNot(HaveOccurred()) 97 }) 98 99 It("removes the plugin config", func() { 100 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 101 Expect(err).ToNot(HaveOccurred()) 102 103 Expect(fakePluginUninstaller.RunCallCount()).To(Equal(0)) 104 105 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(1)) 106 Expect(fakeConfig.RemovePluginArgsForCall(0)).To(Equal("some-plugin")) 107 108 Expect(fakeConfig.WritePluginConfigCallCount()).To(Equal(1)) 109 }) 110 }) 111 112 When("the plugin uninstaller returns an os.PathError", func() { 113 var expectedErr error 114 115 BeforeEach(func() { 116 expectedErr = &os.PathError{} 117 fakePluginUninstaller.RunReturns(expectedErr) 118 }) 119 120 It("returns a PluginExecuteError, deletes the binary and removes the plugin config", func() { 121 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 122 Expect(err).To(MatchError(actionerror.PluginExecuteError{Err: expectedErr})) 123 124 _, err = os.Stat(binaryPath) 125 Expect(os.IsNotExist(err)).To(BeTrue()) 126 127 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(1)) 128 Expect(fakeConfig.WritePluginConfigCallCount()).To(Equal(1)) 129 }) 130 }) 131 132 When("the plugin uninstaller returns an exec.ExitError", func() { 133 var expectedErr error 134 135 BeforeEach(func() { 136 expectedErr = &exec.ExitError{} 137 fakePluginUninstaller.RunReturns(expectedErr) 138 }) 139 140 It("returns the error, deletes the binary and removes the plugin config", func() { 141 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 142 Expect(err).To(MatchError(actionerror.PluginExecuteError{Err: expectedErr})) 143 144 _, err = os.Stat(binaryPath) 145 Expect(os.IsNotExist(err)).To(BeTrue()) 146 147 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(1)) 148 Expect(fakeConfig.WritePluginConfigCallCount()).To(Equal(1)) 149 }) 150 }) 151 152 When("the plugin uninstaller returns any other error", func() { 153 var expectedErr error 154 155 BeforeEach(func() { 156 expectedErr = errors.New("some error") 157 fakePluginUninstaller.RunReturns(expectedErr) 158 }) 159 160 It("returns the error and does not delete the binary or remove the plugin config", func() { 161 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 162 Expect(err).To(MatchError(expectedErr)) 163 164 _, err = os.Stat(binaryPath) 165 Expect(err).ToNot(HaveOccurred()) 166 167 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(0)) 168 }) 169 }) 170 171 When("deleting the plugin binary returns a 'file does not exist' error", func() { 172 BeforeEach(func() { 173 err := os.Remove(binaryPath) 174 Expect(err).ToNot(HaveOccurred()) 175 }) 176 177 It("does not return the error and removes the plugin config", func() { 178 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 179 Expect(err).ToNot(HaveOccurred()) 180 181 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(1)) 182 }) 183 }) 184 185 When("deleting the plugin binary returns a path error", func() { 186 BeforeEach(func() { 187 err := os.Remove(binaryPath) 188 Expect(err).ToNot(HaveOccurred()) 189 err = os.Mkdir(binaryPath, 0700) 190 Expect(err).ToNot(HaveOccurred()) 191 err = ioutil.WriteFile(filepath.Join(binaryPath, "foooooo"), nil, 0500) 192 Expect(err).ToNot(HaveOccurred()) 193 }) 194 195 It("returns the error and removes the plugin config", func() { 196 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 197 pluginBinaryRemoveErr, ok := err.(actionerror.PluginBinaryRemoveFailedError) 198 Expect(ok).To(BeTrue()) 199 _, isPathError := pluginBinaryRemoveErr.Err.(*os.PathError) 200 Expect(isPathError).To(BeTrue()) 201 202 Expect(fakeConfig.RemovePluginCallCount()).To(Equal(1)) 203 Expect(fakeConfig.WritePluginConfigCallCount()).To(Equal(1)) 204 }) 205 }) 206 207 When("writing the config returns an error", func() { 208 var expectedErr error 209 210 BeforeEach(func() { 211 expectedErr = errors.New("some plugin config write error") 212 fakeConfig.WritePluginConfigReturns(expectedErr) 213 }) 214 215 It("returns the error", func() { 216 err := actor.UninstallPlugin(fakePluginUninstaller, "some-plugin") 217 Expect(err).To(MatchError(expectedErr)) 218 }) 219 }) 220 }) 221 }) 222 })