utils_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/docker/docker/pkg/testutil/cmd"
  10. )
  11. func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
  12. if testEnv.DaemonPlatform() == "windows" {
  13. return "c:", `\`
  14. }
  15. return "", "/"
  16. }
  17. // TODO: update code to call cmd.RunCmd directly, and remove this function
  18. // Deprecated: use pkg/testutil/cmd instead
  19. func runCommandWithOutput(execCmd *exec.Cmd) (string, int, error) {
  20. result := cmd.RunCmd(transformCmd(execCmd))
  21. return result.Combined(), result.ExitCode, result.Error
  22. }
  23. // Temporary shim for migrating commands to the new function
  24. func transformCmd(execCmd *exec.Cmd) cmd.Cmd {
  25. return cmd.Cmd{
  26. Command: execCmd.Args,
  27. Env: execCmd.Env,
  28. Dir: execCmd.Dir,
  29. Stdin: execCmd.Stdin,
  30. Stdout: execCmd.Stdout,
  31. }
  32. }
  33. // ParseCgroupPaths parses 'procCgroupData', which is output of '/proc/<pid>/cgroup', and returns
  34. // a map which cgroup name as key and path as value.
  35. func ParseCgroupPaths(procCgroupData string) map[string]string {
  36. cgroupPaths := map[string]string{}
  37. for _, line := range strings.Split(procCgroupData, "\n") {
  38. parts := strings.Split(line, ":")
  39. if len(parts) != 3 {
  40. continue
  41. }
  42. cgroupPaths[parts[1]] = parts[2]
  43. }
  44. return cgroupPaths
  45. }
  46. // RandomTmpDirPath provides a temporary path with rand string appended.
  47. // does not create or checks if it exists.
  48. func RandomTmpDirPath(s string, platform string) string {
  49. // TODO: why doesn't this use os.TempDir() ?
  50. tmp := "/tmp"
  51. if platform == "windows" {
  52. tmp = os.Getenv("TEMP")
  53. }
  54. path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
  55. if platform == "windows" {
  56. return filepath.FromSlash(path) // Using \
  57. }
  58. return filepath.ToSlash(path) // Using /
  59. }