github.com/slene/docker@v1.8.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  	out, _ := dockerCmd(c, "run", "-itd", "busybox", "/bin/cat")
    19  	contID := strings.TrimSpace(out)
    20  
    21  	cmd := exec.Command(dockerBinary, "exec", "-i", contID, "echo", "-n", "hello")
    22  	p, err := pty.Start(cmd)
    23  	if err != nil {
    24  		c.Fatal(err)
    25  	}
    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  		if err != nil {
    36  			c.Errorf("cmd finished with error %v", err)
    37  		}
    38  		if output := b.String(); strings.TrimSpace(output) != "hello" {
    39  			c.Fatalf("Unexpected output %s", output)
    40  		}
    41  	case <-time.After(1 * time.Second):
    42  		c.Fatal("timed out running docker exec")
    43  	}
    44  }