github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/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/integration-cli/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 29 ch := make(chan error) 30 go func() { ch <- cmd.Wait() }() 31 32 select { 33 case err := <-ch: 34 c.Assert(err, checker.IsNil) 35 io.Copy(b, p) 36 p.Close() 37 bs := b.Bytes() 38 bs = bytes.Trim(bs, "\x00") 39 output := string(bs[:]) 40 c.Assert(strings.TrimSpace(output), checker.Equals, "hello") 41 case <-time.After(5 * time.Second): 42 p.Close() 43 c.Fatal("timed out running docker exec") 44 } 45 } 46 47 func (s *DockerSuite) TestExecTTY(c *check.C) { 48 testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) 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, checker.IsNil) 54 defer p.Close() 55 56 _, err = p.Write([]byte("cat /foo && exit\n")) 57 c.Assert(err, checker.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, checker.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, checker.IsNil) 73 c.Assert(bytes.Contains(buf, []byte("hello")), checker.Equals, true, check.Commentf(string(buf[:read]))) 74 } 75 76 // Test the TERM env var is set when -t is provided on exec 77 func (s *DockerSuite) TestExecWithTERM(c *check.C) { 78 testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) 79 out, _ := dockerCmd(c, "run", "-id", "busybox", "/bin/cat") 80 contID := strings.TrimSpace(out) 81 cmd := exec.Command(dockerBinary, "exec", "-t", contID, "sh", "-c", "if [ -z $TERM ]; then exit 1; else exit 0; fi") 82 if err := cmd.Run(); err != nil { 83 c.Assert(err, checker.IsNil) 84 } 85 } 86 87 // Test that the TERM env var is not set on exec when -t is not provided, even if it was set 88 // on run 89 func (s *DockerSuite) TestExecWithNoTERM(c *check.C) { 90 testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon) 91 out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat") 92 contID := strings.TrimSpace(out) 93 cmd := exec.Command(dockerBinary, "exec", contID, "sh", "-c", "if [ -z $TERM ]; then exit 0; else exit 1; fi") 94 if err := cmd.Run(); err != nil { 95 c.Assert(err, checker.IsNil) 96 } 97 }