integration-cli/docker_cli_attach_test.go
72f49e55
 package main
 
 import (
e4cfd9b3
 	"bufio"
4203230c
 	"fmt"
01094c15
 	"io"
72f49e55
 	"os/exec"
f7541b00
 	"runtime"
72f49e55
 	"strings"
 	"sync"
 	"time"
dc944ea7
 
db35c2a5
 	"github.com/docker/docker/integration-cli/cli"
dc944ea7
 	"github.com/go-check/check"
92427b3a
 	"github.com/gotestyourself/gotestyourself/icmd"
72f49e55
 )
 
01094c15
 const attachWait = 5 * time.Second
 
dc944ea7
 func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
01094c15
 	endGroup := &sync.WaitGroup{}
 	startGroup := &sync.WaitGroup{}
 	endGroup.Add(3)
 	startGroup.Add(3)
 
db35c2a5
 	cli.DockerCmd(c, "run", "--name", "attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
 	cli.WaitRun(c, "attacher")
01094c15
 
 	startDone := make(chan struct{})
 	endDone := make(chan struct{})
 
 	go func() {
 		endGroup.Wait()
 		close(endDone)
eb971633
 	}()
 
72f49e55
 	go func() {
01094c15
 		startGroup.Wait()
 		close(startDone)
72f49e55
 	}()
 
 	for i := 0; i < 3; i++ {
 		go func() {
dc944ea7
 			cmd := exec.Command(dockerBinary, "attach", "attacher")
72f49e55
 
01094c15
 			defer func() {
dc944ea7
 				cmd.Wait()
01094c15
 				endGroup.Done()
 			}()
 
dc944ea7
 			out, err := cmd.StdoutPipe()
72f49e55
 			if err != nil {
dc944ea7
 				c.Fatal(err)
01094c15
 			}
256310e9
 			defer out.Close()
01094c15
 
dc944ea7
 			if err := cmd.Start(); err != nil {
 				c.Fatal(err)
72f49e55
 			}
01094c15
 
 			buf := make([]byte, 1024)
 
 			if _, err := out.Read(buf); err != nil && err != io.EOF {
dc944ea7
 				c.Fatal(err)
01094c15
 			}
 
 			startGroup.Done()
 
 			if !strings.Contains(string(buf), "hello") {
dc944ea7
 				c.Fatalf("unexpected output %s expected hello\n", string(buf))
72f49e55
 			}
 		}()
 	}
 
01094c15
 	select {
 	case <-startDone:
 	case <-time.After(attachWait):
dc944ea7
 		c.Fatalf("Attaches did not initialize properly")
01094c15
 	}
 
db35c2a5
 	cli.DockerCmd(c, "kill", "attacher")
01094c15
 
 	select {
 	case <-endDone:
 	case <-time.After(attachWait):
dc944ea7
 		c.Fatalf("Attaches did not finish properly")
01094c15
 	}
72f49e55
 }
67e3ddb7
 
e151ad93
 func (s *DockerSuite) TestAttachTTYWithoutStdin(c *check.C) {
e4ec9195
 	// TODO @jhowardmsft. Figure out how to get this running again reliable on Windows.
 	// It works by accident at the moment. Sometimes. I've gone back to v1.13.0 and see the same.
 	// On Windows, docker run -d -ti busybox causes the container to exit immediately.
 	// Obviously a year back when I updated the test, that was not the case. However,
 	// with this, and the test racing with the tear-down which panic's, sometimes CI
 	// will just fail and `MISS` all the other tests. For now, disabling it. Will
 	// open an issue to track re-enabling this and root-causing the problem.
 	testRequires(c, DaemonIsLinux)
5c295460
 	out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
67e3ddb7
 
 	id := strings.TrimSpace(out)
799d9605
 	c.Assert(waitRun(id), check.IsNil)
67e3ddb7
 
4203230c
 	done := make(chan error)
67e3ddb7
 	go func() {
 		defer close(done)
 
 		cmd := exec.Command(dockerBinary, "attach", id)
 		if _, err := cmd.StdinPipe(); err != nil {
4203230c
 			done <- err
 			return
67e3ddb7
 		}
 
f7541b00
 		expected := "the input device is not a TTY"
 		if runtime.GOOS == "windows" {
 			expected += ".  If you are using mintty, try prefixing the command with 'winpty'"
 		}
67e3ddb7
 		if out, _, err := runCommandWithOutput(cmd); err == nil {
4203230c
 			done <- fmt.Errorf("attach should have failed")
 			return
67e3ddb7
 		} else if !strings.Contains(out, expected) {
4203230c
 			done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
 			return
67e3ddb7
 		}
 	}()
 
 	select {
4203230c
 	case err := <-done:
 		c.Assert(err, check.IsNil)
67e3ddb7
 	case <-time.After(attachWait):
dc944ea7
 		c.Fatal("attach is running but should have failed")
67e3ddb7
 	}
 }
e4cfd9b3
 
dc944ea7
 func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
dc944ea7
 	out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
e4cfd9b3
 	id := strings.TrimSpace(out)
 
 	cmd := exec.Command(dockerBinary, "attach", id)
 	stdin, err := cmd.StdinPipe()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
e4cfd9b3
 	}
 	defer stdin.Close()
 	stdout, err := cmd.StdoutPipe()
a1d4b7dd
 	c.Assert(err, check.IsNil)
e4cfd9b3
 	defer stdout.Close()
a1d4b7dd
 	c.Assert(cmd.Start(), check.IsNil)
ddae20c0
 	defer func() {
 		cmd.Process.Kill()
 		cmd.Wait()
 	}()
e4cfd9b3
 
a1d4b7dd
 	_, err = stdin.Write([]byte("hello\n"))
 	c.Assert(err, check.IsNil)
e4cfd9b3
 	out, err = bufio.NewReader(stdout).ReadString('\n')
a1d4b7dd
 	c.Assert(err, check.IsNil)
 	c.Assert(strings.TrimSpace(out), check.Equals, "hello")
e4cfd9b3
 
a1d4b7dd
 	c.Assert(stdin.Close(), check.IsNil)
e4cfd9b3
 
 	// Expect container to still be running after stdin is closed
62a856e9
 	running := inspectField(c, id, "State.Running")
a1d4b7dd
 	c.Assert(running, check.Equals, "true")
e4cfd9b3
 }
de1d6119
 
 func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
69985e85
 	testRequires(c, IsPausable)
 	runSleepingContainer(c, "-d", "--name=test")
de1d6119
 	dockerCmd(c, "pause", "test")
d7022f2b
 
 	result := dockerCmdWithResult("attach", "test")
92427b3a
 	result.Assert(c, icmd.Expected{
d7022f2b
 		Error:    "exit status 1",
 		ExitCode: 1,
 		Err:      "You cannot attach to a paused container, unpause it first",
 	})
de1d6119
 }