github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/pkg/integration/dockerCmd_utils.go (about)

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