docker_cli_exec_unix_test.go 893 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
  15. contID := strings.TrimSpace(out)
  16. cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
  17. p, err := pty.Start(cmd)
  18. if err != nil {
  19. c.Fatal(err)
  20. }
  21. b := bytes.NewBuffer(nil)
  22. go io.Copy(b, p)
  23. ch := make(chan error)
  24. go func() { ch <- cmd.Wait() }()
  25. select {
  26. case err := <-ch:
  27. if err != nil {
  28. c.Errorf("cmd finished with error %v", err)
  29. }
  30. if output := b.String(); strings.TrimSpace(output) != "hello" {
  31. c.Fatalf("Unexpected output %s", output)
  32. }
  33. case <-time.After(1 * time.Second):
  34. c.Fatal("timed out running docker exec")
  35. }
  36. }