commands_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. func cmdImages(srv *Server, args ...string) (string, error) {
  68. stdout, stdoutPipe := io.Pipe()
  69. go func() {
  70. if err := srv.CmdImages(nil, stdoutPipe, args...); err != nil {
  71. return
  72. }
  73. // force the pipe closed, so that the code below gets an EOF
  74. stdoutPipe.Close()
  75. }()
  76. output, err := ioutil.ReadAll(stdout)
  77. if err != nil {
  78. return "", err
  79. }
  80. // Cleanup pipes
  81. return string(output), closeWrap(stdout, stdoutPipe)
  82. }
  83. // TestImages checks that 'docker images' displays information correctly
  84. func TestImages(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. output, err := cmdImages(srv)
  92. if !strings.Contains(output, "REPOSITORY") {
  93. t.Fatal("'images' should have a header")
  94. }
  95. if !strings.Contains(output, "docker-ut") {
  96. t.Fatal("'images' should show the docker-ut image")
  97. }
  98. if !strings.Contains(output, "e9aa60c60128") {
  99. t.Fatal("'images' should show the docker-ut image id")
  100. }
  101. output, err = cmdImages(srv, "-q")
  102. if strings.Contains(output, "REPOSITORY") {
  103. t.Fatal("'images -q' should not have a header")
  104. }
  105. if strings.Contains(output, "docker-ut") {
  106. t.Fatal("'images' should not show the docker-ut image name")
  107. }
  108. if !strings.Contains(output, "e9aa60c60128") {
  109. t.Fatal("'images' should show the docker-ut image id")
  110. }
  111. output, err = cmdImages(srv, "-viz")
  112. if !strings.HasPrefix(output, "digraph docker {") {
  113. t.Fatal("'images -v' should start with the dot header")
  114. }
  115. if !strings.HasSuffix(output, "}\n") {
  116. t.Fatal("'images -v' should end with a '}'")
  117. }
  118. if !strings.Contains(output, "base -> \"e9aa60c60128\" [style=invis]") {
  119. t.Fatal("'images -v' should have the docker-ut image id node")
  120. }
  121. // todo: add checks for -a
  122. }
  123. // TestRunHostname checks that 'docker run -h' correctly sets a custom hostname
  124. func TestRunHostname(t *testing.T) {
  125. runtime, err := newTestRuntime()
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. defer nuke(runtime)
  130. srv := &Server{runtime: runtime}
  131. stdin, _ := io.Pipe()
  132. stdout, stdoutPipe := io.Pipe()
  133. c := make(chan struct{})
  134. go func() {
  135. if err := srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil {
  136. t.Fatal(err)
  137. }
  138. close(c)
  139. }()
  140. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if cmdOutput != "foobar\n" {
  145. t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", cmdOutput)
  146. }
  147. setTimeout(t, "CmdRun timed out", 2*time.Second, func() {
  148. <-c
  149. cmdWait(srv, srv.runtime.List()[0])
  150. })
  151. }
  152. func TestRunExit(t *testing.T) {
  153. runtime, err := newTestRuntime()
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. defer nuke(runtime)
  158. srv := &Server{runtime: runtime}
  159. stdin, stdinPipe := io.Pipe()
  160. stdout, stdoutPipe := io.Pipe()
  161. c1 := make(chan struct{})
  162. go func() {
  163. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", GetTestImage(runtime).Id, "/bin/cat")
  164. close(c1)
  165. }()
  166. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  167. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  168. t.Fatal(err)
  169. }
  170. })
  171. container := runtime.List()[0]
  172. // Closing /bin/cat stdin, expect it to exit
  173. p, err := container.StdinPipe()
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. if err := p.Close(); err != nil {
  178. t.Fatal(err)
  179. }
  180. // as the process exited, CmdRun must finish and unblock. Wait for it
  181. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  182. <-c1
  183. cmdWait(srv, container)
  184. })
  185. // Make sure that the client has been disconnected
  186. setTimeout(t, "The client should have been disconnected once the remote process exited.", 2*time.Second, func() {
  187. // Expecting pipe i/o error, just check that read does not block
  188. stdin.Read([]byte{})
  189. })
  190. // Cleanup pipes
  191. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  192. t.Fatal(err)
  193. }
  194. }
  195. // Expected behaviour: the process dies when the client disconnects
  196. func TestRunDisconnect(t *testing.T) {
  197. runtime, err := newTestRuntime()
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. defer nuke(runtime)
  202. srv := &Server{runtime: runtime}
  203. stdin, stdinPipe := io.Pipe()
  204. stdout, stdoutPipe := io.Pipe()
  205. c1 := make(chan struct{})
  206. go func() {
  207. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  208. // fact that CmdRun returns.
  209. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", GetTestImage(runtime).Id, "/bin/cat")
  210. close(c1)
  211. }()
  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. // as the pipes are close, we expect the process to die,
  222. // therefore CmdRun to unblock. Wait for CmdRun
  223. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  224. <-c1
  225. })
  226. // Client disconnect after run -i should cause stdin to be closed, which should
  227. // cause /bin/cat to exit.
  228. setTimeout(t, "Waiting for /bin/cat to exit timed out", 2*time.Second, func() {
  229. container := runtime.List()[0]
  230. container.Wait()
  231. if container.State.Running {
  232. t.Fatalf("/bin/cat is still running after closing stdin")
  233. }
  234. })
  235. }
  236. // Expected behaviour: the process dies when the client disconnects
  237. func TestRunDisconnectTty(t *testing.T) {
  238. runtime, err := newTestRuntime()
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. defer nuke(runtime)
  243. srv := &Server{runtime: runtime}
  244. stdin, stdinPipe := io.Pipe()
  245. stdout, stdoutPipe := io.Pipe()
  246. c1 := make(chan struct{})
  247. go func() {
  248. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  249. // fact that CmdRun returns.
  250. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", "-t", GetTestImage(runtime).Id, "/bin/cat")
  251. close(c1)
  252. }()
  253. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  254. for {
  255. // Client disconnect after run -i should keep stdin out in TTY mode
  256. l := runtime.List()
  257. if len(l) == 1 && l[0].State.Running {
  258. break
  259. }
  260. time.Sleep(10 * time.Millisecond)
  261. }
  262. })
  263. // Client disconnect after run -i should keep stdin out in TTY mode
  264. container := runtime.List()[0]
  265. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  266. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  267. t.Fatal(err)
  268. }
  269. })
  270. // Close pipes (simulate disconnect)
  271. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  272. t.Fatal(err)
  273. }
  274. // In tty mode, we expect the process to stay alive even after client's stdin closes.
  275. // Do not wait for run to finish
  276. // Give some time to monitor to do his thing
  277. container.WaitTimeout(500 * time.Millisecond)
  278. if !container.State.Running {
  279. t.Fatalf("/bin/cat should still be running after closing stdin (tty mode)")
  280. }
  281. }
  282. // TestAttachStdin checks attaching to stdin without stdout and stderr.
  283. // 'docker run -i -a stdin' should sends the client's stdin to the command,
  284. // then detach from it and print the container id.
  285. func TestRunAttachStdin(t *testing.T) {
  286. runtime, err := newTestRuntime()
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. defer nuke(runtime)
  291. srv := &Server{runtime: runtime}
  292. stdin, stdinPipe := io.Pipe()
  293. stdout, stdoutPipe := io.Pipe()
  294. ch := make(chan struct{})
  295. go func() {
  296. srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-i", "-a", "stdin", GetTestImage(runtime).Id, "sh", "-c", "echo hello; cat")
  297. close(ch)
  298. }()
  299. // Send input to the command, close stdin
  300. setTimeout(t, "Write timed out", 2*time.Second, func() {
  301. if _, err := stdinPipe.Write([]byte("hi there\n")); err != nil {
  302. t.Fatal(err)
  303. }
  304. if err := stdinPipe.Close(); err != nil {
  305. t.Fatal(err)
  306. }
  307. })
  308. container := runtime.List()[0]
  309. // Check output
  310. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  311. if err != nil {
  312. t.Fatal(err)
  313. }
  314. if cmdOutput != container.ShortId()+"\n" {
  315. t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortId()+"\n", cmdOutput)
  316. }
  317. // wait for CmdRun to return
  318. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  319. <-ch
  320. })
  321. setTimeout(t, "Waiting for command to exit timed out", 2*time.Second, func() {
  322. container.Wait()
  323. })
  324. // Check logs
  325. if cmdLogs, err := container.ReadLog("stdout"); err != nil {
  326. t.Fatal(err)
  327. } else {
  328. if output, err := ioutil.ReadAll(cmdLogs); err != nil {
  329. t.Fatal(err)
  330. } else {
  331. expectedLog := "hello\nhi there\n"
  332. if string(output) != expectedLog {
  333. t.Fatalf("Unexpected logs: should be '%s', not '%s'\n", expectedLog, output)
  334. }
  335. }
  336. }
  337. }
  338. // Expected behaviour, the process stays alive when the client disconnects
  339. func TestAttachDisconnect(t *testing.T) {
  340. runtime, err := newTestRuntime()
  341. if err != nil {
  342. t.Fatal(err)
  343. }
  344. defer nuke(runtime)
  345. srv := &Server{runtime: runtime}
  346. container, err := runtime.Create(
  347. &Config{
  348. Image: GetTestImage(runtime).Id,
  349. Memory: 33554432,
  350. Cmd: []string{"/bin/cat"},
  351. OpenStdin: true,
  352. },
  353. )
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. defer runtime.Destroy(container)
  358. // Start the process
  359. if err := container.Start(); err != nil {
  360. t.Fatal(err)
  361. }
  362. stdin, stdinPipe := io.Pipe()
  363. stdout, stdoutPipe := io.Pipe()
  364. // Attach to it
  365. c1 := make(chan struct{})
  366. go func() {
  367. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  368. // fact that CmdAttach returns.
  369. srv.CmdAttach(stdin, rcli.NewDockerLocalConn(stdoutPipe), container.Id)
  370. close(c1)
  371. }()
  372. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  373. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  374. t.Fatal(err)
  375. }
  376. })
  377. // Close pipes (client disconnects)
  378. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  379. t.Fatal(err)
  380. }
  381. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  382. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  383. <-c1
  384. })
  385. // We closed stdin, expect /bin/cat to still be running
  386. // Wait a little bit to make sure container.monitor() did his thing
  387. err = container.WaitTimeout(500 * time.Millisecond)
  388. if err == nil || !container.State.Running {
  389. t.Fatalf("/bin/cat is not running after closing stdin")
  390. }
  391. // Try to avoid the timeoout in destroy. Best effort, don't check error
  392. cStdin, _ := container.StdinPipe()
  393. cStdin.Close()
  394. container.Wait()
  395. }