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"
|
2015-01-13 18:33:05 -05:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #9860
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-dti", "busybox", "sleep", "2")
|
2015-01-13 18:33:05 -05:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2015-01-13 18:33:05 -05:00
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-13 18:33:05 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
defer close(errChan)
|
2015-01-13 18:33:05 -05:00
|
|
|
|
|
|
|
_, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-27 13:29:48 -04:00
|
|
|
errChan <- err
|
|
|
|
return
|
2015-01-13 18:33:05 -05: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 13:29:48 -04:00
|
|
|
errChan <- err
|
|
|
|
return
|
2015-01-13 18:33:05 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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:
|
|
|
|
c.Assert(err, check.IsNil)
|
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()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("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
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2015-01-22 15:58:59 -05:00
|
|
|
if err := waitRun(name); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-22 15:58:59 -05:00
|
|
|
}
|
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:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("timeout while detaching")
|
|
|
|
}
|
2015-01-13 18:33:05 -05:00
|
|
|
|
|
|
|
cpty, tty, err = pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("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-01-16 18:33:26 -05:00
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-16 18:33:26 -05:00
|
|
|
}
|
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-01-16 18:33:26 -05:00
|
|
|
if err := cmd.Wait(); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-13 18:33:05 -05:00
|
|
|
}
|
|
|
|
|
2015-01-16 18:33:26 -05:00
|
|
|
if !strings.Contains(string(bytes[:nBytes]), "/ #") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to get a new prompt. got %s", string(bytes[:nBytes]))
|
2015-01-16 18:33:26 -05:00
|
|
|
}
|
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)
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error waiting for container to start: %v", err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequence
|
|
|
|
if _, err := cpty.Write([]byte{16}); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := cpty.Write([]byte{17}); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
running, err := inspectField(id, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:17:55 -04:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatal("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))
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
defer cpty.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequence
|
|
|
|
if _, err := cpty.Write([]byte{16}); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := cpty.Write([]byte{17}); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
running, err := inspectField(id, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-04-10 23:21:45 -04:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 16:33:30 -04:00
|
|
|
c.Fatal("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
|
|
|
}
|
|
|
|
|
|
|
|
}
|