commands_test.go 9.3 KB

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