github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/version/file_swapper_test.go (about)

     1  // +build !windows
     2  
     3  package version_test
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  
     9  	"github.com/cloudfoundry-incubator/ltc/version"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("FileSwapper", func() {
    16  	Describe("#SwapTempFile", func() {
    17  		var (
    18  			srcPath        string
    19  			destPath       string
    20  			appFileSwapper *version.AppFileSwapper
    21  		)
    22  
    23  		BeforeEach(func() {
    24  			srcFile, err := ioutil.TempFile("", "src")
    25  			Expect(err).NotTo(HaveOccurred())
    26  			_, err = srcFile.Write([]byte("sourcedata"))
    27  			Expect(err).NotTo(HaveOccurred())
    28  			srcFile.Close()
    29  			srcPath = srcFile.Name()
    30  
    31  			Expect(os.Chmod(srcPath, 0644)).To(Succeed())
    32  
    33  			destFile, err := ioutil.TempFile("", "dest")
    34  			Expect(err).NotTo(HaveOccurred())
    35  			_, err = destFile.Write([]byte("destdata"))
    36  			Expect(err).NotTo(HaveOccurred())
    37  			destFile.Close()
    38  			destPath = destFile.Name()
    39  
    40  			Expect(os.Chmod(destPath, 0755)).To(Succeed())
    41  
    42  			appFileSwapper = &version.AppFileSwapper{}
    43  		})
    44  
    45  		AfterEach(func() {
    46  			Expect(os.Remove(destPath)).To(Succeed())
    47  		})
    48  
    49  		It("writes the contents to the destination file", func() {
    50  			err := appFileSwapper.SwapTempFile(destPath, srcPath)
    51  			Expect(err).NotTo(HaveOccurred())
    52  
    53  			destFile, err := os.OpenFile(destPath, os.O_RDONLY, 0)
    54  			defer destFile.Close()
    55  
    56  			Expect(err).NotTo(HaveOccurred())
    57  
    58  			bytes, err := ioutil.ReadAll(destFile)
    59  			Expect(err).NotTo(HaveOccurred())
    60  			Expect(string(bytes)).To(Equal("sourcedata"))
    61  		})
    62  
    63  		It("removes the source file", func() {
    64  			err := appFileSwapper.SwapTempFile(destPath, srcPath)
    65  			Expect(err).NotTo(HaveOccurred())
    66  
    67  			_, err = os.Stat(srcPath)
    68  			Expect(os.IsExist(err)).To(BeFalse())
    69  		})
    70  
    71  		It("preserves the permissions of the destination file", func() {
    72  			err := appFileSwapper.SwapTempFile(destPath, srcPath)
    73  			Expect(err).NotTo(HaveOccurred())
    74  
    75  			info, err := os.Stat(destPath)
    76  			Expect(err).NotTo(HaveOccurred())
    77  			Expect(info.Mode() & os.ModePerm).To(Equal(os.FileMode(0755)))
    78  		})
    79  
    80  		Context("when there is an error getting dest file stats", func() {
    81  			It("returns an error", func() {
    82  				err := appFileSwapper.SwapTempFile("/this-does-not-exist", srcPath)
    83  				Expect(err).To(MatchError(HavePrefix("failed to stat dest file")))
    84  			})
    85  		})
    86  
    87  		Context("when there is an error renaming the file", func() {
    88  			It("returns an error", func() {
    89  				err := appFileSwapper.SwapTempFile(destPath, "/this-does-not-exist")
    90  				Expect(err).To(MatchError(HavePrefix("failed to rename file")))
    91  			})
    92  		})
    93  	})
    94  })