integration-cli: Remove timeout dependency on TestEventsUntag
Jessie Frazelle authored on 2015/03/04 20:54:33... | ... |
@@ -16,8 +16,11 @@ func TestEventsUntag(t *testing.T) { |
16 | 16 |
dockerCmd(t, "tag", image, "utest:tag2") |
17 | 17 |
dockerCmd(t, "rmi", "utest:tag1") |
18 | 18 |
dockerCmd(t, "rmi", "utest:tag2") |
19 |
- eventsCmd := exec.Command("timeout", "0.2", dockerBinary, "events", "--since=1") |
|
20 |
- out, _, _ := runCommandWithOutput(eventsCmd) |
|
19 |
+ eventsCmd := exec.Command(dockerBinary, "events", "--since=1") |
|
20 |
+ out, exitCode, _, err := runCommandWithOutputForDuration(eventsCmd, time.Duration(time.Millisecond*200)) |
|
21 |
+ if exitCode != 0 || err != nil { |
|
22 |
+ t.Fatalf("Failed to get events - exit code %d: %s", exitCode, err) |
|
23 |
+ } |
|
21 | 24 |
events := strings.Split(out, "\n") |
22 | 25 |
nEvents := len(events) |
23 | 26 |
// The last element after the split above will be an empty string, so we |
... | ... |
@@ -65,27 +65,49 @@ func runCommandWithStdoutStderr(cmd *exec.Cmd) (stdout string, stderr string, ex |
65 | 65 |
return |
66 | 66 |
} |
67 | 67 |
|
68 |
-var ErrCmdTimeout = fmt.Errorf("command timed out") |
|
68 |
+func runCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (output string, exitCode int, timedOut bool, err error) { |
|
69 |
+ var outputBuffer bytes.Buffer |
|
70 |
+ if cmd.Stdout != nil { |
|
71 |
+ err = errors.New("cmd.Stdout already set") |
|
72 |
+ return |
|
73 |
+ } |
|
74 |
+ cmd.Stdout = &outputBuffer |
|
75 |
+ |
|
76 |
+ if cmd.Stderr != nil { |
|
77 |
+ err = errors.New("cmd.Stderr already set") |
|
78 |
+ return |
|
79 |
+ } |
|
80 |
+ cmd.Stderr = &outputBuffer |
|
69 | 81 |
|
70 |
-func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) { |
|
71 | 82 |
done := make(chan error) |
72 | 83 |
go func() { |
73 |
- output, exitCode, err = runCommandWithOutput(cmd) |
|
74 |
- if err != nil || exitCode != 0 { |
|
75 |
- done <- fmt.Errorf("failed to run command: %s", err) |
|
76 |
- return |
|
77 |
- } |
|
78 |
- done <- nil |
|
84 |
+ exitErr := cmd.Run() |
|
85 |
+ exitCode = processExitCode(exitErr) |
|
86 |
+ done <- exitErr |
|
79 | 87 |
}() |
88 |
+ |
|
80 | 89 |
select { |
81 |
- case <-time.After(timeout): |
|
82 |
- killFailed := cmd.Process.Kill() |
|
83 |
- if killFailed == nil { |
|
84 |
- fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, err) |
|
90 |
+ case <-time.After(duration): |
|
91 |
+ killErr := cmd.Process.Kill() |
|
92 |
+ if killErr != nil { |
|
93 |
+ fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, killErr) |
|
85 | 94 |
} |
86 |
- err = ErrCmdTimeout |
|
87 |
- case <-done: |
|
95 |
+ timedOut = true |
|
88 | 96 |
break |
97 |
+ case err = <-done: |
|
98 |
+ break |
|
99 |
+ } |
|
100 |
+ output = outputBuffer.String() |
|
101 |
+ return |
|
102 |
+} |
|
103 |
+ |
|
104 |
+var ErrCmdTimeout = fmt.Errorf("command timed out") |
|
105 |
+ |
|
106 |
+func runCommandWithOutputAndTimeout(cmd *exec.Cmd, timeout time.Duration) (output string, exitCode int, err error) { |
|
107 |
+ var timedOut bool |
|
108 |
+ output, exitCode, timedOut, err = runCommandWithOutputForDuration(cmd, timeout) |
|
109 |
+ if timedOut { |
|
110 |
+ err = ErrCmdTimeout |
|
89 | 111 |
} |
90 | 112 |
return |
91 | 113 |
} |