2015-01-14 17:40:07 -05:00
|
|
|
// +build !windows
|
|
|
|
|
2015-01-13 18:33:05 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-10 23:17:55 -04:00
|
|
|
"bufio"
|
2016-10-05 18:48:04 -04:00
|
|
|
"io/ioutil"
|
2015-01-13 18:33:05 -05:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2015-04-10 23:21:45 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2015-01-13 18:33:05 -05:00
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
2015-09-29 10:57:58 -04:00
|
|
|
// #9860 Make sure attach ends when container ends (with no errors)
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
|
2016-02-10 22:27:02 -05:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-09-29 10:57:58 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2016-10-05 18:48:04 -04:00
|
|
|
pty, tty, err := pty.Open()
|
2015-09-29 10:57:58 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
attachCmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
attachCmd.Stdin = tty
|
|
|
|
attachCmd.Stdout = tty
|
|
|
|
attachCmd.Stderr = tty
|
|
|
|
err = attachCmd.Start()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-04-27 13:29:48 -04:00
|
|
|
errChan := make(chan error)
|
2015-01-13 18:33:05 -05:00
|
|
|
go func() {
|
2016-10-05 18:48:04 -04:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2015-04-27 13:29:48 -04:00
|
|
|
defer close(errChan)
|
2015-12-13 11:00:39 -05:00
|
|
|
// Container is waiting for us to signal it to stop
|
2015-09-29 10:57:58 -04:00
|
|
|
dockerCmd(c, "stop", id)
|
|
|
|
// And wait for the attach command to end
|
|
|
|
errChan <- attachCmd.Wait()
|
2015-01-13 18:33:05 -05:00
|
|
|
}()
|
|
|
|
|
2015-09-29 10:57:58 -04:00
|
|
|
// Wait for the docker to end (should be done by the
|
|
|
|
// stop command in the go routine)
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "wait", id)
|
|
|
|
|
2015-01-13 18:33:05 -05:00
|
|
|
select {
|
2015-04-27 13:29:48 -04:00
|
|
|
case err := <-errChan:
|
2016-10-05 18:48:04 -04:00
|
|
|
tty.Close()
|
|
|
|
out, _ := ioutil.ReadAll(pty)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("out: %v", string(out)))
|
2015-01-13 18:33:05 -05:00
|
|
|
case <-time.After(attachWait):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("timed out without attach returning")
|
2015-01-13 18:33:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestAttachAfterDetach(c *check.C) {
|
2015-01-13 18:33:05 -05:00
|
|
|
name := "detachtest"
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Could not open pty: %v", err))
|
2015-01-13 18:33:05 -05:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "-ti", "--name", name, "busybox")
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
|
2015-04-27 13:29:48 -04:00
|
|
|
errChan := make(chan error)
|
2015-01-13 18:33:05 -05:00
|
|
|
go func() {
|
2015-04-27 13:29:48 -04:00
|
|
|
errChan <- cmd.Run()
|
|
|
|
close(errChan)
|
2015-01-13 18:33:05 -05:00
|
|
|
}()
|
|
|
|
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(name), check.IsNil)
|
|
|
|
|
2015-01-13 18:33:05 -05:00
|
|
|
cpty.Write([]byte{16})
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cpty.Write([]byte{17})
|
|
|
|
|
2015-04-27 13:29:48 -04:00
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
2016-08-04 12:57:34 -04:00
|
|
|
if err != nil {
|
|
|
|
buff := make([]byte, 200)
|
|
|
|
tty.Read(buff)
|
|
|
|
c.Fatalf("%s: %s", err, buff)
|
|
|
|
}
|
2015-04-27 13:29:48 -04:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("timeout while detaching")
|
|
|
|
}
|
2015-01-13 18:33:05 -05:00
|
|
|
|
|
|
|
cpty, tty, err = pty.Open()
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Could not open pty: %v", err))
|
2015-01-13 18:33:05 -05:00
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "attach", name)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
|
2015-10-27 23:18:01 -04:00
|
|
|
err = cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
|
|
|
bytes := make([]byte, 10)
|
2015-01-16 18:33:26 -05:00
|
|
|
var nBytes int
|
|
|
|
readErr := make(chan error, 1)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-01-16 18:33:26 -05:00
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
cpty.Write([]byte("\n"))
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-01-16 18:33:26 -05:00
|
|
|
nBytes, err = cpty.Read(bytes)
|
|
|
|
cpty.Close()
|
|
|
|
readErr <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-readErr:
|
2015-04-27 13:29:48 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-16 18:33:26 -05:00
|
|
|
case <-time.After(2 * time.Second):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("timeout waiting for attach read")
|
2015-01-13 18:33:05 -05:00
|
|
|
}
|
|
|
|
|
2015-10-27 23:18:01 -04:00
|
|
|
err = cmd.Wait()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(string(bytes[:nBytes]), checker.Contains, "/ #")
|
2015-01-13 18:33:05 -05:00
|
|
|
|
|
|
|
}
|
2015-04-10 23:17:55 -04:00
|
|
|
|
|
|
|
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestAttachDetach(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
|
2015-04-10 23:17:55 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
defer stdout.Close()
|
2015-10-27 23:18:01 -04:00
|
|
|
err = cmd.Start()
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
|
2015-10-27 23:18:01 -04:00
|
|
|
_, err = cpty.Write([]byte("hello\n"))
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "hello", check.Commentf("expected 'hello', got %q", out))
|
2015-04-10 23:17:55 -04:00
|
|
|
|
|
|
|
// escape sequence
|
2015-10-27 23:18:01 -04:00
|
|
|
_, err = cpty.Write([]byte{16})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-10-27 23:18:01 -04:00
|
|
|
_, err = cpty.Write([]byte{17})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:17:55 -04:00
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
running := inspectField(c, id, "State.Running")
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
|
2015-04-10 23:17:55 -04:00
|
|
|
|
|
|
|
go func() {
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "kill", id)
|
2015-04-10 23:17:55 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("timed out waiting for container to exit")
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-10 23:21:45 -04:00
|
|
|
|
|
|
|
// TestAttachDetachTruncatedID checks that attach in tty mode can be detached
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestAttachDetachTruncatedID(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
|
2015-04-10 23:21:45 -04:00
|
|
|
id := stringid.TruncateID(strings.TrimSpace(out))
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
defer stdout.Close()
|
2015-10-27 23:18:01 -04:00
|
|
|
err = cmd.Start()
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
|
2015-10-27 23:18:01 -04:00
|
|
|
_, err = cpty.Write([]byte("hello\n"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "hello", check.Commentf("expected 'hello', got %q", out))
|
2015-04-10 23:21:45 -04:00
|
|
|
|
|
|
|
// escape sequence
|
2015-10-27 23:18:01 -04:00
|
|
|
_, err = cpty.Write([]byte{16})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-10-27 23:18:01 -04:00
|
|
|
_, err = cpty.Write([]byte{17})
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-10 23:21:45 -04:00
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
running := inspectField(c, id, "State.Running")
|
2015-10-27 23:18:01 -04:00
|
|
|
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
|
2015-04-10 23:21:45 -04:00
|
|
|
|
|
|
|
go func() {
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "kill", id)
|
2015-04-10 23:21:45 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("timed out waiting for container to exit")
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|