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"
|
2016-10-05 22:48:04 +00:00
|
|
|
"io/ioutil"
|
2015-01-13 23:33:05 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 23:59:08 +00:00
|
|
|
"github.com/creack/pty"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2019-04-04 13:23:19 +00:00
|
|
|
"gotest.tools/assert"
|
2015-01-13 23:33:05 +00:00
|
|
|
)
|
|
|
|
|
2015-09-29 14:57:58 +00:00
|
|
|
// #9860 Make sure attach ends when container ends (with no errors)
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestAttachClosedOnContainerStop(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2015-01-13 23:33:05 +00:00
|
|
|
|
2015-09-29 14:57:58 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`)
|
2015-01-13 23:33:05 +00:00
|
|
|
|
2015-04-06 13:21:18 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, waitRun(id))
|
2015-01-13 23:33:05 +00:00
|
|
|
|
2016-10-05 22:48:04 +00:00
|
|
|
pty, tty, err := pty.Open()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-09-29 14:57:58 +00:00
|
|
|
|
|
|
|
attachCmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
attachCmd.Stdin = tty
|
|
|
|
attachCmd.Stdout = tty
|
|
|
|
attachCmd.Stderr = tty
|
|
|
|
err = attachCmd.Start()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-09-29 14:57:58 +00:00
|
|
|
|
2015-04-27 17:29:48 +00:00
|
|
|
errChan := make(chan error)
|
2015-01-13 23:33:05 +00:00
|
|
|
go func() {
|
2016-10-05 22:48:04 +00:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2015-04-27 17:29:48 +00:00
|
|
|
defer close(errChan)
|
2015-12-13 16:00:39 +00:00
|
|
|
// Container is waiting for us to signal it to stop
|
2015-09-29 14:57:58 +00:00
|
|
|
dockerCmd(c, "stop", id)
|
|
|
|
// And wait for the attach command to end
|
|
|
|
errChan <- attachCmd.Wait()
|
2015-01-13 23:33:05 +00:00
|
|
|
}()
|
|
|
|
|
2015-09-29 14:57:58 +00:00
|
|
|
// Wait for the docker to end (should be done by the
|
|
|
|
// stop command in the go routine)
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "wait", id)
|
|
|
|
|
2015-01-13 23:33:05 +00:00
|
|
|
select {
|
2015-04-27 17:29:48 +00:00
|
|
|
case err := <-errChan:
|
2016-10-05 22:48:04 +00:00
|
|
|
tty.Close()
|
|
|
|
out, _ := ioutil.ReadAll(pty)
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, err == nil, check.Commentf("out: %v", string(out)))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestAttachAfterDetach(c *testing.T) {
|
2015-01-13 23:33:05 +00:00
|
|
|
name := "detachtest"
|
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, "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
|
|
|
|
|
2018-02-20 23:24:21 +00:00
|
|
|
cmdExit := make(chan error)
|
2015-01-13 23:33:05 +00:00
|
|
|
go func() {
|
2018-02-20 23:24:21 +00:00
|
|
|
cmdExit <- cmd.Run()
|
|
|
|
close(cmdExit)
|
2015-01-13 23:33:05 +00:00
|
|
|
}()
|
|
|
|
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, waitRun(name) == nil)
|
2015-08-11 07:41:11 +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 {
|
2018-02-20 23:24:21 +00:00
|
|
|
case <-cmdExit:
|
2015-04-27 17:29:48 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("timeout while detaching")
|
|
|
|
}
|
2015-01-13 23:33:05 +00:00
|
|
|
|
|
|
|
cpty, tty, err = pty.Open()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err, "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-10-28 03:18:01 +00:00
|
|
|
err = cmd.Start()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2018-02-20 23:24:21 +00:00
|
|
|
defer cmd.Process.Kill()
|
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:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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
|
|
|
}
|
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(string(bytes[:nBytes]), "/ #"))
|
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
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestAttachDetach(c *testing.T) {
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-itd", "busybox", "cat")
|
2015-04-11 03:17:55 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, waitRun(id))
|
2015-04-11 03:17:55 +00:00
|
|
|
|
|
|
|
cpty, tty, err := pty.Open()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, 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()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-11 03:17:55 +00:00
|
|
|
defer stdout.Close()
|
2015-10-28 03:18:01 +00:00
|
|
|
err = cmd.Start()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.NilError(c, waitRun(id))
|
2015-04-11 03:17:55 +00:00
|
|
|
|
2015-10-28 03:18:01 +00:00
|
|
|
_, err = cpty.Write([]byte("hello\n"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-11 03:17:55 +00:00
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, strings.TrimSpace(out), "hello")
|
2015-04-11 03:17:55 +00:00
|
|
|
|
|
|
|
// escape sequence
|
2015-10-28 03:18:01 +00:00
|
|
|
_, err = cpty.Write([]byte{16})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-11 03:17:55 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-10-28 03:18:01 +00:00
|
|
|
_, err = cpty.Write([]byte{17})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-11 03:17:55 +00:00
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
integration-cli: fix TestAttachDetach, rm TestAttachDetachTruncatedID
It looks like the logic of the test became wrong after commit
ae0883c ("Move TestAttachDetach to integration-cli").
The original logic was:
* (a few first steps skipped for clarity)
* send escape sequence to "attach";
* check "attach" is exiting (i.e. escape sequence works);
* check the container is still alive;
* kill the container.
Also, timeouts were big at that time, in the order of seconds.
The logic after the above mentioned commit and until now is:
* ...
* send escape sequence to "attach";
* check the container is running (why shouldn't it?);
* kill the container;
* checks that the "attach" has exited.
So, from the "let's check detach using escape sequence is working"
the test became something like "let's check that attach is gone
once we kill the container".
Let's fix the above test, also increasing the timeout waiting
for attach to exit (which fails from time to time on power CI).
Now, the second test, TestAttachDetachTruncatedID, does the exact
same thing, except it uses a truncated container ID. It does not
seem to be of much value, so let's remove it.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-08-24 21:37:26 +00:00
|
|
|
close(ch)
|
2015-04-11 03:17:55 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
integration-cli: fix TestAttachDetach, rm TestAttachDetachTruncatedID
It looks like the logic of the test became wrong after commit
ae0883c ("Move TestAttachDetach to integration-cli").
The original logic was:
* (a few first steps skipped for clarity)
* send escape sequence to "attach";
* check "attach" is exiting (i.e. escape sequence works);
* check the container is still alive;
* kill the container.
Also, timeouts were big at that time, in the order of seconds.
The logic after the above mentioned commit and until now is:
* ...
* send escape sequence to "attach";
* check the container is running (why shouldn't it?);
* kill the container;
* checks that the "attach" has exited.
So, from the "let's check detach using escape sequence is working"
the test became something like "let's check that attach is gone
once we kill the container".
Let's fix the above test, also increasing the timeout waiting
for attach to exit (which fails from time to time on power CI).
Now, the second test, TestAttachDetachTruncatedID, does the exact
same thing, except it uses a truncated container ID. It does not
seem to be of much value, so let's remove it.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-08-24 21:37:26 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
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
|
|
|
}
|
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
running := inspectField(c, id, "State.Running")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, running, "true") // container should be running
|
2015-04-11 03:21:45 +00:00
|
|
|
}
|