2014-04-18 06:14:00 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-11 06:35:54 +03:00
|
|
|
"bufio"
|
2015-04-27 20:29:48 +03:00
|
|
|
"fmt"
|
2014-07-29 09:50:16 +04:00
|
|
|
"io"
|
2014-04-18 06:14:00 +04:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-04-18 06:14:00 +04:00
|
|
|
)
|
|
|
|
|
2014-07-29 09:50:16 +04:00
|
|
|
const attachWait = 5 * time.Second
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
|
2014-07-29 09:50:16 +04:00
|
|
|
|
|
|
|
endGroup := &sync.WaitGroup{}
|
|
|
|
startGroup := &sync.WaitGroup{}
|
|
|
|
endGroup.Add(3)
|
|
|
|
startGroup.Add(3)
|
|
|
|
|
|
|
|
if err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done"); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-07-29 09:50:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
startDone := make(chan struct{})
|
|
|
|
endDone := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
endGroup.Wait()
|
|
|
|
close(endDone)
|
2014-06-13 21:32:25 +04:00
|
|
|
}()
|
|
|
|
|
2014-04-18 06:14:00 +04:00
|
|
|
go func() {
|
2014-07-29 09:50:16 +04:00
|
|
|
startGroup.Wait()
|
|
|
|
close(startDone)
|
2014-04-18 06:14:00 +04:00
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
go func() {
|
2015-04-18 19:46:47 +03:00
|
|
|
cmd := exec.Command(dockerBinary, "attach", "attacher")
|
2014-04-18 06:14:00 +04:00
|
|
|
|
2014-07-29 09:50:16 +04:00
|
|
|
defer func() {
|
2015-04-18 19:46:47 +03:00
|
|
|
cmd.Wait()
|
2014-07-29 09:50:16 +04:00
|
|
|
endGroup.Done()
|
|
|
|
}()
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
out, err := cmd.StdoutPipe()
|
2014-04-18 06:14:00 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-07-29 09:50:16 +04:00
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
c.Fatal(err)
|
2014-04-18 06:14:00 +04:00
|
|
|
}
|
2014-07-29 09:50:16 +04:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
if _, err := out.Read(buf); err != nil && err != io.EOF {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-07-29 09:50:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
startGroup.Done()
|
|
|
|
|
|
|
|
if !strings.Contains(string(buf), "hello") {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("unexpected output %s expected hello\n", string(buf))
|
2014-04-18 06:14:00 +04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:50:16 +04:00
|
|
|
select {
|
|
|
|
case <-startDone:
|
|
|
|
case <-time.After(attachWait):
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Attaches did not initialize properly")
|
2014-07-29 09:50:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "kill", "attacher")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-07-29 09:50:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-endDone:
|
|
|
|
case <-time.After(attachWait):
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Attaches did not finish properly")
|
2014-07-29 09:50:16 +04:00
|
|
|
}
|
2014-04-18 06:14:00 +04:00
|
|
|
|
|
|
|
}
|
2014-12-06 03:50:56 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
|
2014-12-06 03:50:56 +03:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "-d", "-ti", "busybox")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("failed to start container: %v (%v)", out, err)
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
if err := waitRun(id); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
cmd := exec.Command(dockerBinary, "kill", id)
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("failed to kill container: %v (%v)", out, err)
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-27 20:29:48 +03:00
|
|
|
done := make(chan error)
|
2014-12-06 03:50:56 +03:00
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
if _, err := cmd.StdinPipe(); err != nil {
|
2015-04-27 20:29:48 +03:00
|
|
|
done <- err
|
|
|
|
return
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := "cannot enable tty mode"
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err == nil {
|
2015-04-27 20:29:48 +03:00
|
|
|
done <- fmt.Errorf("attach should have failed")
|
|
|
|
return
|
2014-12-06 03:50:56 +03:00
|
|
|
} else if !strings.Contains(out, expected) {
|
2015-04-27 20:29:48 +03:00
|
|
|
done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
|
|
|
|
return
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 20:29:48 +03:00
|
|
|
case err := <-done:
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-06 03:50:56 +03:00
|
|
|
case <-time.After(attachWait):
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("attach is running but should have failed")
|
2014-12-06 03:50:56 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 06:35:54 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
|
2015-04-11 06:35:54 +03:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
defer stdin.Close()
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
|
|
|
|
if _, err := stdin.Write([]byte("hello\n")); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 23:33:30 +03:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := stdin.Close(); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expect container to still be running after stdin is closed
|
|
|
|
running, err := inspectField(id, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 23:33:30 +03:00
|
|
|
c.Fatal("expected container to still be running")
|
2015-04-11 06:35:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|