2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2015-04-22 23:37:15 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-04-22 23:37:15 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-29 23:59:08 +00:00
|
|
|
"github.com/creack/pty"
|
2023-07-27 11:32:46 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-04-22 23:37:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// regression test for #12546
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIExecSuite) TestExecInteractiveStdinClose(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 11:32:46 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-itd", "busybox", "/bin/cat").Stdout()
|
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)
|
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
ch := make(chan error, 1)
|
2015-04-22 23:37:15 +00:00
|
|
|
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
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIExecSuite) TestExecTTY(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2023-07-27 11:32:46 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top")
|
2015-08-14 21:55:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
chErr := make(chan error, 1)
|
2015-08-14 21:55:26 +00:00
|
|
|
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
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIExecSuite) TestExecWithTERM(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2023-07-27 11:32:46 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-id", "busybox", "/bin/cat").Stdout()
|
2016-09-09 23:50:54 +00:00
|
|
|
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
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIExecSuite) TestExecWithNoTERM(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
|
2023-07-27 11:32:46 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-itd", "busybox", "/bin/cat").Stdout()
|
2016-09-09 23:50:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|