utils_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "github.com/docker/docker/pkg/stringutils"
  9. "github.com/gotestyourself/gotestyourself/icmd"
  10. "github.com/pkg/errors"
  11. )
  12. func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
  13. if testEnv.DaemonPlatform() == "windows" {
  14. return "c:", `\`
  15. }
  16. return "", "/"
  17. }
  18. // TODO: update code to call cmd.RunCmd directly, and remove this function
  19. // Deprecated: use gotestyourself/gotestyourself/icmd
  20. func runCommandWithOutput(execCmd *exec.Cmd) (string, int, error) {
  21. result := icmd.RunCmd(transformCmd(execCmd))
  22. return result.Combined(), result.ExitCode, result.Error
  23. }
  24. // Temporary shim for migrating commands to the new function
  25. func transformCmd(execCmd *exec.Cmd) icmd.Cmd {
  26. return icmd.Cmd{
  27. Command: execCmd.Args,
  28. Env: execCmd.Env,
  29. Dir: execCmd.Dir,
  30. Stdin: execCmd.Stdin,
  31. Stdout: execCmd.Stdout,
  32. }
  33. }
  34. // ParseCgroupPaths parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns
  35. // a map which cgroup name as key and path as value.
  36. func ParseCgroupPaths(procCgroupData string) map[string]string {
  37. cgroupPaths := map[string]string{}
  38. for _, line := range strings.Split(procCgroupData, "\n") {
  39. parts := strings.Split(line, ":")
  40. if len(parts) != 3 {
  41. continue
  42. }
  43. cgroupPaths[parts[1]] = parts[2]
  44. }
  45. return cgroupPaths
  46. }
  47. // RandomTmpDirPath provides a temporary path with rand string appended.
  48. // does not create or checks if it exists.
  49. func RandomTmpDirPath(s string, platform string) string {
  50. // TODO: why doesn't this use os.TempDir() ?
  51. tmp := "/tmp"
  52. if platform == "windows" {
  53. tmp = os.Getenv("TEMP")
  54. }
  55. path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
  56. if platform == "windows" {
  57. return filepath.FromSlash(path) // Using \
  58. }
  59. return filepath.ToSlash(path) // Using /
  60. }
  61. // RunCommandPipelineWithOutput runs the array of commands with the output
  62. // of each pipelined with the following (like cmd1 | cmd2 | cmd3 would do).
  63. // It returns the final output, the exitCode different from 0 and the error
  64. // if something bad happened.
  65. // Deprecated: use icmd instead
  66. func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error) {
  67. if len(cmds) < 2 {
  68. return "", errors.New("pipeline does not have multiple cmds")
  69. }
  70. // connect stdin of each cmd to stdout pipe of previous cmd
  71. for i, cmd := range cmds {
  72. if i > 0 {
  73. prevCmd := cmds[i-1]
  74. cmd.Stdin, err = prevCmd.StdoutPipe()
  75. if err != nil {
  76. return "", fmt.Errorf("cannot set stdout pipe for %s: %v", cmd.Path, err)
  77. }
  78. }
  79. }
  80. // start all cmds except the last
  81. for _, cmd := range cmds[:len(cmds)-1] {
  82. if err = cmd.Start(); err != nil {
  83. return "", fmt.Errorf("starting %s failed with error: %v", cmd.Path, err)
  84. }
  85. }
  86. defer func() {
  87. var pipeErrMsgs []string
  88. // wait all cmds except the last to release their resources
  89. for _, cmd := range cmds[:len(cmds)-1] {
  90. if pipeErr := cmd.Wait(); pipeErr != nil {
  91. pipeErrMsgs = append(pipeErrMsgs, fmt.Sprintf("command %s failed with error: %v", cmd.Path, pipeErr))
  92. }
  93. }
  94. if len(pipeErrMsgs) > 0 && err == nil {
  95. err = fmt.Errorf("pipelineError from Wait: %v", strings.Join(pipeErrMsgs, ", "))
  96. }
  97. }()
  98. // wait on last cmd
  99. out, err := cmds[len(cmds)-1].CombinedOutput()
  100. return string(out), err
  101. }