mirror of https://github.com/docker/docs.git
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:
parent
5d1cb9d005
commit
c6965e3e45
|
@ -106,27 +106,9 @@ func TestBuildAddSingleFileToWorkdir(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".")
|
_, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testaddimg", ".")
|
||||||
buildCmd.Dir = buildDirectory
|
if err != nil || exitCode != 0 {
|
||||||
done := make(chan error)
|
t.Fatalf("build failed: %s", err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteImages("testaddimg")
|
deleteImages("testaddimg")
|
||||||
|
@ -343,27 +325,9 @@ func TestBuildCopySingleFileToWorkdir(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".")
|
_, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testcopyimg", ".")
|
||||||
buildCmd.Dir = buildDirectory
|
if err != nil || exitCode != 0 {
|
||||||
done := make(chan error)
|
t.Fatalf("build failed: %s", err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteImages("testcopyimg")
|
deleteImages("testcopyimg")
|
||||||
|
|
|
@ -345,12 +345,34 @@ func dockerCmd(t *testing.T, args ...string) (string, int, error) {
|
||||||
return out, status, err
|
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
|
// execute a docker command in a directory
|
||||||
func dockerCmdInDir(t *testing.T, path string, args ...string) (string, int, error) {
|
func dockerCmdInDir(t *testing.T, path string, args ...string) (string, int, error) {
|
||||||
dockerCommand := exec.Command(dockerBinary, args...)
|
dockerCommand := exec.Command(dockerBinary, args...)
|
||||||
dockerCommand.Dir = path
|
dockerCommand.Dir = path
|
||||||
out, status, err := runCommandWithOutput(dockerCommand)
|
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
|
return out, status, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,31 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex
|
||||||
return
|
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) {
|
func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
|
||||||
exitCode = 0
|
exitCode = 0
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
|
|
Loading…
Reference in New Issue