github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/actor/sharedaction/resource_linux_test.go (about)

     1  // +build !windows,!darwin
     2  
     3  package sharedaction_test
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	. "code.cloudfoundry.org/cli/actor/sharedaction"
    11  	"code.cloudfoundry.org/cli/actor/sharedaction/sharedactionfakes"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Resource Actions", func() {
    17  	var (
    18  		actor      *Actor
    19  		fakeConfig *sharedactionfakes.FakeConfig
    20  		srcDir     string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeConfig = new(sharedactionfakes.FakeConfig)
    25  		actor = NewActor(fakeConfig)
    26  
    27  		// Creates the following directory structure:
    28  		// level1/level2/tmpFile1
    29  		// tmpfile2
    30  		// tmpfile3
    31  
    32  		var err error
    33  		srcDir, err = ioutil.TempDir("", "v2-resource-actions")
    34  		Expect(err).ToNot(HaveOccurred())
    35  
    36  		subDir := filepath.Join(srcDir, "level1", "level2")
    37  		err = os.MkdirAll(subDir, 0777)
    38  		Expect(err).ToNot(HaveOccurred())
    39  
    40  		err = ioutil.WriteFile(filepath.Join(subDir, "tmpFile1"), []byte("why hello"), 0644)
    41  		Expect(err).ToNot(HaveOccurred())
    42  
    43  		err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile2"), []byte("Hello, Binky"), 0751)
    44  		Expect(err).ToNot(HaveOccurred())
    45  
    46  		err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile3"), []byte("Bananarama"), 0655)
    47  		Expect(err).ToNot(HaveOccurred())
    48  
    49  		err = os.Symlink("file-that-may-or-may-not-exist", filepath.Join(srcDir, "symlink1"))
    50  		Expect(err).ToNot(HaveOccurred())
    51  	})
    52  
    53  	AfterEach(func() {
    54  		Expect(os.RemoveAll(srcDir)).ToNot(HaveOccurred())
    55  	})
    56  
    57  	Describe("GatherArchiveResources", func() {
    58  		var (
    59  			archive string
    60  
    61  			resources  []Resource
    62  			executeErr error
    63  		)
    64  
    65  		BeforeEach(func() {
    66  			tmpfile, err := ioutil.TempFile("", "example")
    67  			Expect(err).ToNot(HaveOccurred())
    68  			archive = tmpfile.Name()
    69  			Expect(tmpfile.Close()).ToNot(HaveOccurred())
    70  		})
    71  
    72  		JustBeforeEach(func() {
    73  			err := zipit(srcDir, archive, "")
    74  			Expect(err).ToNot(HaveOccurred())
    75  
    76  			resources, executeErr = actor.GatherArchiveResources(archive)
    77  		})
    78  
    79  		AfterEach(func() {
    80  			Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
    81  		})
    82  
    83  		When("there is a symlinked file in the archive", func() {
    84  			It("gathers a list of all files in a source archive", func() {
    85  				Expect(executeErr).ToNot(HaveOccurred())
    86  
    87  				Expect(resources).To(Equal(
    88  					[]Resource{
    89  						{Filename: "/", Mode: DefaultFolderPermissions},
    90  						{Filename: "/level1/", Mode: DefaultFolderPermissions},
    91  						{Filename: "/level1/level2/", Mode: DefaultFolderPermissions},
    92  						{Filename: "/level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4", Size: 9, Mode: DefaultArchiveFilePermissions},
    93  						{Filename: "/symlink1", Mode: 0777 | os.ModeSymlink},
    94  						{Filename: "/tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95", Size: 12, Mode: DefaultArchiveFilePermissions},
    95  						{Filename: "/tmpFile3", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879", Size: 10, Mode: DefaultArchiveFilePermissions},
    96  					}))
    97  			})
    98  		})
    99  	})
   100  
   101  	Describe("GatherDirectoryResources", func() {
   102  		var (
   103  			gatheredResources []Resource
   104  			executeErr        error
   105  		)
   106  
   107  		JustBeforeEach(func() {
   108  			gatheredResources, executeErr = actor.GatherDirectoryResources(srcDir)
   109  		})
   110  
   111  		When("a symlink file points to an existing file", func() {
   112  			BeforeEach(func() {
   113  				err := ioutil.WriteFile(filepath.Join(srcDir, "file-that-may-or-may-not-exist"), []byte("Bananarama"), 0655)
   114  				Expect(err).ToNot(HaveOccurred())
   115  			})
   116  
   117  			It("does not open the symlink but gathers the name and mode", func() {
   118  				Expect(executeErr).ToNot(HaveOccurred())
   119  
   120  				Expect(gatheredResources).To(Equal(
   121  					[]Resource{
   122  						{Filename: "file-that-may-or-may-not-exist", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879", Size: 10, Mode: 0655},
   123  						{Filename: "level1", Mode: DefaultFolderPermissions},
   124  						{Filename: "level1/level2", Mode: DefaultFolderPermissions},
   125  						{Filename: "level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4", Size: 9, Mode: 0644},
   126  						{Filename: "symlink1", Mode: 0777 | os.ModeSymlink},
   127  						{Filename: "tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95", Size: 12, Mode: 0751},
   128  						{Filename: "tmpFile3", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879", Size: 10, Mode: 0655},
   129  					}))
   130  			})
   131  		})
   132  
   133  		When("a symlink file points to a file that does not exist", func() {
   134  			It("does not open the symlink but gathers the name and mode", func() {
   135  				Expect(executeErr).ToNot(HaveOccurred())
   136  
   137  				Expect(gatheredResources).To(Equal(
   138  					[]Resource{
   139  						{Filename: "level1", Mode: DefaultFolderPermissions},
   140  						{Filename: "level1/level2", Mode: DefaultFolderPermissions},
   141  						{Filename: "level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4", Size: 9, Mode: 0644},
   142  						{Filename: "symlink1", Mode: 0777 | os.ModeSymlink},
   143  						{Filename: "tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95", Size: 12, Mode: 0751},
   144  						{Filename: "tmpFile3", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879", Size: 10, Mode: 0655},
   145  					}))
   146  			})
   147  		})
   148  	})
   149  })