github.com/blystad/deis@v0.11.0/tests/utils/utils.go (about) 1 // Package utils contains commonly useful functions from Deis testing. 2 3 package utils 4 5 import ( 6 "bytes" 7 "crypto/rand" 8 "fmt" 9 "io" 10 "net" 11 "os" 12 "os/exec" 13 "path/filepath" 14 "strings" 15 "syscall" 16 "testing" 17 "time" 18 ) 19 20 // Append grows a string array by appending a new element. 21 func Append(slice []string, data string) []string { 22 m := len(slice) 23 n := m + 1 24 if n > cap(slice) { // if necessary, reallocate 25 // allocate double what's needed, for future growth. 26 newSlice := make([]string, (n + 1)) 27 copy(newSlice, slice) 28 slice = newSlice 29 } 30 slice = slice[0:n] 31 slice[n-1] = data 32 return slice 33 } 34 35 // Chdir sets the current working directory to the relative path specified. 36 func Chdir(app string) error { 37 var wd, _ = os.Getwd() 38 dir, _ := filepath.Abs(filepath.Join(wd, app)) 39 err := os.Chdir(dir) 40 fmt.Println(dir) 41 return 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 // GetFileBytes returns a byte array of the contents of a file. 55 func GetFileBytes(filename string) []byte { 56 file, _ := os.Open(filename) 57 defer file.Close() 58 stat, _ := file.Stat() 59 bs := make([]byte, stat.Size()) 60 _, _ = file.Read(bs) 61 return bs 62 } 63 64 // GetHostIPAddress returns the host IP for accessing etcd and Deis services. 65 func GetHostIPAddress() string { 66 IP := os.Getenv("HOST_IPADDR") 67 if IP == "" { 68 IP = "172.17.8.100" 69 } 70 return IP 71 } 72 73 // GetHostOs returns either "darwin" or "ubuntu". 74 func GetHostOs() string { 75 cmd := exec.Command("uname") 76 out, _ := cmd.Output() 77 if strings.Contains(string(out), "Darwin") { 78 return "darwin" 79 } 80 return "ubuntu" 81 } 82 83 // GetRandomPort returns an unused TCP listen port on the host. 84 func GetRandomPort() string { 85 l, _ := net.Listen("tcp", "127.0.0.1:0") // listen on localhost 86 defer l.Close() 87 port := l.Addr() 88 return strings.Split(port.String(), ":")[1] 89 } 90 91 // NewID returns the first part of a random RFC 4122 UUID 92 // See http://play.golang.org/p/4FkNSiUDMg 93 func NewID() string { 94 uuid := make([]byte, 16) 95 io.ReadFull(rand.Reader, uuid) 96 // variant bits; see section 4.1.1 97 uuid[8] = uuid[8]&^0xc0 | 0x80 98 // version 4 (pseudo-random); see section 4.1.3 99 uuid[6] = uuid[6]&^0xf0 | 0x40 100 return fmt.Sprintf("%x", uuid[0:4]) 101 } 102 103 // Rmdir removes a directory and its contents. 104 func Rmdir(app string) error { 105 var wd, _ = os.Getwd() 106 dir, _ := filepath.Abs(filepath.Join(wd, app)) 107 err := os.RemoveAll(dir) 108 fmt.Println(dir) 109 return err 110 } 111 112 // RunCommandWithStdoutStderr execs a command and returns its output. 113 func RunCommandWithStdoutStderr(cmd *exec.Cmd) (bytes.Buffer, bytes.Buffer, error) { 114 var stdout, stderr bytes.Buffer 115 stderrPipe, err := cmd.StderrPipe() 116 stdoutPipe, err := cmd.StdoutPipe() 117 118 cmd.Env = os.Environ() 119 if err != nil { 120 fmt.Println("error at io pipes") 121 } 122 123 err = cmd.Start() 124 if err != nil { 125 fmt.Println("error at command start") 126 } 127 128 go func() { 129 io.Copy(&stdout, stdoutPipe) 130 fmt.Println(stdout.String()) 131 }() 132 go func() { 133 io.Copy(&stderr, stderrPipe) 134 fmt.Println(stderr.String()) 135 }() 136 time.Sleep(2000 * time.Millisecond) 137 err = cmd.Wait() 138 if err != nil { 139 fmt.Println("error at command wait") 140 } 141 return stdout, stderr, err 142 } 143 144 func errorOut(err error, t *testing.T, message string) { 145 if err != nil { 146 t.Fatal(message) 147 } 148 } 149 150 func errorOutOnNonNilError(err error, t *testing.T, message string) { 151 if err == nil { 152 t.Fatalf(message) 153 } 154 } 155 156 func getExitCode(err error) (int, error) { 157 exitCode := 0 158 if exiterr, ok := err.(*exec.ExitError); ok { 159 if procExit := exiterr.Sys().(syscall.WaitStatus); ok { 160 return procExit.ExitStatus(), nil 161 } 162 } 163 return exitCode, fmt.Errorf("failed to get exit code") 164 } 165 166 func logDone(message string) { 167 fmt.Printf("[PASSED]: %s\n", message) 168 } 169 170 func nLines(s string) int { 171 return strings.Count(s, "\n") 172 } 173 174 func runCommand(cmd *exec.Cmd) (exitCode int, err error) { 175 _, exitCode, err = runCommandWithOutput(cmd) 176 return 177 } 178 179 func runCommandWithOutput( 180 cmd *exec.Cmd) (output string, exitCode int, err error) { 181 exitCode = 0 182 out, err := cmd.CombinedOutput() 183 if err != nil { 184 var exiterr error 185 if exitCode, exiterr = getExitCode(err); exiterr != nil { 186 // TODO: Fix this so we check the error's text. 187 // we've failed to retrieve exit code, so we set it to 127 188 exitCode = 127 189 } 190 } 191 output = string(out) 192 return 193 } 194 195 func startCommand(cmd *exec.Cmd) (exitCode int, err error) { 196 exitCode = 0 197 err = cmd.Start() 198 if err != nil { 199 var exiterr error 200 if exitCode, exiterr = getExitCode(err); exiterr != nil { 201 // TODO: Fix this so we check the error's text. 202 // we've failed to retrieve exit code, so we set it to 127 203 exitCode = 127 204 } 205 } 206 return 207 } 208 209 func stripTrailingCharacters(target string) string { 210 target = strings.Trim(target, "\n") 211 target = strings.Trim(target, " ") 212 return target 213 }