utils.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "reflect"
  14. "strings"
  15. "syscall"
  16. "time"
  17. "github.com/docker/docker/pkg/stringutils"
  18. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  19. )
  20. func getExitCode(err error) (int, error) {
  21. exitCode := 0
  22. if exiterr, ok := err.(*exec.ExitError); ok {
  23. if procExit := exiterr.Sys().(syscall.WaitStatus); ok {
  24. return procExit.ExitStatus(), nil
  25. }
  26. }
  27. return exitCode, fmt.Errorf("failed to get exit code")
  28. }
  29. func processExitCode(err error) (exitCode int) {
  30. if err != nil {
  31. var exiterr error
  32. if exitCode, exiterr = getExitCode(err); exiterr != nil {
  33. // TODO: Fix this so we check the error's text.
  34. // we've failed to retrieve exit code, so we set it to 127
  35. exitCode = 127
  36. }
  37. }
  38. return
  39. }
  40. func IsKilled(err error) bool {
  41. if exitErr, ok := err.(*exec.ExitError); ok {
  42. status, ok := exitErr.Sys().(syscall.WaitStatus)
  43. if !ok {
  44. return false
  45. }
  46. // status.ExitStatus() is required on Windows because it does not
  47. // implement Signal() nor Signaled(). Just check it had a bad exit
  48. // status could mean it was killed (and in tests we do kill)
  49. return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0
  50. }
  51. return false
  52. }
  53. func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
  54. exitCode = 0
  55. out, err := cmd.CombinedOutput()
  56. exitCode = processExitCode(err)
  57. output = string(out)
  58. return
  59. }
  60. func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, exitCode int, err error) {
  61. var (
  62. stderrBuffer, stdoutBuffer bytes.Buffer
  63. )
  64. exitCode = 0
  65. cmd.Stderr = &stderrBuffer
  66. cmd.Stdout = &stdoutBuffer
  67. err = cmd.Run()
  68. exitCode = processExitCode(err)
  69. stdout = stdoutBuffer.String()
  70. stderr = stderrBuffer.String()
  71. return
  72. }
  73. func runCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (output string, exitCode int, timedOut bool, err error) {
  74. var outputBuffer bytes.Buffer
  75. if cmd.Stdout != nil {
  76. err = errors.New("cmd.Stdout already set")
  77. return
  78. }
  79. cmd.Stdout = &outputBuffer
  80. if cmd.Stderr != nil {
  81. err = errors.New("cmd.Stderr already set")
  82. return
  83. }
  84. cmd.Stderr = &outputBuffer
  85. done := make(chan error)
  86. go func() {
  87. exitErr := cmd.Run()
  88. exitCode = processExitCode(exitErr)
  89. done <- exitErr
  90. }()
  91. select {
  92. case <-time.After(duration):
  93. killErr := cmd.Process.Kill()
  94. if killErr != nil {
  95. fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, killErr)
  96. }
  97. timedOut = true
  98. break
  99. case err = <-done:
  100. break
  101. }
  102. output = outputBuffer.String()
  103. return
  104. }
  105. var ErrCmdTimeout = fmt.Errorf("command timed out")
  106. func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) {
  107. var timedOut bool
  108. output, exitCode, timedOut, err = runCommandWithOutputForDuration(cmd, timeout)
  109. if timedOut {
  110. err = ErrCmdTimeout
  111. }
  112. return
  113. }
  114. func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
  115. exitCode = 0
  116. err = cmd.Run()
  117. exitCode = processExitCode(err)
  118. return
  119. }
  120. func runCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode int, err error) {
  121. if len(cmds) < 2 {
  122. return "", 0, errors.New("pipeline does not have multiple cmds")
  123. }
  124. // connect stdin of each cmd to stdout pipe of previous cmd
  125. for i, cmd := range cmds {
  126. if i > 0 {
  127. prevCmd := cmds[i-1]
  128. cmd.Stdin, err = prevCmd.StdoutPipe()
  129. if err != nil {
  130. return "", 0, fmt.Errorf("cannot set stdout pipe for %s: %v", cmd.Path, err)
  131. }
  132. }
  133. }
  134. // start all cmds except the last
  135. for _, cmd := range cmds[:len(cmds)-1] {
  136. if err = cmd.Start(); err != nil {
  137. return "", 0, fmt.Errorf("starting %s failed with error: %v", cmd.Path, err)
  138. }
  139. }
  140. defer func() {
  141. // wait all cmds except the last to release their resources
  142. for _, cmd := range cmds[:len(cmds)-1] {
  143. cmd.Wait()
  144. }
  145. }()
  146. // wait on last cmd
  147. return runCommandWithOutput(cmds[len(cmds)-1])
  148. }
  149. func logDone(message string) {
  150. fmt.Printf("[PASSED]: %.69s\n", message)
  151. }
  152. func unmarshalJSON(data []byte, result interface{}) error {
  153. err := json.Unmarshal(data, result)
  154. if err != nil {
  155. return err
  156. }
  157. return nil
  158. }
  159. func convertSliceOfStringsToMap(input []string) map[string]struct{} {
  160. output := make(map[string]struct{})
  161. for _, v := range input {
  162. output[v] = struct{}{}
  163. }
  164. return output
  165. }
  166. func waitForContainer(contID string, args ...string) error {
  167. args = append([]string{"run", "--name", contID}, args...)
  168. cmd := exec.Command(dockerBinary, args...)
  169. if _, err := runCommand(cmd); err != nil {
  170. return err
  171. }
  172. if err := waitRun(contID); err != nil {
  173. return err
  174. }
  175. return nil
  176. }
  177. func waitRun(contID string) error {
  178. return waitInspect(contID, "{{.State.Running}}", "true", 5)
  179. }
  180. func waitInspect(name, expr, expected string, timeout int) error {
  181. after := time.After(time.Duration(timeout) * time.Second)
  182. for {
  183. cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name)
  184. out, _, err := runCommandWithOutput(cmd)
  185. if err != nil {
  186. if !strings.Contains(out, "No such") {
  187. return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
  188. }
  189. select {
  190. case <-after:
  191. return err
  192. default:
  193. time.Sleep(10 * time.Millisecond)
  194. continue
  195. }
  196. }
  197. out = strings.TrimSpace(out)
  198. if out == expected {
  199. break
  200. }
  201. select {
  202. case <-after:
  203. return fmt.Errorf("condition \"%q == %q\" not true in time", out, expected)
  204. default:
  205. }
  206. time.Sleep(100 * time.Millisecond)
  207. }
  208. return nil
  209. }
  210. func compareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error {
  211. var (
  212. e1Entries = make(map[string]struct{})
  213. e2Entries = make(map[string]struct{})
  214. )
  215. for _, e := range e1 {
  216. e1Entries[e.Name()] = struct{}{}
  217. }
  218. for _, e := range e2 {
  219. e2Entries[e.Name()] = struct{}{}
  220. }
  221. if !reflect.DeepEqual(e1Entries, e2Entries) {
  222. return fmt.Errorf("entries differ")
  223. }
  224. return nil
  225. }
  226. func ListTar(f io.Reader) ([]string, error) {
  227. tr := tar.NewReader(f)
  228. var entries []string
  229. for {
  230. th, err := tr.Next()
  231. if err == io.EOF {
  232. // end of tar archive
  233. return entries, nil
  234. }
  235. if err != nil {
  236. return entries, err
  237. }
  238. entries = append(entries, th.Name)
  239. }
  240. }
  241. type FileServer struct {
  242. *httptest.Server
  243. }
  244. func fileServer(files map[string]string) (*FileServer, error) {
  245. var handler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
  246. if filePath, found := files[r.URL.Path]; found {
  247. http.ServeFile(w, r, filePath)
  248. } else {
  249. http.Error(w, http.StatusText(404), 404)
  250. }
  251. }
  252. for _, file := range files {
  253. if _, err := os.Stat(file); err != nil {
  254. return nil, err
  255. }
  256. }
  257. server := httptest.NewServer(handler)
  258. return &FileServer{
  259. Server: server,
  260. }, nil
  261. }
  262. func copyWithCP(source, target string) error {
  263. copyCmd := exec.Command("cp", "-rp", source, target)
  264. out, exitCode, err := runCommandWithOutput(copyCmd)
  265. if err != nil || exitCode != 0 {
  266. return fmt.Errorf("failed to copy: error: %q ,output: %q", err, out)
  267. }
  268. return nil
  269. }
  270. // randomUnixTmpDirPath provides a temporary unix path with rand string appended.
  271. // does not create or checks if it exists.
  272. func randomUnixTmpDirPath(s string) string {
  273. return path.Join("/tmp", fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
  274. }
  275. // Reads chunkSize bytes from reader after every interval.
  276. // Returns total read bytes.
  277. func consumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {
  278. buffer := make([]byte, chunkSize)
  279. for {
  280. select {
  281. case <-stop:
  282. return
  283. default:
  284. var readBytes int
  285. readBytes, err = reader.Read(buffer)
  286. n += readBytes
  287. if err != nil {
  288. if err == io.EOF {
  289. err = nil
  290. }
  291. return
  292. }
  293. time.Sleep(interval)
  294. }
  295. }
  296. }
  297. // Parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns
  298. // a map which cgroup name as key and path as value.
  299. func parseCgroupPaths(procCgroupData string) map[string]string {
  300. cgroupPaths := map[string]string{}
  301. for _, line := range strings.Split(procCgroupData, "\n") {
  302. parts := strings.Split(line, ":")
  303. if len(parts) != 3 {
  304. continue
  305. }
  306. cgroupPaths[parts[1]] = parts[2]
  307. }
  308. return cgroupPaths
  309. }