docker_cli_attach_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/pkg/integration/checker"
  12. "github.com/go-check/check"
  13. )
  14. const attachWait = 5 * time.Second
  15. func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
  16. testRequires(c, DaemonIsLinux)
  17. endGroup := &sync.WaitGroup{}
  18. startGroup := &sync.WaitGroup{}
  19. endGroup.Add(3)
  20. startGroup.Add(3)
  21. err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
  22. c.Assert(err, check.IsNil)
  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. 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. testRequires(c, DaemonIsLinux)
  72. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  73. id := strings.TrimSpace(out)
  74. c.Assert(waitRun(id), check.IsNil)
  75. done := make(chan error)
  76. go func() {
  77. defer close(done)
  78. cmd := exec.Command(dockerBinary, "attach", id)
  79. if _, err := cmd.StdinPipe(); err != nil {
  80. done <- err
  81. return
  82. }
  83. expected := "the input device is not a TTY"
  84. if runtime.GOOS == "windows" {
  85. expected += ". If you are using mintty, try prefixing the command with 'winpty'"
  86. }
  87. if out, _, err := runCommandWithOutput(cmd); err == nil {
  88. done <- fmt.Errorf("attach should have failed")
  89. return
  90. } else if !strings.Contains(out, expected) {
  91. done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
  92. return
  93. }
  94. }()
  95. select {
  96. case err := <-done:
  97. c.Assert(err, check.IsNil)
  98. case <-time.After(attachWait):
  99. c.Fatal("attach is running but should have failed")
  100. }
  101. }
  102. func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
  103. testRequires(c, DaemonIsLinux)
  104. out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
  105. id := strings.TrimSpace(out)
  106. cmd := exec.Command(dockerBinary, "attach", id)
  107. stdin, err := cmd.StdinPipe()
  108. if err != nil {
  109. c.Fatal(err)
  110. }
  111. defer stdin.Close()
  112. stdout, err := cmd.StdoutPipe()
  113. c.Assert(err, check.IsNil)
  114. defer stdout.Close()
  115. c.Assert(cmd.Start(), check.IsNil)
  116. defer cmd.Process.Kill()
  117. _, err = stdin.Write([]byte("hello\n"))
  118. c.Assert(err, check.IsNil)
  119. out, err = bufio.NewReader(stdout).ReadString('\n')
  120. c.Assert(err, check.IsNil)
  121. c.Assert(strings.TrimSpace(out), check.Equals, "hello")
  122. c.Assert(stdin.Close(), check.IsNil)
  123. // Expect container to still be running after stdin is closed
  124. running := inspectField(c, id, "State.Running")
  125. c.Assert(running, check.Equals, "true")
  126. }
  127. func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
  128. testRequires(c, DaemonIsLinux) // Containers cannot be paused on Windows
  129. defer unpauseAllContainers()
  130. dockerCmd(c, "run", "-d", "--name=test", "busybox", "top")
  131. dockerCmd(c, "pause", "test")
  132. out, _, err := dockerCmdWithError("attach", "test")
  133. c.Assert(err, checker.NotNil, check.Commentf(out))
  134. c.Assert(out, checker.Contains, "You cannot attach to a paused container, unpause it first")
  135. }