commands_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/Sirupsen/logrus"
  11. "github.com/docker/docker/api/client"
  12. "github.com/docker/docker/daemon"
  13. "github.com/docker/docker/pkg/stringid"
  14. "github.com/docker/docker/pkg/term"
  15. "github.com/kr/pty"
  16. )
  17. func closeWrap(args ...io.Closer) error {
  18. e := false
  19. ret := fmt.Errorf("Error closing elements")
  20. for _, c := range args {
  21. if err := c.Close(); err != nil {
  22. e = true
  23. ret = fmt.Errorf("%s\n%s", ret, err)
  24. }
  25. }
  26. if e {
  27. return ret
  28. }
  29. return nil
  30. }
  31. func setRaw(t *testing.T, c *daemon.Container) *term.State {
  32. pty, err := c.GetPtyMaster()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. state, err := term.MakeRaw(pty.Fd())
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. return state
  41. }
  42. func unsetRaw(t *testing.T, c *daemon.Container, state *term.State) {
  43. pty, err := c.GetPtyMaster()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. term.RestoreTerminal(pty.Fd(), state)
  48. }
  49. func waitContainerStart(t *testing.T, timeout time.Duration) *daemon.Container {
  50. var container *daemon.Container
  51. setTimeout(t, "Waiting for the container to be started timed out", timeout, func() {
  52. for {
  53. l := globalDaemon.List()
  54. if len(l) == 1 && l[0].IsRunning() {
  55. container = l[0]
  56. break
  57. }
  58. time.Sleep(10 * time.Millisecond)
  59. }
  60. })
  61. if container == nil {
  62. t.Fatal("An error occured while waiting for the container to start")
  63. }
  64. return container
  65. }
  66. func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
  67. c := make(chan bool)
  68. // Make sure we are not too long
  69. go func() {
  70. time.Sleep(d)
  71. c <- true
  72. }()
  73. go func() {
  74. f()
  75. c <- false
  76. }()
  77. if <-c && msg != "" {
  78. t.Fatal(msg)
  79. }
  80. }
  81. func expectPipe(expected string, r io.Reader) error {
  82. o, err := bufio.NewReader(r).ReadString('\n')
  83. if err != nil {
  84. return err
  85. }
  86. if strings.Trim(o, " \r\n") != expected {
  87. return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", expected, o)
  88. }
  89. return nil
  90. }
  91. func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
  92. for i := 0; i < count; i++ {
  93. if _, err := w.Write([]byte(input)); err != nil {
  94. return err
  95. }
  96. if err := expectPipe(output, r); err != nil {
  97. return err
  98. }
  99. }
  100. return nil
  101. }
  102. // TestAttachDetachTruncatedID checks that attach in tty mode can be detached
  103. func TestAttachDetachTruncatedID(t *testing.T) {
  104. stdout, stdoutPipe := io.Pipe()
  105. cpty, tty, err := pty.Open()
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil)
  110. defer cleanup(globalEngine, t)
  111. // Discard the CmdRun output
  112. go stdout.Read(make([]byte, 1024))
  113. setTimeout(t, "Starting container timed out", 2*time.Second, func() {
  114. if err := cli.CmdRun("-i", "-t", "-d", unitTestImageID, "cat"); err != nil {
  115. t.Fatal(err)
  116. }
  117. })
  118. container := waitContainerStart(t, 10*time.Second)
  119. state := setRaw(t, container)
  120. defer unsetRaw(t, container, state)
  121. stdout, stdoutPipe = io.Pipe()
  122. cpty, tty, err = pty.Open()
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. cli = client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil)
  127. ch := make(chan struct{})
  128. go func() {
  129. defer close(ch)
  130. if err := cli.CmdAttach(stringid.TruncateID(container.ID)); err != nil {
  131. if err != io.ErrClosedPipe {
  132. t.Fatal(err)
  133. }
  134. }
  135. }()
  136. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  137. if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
  138. if err != io.ErrClosedPipe {
  139. t.Fatal(err)
  140. }
  141. }
  142. })
  143. setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
  144. cpty.Write([]byte{16})
  145. time.Sleep(100 * time.Millisecond)
  146. cpty.Write([]byte{17})
  147. })
  148. // wait for CmdRun to return
  149. setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
  150. <-ch
  151. })
  152. closeWrap(cpty, stdout, stdoutPipe)
  153. time.Sleep(500 * time.Millisecond)
  154. if !container.IsRunning() {
  155. t.Fatal("The detached container should be still running")
  156. }
  157. setTimeout(t, "Waiting for container to die timedout", 5*time.Second, func() {
  158. container.Kill()
  159. })
  160. }
  161. // Expected behaviour, the process stays alive when the client disconnects
  162. func TestAttachDisconnect(t *testing.T) {
  163. stdout, stdoutPipe := io.Pipe()
  164. cpty, tty, err := pty.Open()
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil)
  169. defer cleanup(globalEngine, t)
  170. go func() {
  171. // Start a process in daemon mode
  172. if err := cli.CmdRun("-d", "-i", unitTestImageID, "/bin/cat"); err != nil {
  173. logrus.Debugf("Error CmdRun: %s", err)
  174. }
  175. }()
  176. setTimeout(t, "Waiting for CmdRun timed out", 10*time.Second, func() {
  177. if _, err := bufio.NewReader(stdout).ReadString('\n'); err != nil {
  178. t.Fatal(err)
  179. }
  180. })
  181. setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
  182. for {
  183. l := globalDaemon.List()
  184. if len(l) == 1 && l[0].IsRunning() {
  185. break
  186. }
  187. time.Sleep(10 * time.Millisecond)
  188. }
  189. })
  190. container := globalDaemon.List()[0]
  191. // Attach to it
  192. c1 := make(chan struct{})
  193. go func() {
  194. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  195. // fact that CmdAttach returns.
  196. cli.CmdAttach(container.ID)
  197. close(c1)
  198. }()
  199. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  200. if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
  201. t.Fatal(err)
  202. }
  203. })
  204. // Close pipes (client disconnects)
  205. if err := closeWrap(cpty, stdout, stdoutPipe); err != nil {
  206. t.Fatal(err)
  207. }
  208. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  209. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  210. <-c1
  211. })
  212. // We closed stdin, expect /bin/cat to still be running
  213. // Wait a little bit to make sure container.monitor() did his thing
  214. _, err = container.WaitStop(500 * time.Millisecond)
  215. if err == nil || !container.IsRunning() {
  216. t.Fatalf("/bin/cat is not running after closing stdin")
  217. }
  218. // Try to avoid the timeout in destroy. Best effort, don't check error
  219. cStdin := container.StdinPipe()
  220. cStdin.Close()
  221. container.WaitStop(-1 * time.Second)
  222. }
  223. // Expected behaviour: container gets deleted automatically after exit
  224. func TestRunAutoRemove(t *testing.T) {
  225. t.Skip("Fixme. Skipping test for now, race condition")
  226. stdout, stdoutPipe := io.Pipe()
  227. cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil)
  228. defer cleanup(globalEngine, t)
  229. c := make(chan struct{})
  230. go func() {
  231. defer close(c)
  232. if err := cli.CmdRun("--rm", unitTestImageID, "hostname"); err != nil {
  233. t.Fatal(err)
  234. }
  235. }()
  236. var temporaryContainerID string
  237. setTimeout(t, "Reading command output time out", 2*time.Second, func() {
  238. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. temporaryContainerID = cmdOutput
  243. if err := closeWrap(stdout, stdoutPipe); err != nil {
  244. t.Fatal(err)
  245. }
  246. })
  247. setTimeout(t, "CmdRun timed out", 10*time.Second, func() {
  248. <-c
  249. })
  250. time.Sleep(500 * time.Millisecond)
  251. if len(globalDaemon.List()) > 0 {
  252. t.Fatalf("failed to remove container automatically: container %s still exists", temporaryContainerID)
  253. }
  254. }