docker_cli_attach_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "io"
  4. "os/exec"
  5. "strings"
  6. "sync"
  7. "testing"
  8. "time"
  9. )
  10. const attachWait = 5 * time.Second
  11. func TestAttachMultipleAndRestart(t *testing.T) {
  12. defer deleteAllContainers()
  13. endGroup := &sync.WaitGroup{}
  14. startGroup := &sync.WaitGroup{}
  15. endGroup.Add(3)
  16. startGroup.Add(3)
  17. if err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done"); err != nil {
  18. t.Fatal(err)
  19. }
  20. startDone := make(chan struct{})
  21. endDone := make(chan struct{})
  22. go func() {
  23. endGroup.Wait()
  24. close(endDone)
  25. }()
  26. go func() {
  27. startGroup.Wait()
  28. close(startDone)
  29. }()
  30. for i := 0; i < 3; i++ {
  31. go func() {
  32. c := exec.Command(dockerBinary, "attach", "attacher")
  33. defer func() {
  34. c.Wait()
  35. endGroup.Done()
  36. }()
  37. out, err := c.StdoutPipe()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if _, err := startCommand(c); err != nil {
  42. t.Fatal(err)
  43. }
  44. buf := make([]byte, 1024)
  45. if _, err := out.Read(buf); err != nil && err != io.EOF {
  46. t.Fatal(err)
  47. }
  48. startGroup.Done()
  49. if !strings.Contains(string(buf), "hello") {
  50. t.Fatalf("unexpected output %s expected hello\n", string(buf))
  51. }
  52. }()
  53. }
  54. select {
  55. case <-startDone:
  56. case <-time.After(attachWait):
  57. t.Fatalf("Attaches did not initialize properly")
  58. }
  59. cmd := exec.Command(dockerBinary, "kill", "attacher")
  60. if _, err := runCommand(cmd); err != nil {
  61. t.Fatal(err)
  62. }
  63. select {
  64. case <-endDone:
  65. case <-time.After(attachWait):
  66. t.Fatalf("Attaches did not finish properly")
  67. }
  68. logDone("attach - multiple attach")
  69. }