github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/test/helpers/helpers.go (about) 1 package helpers 2 3 import ( 4 "archive/tar" 5 "compress/gzip" 6 "errors" 7 "fmt" 8 "io" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "runtime" 14 "strings" 15 "time" 16 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gexec" 20 ) 21 22 func recursiveFileList(destDir, searchDir string) ([]string, []string, []string, error) { 23 srcFileList := make([]string, 0) 24 destFileList := make([]string, 0) 25 dirList := make([]string, 0) 26 leafSearchDir := searchDir 27 lastSepIndex := strings.LastIndex(searchDir, string(filepath.Separator)) 28 if lastSepIndex >= 0 { 29 leafSearchDir = searchDir[lastSepIndex:len(searchDir)] 30 } 31 32 e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error { 33 if f.IsDir() { 34 dirList = append(dirList, filepath.Join(destDir, leafSearchDir, path[len(searchDir):len(path)])) 35 } else { 36 srcFileList = append(srcFileList, path) 37 destFileList = append(destFileList, filepath.Join(destDir, leafSearchDir, path[len(searchDir):len(path)])) 38 } 39 return err 40 }) 41 42 if e != nil { 43 return nil, nil, nil, e 44 } 45 46 return destFileList, srcFileList, dirList, nil 47 } 48 49 func CopyRecursive(destRoot, srcRoot string) error { 50 var err error 51 destRoot, err = filepath.Abs(destRoot) 52 if err != nil { 53 return err 54 } 55 56 srcRoot, err = filepath.Abs(srcRoot) 57 if err != nil { 58 return err 59 } 60 61 destFileList, srcFileList, dirList, err := recursiveFileList(destRoot, srcRoot) 62 if err != nil { 63 return err 64 } 65 66 // create destination directory hierarchy 67 for _, myDir := range dirList { 68 if err = os.MkdirAll(myDir, os.ModePerm); err != nil { 69 return err 70 } 71 } 72 73 for i, _ := range srcFileList { 74 srcFile, err := os.Open(srcFileList[i]) 75 if err != nil { 76 return err 77 } 78 defer srcFile.Close() 79 80 destFile, err := os.Create(destFileList[i]) 81 if err != nil { 82 return err 83 } 84 defer destFile.Close() 85 86 _, err = io.Copy(destFile, srcFile) 87 if err != nil { 88 return err 89 } 90 91 if err = destFile.Sync(); err != nil { 92 return err 93 } 94 } 95 96 return nil 97 } 98 99 func extractArchive(archive io.Reader, dirname string) error { 100 tr := tar.NewReader(archive) 101 102 limit := 100 103 for ; limit >= 0; limit-- { 104 h, err := tr.Next() 105 if err != nil { 106 if err != io.EOF { 107 return fmt.Errorf("tar: reading from archive: %s", err) 108 } 109 break 110 } 111 112 // expect a flat archive 113 name := h.Name 114 if filepath.Base(name) != name { 115 return fmt.Errorf("tar: archive contains subdirectory: %s", name) 116 } 117 118 // only allow regular files 119 mode := h.FileInfo().Mode() 120 if !mode.IsRegular() { 121 return fmt.Errorf("tar: unexpected file mode (%s): %s", name, mode) 122 } 123 124 path := filepath.Join(dirname, name) 125 f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode) 126 if err != nil { 127 return fmt.Errorf("tar: opening file (%s): %s", path, err) 128 } 129 defer f.Close() 130 131 if _, err := io.Copy(f, tr); err != nil { 132 return fmt.Errorf("tar: writing file (%s): %s", path, err) 133 } 134 } 135 if limit <= 0 { 136 return errors.New("tar: too many files in archive") 137 } 138 return nil 139 } 140 141 // ExtractGzipArchive extracts the tgz archive name to a temp directory 142 // returning the filepath of the temp directory. 143 func ExtractGzipArchive(name string) (string, error) { 144 tmpdir, err := ioutil.TempDir("", "test-") 145 if err != nil { 146 return "", err 147 } 148 149 f, err := os.Open(name) 150 if err != nil { 151 return "", err 152 } 153 defer f.Close() 154 155 w, err := gzip.NewReader(f) 156 if err != nil { 157 return "", err 158 } 159 if err := extractArchive(w, tmpdir); err != nil { 160 return "", err 161 } 162 if err := w.Close(); err != nil { 163 return "", err 164 } 165 return tmpdir, nil 166 } 167 168 func ReadFile(name string) (string, error) { 169 b, err := ioutil.ReadFile(name) 170 return string(b), err 171 } 172 173 func BuildStembuild(version string) (string, error) { 174 command := exec.Command("make", "build-integration") 175 command.Env = AddOrReplaceEnvironment(os.Environ(), "STEMBUILD_VERSION", version) 176 177 _, b, _, _ := runtime.Caller(0) 178 root := filepath.Dir(filepath.Dir(filepath.Dir(b))) 179 command.Dir = root 180 181 session, err := Start( 182 command, 183 NewPrefixedWriter(DebugOutPrefix, GinkgoWriter), 184 NewPrefixedWriter(DebugErrPrefix, GinkgoWriter)) 185 Expect(err).NotTo(HaveOccurred()) 186 Eventually(session, 120*time.Second).Should(Exit(0)) 187 188 files, err := ioutil.ReadDir(filepath.Join(root, "out")) 189 Expect(err).NotTo(HaveOccurred()) 190 191 for _, f := range files { 192 if strings.Contains(filepath.Base(f.Name()), "stembuild") { 193 stem := filepath.Join(root, "out", f.Name()) 194 fmt.Fprintf(GinkgoWriter, "Stembuild: %s", stem) 195 return stem, nil 196 } 197 } 198 199 panic("Unable to find binary generated by 'make build'") 200 } 201 202 func EnvMustExist(variableName string) string { 203 result := os.Getenv(variableName) 204 if result == "" { 205 Fail(fmt.Sprintf("%s must be set", variableName)) 206 } 207 208 return result 209 }