docker_cli_exec_unix_test.go 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // +build !windows,!test_no_exec
  2. package main
  3. import (
  4. "bytes"
  5. "io"
  6. "os/exec"
  7. "strings"
  8. "time"
  9. "github.com/go-check/check"
  10. "github.com/kr/pty"
  11. )
  12. // regression test for #12546
  13. func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) {
  14. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "busybox", "/bin/cat"))
  15. if err != nil {
  16. c.Fatal(err)
  17. }
  18. contId := strings.TrimSpace(out)
  19. cmd := exec.Command(dockerBinary, "exec", "-i", contId, "echo", "-n", "hello")
  20. p, err := pty.Start(cmd)
  21. if err != nil {
  22. c.Fatal(err)
  23. }
  24. b := bytes.NewBuffer(nil)
  25. go io.Copy(b, p)
  26. ch := make(chan error)
  27. go func() { ch <- cmd.Wait() }()
  28. select {
  29. case err := <-ch:
  30. if err != nil {
  31. c.Errorf("cmd finished with error %v", err)
  32. }
  33. if output := b.String(); strings.TrimSpace(output) != "hello" {
  34. c.Fatalf("Unexpected output %s", output)
  35. }
  36. case <-time.After(1 * time.Second):
  37. c.Fatal("timed out running docker exec")
  38. }
  39. }