github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_cli_exec_unix_test.go (about)

     1  //go:build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"bytes"
     7  	"io"
     8  	"os/exec"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/creack/pty"
    14  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/cli"
    15  	"gotest.tools/v3/assert"
    16  )
    17  
    18  // regression test for #12546
    19  func (s *DockerCLIExecSuite) TestExecInteractiveStdinClose(c *testing.T) {
    20  	testRequires(c, DaemonIsLinux)
    21  	out := cli.DockerCmd(c, "run", "-itd", "busybox", "/bin/cat").Stdout()
    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 *DockerCLIExecSuite) TestExecTTY(c *testing.T) {
    49  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    50  	cli.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 *DockerCLIExecSuite) TestExecWithTERM(c *testing.T) {
    79  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    80  	out := cli.DockerCmd(c, "run", "-id", "busybox", "/bin/cat").Stdout()
    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 *DockerCLIExecSuite) TestExecWithNoTERM(c *testing.T) {
    91  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    92  	out := cli.DockerCmd(c, "run", "-itd", "busybox", "/bin/cat").Stdout()
    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  }