integration-cli/docker_cli_attach_unix_test.go
798215af
 // +build !windows
 
28cf8fdd
 package main
 
 import (
ae0883ce
 	"bufio"
9d8f01b4
 	"io/ioutil"
28cf8fdd
 	"os/exec"
 	"strings"
e25352a4
 	"testing"
28cf8fdd
 	"time"
 
892dbfb8
 	"github.com/creack/pty"
6345208b
 	"gotest.tools/assert"
28cf8fdd
 )
 
50852f3b
 // #9860 Make sure attach ends when container ends (with no errors)
64a928a3
 func (s *DockerSuite) TestAttachClosedOnContainerStop(c *testing.T) {
43b15e92
 	testRequires(c, testEnv.IsLocalDaemon)
28cf8fdd
 
50852f3b
 	out, _ := dockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`)
28cf8fdd
 
475c6531
 	id := strings.TrimSpace(out)
6345208b
 	assert.NilError(c, waitRun(id))
28cf8fdd
 
9d8f01b4
 	pty, tty, err := pty.Open()
6345208b
 	assert.NilError(c, err)
50852f3b
 
 	attachCmd := exec.Command(dockerBinary, "attach", id)
 	attachCmd.Stdin = tty
 	attachCmd.Stdout = tty
 	attachCmd.Stderr = tty
 	err = attachCmd.Start()
6345208b
 	assert.NilError(c, err)
50852f3b
 
4203230c
 	errChan := make(chan error)
28cf8fdd
 	go func() {
9d8f01b4
 		time.Sleep(300 * time.Millisecond)
4203230c
 		defer close(errChan)
927b334e
 		// Container is waiting for us to signal it to stop
50852f3b
 		dockerCmd(c, "stop", id)
 		// And wait for the attach command to end
 		errChan <- attachCmd.Wait()
28cf8fdd
 	}()
 
50852f3b
 	// Wait for the docker to end (should be done by the
 	// stop command in the go routine)
5c295460
 	dockerCmd(c, "wait", id)
 
28cf8fdd
 	select {
4203230c
 	case err := <-errChan:
9d8f01b4
 		tty.Close()
 		out, _ := ioutil.ReadAll(pty)
4cf69b99
 		assert.Assert(c, err == nil, "out: %v", string(out))
28cf8fdd
 	case <-time.After(attachWait):
dc944ea7
 		c.Fatal("timed out without attach returning")
28cf8fdd
 	}
 
 }
 
64a928a3
 func (s *DockerSuite) TestAttachAfterDetach(c *testing.T) {
28cf8fdd
 	name := "detachtest"
 
 	cpty, tty, err := pty.Open()
6345208b
 	assert.NilError(c, err, "Could not open pty: %v", err)
28cf8fdd
 	cmd := exec.Command(dockerBinary, "run", "-ti", "--name", name, "busybox")
 	cmd.Stdin = tty
 	cmd.Stdout = tty
 	cmd.Stderr = tty
 
847b6106
 	cmdExit := make(chan error)
28cf8fdd
 	go func() {
847b6106
 		cmdExit <- cmd.Run()
 		close(cmdExit)
28cf8fdd
 	}()
 
ef4c63ac
 	assert.Assert(c, waitRun(name) == nil)
799d9605
 
28cf8fdd
 	cpty.Write([]byte{16})
 	time.Sleep(100 * time.Millisecond)
 	cpty.Write([]byte{17})
 
4203230c
 	select {
847b6106
 	case <-cmdExit:
4203230c
 	case <-time.After(5 * time.Second):
 		c.Fatal("timeout while detaching")
 	}
28cf8fdd
 
 	cpty, tty, err = pty.Open()
6345208b
 	assert.NilError(c, err, "Could not open pty: %v", err)
28cf8fdd
 
 	cmd = exec.Command(dockerBinary, "attach", name)
 	cmd.Stdin = tty
 	cmd.Stdout = tty
 	cmd.Stderr = tty
 
f4abc640
 	err = cmd.Start()
6345208b
 	assert.NilError(c, err)
847b6106
 	defer cmd.Process.Kill()
28cf8fdd
 
 	bytes := make([]byte, 10)
6ef80577
 	var nBytes int
 	readErr := make(chan error, 1)
28cf8fdd
 
6ef80577
 	go func() {
 		time.Sleep(500 * time.Millisecond)
 		cpty.Write([]byte("\n"))
 		time.Sleep(500 * time.Millisecond)
28cf8fdd
 
6ef80577
 		nBytes, err = cpty.Read(bytes)
 		cpty.Close()
 		readErr <- err
 	}()
 
 	select {
 	case err := <-readErr:
6345208b
 		assert.NilError(c, err)
6ef80577
 	case <-time.After(2 * time.Second):
dc944ea7
 		c.Fatal("timeout waiting for attach read")
28cf8fdd
 	}
 
6345208b
 	assert.Assert(c, strings.Contains(string(bytes[:nBytes]), "/ #"))
28cf8fdd
 }
ae0883ce
 
 // TestAttachDetach checks that attach in tty mode can be detached using the long container ID
64a928a3
 func (s *DockerSuite) TestAttachDetach(c *testing.T) {
dc944ea7
 	out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
ae0883ce
 	id := strings.TrimSpace(out)
6345208b
 	assert.NilError(c, waitRun(id))
ae0883ce
 
 	cpty, tty, err := pty.Open()
6345208b
 	assert.NilError(c, err)
ae0883ce
 	defer cpty.Close()
 
 	cmd := exec.Command(dockerBinary, "attach", id)
 	cmd.Stdin = tty
 	stdout, err := cmd.StdoutPipe()
6345208b
 	assert.NilError(c, err)
ae0883ce
 	defer stdout.Close()
f4abc640
 	err = cmd.Start()
6345208b
 	assert.NilError(c, err)
 	assert.NilError(c, waitRun(id))
ae0883ce
 
f4abc640
 	_, err = cpty.Write([]byte("hello\n"))
6345208b
 	assert.NilError(c, err)
ae0883ce
 	out, err = bufio.NewReader(stdout).ReadString('\n')
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, strings.TrimSpace(out), "hello")
ae0883ce
 
 	// escape sequence
f4abc640
 	_, err = cpty.Write([]byte{16})
6345208b
 	assert.NilError(c, err)
ae0883ce
 	time.Sleep(100 * time.Millisecond)
f4abc640
 	_, err = cpty.Write([]byte{17})
6345208b
 	assert.NilError(c, err)
ae0883ce
 
 	ch := make(chan struct{})
 	go func() {
 		cmd.Wait()
9f3a343a
 		close(ch)
ae0883ce
 	}()
 
 	select {
 	case <-ch:
9f3a343a
 	case <-time.After(1 * time.Second):
dc944ea7
 		c.Fatal("timed out waiting for container to exit")
ae0883ce
 	}
 
62a856e9
 	running := inspectField(c, id, "State.Running")
6345208b
 	assert.Equal(c, running, "true") // container should be running
28cda048
 }