docker_cli_attach_test.go 4.3 KB

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