docker_cli_attach_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if err := cmd.Start(); err != nil {
  45. c.Fatal(err)
  46. }
  47. buf := make([]byte, 1024)
  48. if _, err := out.Read(buf); err != nil && err != io.EOF {
  49. c.Fatal(err)
  50. }
  51. startGroup.Done()
  52. if !strings.Contains(string(buf), "hello") {
  53. c.Fatalf("unexpected output %s expected hello\n", string(buf))
  54. }
  55. }()
  56. }
  57. select {
  58. case <-startDone:
  59. case <-time.After(attachWait):
  60. c.Fatalf("Attaches did not initialize properly")
  61. }
  62. dockerCmd(c, "kill", "attacher")
  63. select {
  64. case <-endDone:
  65. case <-time.After(attachWait):
  66. c.Fatalf("Attaches did not finish properly")
  67. }
  68. }
  69. func (s *DockerSuite) TestAttachTTYWithoutStdin(c *check.C) {
  70. testRequires(c, DaemonIsLinux)
  71. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  72. id := strings.TrimSpace(out)
  73. c.Assert(waitRun(id), check.IsNil)
  74. done := make(chan error)
  75. go func() {
  76. defer close(done)
  77. cmd := exec.Command(dockerBinary, "attach", id)
  78. if _, err := cmd.StdinPipe(); err != nil {
  79. done <- err
  80. return
  81. }
  82. expected := "the input device is not a TTY"
  83. if runtime.GOOS == "windows" {
  84. expected += ". If you are using mintty, try prefixing the command with 'winpty'"
  85. }
  86. if out, _, err := runCommandWithOutput(cmd); err == nil {
  87. done <- fmt.Errorf("attach should have failed")
  88. return
  89. } else if !strings.Contains(out, expected) {
  90. done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
  91. return
  92. }
  93. }()
  94. select {
  95. case err := <-done:
  96. c.Assert(err, check.IsNil)
  97. case <-time.After(attachWait):
  98. c.Fatal("attach is running but should have failed")
  99. }
  100. }
  101. func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
  102. testRequires(c, DaemonIsLinux)
  103. out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
  104. id := strings.TrimSpace(out)
  105. cmd := exec.Command(dockerBinary, "attach", id)
  106. stdin, err := cmd.StdinPipe()
  107. if err != nil {
  108. c.Fatal(err)
  109. }
  110. defer stdin.Close()
  111. stdout, err := cmd.StdoutPipe()
  112. c.Assert(err, check.IsNil)
  113. defer stdout.Close()
  114. c.Assert(cmd.Start(), check.IsNil)
  115. defer cmd.Process.Kill()
  116. _, err = stdin.Write([]byte("hello\n"))
  117. c.Assert(err, check.IsNil)
  118. out, err = bufio.NewReader(stdout).ReadString('\n')
  119. c.Assert(err, check.IsNil)
  120. c.Assert(strings.TrimSpace(out), check.Equals, "hello")
  121. c.Assert(stdin.Close(), check.IsNil)
  122. // Expect container to still be running after stdin is closed
  123. running := inspectField(c, id, "State.Running")
  124. c.Assert(running, check.Equals, "true")
  125. }
  126. func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
  127. testRequires(c, DaemonIsLinux) // Containers cannot be paused on Windows
  128. defer unpauseAllContainers()
  129. dockerCmd(c, "run", "-d", "--name=test", "busybox", "top")
  130. dockerCmd(c, "pause", "test")
  131. out, _, err := dockerCmdWithError("attach", "test")
  132. c.Assert(err, checker.NotNil, check.Commentf(out))
  133. c.Assert(out, checker.Contains, "You cannot attach to a paused container, unpause it first")
  134. }