github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/pluginaction/install_unix_test.go (about)

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