1
0
mirror of https://github.com/moby/moby.git synced 2025-04-18 20:44:11 +03:00
moby/integration-cli/docker_cli_exec_unix_test.go
Sebastiaan van Stijn 6ff3dfd88a
integration-cli: avoid allocations with (*os.File).WriteString (mirror)
integration-cli/docker_cli_attach_unix_test.go:107:3: avoid allocations with (*os.File).WriteString (mirror)
            cpty.Write([]byte("\n"))
            ^
    integration-cli/docker_cli_attach_unix_test.go:144:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = cpty.Write([]byte("hello\n"))
                 ^
    integration-cli/docker_cli_exec_test.go:422:16: avoid allocations with (*os.File).WriteString (mirror)
            if _, err := f.Write([]byte("success2\n")); err != nil {
                         ^
    integration-cli/docker_cli_exec_unix_test.go:57:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = p.Write([]byte("cat /foo && exit\n"))
                 ^
    integration-cli/docker_cli_run_test.go:4092:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := tmpFile.Write([]byte(data)); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:110:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = cpty.Write([]byte("hello\n"))
                 ^
    integration-cli/docker_cli_run_unix_test.go:169:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := cpty.Write([]byte("hello\n")); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:283:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := cpty.Write([]byte("hello\n")); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:364:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := cpty.Write([]byte("hello\n")); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:438:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := cpty.Write([]byte("\n")); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:880:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:915:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:952:15: avoid allocations with (*os.File).WriteString (mirror)
        if _, err := tmpFile.Write([]byte(jsonData)); err != nil {
                     ^
    integration-cli/docker_cli_run_unix_test.go:1418:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = tmpFile.Write([]byte(jsonData))
                 ^
    integration-cli/docker_cli_run_unix_test.go:1445:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = tmpFile.Write([]byte(jsonData))
                 ^
    integration-cli/docker_cli_run_unix_test.go:1483:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = tmpFile.Write([]byte(jsonData))
                 ^
    integration-cli/docker_cli_run_unix_test.go:1517:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = tmpFile.Write([]byte(jsonData))
                 ^
    integration-cli/docker_cli_update_unix_test.go:235:11: avoid allocations with (*os.File).WriteString (mirror)
        _, err = cpty.Write([]byte("exit\n"))
                 ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-09 13:22:46 +01:00

99 lines
2.7 KiB
Go

//go:build !windows
package main
import (
"bytes"
"io"
"os/exec"
"strings"
"testing"
"time"
"github.com/creack/pty"
"github.com/docker/docker/integration-cli/cli"
"gotest.tools/v3/assert"
)
// regression test for #12546
func (s *DockerCLIExecSuite) TestExecInteractiveStdinClose(c *testing.T) {
testRequires(c, DaemonIsLinux)
out := cli.DockerCmd(c, "run", "-itd", "busybox", "/bin/cat").Stdout()
contID := strings.TrimSpace(out)
cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
p, err := pty.Start(cmd)
assert.NilError(c, err)
b := bytes.NewBuffer(nil)
ch := make(chan error, 1)
go func() { ch <- cmd.Wait() }()
select {
case err := <-ch:
assert.NilError(c, err)
io.Copy(b, p)
p.Close()
bs := b.Bytes()
bs = bytes.Trim(bs, "\x00")
output := string(bs[:])
assert.Equal(c, strings.TrimSpace(output), "hello")
case <-time.After(5 * time.Second):
p.Close()
c.Fatal("timed out running docker exec")
}
}
func (s *DockerCLIExecSuite) TestExecTTY(c *testing.T) {
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
cli.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)
assert.NilError(c, err)
defer p.Close()
_, err = p.WriteString("cat /foo && exit\n")
assert.NilError(c, err)
chErr := make(chan error, 1)
go func() {
chErr <- cmd.Wait()
}()
select {
case err := <-chErr:
assert.NilError(c, err)
case <-time.After(3 * time.Second):
c.Fatal("timeout waiting for exec to exit")
}
buf := make([]byte, 256)
read, err := p.Read(buf)
assert.NilError(c, err)
assert.Assert(c, bytes.Contains(buf, []byte("hello")), string(buf[:read]))
}
// Test the TERM env var is set when -t is provided on exec
func (s *DockerCLIExecSuite) TestExecWithTERM(c *testing.T) {
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
out := cli.DockerCmd(c, "run", "-id", "busybox", "/bin/cat").Stdout()
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 {
assert.NilError(c, err)
}
}
// Test that the TERM env var is not set on exec when -t is not provided, even if it was set
// on run
func (s *DockerCLIExecSuite) TestExecWithNoTERM(c *testing.T) {
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
out := cli.DockerCmd(c, "run", "-itd", "busybox", "/bin/cat").Stdout()
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 {
assert.NilError(c, err)
}
}