commands_test.go 9.9 KB

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