docker_cli_attach_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os/exec"
  7. "runtime"
  8. "strings"
  9. "sync"
  10. "time"
  11. icmd "github.com/docker/docker/pkg/integration/cmd"
  12. "github.com/go-check/check"
  13. )
  14. const attachWait = 5 * time.Second
  15. func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
  16. endGroup := &sync.WaitGroup{}
  17. startGroup := &sync.WaitGroup{}
  18. endGroup.Add(3)
  19. startGroup.Add(3)
  20. err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
  21. c.Assert(err, check.IsNil)
  22. startDone := make(chan struct{})
  23. endDone := make(chan struct{})
  24. go func() {
  25. endGroup.Wait()
  26. close(endDone)
  27. }()
  28. go func() {
  29. startGroup.Wait()
  30. close(startDone)
  31. }()
  32. for i := 0; i < 3; i++ {
  33. go func() {
  34. cmd := exec.Command(dockerBinary, "attach", "attacher")
  35. defer func() {
  36. cmd.Wait()
  37. endGroup.Done()
  38. }()
  39. out, err := cmd.StdoutPipe()
  40. if err != nil {
  41. c.Fatal(err)
  42. }
  43. defer out.Close()
  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. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  71. id := strings.TrimSpace(out)
  72. c.Assert(waitRun(id), check.IsNil)
  73. done := make(chan error)
  74. go func() {
  75. defer close(done)
  76. cmd := exec.Command(dockerBinary, "attach", id)
  77. if _, err := cmd.StdinPipe(); err != nil {
  78. done <- err
  79. return
  80. }
  81. expected := "the input device is not a TTY"
  82. if runtime.GOOS == "windows" {
  83. expected += ". If you are using mintty, try prefixing the command with 'winpty'"
  84. }
  85. if out, _, err := runCommandWithOutput(cmd); err == nil {
  86. done <- fmt.Errorf("attach should have failed")
  87. return
  88. } else if !strings.Contains(out, expected) {
  89. done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
  90. return
  91. }
  92. }()
  93. select {
  94. case err := <-done:
  95. c.Assert(err, check.IsNil)
  96. case <-time.After(attachWait):
  97. c.Fatal("attach is running but should have failed")
  98. }
  99. }
  100. func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
  101. testRequires(c, DaemonIsLinux)
  102. out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
  103. id := strings.TrimSpace(out)
  104. cmd := exec.Command(dockerBinary, "attach", id)
  105. stdin, err := cmd.StdinPipe()
  106. if err != nil {
  107. c.Fatal(err)
  108. }
  109. defer stdin.Close()
  110. stdout, err := cmd.StdoutPipe()
  111. c.Assert(err, check.IsNil)
  112. defer stdout.Close()
  113. c.Assert(cmd.Start(), check.IsNil)
  114. defer cmd.Process.Kill()
  115. _, err = stdin.Write([]byte("hello\n"))
  116. c.Assert(err, check.IsNil)
  117. out, err = bufio.NewReader(stdout).ReadString('\n')
  118. c.Assert(err, check.IsNil)
  119. c.Assert(strings.TrimSpace(out), check.Equals, "hello")
  120. c.Assert(stdin.Close(), check.IsNil)
  121. // Expect container to still be running after stdin is closed
  122. running := inspectField(c, id, "State.Running")
  123. c.Assert(running, check.Equals, "true")
  124. }
  125. func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
  126. testRequires(c, IsPausable)
  127. defer unpauseAllContainers()
  128. runSleepingContainer(c, "-d", "--name=test")
  129. dockerCmd(c, "pause", "test")
  130. result := dockerCmdWithResult("attach", "test")
  131. c.Assert(result, icmd.Matches, icmd.Expected{
  132. Error: "exit status 1",
  133. ExitCode: 1,
  134. Err: "You cannot attach to a paused container, unpause it first",
  135. })
  136. }