|
@@ -15,6 +15,7 @@ import (
|
|
"github.com/docker/docker/pkg/term"
|
|
"github.com/docker/docker/pkg/term"
|
|
"github.com/docker/docker/utils"
|
|
"github.com/docker/docker/utils"
|
|
"github.com/docker/libtrust"
|
|
"github.com/docker/libtrust"
|
|
|
|
+ "github.com/kr/pty"
|
|
)
|
|
)
|
|
|
|
|
|
func closeWrap(args ...io.Closer) error {
|
|
func closeWrap(args ...io.Closer) error {
|
|
@@ -162,72 +163,20 @@ func TestRunDisconnect(t *testing.T) {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
-// Expected behaviour: the process stay alive when the client disconnects
|
|
|
|
-// but the client detaches.
|
|
|
|
-func TestRunDisconnectTty(t *testing.T) {
|
|
|
|
-
|
|
|
|
- stdin, stdinPipe := io.Pipe()
|
|
|
|
|
|
+// TestRunDetach checks attaching and detaching with the escape sequence.
|
|
|
|
+func TestRunDetach(t *testing.T) {
|
|
stdout, stdoutPipe := io.Pipe()
|
|
stdout, stdoutPipe := io.Pipe()
|
|
- key, err := libtrust.GenerateECP256PrivateKey()
|
|
|
|
|
|
+ cpty, tty, err := pty.Open()
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
- defer cleanup(globalEngine, t)
|
|
|
|
-
|
|
|
|
- c1 := make(chan struct{})
|
|
|
|
- go func() {
|
|
|
|
- defer close(c1)
|
|
|
|
- // We're simulating a disconnect so the return value doesn't matter. What matters is the
|
|
|
|
- // fact that CmdRun returns.
|
|
|
|
- if err := cli.CmdRun("-i", "-t", unitTestImageID, "/bin/cat"); err != nil {
|
|
|
|
- log.Debugf("Error CmdRun: %s", err)
|
|
|
|
- }
|
|
|
|
- }()
|
|
|
|
-
|
|
|
|
- container := waitContainerStart(t, 10*time.Second)
|
|
|
|
-
|
|
|
|
- state := setRaw(t, container)
|
|
|
|
- defer unsetRaw(t, container, state)
|
|
|
|
-
|
|
|
|
- // Client disconnect after run -i should keep stdin out in TTY mode
|
|
|
|
- setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
|
|
|
|
- if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
|
|
|
|
- t.Fatal(err)
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- // Close pipes (simulate disconnect)
|
|
|
|
- if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
|
|
|
|
- t.Fatal(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // wait for CmdRun to return
|
|
|
|
- setTimeout(t, "Waiting for CmdRun timed out", 5*time.Second, func() {
|
|
|
|
- <-c1
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- // In tty mode, we expect the process to stay alive even after client's stdin closes.
|
|
|
|
-
|
|
|
|
- // Give some time to monitor to do his thing
|
|
|
|
- container.WaitStop(500 * time.Millisecond)
|
|
|
|
- if !container.IsRunning() {
|
|
|
|
- t.Fatalf("/bin/cat should still be running after closing stdin (tty mode)")
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// TestRunDetach checks attaching and detaching with the escape sequence.
|
|
|
|
-func TestRunDetach(t *testing.T) {
|
|
|
|
-
|
|
|
|
- stdin, stdinPipe := io.Pipe()
|
|
|
|
- stdout, stdoutPipe := io.Pipe()
|
|
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
+ cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
defer cleanup(globalEngine, t)
|
|
defer cleanup(globalEngine, t)
|
|
|
|
|
|
ch := make(chan struct{})
|
|
ch := make(chan struct{})
|
|
@@ -242,22 +191,22 @@ func TestRunDetach(t *testing.T) {
|
|
defer unsetRaw(t, container, state)
|
|
defer unsetRaw(t, container, state)
|
|
|
|
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
- if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
|
|
|
|
|
|
+ if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
|
|
setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
- stdinPipe.Write([]byte{16})
|
|
|
|
|
|
+ cpty.Write([]byte{16})
|
|
time.Sleep(100 * time.Millisecond)
|
|
time.Sleep(100 * time.Millisecond)
|
|
- stdinPipe.Write([]byte{17})
|
|
|
|
|
|
+ cpty.Write([]byte{17})
|
|
})
|
|
})
|
|
|
|
|
|
// wait for CmdRun to return
|
|
// wait for CmdRun to return
|
|
setTimeout(t, "Waiting for CmdRun timed out", 15*time.Second, func() {
|
|
setTimeout(t, "Waiting for CmdRun timed out", 15*time.Second, func() {
|
|
<-ch
|
|
<-ch
|
|
})
|
|
})
|
|
- closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
|
|
|
|
|
|
+ closeWrap(cpty, stdout, stdoutPipe)
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
time.Sleep(500 * time.Millisecond)
|
|
if !container.IsRunning() {
|
|
if !container.IsRunning() {
|
|
@@ -271,14 +220,18 @@ func TestRunDetach(t *testing.T) {
|
|
|
|
|
|
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID
|
|
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID
|
|
func TestAttachDetach(t *testing.T) {
|
|
func TestAttachDetach(t *testing.T) {
|
|
- stdin, stdinPipe := io.Pipe()
|
|
|
|
stdout, stdoutPipe := io.Pipe()
|
|
stdout, stdoutPipe := io.Pipe()
|
|
|
|
+ cpty, tty, err := pty.Open()
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
+ cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
defer cleanup(globalEngine, t)
|
|
defer cleanup(globalEngine, t)
|
|
|
|
|
|
ch := make(chan struct{})
|
|
ch := make(chan struct{})
|
|
@@ -309,9 +262,13 @@ func TestAttachDetach(t *testing.T) {
|
|
state := setRaw(t, container)
|
|
state := setRaw(t, container)
|
|
defer unsetRaw(t, container, state)
|
|
defer unsetRaw(t, container, state)
|
|
|
|
|
|
- stdin, stdinPipe = io.Pipe()
|
|
|
|
stdout, stdoutPipe = io.Pipe()
|
|
stdout, stdoutPipe = io.Pipe()
|
|
- cli = client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
+ cpty, tty, err = pty.Open()
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cli = client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
ch = make(chan struct{})
|
|
ch = make(chan struct{})
|
|
go func() {
|
|
go func() {
|
|
@@ -324,7 +281,7 @@ func TestAttachDetach(t *testing.T) {
|
|
}()
|
|
}()
|
|
|
|
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
- if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
|
|
|
|
|
|
+ if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
|
|
if err != io.ErrClosedPipe {
|
|
if err != io.ErrClosedPipe {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
@@ -332,9 +289,9 @@ func TestAttachDetach(t *testing.T) {
|
|
})
|
|
})
|
|
|
|
|
|
setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
- stdinPipe.Write([]byte{16})
|
|
|
|
|
|
+ cpty.Write([]byte{16})
|
|
time.Sleep(100 * time.Millisecond)
|
|
time.Sleep(100 * time.Millisecond)
|
|
- stdinPipe.Write([]byte{17})
|
|
|
|
|
|
+ cpty.Write([]byte{17})
|
|
})
|
|
})
|
|
|
|
|
|
// wait for CmdRun to return
|
|
// wait for CmdRun to return
|
|
@@ -342,7 +299,7 @@ func TestAttachDetach(t *testing.T) {
|
|
<-ch
|
|
<-ch
|
|
})
|
|
})
|
|
|
|
|
|
- closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
|
|
|
|
|
|
+ closeWrap(cpty, stdout, stdoutPipe)
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
time.Sleep(500 * time.Millisecond)
|
|
if !container.IsRunning() {
|
|
if !container.IsRunning() {
|
|
@@ -356,14 +313,18 @@ func TestAttachDetach(t *testing.T) {
|
|
|
|
|
|
// TestAttachDetachTruncatedID checks that attach in tty mode can be detached
|
|
// TestAttachDetachTruncatedID checks that attach in tty mode can be detached
|
|
func TestAttachDetachTruncatedID(t *testing.T) {
|
|
func TestAttachDetachTruncatedID(t *testing.T) {
|
|
- stdin, stdinPipe := io.Pipe()
|
|
|
|
stdout, stdoutPipe := io.Pipe()
|
|
stdout, stdoutPipe := io.Pipe()
|
|
|
|
+ cpty, tty, err := pty.Open()
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
+ cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
defer cleanup(globalEngine, t)
|
|
defer cleanup(globalEngine, t)
|
|
|
|
|
|
// Discard the CmdRun output
|
|
// Discard the CmdRun output
|
|
@@ -379,9 +340,13 @@ func TestAttachDetachTruncatedID(t *testing.T) {
|
|
state := setRaw(t, container)
|
|
state := setRaw(t, container)
|
|
defer unsetRaw(t, container, state)
|
|
defer unsetRaw(t, container, state)
|
|
|
|
|
|
- stdin, stdinPipe = io.Pipe()
|
|
|
|
stdout, stdoutPipe = io.Pipe()
|
|
stdout, stdoutPipe = io.Pipe()
|
|
- cli = client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
+ cpty, tty, err = pty.Open()
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cli = client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
ch := make(chan struct{})
|
|
ch := make(chan struct{})
|
|
go func() {
|
|
go func() {
|
|
@@ -394,7 +359,7 @@ func TestAttachDetachTruncatedID(t *testing.T) {
|
|
}()
|
|
}()
|
|
|
|
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
- if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
|
|
|
|
|
|
+ if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
|
|
if err != io.ErrClosedPipe {
|
|
if err != io.ErrClosedPipe {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
@@ -402,16 +367,16 @@ func TestAttachDetachTruncatedID(t *testing.T) {
|
|
})
|
|
})
|
|
|
|
|
|
setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
- stdinPipe.Write([]byte{16})
|
|
|
|
|
|
+ cpty.Write([]byte{16})
|
|
time.Sleep(100 * time.Millisecond)
|
|
time.Sleep(100 * time.Millisecond)
|
|
- stdinPipe.Write([]byte{17})
|
|
|
|
|
|
+ cpty.Write([]byte{17})
|
|
})
|
|
})
|
|
|
|
|
|
// wait for CmdRun to return
|
|
// wait for CmdRun to return
|
|
setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
|
|
setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
|
|
<-ch
|
|
<-ch
|
|
})
|
|
})
|
|
- closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
|
|
|
|
|
|
+ closeWrap(cpty, stdout, stdoutPipe)
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
time.Sleep(500 * time.Millisecond)
|
|
if !container.IsRunning() {
|
|
if !container.IsRunning() {
|
|
@@ -425,14 +390,18 @@ func TestAttachDetachTruncatedID(t *testing.T) {
|
|
|
|
|
|
// Expected behaviour, the process stays alive when the client disconnects
|
|
// Expected behaviour, the process stays alive when the client disconnects
|
|
func TestAttachDisconnect(t *testing.T) {
|
|
func TestAttachDisconnect(t *testing.T) {
|
|
- stdin, stdinPipe := io.Pipe()
|
|
|
|
stdout, stdoutPipe := io.Pipe()
|
|
stdout, stdoutPipe := io.Pipe()
|
|
|
|
+ cpty, tty, err := pty.Open()
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
key, err := libtrust.GenerateECP256PrivateKey()
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|
|
- cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
|
|
|
|
+ cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, key, testDaemonProto, testDaemonAddr, nil)
|
|
defer cleanup(globalEngine, t)
|
|
defer cleanup(globalEngine, t)
|
|
|
|
|
|
go func() {
|
|
go func() {
|
|
@@ -470,12 +439,12 @@ func TestAttachDisconnect(t *testing.T) {
|
|
}()
|
|
}()
|
|
|
|
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
- if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
|
|
|
|
|
|
+ if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
// Close pipes (client disconnects)
|
|
// Close pipes (client disconnects)
|
|
- if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
|
|
|
|
|
|
+ if err := closeWrap(cpty, stdout, stdoutPipe); err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
|