Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
| ... | ... |
@@ -106,27 +106,9 @@ func TestBuildAddSingleFileToWorkdir(t *testing.T) {
|
| 106 | 106 |
t.Fatal(err) |
| 107 | 107 |
} |
| 108 | 108 |
f.Close() |
| 109 |
- buildCmd := exec.Command(dockerBinary, "build", "-t", "testaddimg", ".") |
|
| 110 |
- buildCmd.Dir = buildDirectory |
|
| 111 |
- done := make(chan error) |
|
| 112 |
- go func() {
|
|
| 113 |
- out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 114 |
- if err != nil || exitCode != 0 {
|
|
| 115 |
- done <- fmt.Errorf("build failed to complete: %s %v", out, err)
|
|
| 116 |
- return |
|
| 117 |
- } |
|
| 118 |
- done <- nil |
|
| 119 |
- }() |
|
| 120 |
- select {
|
|
| 121 |
- case <-time.After(5 * time.Second): |
|
| 122 |
- if err := buildCmd.Process.Kill(); err != nil {
|
|
| 123 |
- fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
|
|
| 124 |
- } |
|
| 125 |
- t.Fatal("build timed out")
|
|
| 126 |
- case err := <-done: |
|
| 127 |
- if err != nil {
|
|
| 128 |
- t.Fatal(err) |
|
| 129 |
- } |
|
| 109 |
+ _, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testaddimg", ".") |
|
| 110 |
+ if err != nil || exitCode != 0 {
|
|
| 111 |
+ t.Fatalf("build failed: %s", err)
|
|
| 130 | 112 |
} |
| 131 | 113 |
|
| 132 | 114 |
deleteImages("testaddimg")
|
| ... | ... |
@@ -343,27 +325,9 @@ func TestBuildCopySingleFileToWorkdir(t *testing.T) {
|
| 343 | 343 |
t.Fatal(err) |
| 344 | 344 |
} |
| 345 | 345 |
f.Close() |
| 346 |
- buildCmd := exec.Command(dockerBinary, "build", "-t", "testcopyimg", ".") |
|
| 347 |
- buildCmd.Dir = buildDirectory |
|
| 348 |
- done := make(chan error) |
|
| 349 |
- go func() {
|
|
| 350 |
- out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 351 |
- if err != nil || exitCode != 0 {
|
|
| 352 |
- done <- fmt.Errorf("build failed to complete: %s %v", out, err)
|
|
| 353 |
- return |
|
| 354 |
- } |
|
| 355 |
- done <- nil |
|
| 356 |
- }() |
|
| 357 |
- select {
|
|
| 358 |
- case <-time.After(5 * time.Second): |
|
| 359 |
- if err := buildCmd.Process.Kill(); err != nil {
|
|
| 360 |
- fmt.Printf("could not kill build (pid=%d): %v\n", buildCmd.Process.Pid, err)
|
|
| 361 |
- } |
|
| 362 |
- t.Fatal("build timed out")
|
|
| 363 |
- case err := <-done: |
|
| 364 |
- if err != nil {
|
|
| 365 |
- t.Fatal(err) |
|
| 366 |
- } |
|
| 346 |
+ _, exitCode, err := dockerCmdInDirWithTimeout(5*time.Second, buildDirectory, "build", "-t", "testcopyimg", ".") |
|
| 347 |
+ if err != nil || exitCode != 0 {
|
|
| 348 |
+ t.Fatalf("build failed: %s", err)
|
|
| 367 | 349 |
} |
| 368 | 350 |
|
| 369 | 351 |
deleteImages("testcopyimg")
|
| ... | ... |
@@ -345,12 +345,34 @@ func dockerCmd(t *testing.T, args ...string) (string, int, error) {
|
| 345 | 345 |
return out, status, err |
| 346 | 346 |
} |
| 347 | 347 |
|
| 348 |
+// execute a docker ocmmand with a timeout |
|
| 349 |
+func dockerCmdWithTimeout(timeout time.Duration, args ...string) (string, int, error) {
|
|
| 350 |
+ out, status, err := runCommandWithOutputAndTimeout(exec.Command(dockerBinary, args...), timeout) |
|
| 351 |
+ if err != nil {
|
|
| 352 |
+ return out, status, fmt.Errorf("'%s' failed with errors: %v : %q)", strings.Join(args, " "), err, out)
|
|
| 353 |
+ } |
|
| 354 |
+ return out, status, err |
|
| 355 |
+} |
|
| 356 |
+ |
|
| 348 | 357 |
// execute a docker command in a directory |
| 349 | 358 |
func dockerCmdInDir(t *testing.T, path string, args ...string) (string, int, error) {
|
| 350 | 359 |
dockerCommand := exec.Command(dockerBinary, args...) |
| 351 | 360 |
dockerCommand.Dir = path |
| 352 | 361 |
out, status, err := runCommandWithOutput(dockerCommand) |
| 353 |
- errorOut(err, t, fmt.Sprintf("'%s' failed with errors: %v (%v)", strings.Join(args, " "), err, out))
|
|
| 362 |
+ if err != nil {
|
|
| 363 |
+ return out, status, fmt.Errorf("'%s' failed with errors: %v : %q)", strings.Join(args, " "), err, out)
|
|
| 364 |
+ } |
|
| 365 |
+ return out, status, err |
|
| 366 |
+} |
|
| 367 |
+ |
|
| 368 |
+// execute a docker command in a directory with a timeout |
|
| 369 |
+func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) (string, int, error) {
|
|
| 370 |
+ dockerCommand := exec.Command(dockerBinary, args...) |
|
| 371 |
+ dockerCommand.Dir = path |
|
| 372 |
+ out, status, err := runCommandWithOutputAndTimeout(dockerCommand, timeout) |
|
| 373 |
+ if err != nil {
|
|
| 374 |
+ return out, status, fmt.Errorf("'%s' failed with errors: %v : %q)", strings.Join(args, " "), err, out)
|
|
| 375 |
+ } |
|
| 354 | 376 |
return out, status, err |
| 355 | 377 |
} |
| 356 | 378 |
|
| ... | ... |
@@ -63,6 +63,31 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex |
| 63 | 63 |
return |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 |
+var ErrCmdTimeout = fmt.Errorf("command timed out")
|
|
| 67 |
+ |
|
| 68 |
+func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) {
|
|
| 69 |
+ done := make(chan error) |
|
| 70 |
+ go func() {
|
|
| 71 |
+ output, exitCode, err = runCommandWithOutput(cmd) |
|
| 72 |
+ if err != nil || exitCode != 0 {
|
|
| 73 |
+ done <- fmt.Errorf("failed to run command: %s", err)
|
|
| 74 |
+ return |
|
| 75 |
+ } |
|
| 76 |
+ done <- nil |
|
| 77 |
+ }() |
|
| 78 |
+ select {
|
|
| 79 |
+ case <-time.After(timeout): |
|
| 80 |
+ killFailed := cmd.Process.Kill() |
|
| 81 |
+ if killFailed == nil {
|
|
| 82 |
+ fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, err)
|
|
| 83 |
+ } |
|
| 84 |
+ err = ErrCmdTimeout |
|
| 85 |
+ case <-done: |
|
| 86 |
+ break |
|
| 87 |
+ } |
|
| 88 |
+ return |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 66 | 91 |
func runCommand(cmd *exec.Cmd) (exitCode int, err error) {
|
| 67 | 92 |
exitCode = 0 |
| 68 | 93 |
err = cmd.Run() |