github.com/geofffranks/garden-linux@v0.0.0-20160715111146-26c893169cfa/system/mkdirchown_linux_test.go (about) 1 package system_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "syscall" 8 9 "code.cloudfoundry.org/garden-linux/system" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("MkdirChown", func() { 16 var ( 17 tmpdir, mypath string 18 ) 19 20 BeforeEach(func() { 21 var err error 22 tmpdir, err = ioutil.TempDir("", "mkdirchowner") 23 Expect(err).NotTo(HaveOccurred()) 24 }) 25 26 AfterEach(func() { 27 os.RemoveAll(tmpdir) 28 }) 29 30 Context("when we can make and chown a directory", func() { 31 BeforeEach(func() { 32 mypath = filepath.Join(tmpdir, "thing") 33 }) 34 35 JustBeforeEach(func() { 36 err := system.MkdirChown(mypath, 12, 32, 0755) 37 Expect(err).NotTo(HaveOccurred()) 38 }) 39 40 It("makes the directory", func() { 41 Expect(mypath).To(BeADirectory()) 42 }) 43 44 It("gives it the right mode", func() { 45 info, err := os.Stat(mypath) 46 Expect(err).NotTo(HaveOccurred()) 47 Expect(info.Mode() & 0755).To(BeEquivalentTo((0755))) 48 }) 49 50 Context("when the parent of the dir to create doesn't exist", func() { 51 BeforeEach(func() { 52 mypath = filepath.Join(tmpdir, "my", "box", "of", "things") 53 }) 54 55 It("makes all the directories", func() { 56 Expect(mypath).To(BeADirectory()) 57 }) 58 59 It("chowns all the directories to uid,gid", func() { 60 info, err := os.Stat(mypath) 61 Expect(err).NotTo(HaveOccurred()) 62 Expect(info.Sys().(*syscall.Stat_t).Uid).To(BeEquivalentTo(12)) 63 Expect(info.Sys().(*syscall.Stat_t).Gid).To(BeEquivalentTo(32)) 64 65 info, err = os.Stat(filepath.Dir(mypath)) 66 Expect(err).NotTo(HaveOccurred()) 67 Expect(info.Sys().(*syscall.Stat_t).Uid).To(BeEquivalentTo(12)) 68 Expect(info.Sys().(*syscall.Stat_t).Gid).To(BeEquivalentTo(32)) 69 70 info, err = os.Stat(filepath.Dir(filepath.Dir(mypath))) 71 Expect(err).NotTo(HaveOccurred()) 72 Expect(info.Sys().(*syscall.Stat_t).Uid).To(BeEquivalentTo(12)) 73 Expect(info.Sys().(*syscall.Stat_t).Gid).To(BeEquivalentTo(32)) 74 }) 75 }) 76 }) 77 78 Context("when one of the directories in the stack is impossible to create", func() { 79 var ( 80 tmpfile *os.File 81 ) 82 83 BeforeEach(func() { 84 var err error 85 tmpfile, err = ioutil.TempFile(tmpdir, "mkdirchown") 86 Expect(err).NotTo(HaveOccurred()) 87 mypath = filepath.Join(tmpfile.Name(), "my", "box", "of", "things") 88 }) 89 90 It("returns a sensible error", func() { 91 err := system.MkdirChown(mypath, 12, 32, 0755) 92 Expect(err).To(MatchError(ContainSubstring("mkdir"))) 93 }) 94 }) 95 })