docker_cli_exec_unix_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/docker/docker/pkg/integration/checker"
  10. "github.com/go-check/check"
  11. "github.com/kr/pty"
  12. )
  13. // regression test for #12546
  14. func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) {
  15. testRequires(c, DaemonIsLinux)
  16. out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
  17. contID := strings.TrimSpace(out)
  18. cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
  19. p, err := pty.Start(cmd)
  20. c.Assert(err, checker.IsNil)
  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. c.Assert(err, checker.IsNil)
  28. output := b.String()
  29. c.Assert(strings.TrimSpace(output), checker.Equals, "hello")
  30. case <-time.After(1 * time.Second):
  31. c.Fatal("timed out running docker exec")
  32. }
  33. }
  34. func (s *DockerSuite) TestExecTTY(c *check.C) {
  35. testRequires(c, DaemonIsLinux)
  36. dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top")
  37. cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh")
  38. p, err := pty.Start(cmd)
  39. c.Assert(err, checker.IsNil)
  40. defer p.Close()
  41. _, err = p.Write([]byte("cat /foo && exit\n"))
  42. c.Assert(err, checker.IsNil)
  43. chErr := make(chan error)
  44. go func() {
  45. chErr <- cmd.Wait()
  46. }()
  47. select {
  48. case err := <-chErr:
  49. c.Assert(err, checker.IsNil)
  50. case <-time.After(3 * time.Second):
  51. c.Fatal("timeout waiting for exec to exit")
  52. }
  53. buf := make([]byte, 256)
  54. read, err := p.Read(buf)
  55. c.Assert(err, checker.IsNil)
  56. c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read])))
  57. }