docker_cli_attach_test.go 4.7 KB

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