docker_cli_exec_unix_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // +build !windows
  2. package main
  3. import (
  4. "bytes"
  5. "io"
  6. "os/exec"
  7. "strings"
  8. "time"
  9. "github.com/creack/pty"
  10. "github.com/go-check/check"
  11. "gotest.tools/assert"
  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. assert.NilError(c, err)
  21. b := bytes.NewBuffer(nil)
  22. ch := make(chan error)
  23. go func() { ch <- cmd.Wait() }()
  24. select {
  25. case err := <-ch:
  26. assert.NilError(c, err)
  27. io.Copy(b, p)
  28. p.Close()
  29. bs := b.Bytes()
  30. bs = bytes.Trim(bs, "\x00")
  31. output := string(bs[:])
  32. assert.Equal(c, strings.TrimSpace(output), "hello")
  33. case <-time.After(5 * time.Second):
  34. p.Close()
  35. c.Fatal("timed out running docker exec")
  36. }
  37. }
  38. func (s *DockerSuite) TestExecTTY(c *check.C) {
  39. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  40. dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top")
  41. cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh")
  42. p, err := pty.Start(cmd)
  43. assert.NilError(c, err)
  44. defer p.Close()
  45. _, err = p.Write([]byte("cat /foo && exit\n"))
  46. assert.NilError(c, err)
  47. chErr := make(chan error)
  48. go func() {
  49. chErr <- cmd.Wait()
  50. }()
  51. select {
  52. case err := <-chErr:
  53. assert.NilError(c, err)
  54. case <-time.After(3 * time.Second):
  55. c.Fatal("timeout waiting for exec to exit")
  56. }
  57. buf := make([]byte, 256)
  58. read, err := p.Read(buf)
  59. assert.NilError(c, err)
  60. assert.Assert(c, bytes.Contains(buf, []byte("hello")), string(buf[:read]))
  61. }
  62. // Test the TERM env var is set when -t is provided on exec
  63. func (s *DockerSuite) TestExecWithTERM(c *check.C) {
  64. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  65. out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat")
  66. contID := strings.TrimSpace(out)
  67. cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi")
  68. if err := cmd.Run(); err != nil {
  69. assert.NilError(c, err)
  70. }
  71. }
  72. // Test that the TERM env var is not set on exec when -t is not provided, even if it was set
  73. // on run
  74. func (s *DockerSuite) TestExecWithNoTERM(c *check.C) {
  75. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  76. out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
  77. contID := strings.TrimSpace(out)
  78. cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi")
  79. if err := cmd.Run(); err != nil {
  80. assert.NilError(c, err)
  81. }
  82. }