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