2019-07-08 16:31:34 +00:00
|
|
|
// +build !windows
|
2015-04-22 23:37:15 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-check/check"
|
|
|
|
"github.com/kr/pty"
|
2019-04-04 13:23:19 +00:00
|
|
|
"gotest.tools/assert"
|
2015-04-22 23:37:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-22 23:37:15 +00:00
|
|
|
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() { ch <- cmd.Wait() }()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2018-05-24 02:14:55 +00:00
|
|
|
io.Copy(b, p)
|
|
|
|
p.Close()
|
|
|
|
bs := b.Bytes()
|
|
|
|
bs = bytes.Trim(bs, "\x00")
|
|
|
|
output := string(bs[:])
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, strings.TrimSpace(output), "hello")
|
2015-11-20 22:12:12 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2018-05-24 02:14:55 +00:00
|
|
|
p.Close()
|
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) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-08-14 21:55:26 +00:00
|
|
|
|
|
|
|
chErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
chErr <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-chErr:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, bytes.Contains(buf, []byte("hello")), string(buf[:read]))
|
2015-08-14 21:55:26 +00:00
|
|
|
}
|
2016-09-09 23:50:54 +00:00
|
|
|
|
2016-12-21 03:03:39 +00:00
|
|
|
// Test the TERM env var is set when -t is provided on exec
|
2016-09-09 23:50:54 +00:00
|
|
|
func (s *DockerSuite) TestExecWithTERM(c *check.C) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2016-09-09 23:50:54 +00:00
|
|
|
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 {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-09-09 23:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2016-09-09 23:50:54 +00:00
|
|
|
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 {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-09-09 23:50:54 +00:00
|
|
|
}
|
|
|
|
}
|