gopkg.in/docker/docker.v20@v20.10.27/integration-cli/docker_cli_exec_unix_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"os/exec"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/creack/pty"
    15  	"gotest.tools/v3/assert"
    16  )
    17  
    18  // regression test for #12546
    19  func (s *DockerSuite) TestExecInteractiveStdinClose(c *testing.T) {
    20  	testRequires(c, DaemonIsLinux)
    21  	out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
    22  	contID := strings.TrimSpace(out)
    23  
    24  	cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
    25  	p, err := pty.Start(cmd)
    26  	assert.NilError(c, err)
    27  
    28  	b := bytes.NewBuffer(nil)
    29  
    30  	ch := make(chan error, 1)
    31  	go func() { ch <- cmd.Wait() }()
    32  
    33  	select {
    34  	case err := <-ch:
    35  		assert.NilError(c, err)
    36  		io.Copy(b, p)
    37  		p.Close()
    38  		bs := b.Bytes()
    39  		bs = bytes.Trim(bs, "\x00")
    40  		output := string(bs[:])
    41  		assert.Equal(c, strings.TrimSpace(output), "hello")
    42  	case <-time.After(5 * time.Second):
    43  		p.Close()
    44  		c.Fatal("timed out running docker exec")
    45  	}
    46  }
    47  
    48  func (s *DockerSuite) TestExecTTY(c *testing.T) {
    49  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    50  	dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top")
    51  
    52  	cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh")
    53  	p, err := pty.Start(cmd)
    54  	assert.NilError(c, err)
    55  	defer p.Close()
    56  
    57  	_, err = p.Write([]byte("cat /foo && exit\n"))
    58  	assert.NilError(c, err)
    59  
    60  	chErr := make(chan error, 1)
    61  	go func() {
    62  		chErr <- cmd.Wait()
    63  	}()
    64  	select {
    65  	case err := <-chErr:
    66  		assert.NilError(c, err)
    67  	case <-time.After(3 * time.Second):
    68  		c.Fatal("timeout waiting for exec to exit")
    69  	}
    70  
    71  	buf := make([]byte, 256)
    72  	read, err := p.Read(buf)
    73  	assert.NilError(c, err)
    74  	assert.Assert(c, bytes.Contains(buf, []byte("hello")), string(buf[:read]))
    75  }
    76  
    77  // Test the TERM env var is set when -t is provided on exec
    78  func (s *DockerSuite) TestExecWithTERM(c *testing.T) {
    79  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    80  	out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat")
    81  	contID := strings.TrimSpace(out)
    82  	cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi")
    83  	if err := cmd.Run(); err != nil {
    84  		assert.NilError(c, err)
    85  	}
    86  }
    87  
    88  // Test that the TERM env var is not set on exec when -t is not provided, even if it was set
    89  // on run
    90  func (s *DockerSuite) TestExecWithNoTERM(c *testing.T) {
    91  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    92  	out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
    93  	contID := strings.TrimSpace(out)
    94  	cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi")
    95  	if err := cmd.Run(); err != nil {
    96  		assert.NilError(c, err)
    97  	}
    98  }