1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Move TestAttachDetach to integration-cli

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2015-04-10 23:17:55 -04:00
parent f7538c77ef
commit ae0883ce00
2 changed files with 76 additions and 88 deletions

View file

@ -3,6 +3,7 @@
package main
import (
"bufio"
"os/exec"
"strings"
"testing"
@ -137,3 +138,78 @@ func TestAttachAfterDetach(t *testing.T) {
logDone("attach - reconnect after detaching")
}
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID
func TestAttachDetach(t *testing.T) {
out, _, _ := dockerCmd(t, "run", "-itd", "busybox", "cat")
id := strings.TrimSpace(out)
if err := waitRun(id); err != nil {
t.Fatal(err)
}
cpty, tty, err := pty.Open()
if err != nil {
t.Fatal(err)
}
defer cpty.Close()
cmd := exec.Command(dockerBinary, "attach", id)
cmd.Stdin = tty
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
defer stdout.Close()
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
if err := waitRun(id); err != nil {
t.Fatalf("error waiting for container to start: %v", err)
}
if _, err := cpty.Write([]byte("hello\n")); err != nil {
t.Fatal(err)
}
out, err = bufio.NewReader(stdout).ReadString('\n')
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(out) != "hello" {
t.Fatalf("exepected 'hello', got %q", out)
}
// escape sequence
if _, err := cpty.Write([]byte{16}); err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write([]byte{17}); err != nil {
t.Fatal(err)
}
ch := make(chan struct{})
go func() {
cmd.Wait()
ch <- struct{}{}
}()
running, err := inspectField(id, "State.Running")
if err != nil {
t.Fatal(err)
}
if running != "true" {
t.Fatal("exepected container to still be running")
}
go func() {
dockerCmd(t, "kill", id)
}()
select {
case <-ch:
case <-time.After(10 * time.Millisecond):
t.Fatal("timed out waiting for container to exit")
}
logDone("attach - detach")
}