github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/sharedaction/resource_linux_test.go (about)

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