|
| 1 | +// Package utils contains commonly useful functions from Deis testing. |
| 2 | + |
| 3 | +package utils |
| 4 | + |
| 5 | +import ( |
| 6 | + _ "bufio" |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + "syscall" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/satori/go.uuid" |
| 20 | +) |
| 21 | + |
| 22 | +// NewUuid returns a new V4-style unique identifier. |
| 23 | +func NewUuid() string { |
| 24 | + u1 := uuid.NewV4() |
| 25 | + s1 := fmt.Sprintf("%s", u1) |
| 26 | + return strings.Split(s1, "-")[0] |
| 27 | +} |
| 28 | + |
| 29 | +// GetFileBytes returns a byte array of the contents of a file. |
| 30 | +func GetFileBytes(filename string) []byte { |
| 31 | + file, _ := os.Open(filename) |
| 32 | + defer file.Close() |
| 33 | + stat, _ := file.Stat() |
| 34 | + bs := make([]byte, stat.Size()) |
| 35 | + _, _ = file.Read(bs) |
| 36 | + return bs |
| 37 | +} |
| 38 | + |
| 39 | +func ListFiles(dir string) ([]string, error) { |
| 40 | + files, err := filepath.Glob(dir) |
| 41 | + return files, err |
| 42 | +} |
| 43 | + |
| 44 | +// CreateFile creates an empty file at the specified path. |
| 45 | +func CreateFile(path string) error { |
| 46 | + fo, err := os.Create(path) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + defer fo.Close() |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// Chdir sets the current working directory to the relative path specified. |
| 55 | +func Chdir(app string) error { |
| 56 | + var wd, _ = os.Getwd() |
| 57 | + dir, _ := filepath.Abs(filepath.Join(wd, app)) |
| 58 | + err := os.Chdir(dir) |
| 59 | + fmt.Println(dir) |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +func Extract(file, dir string) { |
| 64 | + Chdir(dir) |
| 65 | + cmdl := exec.Command("tar", "-xvf", file) |
| 66 | + if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil { |
| 67 | + fmt.Printf("Failed:\n%v", err) |
| 68 | + } else { |
| 69 | + fmt.Println("ok") |
| 70 | + } |
| 71 | + Chdir("/home/core/") |
| 72 | +} |
| 73 | + |
| 74 | +// Rmdir removes a directory and its contents. |
| 75 | +func Rmdir(app string) error { |
| 76 | + var wd, _ = os.Getwd() |
| 77 | + dir, _ := filepath.Abs(filepath.Join(wd, app)) |
| 78 | + err := os.RemoveAll(dir) |
| 79 | + fmt.Println(dir) |
| 80 | + return err |
| 81 | +} |
| 82 | + |
| 83 | +// GetUserDetails returns sections of a UUID. |
| 84 | +func GetUserDetails() (string, string) { |
| 85 | + u1 := uuid.NewV4() |
| 86 | + s1 := fmt.Sprintf("%s", u1) |
| 87 | + return strings.Split(s1, "-")[0], strings.Split(s1, "-")[1] |
| 88 | +} |
| 89 | + |
| 90 | +// GetHostOs returns either "darwin" or "ubuntu". |
| 91 | +func GetHostOs() string { |
| 92 | + cmd := exec.Command("uname") |
| 93 | + out, _ := cmd.Output() |
| 94 | + if strings.Contains(string(out), "Darwin") { |
| 95 | + return "darwin" |
| 96 | + } |
| 97 | + return "ubuntu" |
| 98 | +} |
| 99 | + |
| 100 | +// GetHostIPAddress returns the host IP for accessing etcd and Deis services. |
| 101 | +func GetHostIPAddress() string { |
| 102 | + IP := os.Getenv("HOST_IPADDR") |
| 103 | + if IP == "" { |
| 104 | + IP = "172.17.8.100" |
| 105 | + } |
| 106 | + return IP |
| 107 | +} |
| 108 | + |
| 109 | +// Append grows a string array by appending a new element. |
| 110 | +func Append(slice []string, data string) []string { |
| 111 | + m := len(slice) |
| 112 | + n := m + 1 |
| 113 | + if n > cap(slice) { // if necessary, reallocate |
| 114 | + // allocate double what's needed, for future growth. |
| 115 | + newSlice := make([]string, (n + 1)) |
| 116 | + copy(newSlice, slice) |
| 117 | + slice = newSlice |
| 118 | + } |
| 119 | + slice = slice[0:n] |
| 120 | + slice[n-1] = data |
| 121 | + return slice |
| 122 | +} |
| 123 | + |
| 124 | +// GetRandomPort returns an unused TCP listen port on the host. |
| 125 | +func GetRandomPort() string { |
| 126 | + l, _ := net.Listen("tcp", "127.0.0.1:0") // listen on localhost |
| 127 | + defer l.Close() |
| 128 | + port := l.Addr() |
| 129 | + return strings.Split(port.String(), ":")[1] |
| 130 | +} |
| 131 | + |
| 132 | +func getExitCode(err error) (int, error) { |
| 133 | + exitCode := 0 |
| 134 | + if exiterr, ok := err.(*exec.ExitError); ok { |
| 135 | + if procExit := exiterr.Sys().(syscall.WaitStatus); ok { |
| 136 | + return procExit.ExitStatus(), nil |
| 137 | + } |
| 138 | + } |
| 139 | + return exitCode, fmt.Errorf("failed to get exit code") |
| 140 | +} |
| 141 | + |
| 142 | +// RunCommandWithStdoutStderr execs a command and returns its output. |
| 143 | +func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) { |
| 144 | + var stdout, stderr bytes.Buffer |
| 145 | + stderrPipe, err := cmd.StderrPipe() |
| 146 | + stdoutPipe, err := cmd.StdoutPipe() |
| 147 | + |
| 148 | + cmd.Env = os.Environ() |
| 149 | + if err != nil { |
| 150 | + fmt.Println("error at io pipes") |
| 151 | + } |
| 152 | + |
| 153 | + err = cmd.Start() |
| 154 | + if err != nil { |
| 155 | + fmt.Println("error at command start") |
| 156 | + } |
| 157 | + |
| 158 | + go func() { |
| 159 | + io.Copy(&stdout, stdoutPipe) |
| 160 | + fmt.Println(stdout.String()) |
| 161 | + }() |
| 162 | + go func() { |
| 163 | + io.Copy(&stderr, stderrPipe) |
| 164 | + fmt.Println(stderr.String()) |
| 165 | + }() |
| 166 | + time.Sleep(2000 * time.Millisecond) |
| 167 | + err = cmd.Wait() |
| 168 | + if err != nil { |
| 169 | + fmt.Println("error at command wait") |
| 170 | + } |
| 171 | + return stdout, stderr, err |
| 172 | +} |
| 173 | + |
| 174 | +func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) { |
| 175 | + exitCode = 0 |
| 176 | + out, err := cmd.CombinedOutput() |
| 177 | + if err != nil { |
| 178 | + var exiterr error |
| 179 | + if exitCode, exiterr = getExitCode(err); exiterr != nil { |
| 180 | + // TODO: Fix this so we check the error's text. |
| 181 | + // we've failed to retrieve exit code, so we set it to 127 |
| 182 | + exitCode = 127 |
| 183 | + } |
| 184 | + } |
| 185 | + output = string(out) |
| 186 | + return |
| 187 | +} |
| 188 | + |
| 189 | +func runCommand(cmd *exec.Cmd) (exitCode int, err error) { |
| 190 | + exitCode = 0 |
| 191 | + err = cmd.Run() |
| 192 | + if err != nil { |
| 193 | + var exiterr error |
| 194 | + if exitCode, exiterr = getExitCode(err); exiterr != nil { |
| 195 | + // TODO: Fix this so we check the error's text. |
| 196 | + // we've failed to retrieve exit code, so we set it to 127 |
| 197 | + exitCode = 127 |
| 198 | + } |
| 199 | + } |
| 200 | + return |
| 201 | +} |
| 202 | + |
| 203 | +func startCommand(cmd *exec.Cmd) (exitCode int, err error) { |
| 204 | + exitCode = 0 |
| 205 | + err = cmd.Start() |
| 206 | + if err != nil { |
| 207 | + var exiterr error |
| 208 | + if exitCode, exiterr = getExitCode(err); exiterr != nil { |
| 209 | + // TODO: Fix this so we check the error's text. |
| 210 | + // we've failed to retrieve exit code, so we set it to 127 |
| 211 | + exitCode = 127 |
| 212 | + } |
| 213 | + } |
| 214 | + return |
| 215 | +} |
| 216 | + |
| 217 | +func logDone(message string) { |
| 218 | + fmt.Printf("[PASSED]: %s\n", message) |
| 219 | +} |
| 220 | + |
| 221 | +func stripTrailingCharacters(target string) string { |
| 222 | + target = strings.Trim(target, "\n") |
| 223 | + target = strings.Trim(target, " ") |
| 224 | + return target |
| 225 | +} |
| 226 | + |
| 227 | +func errorOut(err error, t *testing.T, message string) { |
| 228 | + if err != nil { |
| 229 | + t.Fatal(message) |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +func errorOutOnNonNilError(err error, t *testing.T, message string) { |
| 234 | + if err == nil { |
| 235 | + t.Fatalf(message) |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +func nLines(s string) int { |
| 240 | + return strings.Count(s, "\n") |
| 241 | +} |
| 242 | + |
| 243 | +//func deis(bash string , arg string , cmd string ) |
0 commit comments