integration-cli/docker_cli_exec_unix_test.go
72043419
 // +build !windows
ade8146a
 
 package main
 
 import (
 	"bytes"
 	"io"
 	"os/exec"
 	"strings"
59e55dcd
 	"testing"
ade8146a
 	"time"
 
0595c017
 	"github.com/creack/pty"
9f0b3f56
 	"gotest.tools/v3/assert"
ade8146a
 )
 
 // regression test for #12546
1d92789b
 func (s *DockerSuite) TestExecInteractiveStdinClose(c *testing.T) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
668e2369
 	out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
6b3c9281
 	contID := strings.TrimSpace(out)
ade8146a
 
6b3c9281
 	cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
ade8146a
 	p, err := pty.Start(cmd)
6345208b
 	assert.NilError(c, err)
ade8146a
 
 	b := bytes.NewBuffer(nil)
 
c322af80
 	ch := make(chan error, 1)
ade8146a
 	go func() { ch <- cmd.Wait() }()
 
 	select {
 	case err := <-ch:
6345208b
 		assert.NilError(c, err)
386e0f36
 		io.Copy(b, p)
 		p.Close()
 		bs := b.Bytes()
 		bs = bytes.Trim(bs, "\x00")
 		output := string(bs[:])
6345208b
 		assert.Equal(c, strings.TrimSpace(output), "hello")
88c1bc10
 	case <-time.After(5 * time.Second):
386e0f36
 		p.Close()
ade8146a
 		c.Fatal("timed out running docker exec")
 	}
 }
5ffcecf1
 
1d92789b
 func (s *DockerSuite) TestExecTTY(c *testing.T) {
43b15e92
 	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
5ffcecf1
 	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)
6345208b
 	assert.NilError(c, err)
5ffcecf1
 	defer p.Close()
 
021a12ef
 	_, err = p.Write([]byte("cat /foo && exit\n"))
6345208b
 	assert.NilError(c, err)
5ffcecf1
 
c322af80
 	chErr := make(chan error, 1)
5ffcecf1
 	go func() {
 		chErr <- cmd.Wait()
 	}()
 	select {
 	case err := <-chErr:
6345208b
 		assert.NilError(c, err)
5ffcecf1
 	case <-time.After(3 * time.Second):
 		c.Fatal("timeout waiting for exec to exit")
 	}
 
 	buf := make([]byte, 256)
 	read, err := p.Read(buf)
6345208b
 	assert.NilError(c, err)
 	assert.Assert(c, bytes.Contains(buf, []byte("hello")), string(buf[:read]))
5ffcecf1
 }
4633f15f
 
ea2dd4b5
 // Test the TERM env var is set when -t is provided on exec
1d92789b
 func (s *DockerSuite) TestExecWithTERM(c *testing.T) {
43b15e92
 	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
4633f15f
 	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 {
6345208b
 		assert.NilError(c, err)
4633f15f
 	}
 }
 
 // Test that the TERM env var is not set on exec when -t is not provided, even if it was set
 // on run
1d92789b
 func (s *DockerSuite) TestExecWithNoTERM(c *testing.T) {
43b15e92
 	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
4633f15f
 	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 {
6345208b
 		assert.NilError(c, err)
4633f15f
 	}
 }