github.com/jingleWang/moby@v1.13.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 } 71 72 // Test the the TERM env var is set when -t is provided on exec 73 func (s *DockerSuite) TestExecWithTERM(c *check.C) { 74 testRequires(c, DaemonIsLinux, SameHostDaemon) 75 out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat") 76 contID := strings.TrimSpace(out) 77 cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi") 78 if err := cmd.Run(); err != nil { 79 c.Assert(err, checker.IsNil) 80 } 81 } 82 83 // Test that the TERM env var is not set on exec when -t is not provided, even if it was set 84 // on run 85 func (s *DockerSuite) TestExecWithNoTERM(c *check.C) { 86 testRequires(c, DaemonIsLinux, SameHostDaemon) 87 out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat") 88 contID := strings.TrimSpace(out) 89 cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi") 90 if err := cmd.Run(); err != nil { 91 c.Assert(err, checker.IsNil) 92 } 93 }