integration-cli/docker_cli_exec_unix_test.go
ade8146a
 // +build !windows,!test_no_exec
 
 package main
 
 import (
 	"bytes"
 	"io"
 	"os/exec"
 	"strings"
 	"time"
 
d65ac42c
 	"github.com/docker/docker/pkg/integration/checker"
ade8146a
 	"github.com/go-check/check"
 	"github.com/kr/pty"
 )
 
 // regression test for #12546
 func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) {
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)
d65ac42c
 	c.Assert(err, checker.IsNil)
ade8146a
 
 	b := bytes.NewBuffer(nil)
 	go io.Copy(b, p)
 
 	ch := make(chan error)
 	go func() { ch <- cmd.Wait() }()
 
 	select {
 	case err := <-ch:
d65ac42c
 		c.Assert(err, checker.IsNil)
 		output := b.String()
 		c.Assert(strings.TrimSpace(output), checker.Equals, "hello")
88c1bc10
 	case <-time.After(5 * time.Second):
ade8146a
 		c.Fatal("timed out running docker exec")
 	}
 }
5ffcecf1
 
 func (s *DockerSuite) TestExecTTY(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
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)
d65ac42c
 	c.Assert(err, checker.IsNil)
5ffcecf1
 	defer p.Close()
 
0e16eaca
 	_, err = p.Write([]byte("cat /foo && sleep 2 && exit\n"))
d65ac42c
 	c.Assert(err, checker.IsNil)
5ffcecf1
 
 	chErr := make(chan error)
 	go func() {
 		chErr <- cmd.Wait()
 	}()
 	select {
 	case err := <-chErr:
d65ac42c
 		c.Assert(err, checker.IsNil)
5ffcecf1
 	case <-time.After(3 * time.Second):
 		c.Fatal("timeout waiting for exec to exit")
 	}
 
 	buf := make([]byte, 256)
 	read, err := p.Read(buf)
d65ac42c
 	c.Assert(err, checker.IsNil)
 	c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read])))
5ffcecf1
 }