github.com/gondor/docker@v1.9.0-rc1/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/go-check/check" 13 "github.com/kr/pty" 14 ) 15 16 // regression test for #12546 17 func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) { 18 testRequires(c, DaemonIsLinux) 19 out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat") 20 contID := strings.TrimSpace(out) 21 22 cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello") 23 p, err := pty.Start(cmd) 24 if err != nil { 25 c.Fatal(err) 26 } 27 28 b := bytes.NewBuffer(nil) 29 go io.Copy(b, p) 30 31 ch := make(chan error) 32 go func() { ch <- cmd.Wait() }() 33 34 select { 35 case err := <-ch: 36 if err != nil { 37 c.Errorf("cmd finished with error %v", err) 38 } 39 if output := b.String(); strings.TrimSpace(output) != "hello" { 40 c.Fatalf("Unexpected output %s", output) 41 } 42 case <-time.After(1 * time.Second): 43 c.Fatal("timed out running docker exec") 44 } 45 } 46 47 func (s *DockerSuite) TestExecTTY(c *check.C) { 48 testRequires(c, DaemonIsLinux) 49 dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top") 50 51 cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh") 52 p, err := pty.Start(cmd) 53 c.Assert(err, check.IsNil) 54 defer p.Close() 55 56 _, err = p.Write([]byte("cat /foo && exit\n")) 57 c.Assert(err, check.IsNil) 58 59 chErr := make(chan error) 60 go func() { 61 chErr <- cmd.Wait() 62 }() 63 select { 64 case err := <-chErr: 65 c.Assert(err, check.IsNil) 66 case <-time.After(3 * time.Second): 67 c.Fatal("timeout waiting for exec to exit") 68 } 69 70 buf := make([]byte, 256) 71 read, err := p.Read(buf) 72 c.Assert(err, check.IsNil) 73 c.Assert(bytes.Contains(buf, []byte("hello")), check.Equals, true, check.Commentf(string(buf[:read]))) 74 }