docker_cli_attach_test.go 1018 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "sync"
  6. "testing"
  7. "time"
  8. )
  9. func TestMultipleAttachRestart(t *testing.T) {
  10. cmd := exec.Command(dockerBinary, "run", "--name", "attacher", "-d", "busybox",
  11. "/bin/sh", "-c", "sleep 2 && echo hello")
  12. group := sync.WaitGroup{}
  13. group.Add(4)
  14. defer func() {
  15. cmd = exec.Command(dockerBinary, "kill", "attacher")
  16. if _, err := runCommand(cmd); err != nil {
  17. t.Fatal(err)
  18. }
  19. deleteAllContainers()
  20. }()
  21. go func() {
  22. defer group.Done()
  23. out, _, err := runCommandWithOutput(cmd)
  24. if err != nil {
  25. t.Fatal(err, out)
  26. }
  27. }()
  28. time.Sleep(500 * time.Millisecond)
  29. for i := 0; i < 3; i++ {
  30. go func() {
  31. defer group.Done()
  32. c := exec.Command(dockerBinary, "attach", "attacher")
  33. out, _, err := runCommandWithOutput(c)
  34. if err != nil {
  35. t.Fatal(err, out)
  36. }
  37. if actual := strings.Trim(out, "\r\n"); actual != "hello" {
  38. t.Fatalf("unexpected output %s expected hello", actual)
  39. }
  40. }()
  41. }
  42. group.Wait()
  43. logDone("attach - multiple attach")
  44. }