commands_test.go 7.7 KB

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