github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/v2action/resource_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"archive/zip"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	. "code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    11  	"code.cloudfoundry.org/ykk"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Resource Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    20  		srcDir                    string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil)
    26  
    27  		var err error
    28  		srcDir, err = ioutil.TempDir("", "")
    29  		Expect(err).ToNot(HaveOccurred())
    30  
    31  		subDir := filepath.Join(srcDir, "level1", "level2")
    32  		err = os.MkdirAll(subDir, 0777)
    33  		Expect(err).ToNot(HaveOccurred())
    34  
    35  		err = ioutil.WriteFile(filepath.Join(subDir, "tmpFile1"), []byte("why hello"), 0600)
    36  		Expect(err).ToNot(HaveOccurred())
    37  
    38  		err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile2"), []byte("Hello, Binky"), 0600)
    39  		Expect(err).ToNot(HaveOccurred())
    40  
    41  		err = ioutil.WriteFile(filepath.Join(srcDir, "tmpFile3"), []byte("Bananarama"), 0600)
    42  		Expect(err).ToNot(HaveOccurred())
    43  	})
    44  
    45  	AfterEach(func() {
    46  		Expect(os.RemoveAll(srcDir)).ToNot(HaveOccurred())
    47  	})
    48  
    49  	Describe("GatherArchiveResources", func() {
    50  		// tests are under resource_unix_test.go and resource_windows_test.go
    51  	})
    52  
    53  	Describe("GatherDirectoryResources", func() {
    54  		// tests are under resource_unix_test.go and resource_windows_test.go
    55  	})
    56  
    57  	Describe("ZipArchiveResources", func() {
    58  		var (
    59  			archive    string
    60  			resultZip  string
    61  			resources  []Resource
    62  			executeErr error
    63  		)
    64  
    65  		BeforeEach(func() {
    66  			tmpfile, err := ioutil.TempFile("", "zip-archive-resources")
    67  			Expect(err).ToNot(HaveOccurred())
    68  			defer tmpfile.Close()
    69  			archive = tmpfile.Name()
    70  
    71  			err = zipit(srcDir, archive, "")
    72  			Expect(err).ToNot(HaveOccurred())
    73  		})
    74  
    75  		JustBeforeEach(func() {
    76  			resultZip, executeErr = actor.ZipArchiveResources(archive, resources)
    77  		})
    78  
    79  		AfterEach(func() {
    80  			Expect(os.RemoveAll(archive)).ToNot(HaveOccurred())
    81  			Expect(os.RemoveAll(resultZip)).ToNot(HaveOccurred())
    82  		})
    83  
    84  		Context("when the files have not been changed since scanning them", func() {
    85  			BeforeEach(func() {
    86  				resources = []Resource{
    87  					{Filename: "/"},
    88  					{Filename: "/level1/"},
    89  					{Filename: "/level1/level2/"},
    90  					{Filename: "/level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"},
    91  					{Filename: "/tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"},
    92  					{Filename: "/tmpFile3", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879"},
    93  				}
    94  			})
    95  
    96  			It("zips the file and returns a populated resources list", func() {
    97  				Expect(executeErr).ToNot(HaveOccurred())
    98  
    99  				Expect(resultZip).ToNot(BeEmpty())
   100  				zipFile, err := os.Open(resultZip)
   101  				Expect(err).ToNot(HaveOccurred())
   102  				defer zipFile.Close()
   103  
   104  				zipInfo, err := zipFile.Stat()
   105  				Expect(err).ToNot(HaveOccurred())
   106  
   107  				reader, err := ykk.NewReader(zipFile, zipInfo.Size())
   108  				Expect(err).ToNot(HaveOccurred())
   109  
   110  				Expect(reader.File).To(HaveLen(6))
   111  				Expect(reader.File[0].Name).To(Equal("/"))
   112  				Expect(reader.File[1].Name).To(Equal("/level1/"))
   113  				Expect(reader.File[2].Name).To(Equal("/level1/level2/"))
   114  				Expect(reader.File[3].Name).To(Equal("/level1/level2/tmpFile1"))
   115  				Expect(reader.File[4].Name).To(Equal("/tmpFile2"))
   116  				Expect(reader.File[5].Name).To(Equal("/tmpFile3"))
   117  
   118  				expectFileContentsToEqual(reader.File[3], "why hello")
   119  				expectFileContentsToEqual(reader.File[4], "Hello, Binky")
   120  				expectFileContentsToEqual(reader.File[5], "Bananarama")
   121  
   122  				for _, file := range reader.File {
   123  					Expect(file.Method).To(Equal(zip.Deflate))
   124  				}
   125  			})
   126  		})
   127  
   128  		Context("when the files have changed since the scanning", func() {
   129  			BeforeEach(func() {
   130  				resources = []Resource{
   131  					{Filename: "/"},
   132  					{Filename: "/level1/"},
   133  					{Filename: "/level1/level2/"},
   134  					{Filename: "/level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"},
   135  					{Filename: "/tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"},
   136  					{Filename: "/tmpFile3", SHA1: "i dunno, 7?"},
   137  				}
   138  			})
   139  
   140  			It("returns an FileChangedError", func() {
   141  				Expect(executeErr).To(Equal(FileChangedError{Filename: "/tmpFile3"}))
   142  			})
   143  		})
   144  	})
   145  
   146  	Describe("ZipDirectoryResources", func() {
   147  		var (
   148  			resultZip  string
   149  			resources  []Resource
   150  			executeErr error
   151  		)
   152  
   153  		JustBeforeEach(func() {
   154  			resultZip, executeErr = actor.ZipDirectoryResources(srcDir, resources)
   155  		})
   156  
   157  		AfterEach(func() {
   158  			Expect(os.RemoveAll(resultZip)).ToNot(HaveOccurred())
   159  		})
   160  
   161  		Context("when the files have not been changed since scanning them", func() {
   162  			BeforeEach(func() {
   163  				resources = []Resource{
   164  					{Filename: "level1"},
   165  					{Filename: "level1/level2"},
   166  					{Filename: "level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"},
   167  					{Filename: "tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"},
   168  					{Filename: "tmpFile3", SHA1: "f4c9ca85f3e084ffad3abbdabbd2a890c034c879"},
   169  				}
   170  			})
   171  
   172  			It("zips the file and returns a populated resources list", func() {
   173  				Expect(executeErr).ToNot(HaveOccurred())
   174  
   175  				Expect(resultZip).ToNot(BeEmpty())
   176  				zipFile, err := os.Open(resultZip)
   177  				Expect(err).ToNot(HaveOccurred())
   178  				defer zipFile.Close()
   179  
   180  				zipInfo, err := zipFile.Stat()
   181  				Expect(err).ToNot(HaveOccurred())
   182  
   183  				reader, err := ykk.NewReader(zipFile, zipInfo.Size())
   184  				Expect(err).ToNot(HaveOccurred())
   185  
   186  				Expect(reader.File).To(HaveLen(5))
   187  				Expect(reader.File[0].Name).To(Equal("level1/"))
   188  				Expect(reader.File[1].Name).To(Equal("level1/level2/"))
   189  				Expect(reader.File[2].Name).To(Equal("level1/level2/tmpFile1"))
   190  				Expect(reader.File[3].Name).To(Equal("tmpFile2"))
   191  				Expect(reader.File[4].Name).To(Equal("tmpFile3"))
   192  
   193  				expectFileContentsToEqual(reader.File[2], "why hello")
   194  				expectFileContentsToEqual(reader.File[3], "Hello, Binky")
   195  				expectFileContentsToEqual(reader.File[4], "Bananarama")
   196  
   197  				for _, file := range reader.File {
   198  					Expect(file.Method).To(Equal(zip.Deflate))
   199  				}
   200  			})
   201  		})
   202  
   203  		Context("when the files have changed since the scanning", func() {
   204  			BeforeEach(func() {
   205  				resources = []Resource{
   206  					{Filename: "level1"},
   207  					{Filename: "level1/level2"},
   208  					{Filename: "level1/level2/tmpFile1", SHA1: "9e36efec86d571de3a38389ea799a796fe4782f4"},
   209  					{Filename: "tmpFile2", SHA1: "e594bdc795bb293a0e55724137e53a36dc0d9e95"},
   210  					{Filename: "tmpFile3", SHA1: "i dunno, 7?"},
   211  				}
   212  			})
   213  
   214  			It("returns an FileChangedError", func() {
   215  				Expect(executeErr).To(Equal(FileChangedError{Filename: filepath.Join(srcDir, "tmpFile3")}))
   216  			})
   217  		})
   218  	})
   219  })
   220  
   221  func expectFileContentsToEqual(file *zip.File, expectedContents string) {
   222  	reader, err := file.Open()
   223  	Expect(err).ToNot(HaveOccurred())
   224  	defer reader.Close()
   225  
   226  	body, err := ioutil.ReadAll(reader)
   227  	Expect(err).ToNot(HaveOccurred())
   228  
   229  	Expect(string(body)).To(Equal(expectedContents))
   230  }