docker_cli_attach_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os/exec"
  7. "runtime"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/docker/docker/integration-cli/cli"
  12. "github.com/go-check/check"
  13. "github.com/gotestyourself/gotestyourself/icmd"
  14. )
  15. const attachWait = 5 * time.Second
  16. func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
  17. endGroup := &sync.WaitGroup{}
  18. startGroup := &sync.WaitGroup{}
  19. endGroup.Add(3)
  20. startGroup.Add(3)
  21. cli.DockerCmd(c, "run", "--name", "attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
  22. cli.WaitRun(c, "attacher")
  23. startDone := make(chan struct{})
  24. endDone := make(chan struct{})
  25. go func() {
  26. endGroup.Wait()
  27. close(endDone)
  28. }()
  29. go func() {
  30. startGroup.Wait()
  31. close(startDone)
  32. }()
  33. for i := 0; i < 3; i++ {
  34. go func() {
  35. cmd := exec.Command(dockerBinary, "attach", "attacher")
  36. defer func() {
  37. cmd.Wait()
  38. endGroup.Done()
  39. }()
  40. out, err := cmd.StdoutPipe()
  41. if err != nil {
  42. c.Fatal(err)
  43. }
  44. defer out.Close()
  45. if err := cmd.Start(); err != nil {
  46. c.Fatal(err)
  47. }
  48. buf := make([]byte, 1024)
  49. if _, err := out.Read(buf); err != nil && err != io.EOF {
  50. c.Fatal(err)
  51. }
  52. startGroup.Done()
  53. if !strings.Contains(string(buf), "hello") {
  54. c.Fatalf("unexpected output %s expected hello\n", string(buf))
  55. }
  56. }()
  57. }
  58. select {
  59. case <-startDone:
  60. case <-time.After(attachWait):
  61. c.Fatalf("Attaches did not initialize properly")
  62. }
  63. cli.DockerCmd(c, "kill", "attacher")
  64. select {
  65. case <-endDone:
  66. case <-time.After(attachWait):
  67. c.Fatalf("Attaches did not finish properly")
  68. }
  69. }
  70. func (s *DockerSuite) TestAttachTTYWithoutStdin(c *check.C) {
  71. // TODO @jhowardmsft. Figure out how to get this running again reliable on Windows.
  72. // It works by accident at the moment. Sometimes. I've gone back to v1.13.0 and see the same.
  73. // On Windows, docker run -d -ti busybox causes the container to exit immediately.
  74. // Obviously a year back when I updated the test, that was not the case. However,
  75. // with this, and the test racing with the tear-down which panic's, sometimes CI
  76. // will just fail and `MISS` all the other tests. For now, disabling it. Will
  77. // open an issue to track re-enabling this and root-causing the problem.
  78. testRequires(c, DaemonIsLinux)
  79. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  80. id := strings.TrimSpace(out)
  81. c.Assert(waitRun(id), check.IsNil)
  82. done := make(chan error)
  83. go func() {
  84. defer close(done)
  85. cmd := exec.Command(dockerBinary, "attach", id)
  86. if _, err := cmd.StdinPipe(); err != nil {
  87. done <- err
  88. return
  89. }
  90. expected := "the input device is not a TTY"
  91. if runtime.GOOS == "windows" {
  92. expected += ". If you are using mintty, try prefixing the command with 'winpty'"
  93. }
  94. if out, _, err := runCommandWithOutput(cmd); err == nil {
  95. done <- fmt.Errorf("attach should have failed")
  96. return
  97. } else if !strings.Contains(out, expected) {
  98. done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
  99. return
  100. }
  101. }()
  102. select {
  103. case err := <-done:
  104. c.Assert(err, check.IsNil)
  105. case <-time.After(attachWait):
  106. c.Fatal("attach is running but should have failed")
  107. }
  108. }
  109. func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
  110. testRequires(c, DaemonIsLinux)
  111. out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
  112. id := strings.TrimSpace(out)
  113. cmd := exec.Command(dockerBinary, "attach", id)
  114. stdin, err := cmd.StdinPipe()
  115. if err != nil {
  116. c.Fatal(err)
  117. }
  118. defer stdin.Close()
  119. stdout, err := cmd.StdoutPipe()
  120. c.Assert(err, check.IsNil)
  121. defer stdout.Close()
  122. c.Assert(cmd.Start(), check.IsNil)
  123. defer cmd.Process.Kill()
  124. _, err = stdin.Write([]byte("hello\n"))
  125. c.Assert(err, check.IsNil)
  126. out, err = bufio.NewReader(stdout).ReadString('\n')
  127. c.Assert(err, check.IsNil)
  128. c.Assert(strings.TrimSpace(out), check.Equals, "hello")
  129. c.Assert(stdin.Close(), check.IsNil)
  130. // Expect container to still be running after stdin is closed
  131. running := inspectField(c, id, "State.Running")
  132. c.Assert(running, check.Equals, "true")
  133. }
  134. func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
  135. testRequires(c, IsPausable)
  136. runSleepingContainer(c, "-d", "--name=test")
  137. dockerCmd(c, "pause", "test")
  138. result := dockerCmdWithResult("attach", "test")
  139. result.Assert(c, icmd.Expected{
  140. Error: "exit status 1",
  141. ExitCode: 1,
  142. Err: "You cannot attach to a paused container, unpause it first",
  143. })
  144. }