2021-08-23 09:14:53 -04:00
|
|
|
//go:build !windows
|
2019-07-08 12:31:34 -04:00
|
|
|
// +build !windows
|
2015-04-22 19:37:15 -04:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-04-22 19:37:15 -04:00
|
|
|
"time"
|
|
|
|
|
2019-07-29 19:59:08 -04:00
|
|
|
"github.com/creack/pty"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2015-04-22 19:37:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// regression test for #12546
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIExecSuite) TestExecInteractiveStdinClose(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
|
2015-07-22 08:59:24 -04:00
|
|
|
contID := strings.TrimSpace(out)
|
2015-04-22 19:37:15 -04:00
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
|
2015-04-22 19:37:15 -04:00
|
|
|
p, err := pty.Start(cmd)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-22 19:37:15 -04:00
|
|
|
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
|
2020-02-25 17:13:25 -05:00
|
|
|
ch := make(chan error, 1)
|
2015-04-22 19:37:15 -04:00
|
|
|
go func() { ch <- cmd.Wait() }()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2018-05-23 22:14:55 -04:00
|
|
|
io.Copy(b, p)
|
|
|
|
p.Close()
|
|
|
|
bs := b.Bytes()
|
|
|
|
bs = bytes.Trim(bs, "\x00")
|
|
|
|
output := string(bs[:])
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(output), "hello")
|
2015-11-20 17:12:12 -05:00
|
|
|
case <-time.After(5 * time.Second):
|
2018-05-23 22:14:55 -04:00
|
|
|
p.Close()
|
2015-04-22 19:37:15 -04:00
|
|
|
c.Fatal("timed out running docker exec")
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 17:55:26 -04:00
|
|
|
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIExecSuite) TestExecTTY(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2015-08-14 17:55:26 -04: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 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-14 17:55:26 -04:00
|
|
|
defer p.Close()
|
|
|
|
|
2016-03-07 21:54:18 -05:00
|
|
|
_, err = p.Write([]byte("cat /foo && exit\n"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-14 17:55:26 -04:00
|
|
|
|
2020-02-25 17:13:25 -05:00
|
|
|
chErr := make(chan error, 1)
|
2015-08-14 17:55:26 -04:00
|
|
|
go func() {
|
|
|
|
chErr <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-chErr:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-14 17:55:26 -04: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 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, bytes.Contains(buf, []byte("hello")), string(buf[:read]))
|
2015-08-14 17:55:26 -04:00
|
|
|
}
|
2016-09-09 19:50:54 -04:00
|
|
|
|
2016-12-20 22:03:39 -05:00
|
|
|
// Test the TERM env var is set when -t is provided on exec
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIExecSuite) TestExecWithTERM(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2016-09-09 19:50:54 -04: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 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-09-09 19:50:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the TERM env var is not set on exec when -t is not provided, even if it was set
|
|
|
|
// on run
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIExecSuite) TestExecWithNoTERM(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2016-09-09 19:50:54 -04: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 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-09-09 19:50:54 -04:00
|
|
|
}
|
|
|
|
}
|