dockerCmd_utils.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package integration
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/go-check/check"
  8. )
  9. // We use the elongated quote mechanism for quoting error returns as
  10. // the use of strconv.Quote or %q in fmt.Errorf will escape characters. This
  11. // has a big downside on Windows where the args include paths, so instead
  12. // of something like c:\directory\file.txt, the output would be
  13. // c:\\directory\\file.txt. This is highly misleading.
  14. const quote = `"`
  15. var execCommand = exec.Command
  16. // DockerCmdWithError executes a docker command that is supposed to fail and returns
  17. // the output, the exit code and the error.
  18. func DockerCmdWithError(dockerBinary string, args ...string) (string, int, error) {
  19. return RunCommandWithOutput(execCommand(dockerBinary, args...))
  20. }
  21. // DockerCmdWithStdoutStderr executes a docker command and returns the content of the
  22. // stdout, stderr and the exit code. If a check.C is passed, it will fail and stop tests
  23. // if the error is not nil.
  24. func DockerCmdWithStdoutStderr(dockerBinary string, c *check.C, args ...string) (string, string, int) {
  25. stdout, stderr, status, err := RunCommandWithStdoutStderr(execCommand(dockerBinary, args...))
  26. if c != nil {
  27. c.Assert(err, check.IsNil, check.Commentf(quote+"%v"+quote+" failed with errors: %s, %v", strings.Join(args, " "), stderr, err))
  28. }
  29. return stdout, stderr, status
  30. }
  31. // DockerCmd executes a docker command and returns the output and the exit code. If the
  32. // command returns an error, it will fail and stop the tests.
  33. func DockerCmd(dockerBinary string, c *check.C, args ...string) (string, int) {
  34. out, status, err := RunCommandWithOutput(execCommand(dockerBinary, args...))
  35. c.Assert(err, check.IsNil, check.Commentf(quote+"%v"+quote+" failed with errors: %s, %v", strings.Join(args, " "), out, err))
  36. return out, status
  37. }
  38. // DockerCmdWithTimeout executes a docker command with a timeout, and returns the output,
  39. // the exit code and the error (if any).
  40. func DockerCmdWithTimeout(dockerBinary string, timeout time.Duration, args ...string) (string, int, error) {
  41. out, status, err := RunCommandWithOutputAndTimeout(execCommand(dockerBinary, args...), timeout)
  42. if err != nil {
  43. return out, status, fmt.Errorf(quote+"%v"+quote+" failed with errors: %v : %q", strings.Join(args, " "), err, out)
  44. }
  45. return out, status, err
  46. }
  47. // DockerCmdInDir executes a docker command in a directory and returns the output, the
  48. // exit code and the error (if any).
  49. func DockerCmdInDir(dockerBinary string, path string, args ...string) (string, int, error) {
  50. dockerCommand := execCommand(dockerBinary, args...)
  51. dockerCommand.Dir = path
  52. out, status, err := RunCommandWithOutput(dockerCommand)
  53. if err != nil {
  54. return out, status, fmt.Errorf(quote+"%v"+quote+" failed with errors: %v : %q", strings.Join(args, " "), err, out)
  55. }
  56. return out, status, err
  57. }
  58. // DockerCmdInDirWithTimeout executes a docker command in a directory with a timeout and
  59. // returns the output, the exit code and the error (if any).
  60. func DockerCmdInDirWithTimeout(dockerBinary string, timeout time.Duration, path string, args ...string) (string, int, error) {
  61. dockerCommand := execCommand(dockerBinary, args...)
  62. dockerCommand.Dir = path
  63. out, status, err := RunCommandWithOutputAndTimeout(dockerCommand, timeout)
  64. if err != nil {
  65. return out, status, fmt.Errorf(quote+"%v"+quote+" failed with errors: %v : %q", strings.Join(args, " "), err, out)
  66. }
  67. return out, status, err
  68. }