2015-04-22 19:37:15 -04:00
|
|
|
// +build !windows,!test_no_exec
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-10-19 03:14:59 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-22 19:37:15 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// regression test for #12546
|
|
|
|
func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
|
2015-07-22 08:59:24 -04:00
|
|
|
contID := strings.TrimSpace(out)
|
2015-04-22 19:37:15 -04:00
|
|
|
|
2015-07-22 08:59:24 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
|
2015-04-22 19:37:15 -04:00
|
|
|
p, err := pty.Start(cmd)
|
2015-10-19 03:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-22 19:37:15 -04:00
|
|
|
|
|
|
|
b := bytes.NewBuffer(nil)
|
|
|
|
go io.Copy(b, p)
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() { ch <- cmd.Wait() }()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
2015-10-19 03:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
output := b.String()
|
|
|
|
c.Assert(strings.TrimSpace(output), checker.Equals, "hello")
|
2015-11-20 17:12:12 -05:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-22 19:37:15 -04:00
|
|
|
c.Fatal("timed out running docker exec")
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 17:55:26 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestExecTTY(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-14 17:55:26 -04:00
|
|
|
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)
|
2015-10-19 03:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-14 17:55:26 -04:00
|
|
|
defer p.Close()
|
|
|
|
|
2015-12-10 04:13:58 -05:00
|
|
|
_, err = p.Write([]byte("cat /foo && sleep 2 && exit\n"))
|
2015-10-19 03:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-14 17:55:26 -04:00
|
|
|
|
|
|
|
chErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
chErr <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-chErr:
|
2015-10-19 03:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-14 17:55:26 -04:00
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
c.Fatal("timeout waiting for exec to exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 256)
|
|
|
|
read, err := p.Read(buf)
|
2015-10-19 03:14:59 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read])))
|
2015-08-14 17:55:26 -04:00
|
|
|
}
|