github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/pluginaction/install_unix_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package pluginaction_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 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 // Checks file permissions for UNIX platforms 19 var _ = Describe("install actions", func() { 20 var ( 21 actor *Actor 22 fakeConfig *pluginactionfakes.FakeConfig 23 tempPluginDir string 24 ) 25 26 BeforeEach(func() { 27 fakeConfig = new(pluginactionfakes.FakeConfig) 28 var err error 29 tempPluginDir, err = ioutil.TempDir("", "") 30 Expect(err).ToNot(HaveOccurred()) 31 actor = NewActor(fakeConfig, nil) 32 }) 33 34 AfterEach(func() { 35 err := os.RemoveAll(tempPluginDir) 36 Expect(err).ToNot(HaveOccurred()) 37 }) 38 39 Describe("CreateExecutableCopy", func() { 40 When("the file exists", func() { 41 var pluginPath string 42 43 BeforeEach(func() { 44 tempFile, err := ioutil.TempFile("", "") 45 Expect(err).ToNot(HaveOccurred()) 46 47 _, err = tempFile.WriteString("cthulhu") 48 Expect(err).ToNot(HaveOccurred()) 49 err = tempFile.Close() 50 Expect(err).ToNot(HaveOccurred()) 51 52 pluginPath = tempFile.Name() 53 }) 54 55 AfterEach(func() { 56 err := os.Remove(pluginPath) 57 Expect(err).ToNot(HaveOccurred()) 58 }) 59 60 It("gives the copy 0700 permission", func() { 61 copyPath, err := actor.CreateExecutableCopy(pluginPath, tempPluginDir) 62 Expect(err).ToNot(HaveOccurred()) 63 64 stat, err := os.Stat(copyPath) 65 Expect(err).ToNot(HaveOccurred()) 66 Expect(stat.Mode()).To(Equal(os.FileMode(0700))) 67 }) 68 }) 69 }) 70 71 Describe("InstallPluginFromLocalPath", func() { 72 var ( 73 plugin configv3.Plugin 74 installErr error 75 76 pluginHomeDir string 77 pluginPath string 78 tempDir string 79 ) 80 81 BeforeEach(func() { 82 plugin = configv3.Plugin{ 83 Name: "some-plugin", 84 Commands: []configv3.PluginCommand{ 85 {Name: "some-command"}, 86 }, 87 } 88 89 pluginFile, err := ioutil.TempFile("", "") 90 Expect(err).NotTo(HaveOccurred()) 91 err = pluginFile.Close() 92 Expect(err).NotTo(HaveOccurred()) 93 94 pluginPath = pluginFile.Name() 95 96 tempDir, err = ioutil.TempDir("", "") 97 Expect(err).ToNot(HaveOccurred()) 98 99 pluginHomeDir = filepath.Join(tempDir, ".cf", "plugin") 100 }) 101 102 AfterEach(func() { 103 err := os.Remove(pluginPath) 104 Expect(err).NotTo(HaveOccurred()) 105 106 err = os.RemoveAll(tempDir) 107 Expect(err).NotTo(HaveOccurred()) 108 }) 109 110 JustBeforeEach(func() { 111 installErr = actor.InstallPluginFromPath(pluginPath, plugin) 112 }) 113 114 When("no errors are encountered", func() { 115 BeforeEach(func() { 116 fakeConfig.PluginHomeReturns(pluginHomeDir) 117 }) 118 119 It("gives the executable 0755 permission", func() { 120 Expect(installErr).ToNot(HaveOccurred()) 121 122 installedPluginPath := filepath.Join(pluginHomeDir, "some-plugin") 123 stat, err := os.Stat(installedPluginPath) 124 Expect(err).ToNot(HaveOccurred()) 125 Expect(stat.Mode()).To(Equal(os.FileMode(0755))) 126 }) 127 }) 128 }) 129 })