docker_cli_attach_unix_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "os/exec"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/docker/docker/pkg/stringid"
  10. "github.com/kr/pty"
  11. )
  12. // #9860
  13. func TestAttachClosedOnContainerStop(t *testing.T) {
  14. defer deleteAllContainers()
  15. cmd := exec.Command(dockerBinary, "run", "-dti", "busybox", "sleep", "2")
  16. out, _, err := runCommandWithOutput(cmd)
  17. if err != nil {
  18. t.Fatalf("failed to start container: %v (%v)", out, err)
  19. }
  20. id := strings.TrimSpace(out)
  21. if err := waitRun(id); err != nil {
  22. t.Fatal(err)
  23. }
  24. done := make(chan struct{})
  25. go func() {
  26. defer close(done)
  27. _, tty, err := pty.Open()
  28. if err != nil {
  29. t.Fatalf("could not open pty: %v", err)
  30. }
  31. attachCmd := exec.Command(dockerBinary, "attach", id)
  32. attachCmd.Stdin = tty
  33. attachCmd.Stdout = tty
  34. attachCmd.Stderr = tty
  35. if err := attachCmd.Run(); err != nil {
  36. t.Fatalf("attach returned error %s", err)
  37. }
  38. }()
  39. waitCmd := exec.Command(dockerBinary, "wait", id)
  40. if out, _, err = runCommandWithOutput(waitCmd); err != nil {
  41. t.Fatalf("error thrown while waiting for container: %s, %v", out, err)
  42. }
  43. select {
  44. case <-done:
  45. case <-time.After(attachWait):
  46. t.Fatal("timed out without attach returning")
  47. }
  48. logDone("attach - return after container finished")
  49. }
  50. func TestAttachAfterDetach(t *testing.T) {
  51. defer deleteAllContainers()
  52. name := "detachtest"
  53. cpty, tty, err := pty.Open()
  54. if err != nil {
  55. t.Fatalf("Could not open pty: %v", err)
  56. }
  57. cmd := exec.Command(dockerBinary, "run", "-ti", "--name", name, "busybox")
  58. cmd.Stdin = tty
  59. cmd.Stdout = tty
  60. cmd.Stderr = tty
  61. detached := make(chan struct{})
  62. go func() {
  63. if err := cmd.Run(); err != nil {
  64. t.Fatalf("attach returned error %s", err)
  65. }
  66. close(detached)
  67. }()
  68. time.Sleep(500 * time.Millisecond)
  69. if err := waitRun(name); err != nil {
  70. t.Fatal(err)
  71. }
  72. cpty.Write([]byte{16})
  73. time.Sleep(100 * time.Millisecond)
  74. cpty.Write([]byte{17})
  75. <-detached
  76. cpty, tty, err = pty.Open()
  77. if err != nil {
  78. t.Fatalf("Could not open pty: %v", err)
  79. }
  80. cmd = exec.Command(dockerBinary, "attach", name)
  81. cmd.Stdin = tty
  82. cmd.Stdout = tty
  83. cmd.Stderr = tty
  84. if err := cmd.Start(); err != nil {
  85. t.Fatal(err)
  86. }
  87. bytes := make([]byte, 10)
  88. var nBytes int
  89. readErr := make(chan error, 1)
  90. go func() {
  91. time.Sleep(500 * time.Millisecond)
  92. cpty.Write([]byte("\n"))
  93. time.Sleep(500 * time.Millisecond)
  94. nBytes, err = cpty.Read(bytes)
  95. cpty.Close()
  96. readErr <- err
  97. }()
  98. select {
  99. case err := <-readErr:
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. case <-time.After(2 * time.Second):
  104. t.Fatal("timeout waiting for attach read")
  105. }
  106. if err := cmd.Wait(); err != nil {
  107. t.Fatal(err)
  108. }
  109. if !strings.Contains(string(bytes[:nBytes]), "/ #") {
  110. t.Fatalf("failed to get a new prompt. got %s", string(bytes[:nBytes]))
  111. }
  112. logDone("attach - reconnect after detaching")
  113. }
  114. // TestAttachDetach checks that attach in tty mode can be detached using the long container ID
  115. func TestAttachDetach(t *testing.T) {
  116. out, _, _ := dockerCmd(t, "run", "-itd", "busybox", "cat")
  117. id := strings.TrimSpace(out)
  118. if err := waitRun(id); err != nil {
  119. t.Fatal(err)
  120. }
  121. cpty, tty, err := pty.Open()
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. defer cpty.Close()
  126. cmd := exec.Command(dockerBinary, "attach", id)
  127. cmd.Stdin = tty
  128. stdout, err := cmd.StdoutPipe()
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. defer stdout.Close()
  133. if err := cmd.Start(); err != nil {
  134. t.Fatal(err)
  135. }
  136. if err := waitRun(id); err != nil {
  137. t.Fatalf("error waiting for container to start: %v", err)
  138. }
  139. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  140. t.Fatal(err)
  141. }
  142. out, err = bufio.NewReader(stdout).ReadString('\n')
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. if strings.TrimSpace(out) != "hello" {
  147. t.Fatalf("exepected 'hello', got %q", out)
  148. }
  149. // escape sequence
  150. if _, err := cpty.Write([]byte{16}); err != nil {
  151. t.Fatal(err)
  152. }
  153. time.Sleep(100 * time.Millisecond)
  154. if _, err := cpty.Write([]byte{17}); err != nil {
  155. t.Fatal(err)
  156. }
  157. ch := make(chan struct{})
  158. go func() {
  159. cmd.Wait()
  160. ch <- struct{}{}
  161. }()
  162. running, err := inspectField(id, "State.Running")
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. if running != "true" {
  167. t.Fatal("exepected container to still be running")
  168. }
  169. go func() {
  170. dockerCmd(t, "kill", id)
  171. }()
  172. select {
  173. case <-ch:
  174. case <-time.After(10 * time.Millisecond):
  175. t.Fatal("timed out waiting for container to exit")
  176. }
  177. logDone("attach - detach")
  178. }
  179. // TestAttachDetachTruncatedID checks that attach in tty mode can be detached
  180. func TestAttachDetachTruncatedID(t *testing.T) {
  181. out, _, _ := dockerCmd(t, "run", "-itd", "busybox", "cat")
  182. id := stringid.TruncateID(strings.TrimSpace(out))
  183. if err := waitRun(id); err != nil {
  184. t.Fatal(err)
  185. }
  186. cpty, tty, err := pty.Open()
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. defer cpty.Close()
  191. cmd := exec.Command(dockerBinary, "attach", id)
  192. cmd.Stdin = tty
  193. stdout, err := cmd.StdoutPipe()
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. defer stdout.Close()
  198. if err := cmd.Start(); err != nil {
  199. t.Fatal(err)
  200. }
  201. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  202. t.Fatal(err)
  203. }
  204. out, err = bufio.NewReader(stdout).ReadString('\n')
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. if strings.TrimSpace(out) != "hello" {
  209. t.Fatalf("exepected 'hello', got %q", out)
  210. }
  211. // escape sequence
  212. if _, err := cpty.Write([]byte{16}); err != nil {
  213. t.Fatal(err)
  214. }
  215. time.Sleep(100 * time.Millisecond)
  216. if _, err := cpty.Write([]byte{17}); err != nil {
  217. t.Fatal(err)
  218. }
  219. ch := make(chan struct{})
  220. go func() {
  221. cmd.Wait()
  222. ch <- struct{}{}
  223. }()
  224. running, err := inspectField(id, "State.Running")
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. if running != "true" {
  229. t.Fatal("exepected container to still be running")
  230. }
  231. go func() {
  232. dockerCmd(t, "kill", id)
  233. }()
  234. select {
  235. case <-ch:
  236. case <-time.After(10 * time.Millisecond):
  237. t.Fatal("timed out waiting for container to exit")
  238. }
  239. logDone("attach - detach truncated ID")
  240. }