dockerCmd_utils.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package integration
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/go-check/check"
  8. )
  9. var execCommand = exec.Command
  10. // DockerCmdWithError executes a docker command that is supposed to fail and returns
  11. // the output, the exit code and the error.
  12. func DockerCmdWithError(dockerBinary string, args ...string) (string, int, error) {
  13. return RunCommandWithOutput(execCommand(dockerBinary, args...))
  14. }
  15. // DockerCmdWithStdoutStderr executes a docker command and returns the content of the
  16. // stdout, stderr and the exit code. If a check.C is passed, it will fail and stop tests
  17. // if the error is not nil.
  18. func DockerCmdWithStdoutStderr(dockerBinary string, c *check.C, args ...string) (string, string, int) {
  19. stdout, stderr, status, err := RunCommandWithStdoutStderr(execCommand(dockerBinary, args...))
  20. if c != nil {
  21. c.Assert(err, check.IsNil, check.Commentf("%q failed with errors: %s, %v", strings.Join(args, " "), stderr, err))
  22. }
  23. return stdout, stderr, status
  24. }
  25. // DockerCmd executes a docker command and returns the output and the exit code. If the
  26. // command returns an error, it will fail and stop the tests.
  27. func DockerCmd(dockerBinary string, c *check.C, args ...string) (string, int) {
  28. out, status, err := RunCommandWithOutput(execCommand(dockerBinary, args...))
  29. c.Assert(err, check.IsNil, check.Commentf("%q failed with errors: %s, %v", strings.Join(args, " "), out, err))
  30. return out, status
  31. }
  32. // DockerCmdWithTimeout executes a docker command with a timeout, and returns the output,
  33. // the exit code and the error (if any).
  34. func DockerCmdWithTimeout(dockerBinary string, timeout time.Duration, args ...string) (string, int, error) {
  35. out, status, err := RunCommandWithOutputAndTimeout(execCommand(dockerBinary, args...), timeout)
  36. if err != nil {
  37. return out, status, fmt.Errorf("%q failed with errors: %v : %q", strings.Join(args, " "), err, out)
  38. }
  39. return out, status, err
  40. }
  41. // DockerCmdInDir executes a docker command in a directory and returns the output, the
  42. // exit code and the error (if any).
  43. func DockerCmdInDir(dockerBinary string, path string, args ...string) (string, int, error) {
  44. dockerCommand := execCommand(dockerBinary, args...)
  45. dockerCommand.Dir = path
  46. out, status, err := RunCommandWithOutput(dockerCommand)
  47. if err != nil {
  48. return out, status, fmt.Errorf("%q failed with errors: %v : %q", strings.Join(args, " "), err, out)
  49. }
  50. return out, status, err
  51. }
  52. // DockerCmdInDirWithTimeout executes a docker command in a directory with a timeout and
  53. // returns the output, the exit code and the error (if any).
  54. func DockerCmdInDirWithTimeout(dockerBinary string, timeout time.Duration, path string, args ...string) (string, int, error) {
  55. dockerCommand := execCommand(dockerBinary, args...)
  56. dockerCommand.Dir = path
  57. out, status, err := RunCommandWithOutputAndTimeout(dockerCommand, timeout)
  58. if err != nil {
  59. return out, status, fmt.Errorf("%q failed with errors: %v : %q", strings.Join(args, " "), err, out)
  60. }
  61. return out, status, err
  62. }