commands_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package docker
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "log"
  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. // Expected behaviour: the process dies when the client disconnects
  58. func TestRunDisconnect(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. stdin, stdinPipe := io.Pipe()
  66. stdout, stdoutPipe := io.Pipe()
  67. c1 := make(chan struct{})
  68. go func() {
  69. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  70. // fact that CmdRun returns.
  71. srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat")
  72. close(c1)
  73. }()
  74. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  75. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  76. t.Fatal(err)
  77. }
  78. })
  79. // Close pipes (simulate disconnect)
  80. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  81. t.Fatal(err)
  82. }
  83. // as the pipes are close, we expect the process to die,
  84. // therefore CmdRun to unblock. Wait for CmdRun
  85. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  86. <-c1
  87. })
  88. // Client disconnect after run -i should cause stdin to be closed, which should
  89. // cause /bin/cat to exit.
  90. setTimeout(t, "Waiting for /bin/cat to exit timed out", 2*time.Second, func() {
  91. container := runtime.List()[0]
  92. container.Wait()
  93. if container.State.Running {
  94. t.Fatalf("/bin/cat is still running after closing stdin")
  95. }
  96. })
  97. }
  98. // TestAttachStdin checks attaching to stdin without stdout and stderr.
  99. // 'docker run -i -a stdin' should sends the client's stdin to the command,
  100. // then detach from it and print the container id.
  101. func TestAttachStdin(t *testing.T) {
  102. runtime, err := newTestRuntime()
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. defer nuke(runtime)
  107. srv := &Server{runtime: runtime}
  108. stdinR, stdinW := io.Pipe()
  109. var stdout bytes.Buffer
  110. ch := make(chan struct{})
  111. go func() {
  112. srv.CmdRun(stdinR, &stdout, "-i", "-a", "stdin", GetTestImage(runtime).Id, "sh", "-c", "echo hello; cat")
  113. close(ch)
  114. }()
  115. // Send input to the command, close stdin, wait for CmdRun to return
  116. setTimeout(t, "Read/Write timed out", 2*time.Second, func() {
  117. if _, err := stdinW.Write([]byte("hi there\n")); err != nil {
  118. t.Fatal(err)
  119. }
  120. stdinW.Close()
  121. <-ch
  122. })
  123. // Check output
  124. cmdOutput := string(stdout.Bytes())
  125. container := runtime.List()[0]
  126. if cmdOutput != container.ShortId()+"\n" {
  127. t.Fatalf("Wrong output: should be '%s', not '%s'\n", container.ShortId()+"\n", cmdOutput)
  128. }
  129. setTimeout(t, "Waiting for command to exit timed out", 2*time.Second, func() {
  130. container.Wait()
  131. })
  132. // Check logs
  133. if cmdLogs, err := container.ReadLog("stdout"); err != nil {
  134. t.Fatal(err)
  135. } else {
  136. if output, err := ioutil.ReadAll(cmdLogs); err != nil {
  137. t.Fatal(err)
  138. } else {
  139. expectedLog := "hello\nhi there\n"
  140. if string(output) != expectedLog {
  141. t.Fatalf("Unexpected logs: should be '%s', not '%s'\n", expectedLog, output)
  142. }
  143. }
  144. }
  145. }
  146. // Expected behaviour, the process stays alive when the client disconnects
  147. func TestAttachDisconnect(t *testing.T) {
  148. runtime, err := newTestRuntime()
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. defer nuke(runtime)
  153. srv := &Server{runtime: runtime}
  154. container, err := runtime.Create(
  155. &Config{
  156. Image: GetTestImage(runtime).Id,
  157. Memory: 33554432,
  158. Cmd: []string{"/bin/cat"},
  159. OpenStdin: true,
  160. },
  161. )
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. defer runtime.Destroy(container)
  166. // Start the process
  167. if err := container.Start(); err != nil {
  168. t.Fatal(err)
  169. }
  170. stdin, stdinPipe := io.Pipe()
  171. stdout, stdoutPipe := io.Pipe()
  172. // Attach to it
  173. c1 := make(chan struct{})
  174. go func() {
  175. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  176. // fact that CmdAttach returns.
  177. srv.CmdAttach(stdin, stdoutPipe, container.Id)
  178. close(c1)
  179. }()
  180. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  181. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  182. t.Fatal(err)
  183. }
  184. })
  185. // Close pipes (client disconnects)
  186. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  187. t.Fatal(err)
  188. }
  189. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  190. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  191. <-c1
  192. })
  193. // We closed stdin, expect /bin/cat to still be running
  194. // Wait a little bit to make sure container.monitor() did his thing
  195. err = container.WaitTimeout(500 * time.Millisecond)
  196. if err == nil || !container.State.Running {
  197. t.Fatalf("/bin/cat is not running after closing stdin")
  198. }
  199. // Try to avoid the timeoout in destroy. Best effort, don't check error
  200. cStdin, _ := container.StdinPipe()
  201. cStdin.Close()
  202. }