1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_exec_unix_test.go
Tibor Vass e25352a42a
rm-gocheck: run goimports to compile successfully
goimports -w \
-- "./integration-cli/daemon" "./pkg/discovery" "./pkg/discovery/file" "./pkg/discovery/kv" "./pkg/discovery/memory" "./pkg/discovery/nodes" "./integration-cli" \
&& \
 gofmt -w -s \
-- "./integration-cli/daemon" "./pkg/discovery" "./pkg/discovery/file" "./pkg/discovery/kv" "./pkg/discovery/memory" "./pkg/discovery/nodes" "./integration-cli"

Signed-off-by: Tibor Vass <tibor@docker.com>
(cherry picked from commit 59e55dcdd0)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-09-26 23:52:28 +02:00

97 lines
2.6 KiB
Go

// +build !windows
package main
import (
"bytes"
"io"
"os/exec"
"strings"
"testing"
"time"
"github.com/creack/pty"
"gotest.tools/assert"
)
// regression test for #12546
func (s *DockerSuite) TestExecInteractiveStdinClose(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
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)
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 *DockerSuite) TestExecTTY(c *testing.T) {
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
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.Write([]byte("cat /foo && exit\n"))
assert.NilError(c, err)
chErr := make(chan error)
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 *DockerSuite) TestExecWithTERM(c *testing.T) {
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
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 {
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 *DockerSuite) TestExecWithNoTERM(c *testing.T) {
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
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 {
assert.NilError(c, err)
}
}