2015-01-14 22:40:07 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2015-01-13 23:33:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-11 03:17:55 +00:00
|
|
|
"bufio"
|
2015-01-13 23:33:05 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-04-11 03:21:45 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2015-01-13 23:33:05 +00:00
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #9860
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-dti", "busybox", "sleep", "2")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("failed to start container: %v (%v)", out, err)
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 13:21:18 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2015-01-13 23:33:05 +00:00
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan := make(chan error)
|
2015-01-13 23:33:05 +00:00
|
|
|
go func() {
|
2015-04-27 17:29:48 +00:00
|
|
|
defer close(errChan)
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
_, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan <- err
|
|
|
|
return
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
attachCmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
attachCmd.Stdin = tty
|
|
|
|
attachCmd.Stdout = tty
|
|
|
|
attachCmd.Stderr = tty
|
|
|
|
|
|
|
|
if err := attachCmd.Run(); err != nil {
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan <- err
|
|
|
|
return
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", id)
|
|
|
|
if out, _, err = runCommandWithOutput(waitCmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("error thrown while waiting for container: %s, %v", out, err)
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
select {
|
2015-04-27 17:29:48 +00:00
|
|
|
case err := <-errChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-13 23:33:05 +00:00
|
|
|
case <-time.After(attachWait):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("timed out without attach returning")
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestAttachAfterDetach(c *check.C) {
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
name := "detachtest"
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Could not open pty: %v", err)
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-ti", "--name", name, "busybox")
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan := make(chan error)
|
2015-01-13 23:33:05 +00:00
|
|
|
go func() {
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan <- cmd.Run()
|
|
|
|
close(errChan)
|
2015-01-13 23:33:05 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2015-01-22 20:58:59 +00:00
|
|
|
if err := waitRun(name); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-22 20:58:59 +00:00
|
|
|
}
|
2015-01-13 23:33:05 +00:00
|
|
|
cpty.Write([]byte{16})
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cpty.Write([]byte{17})
|
|
|
|
|
2015-04-27 17:29:48 +00:00
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("timeout while detaching")
|
|
|
|
}
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
cpty, tty, err = pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Could not open pty: %v", err)
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "attach", name)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
|
2015-01-16 23:33:26 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-16 23:33:26 +00:00
|
|
|
}
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
bytes := make([]byte, 10)
|
2015-01-16 23:33:26 +00:00
|
|
|
var nBytes int
|
|
|
|
readErr := make(chan error, 1)
|
2015-01-13 23:33:05 +00:00
|
|
|
|
2015-01-16 23:33:26 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
cpty.Write([]byte("\n"))
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2015-01-13 23:33:05 +00:00
|
|
|
|
2015-01-16 23:33:26 +00:00
|
|
|
nBytes, err = cpty.Read(bytes)
|
|
|
|
cpty.Close()
|
|
|
|
readErr <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-readErr:
|
2015-04-27 17:29:48 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-16 23:33:26 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("timeout waiting for attach read")
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 23:33:26 +00:00
|
|
|
if err := cmd.Wait(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-01-13 23:33:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 23:33:26 +00:00
|
|
|
if !strings.Contains(string(bytes[:nBytes]), "/ #") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("failed to get a new prompt. got %s", string(bytes[:nBytes]))
|
2015-01-16 23:33:26 +00:00
|
|
|
}
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
}
|
2015-04-11 03:17:55 +00:00
|
|
|
|
|
|
|
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestAttachDetach(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
|
2015-04-11 03:17:55 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("error waiting for container to start: %v", err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 20:33:30 +00:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequence
|
|
|
|
if _, err := cpty.Write([]byte{16}); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := cpty.Write([]byte{17}); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
running, err := inspectField(id, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 20:33:30 +00:00
|
|
|
c.Fatal("expected container to still be running")
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2015-04-18 16:46:47 +00:00
|
|
|
dockerCmd(c, "kill", id)
|
2015-04-11 03:17:55 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("timed out waiting for container to exit")
|
2015-04-11 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-11 03:21:45 +00:00
|
|
|
|
|
|
|
// TestAttachDetachTruncatedID checks that attach in tty mode can be detached
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestAttachDetachTruncatedID(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
|
2015-04-11 03:21:45 +00:00
|
|
|
id := stringid.TruncateID(strings.TrimSpace(out))
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 20:33:30 +00:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequence
|
|
|
|
if _, err := cpty.Write([]byte{16}); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := cpty.Write([]byte{17}); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
running, err := inspectField(id, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 20:33:30 +00:00
|
|
|
c.Fatal("expected container to still be running")
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2015-04-18 16:46:47 +00:00
|
|
|
dockerCmd(c, "kill", id)
|
2015-04-11 03:21:45 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("timed out waiting for container to exit")
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|