... | ... |
@@ -2,11 +2,10 @@ package docker |
2 | 2 |
|
3 | 3 |
import ( |
4 | 4 |
"bufio" |
5 |
- _ "bytes" |
|
6 | 5 |
"fmt" |
7 | 6 |
"github.com/dotcloud/docker/rcli" |
8 | 7 |
"io" |
9 |
- _ "io/ioutil" |
|
8 |
+ "io/ioutil" |
|
10 | 9 |
"strings" |
11 | 10 |
"testing" |
12 | 11 |
"time" |
... | ... |
@@ -62,23 +61,35 @@ func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error |
62 | 62 |
|
63 | 63 |
// TestRunHostname checks that 'docker run -h' correctly sets a custom hostname |
64 | 64 |
func TestRunHostname(t *testing.T) { |
65 |
- // runtime, err := newTestRuntime() |
|
66 |
- // if err != nil { |
|
67 |
- // t.Fatal(err) |
|
68 |
- // } |
|
69 |
- // defer nuke(runtime) |
|
70 |
- |
|
71 |
- // srv := &Server{runtime: runtime} |
|
72 |
- |
|
73 |
- // var stdin, stdout bytes.Buffer |
|
74 |
- // setTimeout(t, "CmdRun timed out", 2*time.Second, func() { |
|
75 |
- // if err := srv.CmdRun(ioutil.NopCloser(&stdin), &nopWriteCloser{&stdout}, "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil { |
|
76 |
- // t.Fatal(err) |
|
77 |
- // } |
|
78 |
- // }) |
|
79 |
- // if output := string(stdout.Bytes()); output != "foobar\n" { |
|
80 |
- // t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", output) |
|
81 |
- // } |
|
65 |
+ runtime, err := newTestRuntime() |
|
66 |
+ if err != nil { |
|
67 |
+ t.Fatal(err) |
|
68 |
+ } |
|
69 |
+ defer nuke(runtime) |
|
70 |
+ |
|
71 |
+ srv := &Server{runtime: runtime} |
|
72 |
+ |
|
73 |
+ stdin, _ := io.Pipe() |
|
74 |
+ stdout, stdoutPipe := io.Pipe() |
|
75 |
+ |
|
76 |
+ c := make(chan struct{}) |
|
77 |
+ go func() { |
|
78 |
+ if err := srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil { |
|
79 |
+ t.Fatal(err) |
|
80 |
+ } |
|
81 |
+ close(c) |
|
82 |
+ }() |
|
83 |
+ cmdOutput, err := bufio.NewReader(stdout).ReadString('\n') |
|
84 |
+ if err != nil { |
|
85 |
+ t.Fatal(err) |
|
86 |
+ } |
|
87 |
+ if cmdOutput != "foobar\n" { |
|
88 |
+ t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", cmdOutput) |
|
89 |
+ } |
|
90 |
+ |
|
91 |
+ setTimeout(t, "CmdRun timed out", 2*time.Second, func() { |
|
92 |
+ <-c |
|
93 |
+ }) |
|
82 | 94 |
} |
83 | 95 |
|
84 | 96 |
func TestRunExit(t *testing.T) { |
... | ... |
@@ -183,56 +194,66 @@ func TestRunDisconnect(t *testing.T) { |
183 | 183 |
// TestAttachStdin checks attaching to stdin without stdout and stderr. |
184 | 184 |
// 'docker run -i -a stdin' should sends the client's stdin to the command, |
185 | 185 |
// then detach from it and print the container id. |
186 |
-func TestAttachStdin(t *testing.T) { |
|
187 |
- // runtime, err := newTestRuntime() |
|
188 |
- // if err != nil { |
|
189 |
- // t.Fatal(err) |
|
190 |
- // } |
|
191 |
- // defer nuke(runtime) |
|
192 |
- // srv := &Server{runtime: runtime} |
|
193 |
- |
|
194 |
- // stdinR, stdinW := io.Pipe() |
|
195 |
- // var stdout bytes.Buffer |
|
196 |
- |
|
197 |
- // ch := make(chan struct{}) |
|
198 |
- // go func() { |
|
199 |
- // srv.CmdRun(stdinR, &stdout, "-i", "-a", "stdin", GetTestImage(runtime).Id, "sh", "-c", "echo hello; cat") |
|
200 |
- // close(ch) |
|
201 |
- // }() |
|
202 |
- |
|
203 |
- // // Send input to the command, close stdin, wait for CmdRun to return |
|
204 |
- // setTimeout(t, "Read/Write timed out", 2*time.Second, func() { |
|
205 |
- // if _, err := stdinW.Write([]byte("hi there\n")); err != nil { |
|
206 |
- // t.Fatal(err) |
|
207 |
- // } |
|
208 |
- // stdinW.Close() |
|
209 |
- // <-ch |
|
210 |
- // }) |
|
211 |
- |
|
212 |
- // // Check output |
|
213 |
- // cmdOutput := string(stdout.Bytes()) |
|
214 |
- // container := runtime.List()[0] |
|
215 |
- // if cmdOutput != container.ShortId()+"\n" { |
|
216 |
- // t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortId()+"\n", cmdOutput) |
|
217 |
- // } |
|
218 |
- |
|
219 |
- // setTimeout(t, "Waiting for command to exit timed out", 2*time.Second, func() { |
|
220 |
- // container.Wait() |
|
221 |
- // }) |
|
222 |
- |
|
223 |
- // // Check logs |
|
224 |
- // if cmdLogs, err := container.ReadLog("stdout"); err != nil { |
|
225 |
- // t.Fatal(err) |
|
226 |
- // } else { |
|
227 |
- // if output, err := ioutil.ReadAll(cmdLogs); err != nil { |
|
228 |
- // t.Fatal(err) |
|
229 |
- // } else { |
|
230 |
- // expectedLog := "hello\nhi there\n" |
|
231 |
- // if string(output) != expectedLog { |
|
232 |
- // t.Fatalf("Unexpected logs: should be '%s', not '%s'\n", expectedLog, output) |
|
233 |
- // } |
|
234 |
- // } |
|
235 |
- // } |
|
186 |
+func TestRunAttachStdin(t *testing.T) { |
|
187 |
+ runtime, err := newTestRuntime() |
|
188 |
+ if err != nil { |
|
189 |
+ t.Fatal(err) |
|
190 |
+ } |
|
191 |
+ defer nuke(runtime) |
|
192 |
+ srv := &Server{runtime: runtime} |
|
193 |
+ |
|
194 |
+ stdin, stdinPipe := io.Pipe() |
|
195 |
+ stdout, stdoutPipe := io.Pipe() |
|
196 |
+ |
|
197 |
+ ch := make(chan struct{}) |
|
198 |
+ go func() { |
|
199 |
+ srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", "-a", "stdin", GetTestImage(runtime).Id, "sh", "-c", "echo hello; cat") |
|
200 |
+ close(ch) |
|
201 |
+ }() |
|
202 |
+ |
|
203 |
+ // Send input to the command, close stdin |
|
204 |
+ setTimeout(t, "Write timed out", 2*time.Second, func() { |
|
205 |
+ if _, err := stdinPipe.Write([]byte("hi there\n")); err != nil { |
|
206 |
+ t.Fatal(err) |
|
207 |
+ } |
|
208 |
+ if err := stdinPipe.Close(); err != nil { |
|
209 |
+ t.Fatal(err) |
|
210 |
+ } |
|
211 |
+ }) |
|
212 |
+ |
|
213 |
+ container := runtime.List()[0] |
|
214 |
+ |
|
215 |
+ // Check output |
|
216 |
+ cmdOutput, err := bufio.NewReader(stdout).ReadString('\n') |
|
217 |
+ if err != nil { |
|
218 |
+ t.Fatal(err) |
|
219 |
+ } |
|
220 |
+ if cmdOutput != container.ShortId()+"\n" { |
|
221 |
+ t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortId()+"\n", cmdOutput) |
|
222 |
+ } |
|
223 |
+ |
|
224 |
+ // wait for CmdRun to return |
|
225 |
+ setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() { |
|
226 |
+ <-ch |
|
227 |
+ }) |
|
228 |
+ |
|
229 |
+ setTimeout(t, "Waiting for command to exit timed out", 2*time.Second, func() { |
|
230 |
+ container.Wait() |
|
231 |
+ }) |
|
232 |
+ |
|
233 |
+ // Check logs |
|
234 |
+ if cmdLogs, err := container.ReadLog("stdout"); err != nil { |
|
235 |
+ t.Fatal(err) |
|
236 |
+ } else { |
|
237 |
+ if output, err := ioutil.ReadAll(cmdLogs); err != nil { |
|
238 |
+ t.Fatal(err) |
|
239 |
+ } else { |
|
240 |
+ expectedLog := "hello\nhi there\n" |
|
241 |
+ if string(output) != expectedLog { |
|
242 |
+ t.Fatalf("Unexpected logs: should be '%s', not '%s'\n", expectedLog, output) |
|
243 |
+ } |
|
244 |
+ } |
|
245 |
+ } |
|
236 | 246 |
} |
237 | 247 |
|
238 | 248 |
// Expected behaviour, the process stays alive when the client disconnects |