docker_cli_attach_test.go 4.6 KB

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