github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/utils/utils_test.go (about)

     1  package utils_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/cloudcredo/cloudrocker/utils"
     8  
     9  	. "github.com/cloudcredo/cloudrocker/Godeps/_workspace/src/github.com/onsi/ginkgo"
    10  	. "github.com/cloudcredo/cloudrocker/Godeps/_workspace/src/github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Utils", func() {
    14  	Describe("Getting a rootfs URL", func() {
    15  		Context("without a rootfs env var set", func() {
    16  			It("should return the default URL", func() {
    17  				os.Setenv("ROCKER_ROOTFS_URL", "")
    18  				Expect(utils.GetRootfsUrl()).To(Equal("https://s3.amazonaws.com/blob.cfblob.com/978883d5-2e4d-495b-8aec-fc7c7e2988ad"))
    19  			})
    20  		})
    21  
    22  		Context("with a rootfs env var set", func() {
    23  			It("should return the specified URL", func() {
    24  				os.Setenv("ROCKER_ROOTFS_URL", "dave")
    25  				Expect(utils.GetRootfsUrl()).To(Equal("dave"))
    26  			})
    27  		})
    28  	})
    29  
    30  	Describe("Getting the CLOUDROCKER_HOME", func() {
    31  		Context("without a CLOUDROCKER_HOME env var set", func() {
    32  			It("should return the default URL", func() {
    33  				os.Setenv("CLOUDROCKER_HOME", "")
    34  				Expect(utils.CloudrockerHome()).To(Equal(os.Getenv("HOME") + "/cloudrocker"))
    35  			})
    36  		})
    37  
    38  		Context("with a CLOUDROCKER_HOME env var set", func() {
    39  			It("should return the specified URL", func() {
    40  				os.Setenv("CLOUDROCKER_HOME", "/dave")
    41  				Expect(utils.CloudrockerHome()).To(Equal("/dave"))
    42  			})
    43  		})
    44  	})
    45  
    46  	Describe("Finding the subdirectories in a directory", func() {
    47  		It("should return a slice of found subdirectories", func() {
    48  			parentDir, _ := ioutil.TempDir(os.TempDir(), "utils-test-subdirs")
    49  			os.Mkdir(parentDir+"/dir1", 0755)
    50  			os.Mkdir(parentDir+"/dir2", 0755)
    51  			ioutil.WriteFile(parentDir+"/testfile", []byte("test"), 0644)
    52  			dirs, err := utils.SubDirs(parentDir)
    53  			Expect(err).ShouldNot(HaveOccurred())
    54  			Expect(dirs).Should(ContainElement("dir1"))
    55  			Expect(dirs).Should(ContainElement("dir2"))
    56  			Expect(dirs).ShouldNot(ContainElement("testfile"))
    57  			os.RemoveAll(parentDir)
    58  		})
    59  	})
    60  
    61  	Describe("Copying the rocker binary to its own directory", func() {
    62  		It("should create a rocker subdirectory with the rock binary inside it", func() {
    63  			cloudrockerHome, _ := ioutil.TempDir(os.TempDir(), "utils-test-cp-rocker")
    64  			err := utils.CopyRockerBinaryToDir(cloudrockerHome + "/rocker")
    65  			Expect(err).ShouldNot(HaveOccurred())
    66  			rockerOwnDirFile, err := os.Open(cloudrockerHome + "/rocker")
    67  			rockerOwnDirContents, err := rockerOwnDirFile.Readdirnames(0)
    68  			Expect(rockerOwnDirContents, err).Should(ContainElement("rock"))
    69  			info, _ := os.Stat(cloudrockerHome + "/rocker/rock")
    70  			mode := info.Mode()
    71  			Expect(mode).To(Equal(os.FileMode(0755)))
    72  			os.RemoveAll(cloudrockerHome)
    73  		})
    74  	})
    75  
    76  	Describe("Adding the launcher run script to a directory", func() {
    77  		It("should create a script called cloudrocker-start... with expected contents", func() {
    78  			appDir, _ := ioutil.TempDir(os.TempDir(), "utils-test-launcher")
    79  			utils.AddLauncherRunScript(appDir)
    80  			written, _ := ioutil.ReadFile(appDir + "/cloudrocker-start-1c4352a23e52040ddb1857d7675fe3cc.sh")
    81  			fixture, _ := ioutil.ReadFile("fixtures/cloudrocker-start.sh")
    82  			Expect(written).To(Equal(fixture))
    83  			os.RemoveAll(appDir)
    84  		})
    85  	})
    86  
    87  	Describe("Getting the user's PWD", func() {
    88  		It("should return the PWD", func() {
    89  			testDir, _ := ioutil.TempDir(os.TempDir(), "utils-test-pwd")
    90  			os.Chdir(testDir)
    91  			rootedPath, _ := os.Getwd()
    92  			Expect(utils.Pwd()).To(Equal(rootedPath))
    93  		})
    94  	})
    95  })