Files
docker-cli/components/engine/pkg/testutil/utils.go
T
Daniel Nephin e13afe9c59 Remove RandomTmpDirPath
Signed-off-by: Daniel Nephin <dnephin@docker.com>
Upstream-commit: 9db68f4dea6379b7e85ee5c6f2be080a6fcbe4ca
Component: engine
2017-08-22 17:15:26 -04:00

63 lines
1.7 KiB
Go

package testutil
import (
"errors"
"fmt"
"os/exec"
"strings"
"github.com/docker/docker/pkg/system"
)
func runCommandWithOutput(cmd *exec.Cmd) (output string, exitCode int, err error) {
out, err := cmd.CombinedOutput()
exitCode = system.ProcessExitCode(err)
output = string(out)
return
}
// RunCommandPipelineWithOutput runs the array of commands with the output
// of each pipelined with the following (like cmd1 | cmd2 | cmd3 would do).
// It returns the final output, the exitCode different from 0 and the error
// if something bad happened.
func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, exitCode int, err error) {
if len(cmds) < 2 {
return "", 0, errors.New("pipeline does not have multiple cmds")
}
// connect stdin of each cmd to stdout pipe of previous cmd
for i, cmd := range cmds {
if i > 0 {
prevCmd := cmds[i-1]
cmd.Stdin, err = prevCmd.StdoutPipe()
if err != nil {
return "", 0, fmt.Errorf("cannot set stdout pipe for %s: %v", cmd.Path, err)
}
}
}
// start all cmds except the last
for _, cmd := range cmds[:len(cmds)-1] {
if err = cmd.Start(); err != nil {
return "", 0, fmt.Errorf("starting %s failed with error: %v", cmd.Path, err)
}
}
defer func() {
var pipeErrMsgs []string
// wait all cmds except the last to release their resources
for _, cmd := range cmds[:len(cmds)-1] {
if pipeErr := cmd.Wait(); pipeErr != nil {
pipeErrMsgs = append(pipeErrMsgs, fmt.Sprintf("command %s failed with error: %v", cmd.Path, pipeErr))
}
}
if len(pipeErrMsgs) > 0 && err == nil {
err = fmt.Errorf("pipelineError from Wait: %v", strings.Join(pipeErrMsgs, ", "))
}
}()
// wait on last cmd
return runCommandWithOutput(cmds[len(cmds)-1])
}