1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

integcli: add & use dockerCmdWithTimeout & InDir

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-08-13 19:02:04 +03:00
parent 5d1cb9d005
commit c6965e3e45
3 changed files with 54 additions and 43 deletions

View file

@ -106,27 +106,9 @@ func TestBuildAddSingleFileToWorkdir(t *testing.T) {
t.Fatal(err)
}
f.Close()
buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
buildCmd.Dir = buildDirectory
done := make(chan error)
go func() {
out, exitCode, err := runCommandWithOutput(buildCmd)
if err != nil || exitCode != 0 {
done <- fmt.Errorf("build failed to complete: %s %v", out, err)
return
}
done <- nil
}()
select {
case <-time.After(5 * time.Second):
if err := buildCmd.Process.Kill(); err != nil {
fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
}
t.Fatal("build timed out")
case err := <-done:
if err != nil {
t.Fatal(err)
}
_, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testaddimg", ".")
if err != nil || exitCode != 0 {
t.Fatalf("build failed: %s", err)
}
deleteImages("testaddimg")
@ -343,27 +325,9 @@ func TestBuildCopySingleFileToWorkdir(t *testing.T) {
t.Fatal(err)
}
f.Close()
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
buildCmd.Dir = buildDirectory
done := make(chan error)
go func() {
out, exitCode, err := runCommandWithOutput(buildCmd)
if err != nil || exitCode != 0 {
done <- fmt.Errorf("build failed to complete: %s %v", out, err)
return
}
done <- nil
}()
select {
case <-time.After(5 * time.Second):
if err := buildCmd.Process.Kill(); err != nil {
fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
}
t.Fatal("build timed out")
case err := <-done:
if err != nil {
t.Fatal(err)
}
_, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testcopyimg", ".")
if err != nil || exitCode != 0 {
t.Fatalf("build failed: %s", err)
}
deleteImages("testcopyimg")

View file

@ -345,12 +345,34 @@ func dockerCmd(t *testing.T, args ...string) (string, int, error) {
return out, status, err
}
// execute a docker ocmmand with a timeout
func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
out, status, err := runCommandWithOutputAndTimeout(exec.Command(dockerBinary, args...), timeout)
if err != nil {
return out, status, fmt.Errorf("'%s' failed with errors: %v : %q)", strings.Join(args, " "), err, out)
}
return out, status, err
}
// execute a docker command in a directory
func dockerCmdInDir(t *testing.T, path string, args ...string) (string, int, error) {
dockerCommand := exec.Command(dockerBinary, args...)
dockerCommand.Dir = path
out, status, err := runCommandWithOutput(dockerCommand)
errorOut(err, t, fmt.Sprintf("'%s' failed with errors: %v (%v)", strings.Join(args, " "), err, out))
if err != nil {
return out, status, fmt.Errorf("'%s' failed with errors: %v : %q)", strings.Join(args, " "), err, out)
}
return out, status, err
}
// execute a docker command in a directory with a timeout
func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
dockerCommand := exec.Command(dockerBinary, args...)
dockerCommand.Dir = path
out, status, err := runCommandWithOutputAndTimeout(dockerCommand, timeout)
if err != nil {
return out, status, fmt.Errorf("'%s' failed with errors: %v : %q)", strings.Join(args, " "), err, out)
}
return out, status, err
}

View file

@ -63,6 +63,31 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex
return
}
var ErrCmdTimeout = fmt.Errorf("command timed out")
func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) {
done := make(chan error)
go func() {
output, exitCode, err = runCommandWithOutput(cmd)
if err != nil || exitCode != 0 {
done <- fmt.Errorf("failed to run command: %s", err)
return
}
done <- nil
}()
select {
case <-time.After(timeout):
killFailed := cmd.Process.Kill()
if killFailed == nil {
fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, err)
}
err = ErrCmdTimeout
case <-done:
break
}
return
}
func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
exitCode = 0
err = cmd.Run()