2015-04-22 23:37:15 +00:00
|
|
|
// +build !windows,!test_no_exec
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-10-19 07:14:59 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-22 23:37:15 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// regression test for #12546
|
|
|
|
func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 06:55:40 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
|
2015-07-22 12:59:24 +00:00
|
|
|
contID := strings.TrimSpace(out)
|
2015-04-22 23:37:15 +00:00
|
|
|
|
2015-07-22 12:59:24 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
|
2015-04-22 23:37:15 +00:00
|
|
|
p, err := pty.Start(cmd)
|
2015-10-19 07:14:59 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-22 23:37:15 +00:00
|
|
|
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
go io.Copy(b, p)
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() { ch <- cmd.Wait() }()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
2015-10-19 07:14:59 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
output := b.String()
|
|
|
|
c.Assert(strings.TrimSpace(output), checker.Equals, "hello")
|
2015-11-20 22:12:12 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-22 23:37:15 +00:00
|
|
|
c.Fatal("timed out running docker exec")
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 21:55:26 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestExecTTY(c *check.C) {
|
2016-02-11 03:27:02 +00:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
2015-08-14 21:55:26 +00:00
|
|
|
dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top")
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh")
|
|
|
|
p, err := pty.Start(cmd)
|
2015-10-19 07:14:59 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-14 21:55:26 +00:00
|
|
|
defer p.Close()
|
|
|
|
|
2016-03-08 02:54:18 +00:00
|
|
|
_, err = p.Write([]byte("cat /foo && exit\n"))
|
2015-10-19 07:14:59 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-14 21:55:26 +00:00
|
|
|
|
|
|
|
chErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
chErr <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-chErr:
|
2015-10-19 07:14:59 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-14 21:55:26 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
c.Fatal("timeout waiting for exec to exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 256)
|
|
|
|
read, err := p.Read(buf)
|
2015-10-19 07:14:59 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read])))
|
2015-08-14 21:55:26 +00:00
|
|
|
}
|
2016-09-09 23:50:54 +00:00
|
|
|
|
|
|
|
// Test the the TERM env var is set when -t is provided on exec
|
|
|
|
func (s *DockerSuite) TestExecWithTERM(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
|
|
|
out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat")
|
|
|
|
contID := strings.TrimSpace(out)
|
|
|
|
cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi")
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the TERM env var is not set on exec when -t is not provided, even if it was set
|
|
|
|
// on run
|
|
|
|
func (s *DockerSuite) TestExecWithNoTERM(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
|
|
|
|
contID := strings.TrimSpace(out)
|
|
|
|
cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi")
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|
|
|
|
}
|