commands_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package docker
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func closeWrap(args ...io.Closer) error {
  13. e := false
  14. ret := fmt.Errorf("Error closing elements")
  15. for _, c := range args {
  16. if err := c.Close(); err != nil {
  17. e = true
  18. ret = fmt.Errorf("%s\n%s", ret, err)
  19. }
  20. }
  21. if e {
  22. return ret
  23. }
  24. return nil
  25. }
  26. func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
  27. c := make(chan bool)
  28. // Make sure we are not too long
  29. go func() {
  30. time.Sleep(d)
  31. c <- true
  32. }()
  33. go func() {
  34. f()
  35. c <- false
  36. }()
  37. if <-c {
  38. t.Fatal(msg)
  39. }
  40. }
  41. func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
  42. for i := 0; i < count; i++ {
  43. if _, err := w.Write([]byte(input)); err != nil {
  44. return err
  45. }
  46. o, err := bufio.NewReader(r).ReadString('\n')
  47. if err != nil {
  48. return err
  49. }
  50. if strings.Trim(o, " \r\n") != output {
  51. return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", output, o)
  52. }
  53. }
  54. return nil
  55. }
  56. // TestRunHostname checks that 'docker run -h' correctly sets a custom hostname
  57. func TestRunHostname(t *testing.T) {
  58. runtime, err := newTestRuntime()
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. defer nuke(runtime)
  63. srv := &Server{runtime: runtime}
  64. var stdin, stdout bytes.Buffer
  65. setTimeout(t, "CmdRun timed out", 2*time.Second, func() {
  66. if err := srv.CmdRun(ioutil.NopCloser(&stdin), &nopWriteCloser{&stdout}, "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil {
  67. t.Fatal(err)
  68. }
  69. })
  70. if output := string(stdout.Bytes()); output != "foobar\n" {
  71. t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", output)
  72. }
  73. }
  74. func TestRunExit(t *testing.T) {
  75. runtime, err := newTestRuntime()
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. defer nuke(runtime)
  80. srv := &Server{runtime: runtime}
  81. stdin, stdinPipe := io.Pipe()
  82. stdout, stdoutPipe := io.Pipe()
  83. c1 := make(chan struct{})
  84. go func() {
  85. srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat")
  86. close(c1)
  87. }()
  88. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  89. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  90. t.Fatal(err)
  91. }
  92. })
  93. container := runtime.List()[0]
  94. // Closing /bin/cat stdin, expect it to exit
  95. p, err := container.StdinPipe()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if err := p.Close(); err != nil {
  100. t.Fatal(err)
  101. }
  102. // as the process exited, CmdRun must finish and unblock. Wait for it
  103. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  104. <-c1
  105. })
  106. // Make sure that the client has been disconnected
  107. setTimeout(t, "The client should have been disconnected once the remote process exited.", 2*time.Second, func() {
  108. // Expecting pipe i/o error, just check that read does not block
  109. stdin.Read([]byte{})
  110. })
  111. // Cleanup pipes
  112. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  113. t.Fatal(err)
  114. }
  115. }
  116. // Expected behaviour: the process dies when the client disconnects
  117. func TestRunDisconnect(t *testing.T) {
  118. runtime, err := newTestRuntime()
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. defer nuke(runtime)
  123. srv := &Server{runtime: runtime}
  124. stdin, stdinPipe := io.Pipe()
  125. stdout, stdoutPipe := io.Pipe()
  126. c1 := make(chan struct{})
  127. go func() {
  128. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  129. // fact that CmdRun returns.
  130. srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat")
  131. close(c1)
  132. }()
  133. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  134. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  135. t.Fatal(err)
  136. }
  137. })
  138. // Close pipes (simulate disconnect)
  139. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  140. t.Fatal(err)
  141. }
  142. // as the pipes are close, we expect the process to die,
  143. // therefore CmdRun to unblock. Wait for CmdRun
  144. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  145. <-c1
  146. })
  147. // Client disconnect after run -i should cause stdin to be closed, which should
  148. // cause /bin/cat to exit.
  149. setTimeout(t, "Waiting for /bin/cat to exit timed out", 2*time.Second, func() {
  150. container := runtime.List()[0]
  151. container.Wait()
  152. if container.State.Running {
  153. t.Fatalf("/bin/cat is still running after closing stdin")
  154. }
  155. })
  156. }
  157. // TestAttachStdin checks attaching to stdin without stdout and stderr.
  158. // 'docker run -i -a stdin' should sends the client's stdin to the command,
  159. // then detach from it and print the container id.
  160. func TestAttachStdin(t *testing.T) {
  161. runtime, err := newTestRuntime()
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. defer nuke(runtime)
  166. srv := &Server{runtime: runtime}
  167. stdinR, stdinW := io.Pipe()
  168. var stdout bytes.Buffer
  169. ch := make(chan struct{})
  170. go func() {
  171. srv.CmdRun(stdinR, &stdout, "-i", "-a", "stdin", GetTestImage(runtime).Id, "sh", "-c", "echo hello; cat")
  172. close(ch)
  173. }()
  174. // Send input to the command, close stdin, wait for CmdRun to return
  175. setTimeout(t, "Read/Write timed out", 2*time.Second, func() {
  176. if _, err := stdinW.Write([]byte("hi there\n")); err != nil {
  177. t.Fatal(err)
  178. }
  179. stdinW.Close()
  180. <-ch
  181. })
  182. // Check output
  183. cmdOutput := string(stdout.Bytes())
  184. container := runtime.List()[0]
  185. if cmdOutput != container.ShortId()+"\n" {
  186. t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortId()+"\n", cmdOutput)
  187. }
  188. setTimeout(t, "Waiting for command to exit timed out", 2*time.Second, func() {
  189. container.Wait()
  190. })
  191. // Check logs
  192. if cmdLogs, err := container.ReadLog("stdout"); err != nil {
  193. t.Fatal(err)
  194. } else {
  195. if output, err := ioutil.ReadAll(cmdLogs); err != nil {
  196. t.Fatal(err)
  197. } else {
  198. expectedLog := "hello\nhi there\n"
  199. if string(output) != expectedLog {
  200. t.Fatalf("Unexpected logs: should be '%s', not '%s'\n", expectedLog, output)
  201. }
  202. }
  203. }
  204. }
  205. // Expected behaviour, the process stays alive when the client disconnects
  206. func TestAttachDisconnect(t *testing.T) {
  207. runtime, err := newTestRuntime()
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. defer nuke(runtime)
  212. srv := &Server{runtime: runtime}
  213. container, err := runtime.Create(
  214. &Config{
  215. Image: GetTestImage(runtime).Id,
  216. Memory: 33554432,
  217. Cmd: []string{"/bin/cat"},
  218. OpenStdin: true,
  219. },
  220. )
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. defer runtime.Destroy(container)
  225. // Start the process
  226. if err := container.Start(); err != nil {
  227. t.Fatal(err)
  228. }
  229. stdin, stdinPipe := io.Pipe()
  230. stdout, stdoutPipe := io.Pipe()
  231. // Attach to it
  232. c1 := make(chan struct{})
  233. go func() {
  234. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  235. // fact that CmdAttach returns.
  236. srv.CmdAttach(stdin, stdoutPipe, container.Id)
  237. close(c1)
  238. }()
  239. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  240. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  241. t.Fatal(err)
  242. }
  243. })
  244. // Close pipes (client disconnects)
  245. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  246. t.Fatal(err)
  247. }
  248. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  249. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  250. <-c1
  251. })
  252. // We closed stdin, expect /bin/cat to still be running
  253. // Wait a little bit to make sure container.monitor() did his thing
  254. err = container.WaitTimeout(500 * time.Millisecond)
  255. if err == nil || !container.State.Running {
  256. t.Fatalf("/bin/cat is not running after closing stdin")
  257. }
  258. // Try to avoid the timeoout in destroy. Best effort, don't check error
  259. cStdin, _ := container.StdinPipe()
  260. cStdin.Close()
  261. }