commands_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "github.com/dotcloud/docker/rcli"
  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. func cmdWait(srv *Server, container *Container) error {
  57. stdout, stdoutPipe := io.Pipe()
  58. go func() {
  59. srv.CmdWait(nil, stdoutPipe, container.Id)
  60. }()
  61. if _, err := bufio.NewReader(stdout).ReadString('\n'); err != nil {
  62. return err
  63. }
  64. // Cleanup pipes
  65. return closeWrap(stdout, stdoutPipe)
  66. }
  67. // TestRunHostname checks that 'docker run -h' correctly sets a custom hostname
  68. func TestRunHostname(t *testing.T) {
  69. runtime, err := newTestRuntime()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer nuke(runtime)
  74. srv := &Server{runtime: runtime}
  75. stdin, _ := io.Pipe()
  76. stdout, stdoutPipe := io.Pipe()
  77. c := make(chan struct{})
  78. go func() {
  79. if err := srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil {
  80. t.Fatal(err)
  81. }
  82. close(c)
  83. }()
  84. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if cmdOutput != "foobar\n" {
  89. t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", cmdOutput)
  90. }
  91. setTimeout(t, "CmdRun timed out", 2*time.Second, func() {
  92. <-c
  93. cmdWait(srv, srv.runtime.List()[0])
  94. })
  95. }
  96. func TestRunExit(t *testing.T) {
  97. runtime, err := newTestRuntime()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. defer nuke(runtime)
  102. srv := &Server{runtime: runtime}
  103. stdin, stdinPipe := io.Pipe()
  104. stdout, stdoutPipe := io.Pipe()
  105. c1 := make(chan struct{})
  106. go func() {
  107. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", GetTestImage(runtime).Id, "/bin/cat")
  108. close(c1)
  109. }()
  110. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  111. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  112. t.Fatal(err)
  113. }
  114. })
  115. container := runtime.List()[0]
  116. // Closing /bin/cat stdin, expect it to exit
  117. p, err := container.StdinPipe()
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. if err := p.Close(); err != nil {
  122. t.Fatal(err)
  123. }
  124. // as the process exited, CmdRun must finish and unblock. Wait for it
  125. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  126. <-c1
  127. cmdWait(srv, container)
  128. })
  129. // Make sure that the client has been disconnected
  130. setTimeout(t, "The client should have been disconnected once the remote process exited.", 2*time.Second, func() {
  131. // Expecting pipe i/o error, just check that read does not block
  132. stdin.Read([]byte{})
  133. })
  134. // Cleanup pipes
  135. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  136. t.Fatal(err)
  137. }
  138. }
  139. // Expected behaviour: the process dies when the client disconnects
  140. func TestRunDisconnect(t *testing.T) {
  141. runtime, err := newTestRuntime()
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. defer nuke(runtime)
  146. srv := &Server{runtime: runtime}
  147. stdin, stdinPipe := io.Pipe()
  148. stdout, stdoutPipe := io.Pipe()
  149. c1 := make(chan struct{})
  150. go func() {
  151. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  152. // fact that CmdRun returns.
  153. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", GetTestImage(runtime).Id, "/bin/cat")
  154. close(c1)
  155. }()
  156. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  157. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  158. t.Fatal(err)
  159. }
  160. })
  161. // Close pipes (simulate disconnect)
  162. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  163. t.Fatal(err)
  164. }
  165. // as the pipes are close, we expect the process to die,
  166. // therefore CmdRun to unblock. Wait for CmdRun
  167. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  168. <-c1
  169. })
  170. // Client disconnect after run -i should cause stdin to be closed, which should
  171. // cause /bin/cat to exit.
  172. setTimeout(t, "Waiting for /bin/cat to exit timed out", 2*time.Second, func() {
  173. container := runtime.List()[0]
  174. container.Wait()
  175. if container.State.Running {
  176. t.Fatalf("/bin/cat is still running after closing stdin")
  177. }
  178. })
  179. }
  180. // Expected behaviour: the process dies when the client disconnects
  181. func TestRunDisconnectTty(t *testing.T) {
  182. runtime, err := newTestRuntime()
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. defer nuke(runtime)
  187. srv := &Server{runtime: runtime}
  188. stdin, stdinPipe := io.Pipe()
  189. stdout, stdoutPipe := io.Pipe()
  190. c1 := make(chan struct{})
  191. go func() {
  192. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  193. // fact that CmdRun returns.
  194. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", "-t", GetTestImage(runtime).Id, "/bin/cat")
  195. close(c1)
  196. }()
  197. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  198. for {
  199. // Client disconnect after run -i should keep stdin out in TTY mode
  200. l := runtime.List()
  201. if len(l) == 1 && l[0].State.Running {
  202. break
  203. }
  204. time.Sleep(10 * time.Millisecond)
  205. }
  206. })
  207. // Client disconnect after run -i should keep stdin out in TTY mode
  208. container := runtime.List()[0]
  209. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  210. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  211. t.Fatal(err)
  212. }
  213. })
  214. // Close pipes (simulate disconnect)
  215. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  216. t.Fatal(err)
  217. }
  218. // In tty mode, we expect the process to stay alive even after client's stdin closes.
  219. // Do not wait for run to finish
  220. // Give some time to monitor to do his thing
  221. container.WaitTimeout(500 * time.Millisecond)
  222. if !container.State.Running {
  223. t.Fatalf("/bin/cat should still be running after closing stdin (tty mode)")
  224. }
  225. }
  226. // TestAttachStdin checks attaching to stdin without stdout and stderr.
  227. // 'docker run -i -a stdin' should sends the client's stdin to the command,
  228. // then detach from it and print the container id.
  229. func TestRunAttachStdin(t *testing.T) {
  230. runtime, err := newTestRuntime()
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. defer nuke(runtime)
  235. srv := &Server{runtime: runtime}
  236. stdin, stdinPipe := io.Pipe()
  237. stdout, stdoutPipe := io.Pipe()
  238. ch := make(chan struct{})
  239. go func() {
  240. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", "-a", "stdin", GetTestImage(runtime).Id, "sh", "-c", "echo hello; cat")
  241. close(ch)
  242. }()
  243. // Send input to the command, close stdin
  244. setTimeout(t, "Write timed out", 2*time.Second, func() {
  245. if _, err := stdinPipe.Write([]byte("hi there\n")); err != nil {
  246. t.Fatal(err)
  247. }
  248. if err := stdinPipe.Close(); err != nil {
  249. t.Fatal(err)
  250. }
  251. })
  252. container := runtime.List()[0]
  253. // Check output
  254. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. if cmdOutput != container.ShortId()+"\n" {
  259. t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortId()+"\n", cmdOutput)
  260. }
  261. // wait for CmdRun to return
  262. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  263. <-ch
  264. })
  265. setTimeout(t, "Waiting for command to exit timed out", 2*time.Second, func() {
  266. container.Wait()
  267. })
  268. // Check logs
  269. if cmdLogs, err := container.ReadLog("stdout"); err != nil {
  270. t.Fatal(err)
  271. } else {
  272. if output, err := ioutil.ReadAll(cmdLogs); err != nil {
  273. t.Fatal(err)
  274. } else {
  275. expectedLog := "hello\nhi there\n"
  276. if string(output) != expectedLog {
  277. t.Fatalf("Unexpected logs: should be '%s', not '%s'\n", expectedLog, output)
  278. }
  279. }
  280. }
  281. }
  282. // Expected behaviour, the process stays alive when the client disconnects
  283. func TestAttachDisconnect(t *testing.T) {
  284. runtime, err := newTestRuntime()
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. defer nuke(runtime)
  289. srv := &Server{runtime: runtime}
  290. container, err := runtime.Create(
  291. &Config{
  292. Image: GetTestImage(runtime).Id,
  293. Memory: 33554432,
  294. Cmd: []string{"/bin/cat"},
  295. OpenStdin: true,
  296. },
  297. )
  298. if err != nil {
  299. t.Fatal(err)
  300. }
  301. defer runtime.Destroy(container)
  302. // Start the process
  303. if err := container.Start(); err != nil {
  304. t.Fatal(err)
  305. }
  306. stdin, stdinPipe := io.Pipe()
  307. stdout, stdoutPipe := io.Pipe()
  308. // Attach to it
  309. c1 := make(chan struct{})
  310. go func() {
  311. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  312. // fact that CmdAttach returns.
  313. srv.CmdAttach(stdin, rcli.NewDockerLocalConn(stdoutPipe), container.Id)
  314. close(c1)
  315. }()
  316. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  317. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  318. t.Fatal(err)
  319. }
  320. })
  321. // Close pipes (client disconnects)
  322. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  323. t.Fatal(err)
  324. }
  325. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  326. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  327. <-c1
  328. })
  329. // We closed stdin, expect /bin/cat to still be running
  330. // Wait a little bit to make sure container.monitor() did his thing
  331. err = container.WaitTimeout(500 * time.Millisecond)
  332. if err == nil || !container.State.Running {
  333. t.Fatalf("/bin/cat is not running after closing stdin")
  334. }
  335. // Try to avoid the timeoout in destroy. Best effort, don't check error
  336. cStdin, _ := container.StdinPipe()
  337. cStdin.Close()
  338. container.Wait()
  339. }