github.com/portworx/docker@v1.12.1/integration-cli/docker_cli_exec_unix_test.go (about) 1 // +build !windows,!test_no_exec 2 3 package main 4 5 import ( 6 "bytes" 7 "io" 8 "os/exec" 9 "strings" 10 "time" 11 12 "github.com/docker/docker/pkg/integration/checker" 13 "github.com/go-check/check" 14 "github.com/kr/pty" 15 ) 16 17 // regression test for #12546 18 func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) { 19 testRequires(c, DaemonIsLinux) 20 out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat") 21 contID := strings.TrimSpace(out) 22 23 cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello") 24 p, err := pty.Start(cmd) 25 c.Assert(err, checker.IsNil) 26 27 b := bytes.NewBuffer(nil) 28 go io.Copy(b, p) 29 30 ch := make(chan error) 31 go func() { ch <- cmd.Wait() }() 32 33 select { 34 case err := <-ch: 35 c.Assert(err, checker.IsNil) 36 output := b.String() 37 c.Assert(strings.TrimSpace(output), checker.Equals, "hello") 38 case <-time.After(5 * time.Second): 39 c.Fatal("timed out running docker exec") 40 } 41 } 42 43 func (s *DockerSuite) TestExecTTY(c *check.C) { 44 testRequires(c, DaemonIsLinux, SameHostDaemon) 45 dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top") 46 47 cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh") 48 p, err := pty.Start(cmd) 49 c.Assert(err, checker.IsNil) 50 defer p.Close() 51 52 _, err = p.Write([]byte("cat /foo && exit\n")) 53 c.Assert(err, checker.IsNil) 54 55 chErr := make(chan error) 56 go func() { 57 chErr <- cmd.Wait() 58 }() 59 select { 60 case err := <-chErr: 61 c.Assert(err, checker.IsNil) 62 case <-time.After(3 * time.Second): 63 c.Fatal("timeout waiting for exec to exit") 64 } 65 66 buf := make([]byte, 256) 67 read, err := p.Read(buf) 68 c.Assert(err, checker.IsNil) 69 c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read]))) 70 }