github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/linux_backend/rootfs_cleaner_test.go (about) 1 package linux_backend_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 8 "github.com/cloudfoundry-incubator/garden-linux/linux_backend" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/pivotal-golang/lager" 12 "github.com/pivotal-golang/lager/lagertest" 13 ) 14 15 var _ = Describe("RootfsCleaner", func() { 16 var ( 17 filePaths []string 18 19 cleaner *linux_backend.RootFSCleaner 20 log lager.Logger 21 rootPath string 22 ) 23 24 BeforeEach(func() { 25 var err error 26 27 filePaths = []string{} 28 29 log = lagertest.NewTestLogger("test") 30 rootPath, err = ioutil.TempDir("", "") 31 Expect(err).NotTo(HaveOccurred()) 32 }) 33 34 JustBeforeEach(func() { 35 cleaner = &linux_backend.RootFSCleaner{ 36 FilePaths: filePaths, 37 } 38 }) 39 40 AfterEach(func() { 41 Expect(os.RemoveAll(rootPath)).To(Succeed()) 42 }) 43 44 Describe("Clean", func() { 45 createFile := func(filePath string) { 46 Expect(os.MkdirAll(filepath.Dir(filePath), 0777)).To(Succeed()) 47 f, err := os.Create(filePath) 48 defer f.Close() 49 Expect(err).NotTo(HaveOccurred()) 50 } 51 52 createSymlink := func(srcFilePath, destFilePath string) { 53 Expect(os.MkdirAll(filepath.Dir(srcFilePath), 0777)).To(Succeed()) 54 Expect(os.Symlink(destFilePath, srcFilePath)).To(Succeed()) 55 } 56 57 createTempFile := func() string { 58 destFile, err := ioutil.TempFile("", "") 59 defer destFile.Close() 60 Expect(err).NotTo(HaveOccurred()) 61 return destFile.Name() 62 } 63 64 Context("when the list is empty", func() { 65 It("should succeed", func() { 66 Expect(cleaner.Clean(log, rootPath)).To(Succeed()) 67 }) 68 }) 69 70 Context("when there is a single path", func() { 71 BeforeEach(func() { 72 filePaths = append(filePaths, "/etc/config") 73 }) 74 75 Context("and it does not exist in the root path", func() { 76 It("should succeed", func() { 77 Expect(cleaner.Clean(log, rootPath)).To(Succeed()) 78 }) 79 }) 80 81 Context("and it exists in the root path", func() { 82 Context("and it is a symlink", func() { 83 var destFilePath string 84 85 BeforeEach(func() { 86 destFilePath = createTempFile() 87 88 createSymlink(filepath.Join(rootPath, "/etc/config"), destFilePath) 89 }) 90 91 AfterEach(func() { 92 Expect(os.Remove(destFilePath)).To(Succeed()) 93 }) 94 95 It("should remove it", func() { 96 Expect(cleaner.Clean(log, rootPath)).To(Succeed()) 97 98 Expect(filepath.Join(rootPath, "/etc/config")).NotTo(BeAnExistingFile()) 99 }) 100 }) 101 102 Context("and it is not a symlink", func() { 103 BeforeEach(func() { 104 createFile(filepath.Join(rootPath, "/etc/config")) 105 }) 106 107 It("should not touch it", func() { 108 Expect(cleaner.Clean(log, rootPath)).To(Succeed()) 109 110 Expect(filepath.Join(rootPath, "/etc/config")).To(BeAnExistingFile()) 111 }) 112 }) 113 }) 114 }) 115 116 Context("when there are multiple paths", func() { 117 var ( 118 configDestFile string 119 nosymlinkDestFile string 120 ) 121 122 BeforeEach(func() { 123 filePaths = append( 124 filePaths, 125 "/etc/config", 126 "/a_root_file", 127 "/var/no_symlink", 128 "/home/alice/in_wonderland", 129 ) 130 131 configDestFile = createTempFile() 132 createSymlink(filepath.Join(rootPath, "/etc/config"), configDestFile) 133 nosymlinkDestFile = createTempFile() 134 createSymlink(filepath.Join(rootPath, "/var/no_symlink"), nosymlinkDestFile) 135 createFile(filepath.Join(rootPath, "/home/alice/in_wonderland")) 136 }) 137 138 AfterEach(func() { 139 Expect(os.Remove(configDestFile)).To(Succeed()) 140 Expect(os.Remove(nosymlinkDestFile)).To(Succeed()) 141 }) 142 143 It("should delete the symlinks", func() { 144 Expect(cleaner.Clean(log, rootPath)).To(Succeed()) 145 146 Expect(filepath.Join(rootPath, "/etc/config")).NotTo(BeAnExistingFile()) 147 Expect(filepath.Join(rootPath, "/var/no_symlink")).NotTo(BeAnExistingFile()) 148 }) 149 150 It("should not touch the existing files that are not symlinks", func() { 151 Expect(cleaner.Clean(log, rootPath)).To(Succeed()) 152 153 Expect(filepath.Join(rootPath, "/a_root_file")).NotTo(BeAnExistingFile()) 154 Expect(filepath.Join(rootPath, "/home/alice/in_wonderland")).To(BeAnExistingFile()) 155 }) 156 }) 157 }) 158 })