docker_cli_attach_test.go 989 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 1 && echo hello")
  12. group := sync.WaitGroup{}
  13. group.Add(4)
  14. go func() {
  15. defer group.Done()
  16. out, _, err := runCommandWithOutput(cmd)
  17. if err != nil {
  18. t.Fatal(err, out)
  19. }
  20. }()
  21. time.Sleep(500 * time.Millisecond)
  22. for i := 0; i < 3; i++ {
  23. go func() {
  24. defer group.Done()
  25. c := exec.Command(dockerBinary, "attach", "attacher")
  26. out, _, err := runCommandWithOutput(c)
  27. if err != nil {
  28. t.Fatal(err, out)
  29. }
  30. if actual := strings.Trim(out, "\r\n"); actual != "hello" {
  31. t.Fatalf("unexpected output %s expected hello", actual)
  32. }
  33. }()
  34. }
  35. group.Wait()
  36. cmd = exec.Command(dockerBinary, "kill", "attacher")
  37. if _, err := runCommand(cmd); err != nil {
  38. t.Fatal(err)
  39. }
  40. deleteAllContainers()
  41. logDone("run - multiple attach")
  42. }