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