2014-04-18 02:14:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-11 03:35:54 +00:00
|
|
|
"bufio"
|
2015-04-27 17:29:48 +00:00
|
|
|
"fmt"
|
2014-07-29 05:50:16 +00:00
|
|
|
"io"
|
2014-04-18 02:14:00 +00:00
|
|
|
"os/exec"
|
2016-05-25 17:19:17 +00:00
|
|
|
"runtime"
|
2014-04-18 02:14:00 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2014-04-18 02:14:00 +00:00
|
|
|
"time"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-04-18 02:14:00 +00:00
|
|
|
)
|
|
|
|
|
2014-07-29 05:50:16 +00:00
|
|
|
const attachWait = 5 * time.Second
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIAttachSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIAttachSuite) TearDownTest(c *testing.T) {
|
|
|
|
s.ds.TearDownTest(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIAttachSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIAttachSuite) TestAttachMultipleAndRestart(c *testing.T) {
|
2014-07-29 05:50:16 +00:00
|
|
|
endGroup := &sync.WaitGroup{}
|
|
|
|
startGroup := &sync.WaitGroup{}
|
|
|
|
endGroup.Add(3)
|
|
|
|
startGroup.Add(3)
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name", "attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
|
|
|
|
cli.WaitRun(c, "attacher")
|
2014-07-29 05:50:16 +00:00
|
|
|
|
|
|
|
startDone := make(chan struct{})
|
|
|
|
endDone := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
endGroup.Wait()
|
|
|
|
close(endDone)
|
2014-06-13 17:32:25 +00:00
|
|
|
}()
|
|
|
|
|
2014-04-18 02:14:00 +00:00
|
|
|
go func() {
|
2014-07-29 05:50:16 +00:00
|
|
|
startGroup.Wait()
|
|
|
|
close(startDone)
|
2014-04-18 02:14:00 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
go func() {
|
2015-04-18 16:46:47 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "attach", "attacher")
|
2014-04-18 02:14:00 +00:00
|
|
|
|
2014-07-29 05:50:16 +00:00
|
|
|
defer func() {
|
2015-04-18 16:46:47 +00:00
|
|
|
cmd.Wait()
|
2014-07-29 05:50:16 +00:00
|
|
|
endGroup.Done()
|
|
|
|
}()
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
out, err := cmd.StdoutPipe()
|
2014-04-18 02:14:00 +00:00
|
|
|
if err != nil {
|
2019-09-11 14:24:53 +00:00
|
|
|
c.Error(err)
|
2014-07-29 05:50:16 +00:00
|
|
|
}
|
2016-08-08 07:16:01 +00:00
|
|
|
defer out.Close()
|
2014-07-29 05:50:16 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2019-09-11 14:24:53 +00:00
|
|
|
c.Error(err)
|
2014-04-18 02:14:00 +00:00
|
|
|
}
|
2014-07-29 05:50:16 +00:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
if _, err := out.Read(buf); err != nil && err != io.EOF {
|
2019-09-11 14:24:53 +00:00
|
|
|
c.Error(err)
|
2014-07-29 05:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
startGroup.Done()
|
|
|
|
|
|
|
|
if !strings.Contains(string(buf), "hello") {
|
2019-09-11 14:24:53 +00:00
|
|
|
c.Errorf("unexpected output %s expected hello\n", string(buf))
|
2014-04-18 02:14:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-07-29 05:50:16 +00:00
|
|
|
select {
|
|
|
|
case <-startDone:
|
|
|
|
case <-time.After(attachWait):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Attaches did not initialize properly")
|
2014-07-29 05:50:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.DockerCmd(c, "kill", "attacher")
|
2014-07-29 05:50:16 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-endDone:
|
|
|
|
case <-time.After(attachWait):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Attaches did not finish properly")
|
2014-07-29 05:50:16 +00:00
|
|
|
}
|
2014-04-18 02:14:00 +00:00
|
|
|
}
|
2014-12-06 00:50:56 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIAttachSuite) TestAttachTTYWithoutStdin(c *testing.T) {
|
2019-09-25 17:51:18 +00:00
|
|
|
// TODO: Figure out how to get this running again reliable on Windows.
|
2017-07-18 21:23:23 +00:00
|
|
|
// It works by accident at the moment. Sometimes. I've gone back to v1.13.0 and see the same.
|
|
|
|
// On Windows, docker run -d -ti busybox causes the container to exit immediately.
|
|
|
|
// Obviously a year back when I updated the test, that was not the case. However,
|
|
|
|
// with this, and the test racing with the tear-down which panic's, sometimes CI
|
|
|
|
// will just fail and `MISS` all the other tests. For now, disabling it. Will
|
|
|
|
// open an issue to track re-enabling this and root-causing the problem.
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
|
2014-12-06 00:50:56 +00:00
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, waitRun(id))
|
2014-12-06 00:50:56 +00:00
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
done := make(chan error, 1)
|
2014-12-06 00:50:56 +00:00
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
if _, err := cmd.StdinPipe(); err != nil {
|
2015-04-27 17:29:48 +00:00
|
|
|
done <- err
|
|
|
|
return
|
2014-12-06 00:50:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 17:19:17 +00:00
|
|
|
expected := "the input device is not a TTY"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected += ". If you are using mintty, try prefixing the command with 'winpty'"
|
|
|
|
}
|
2014-12-06 00:50:56 +00:00
|
|
|
if out, _, err := runCommandWithOutput(cmd); err == nil {
|
2015-04-27 17:29:48 +00:00
|
|
|
done <- fmt.Errorf("attach should have failed")
|
|
|
|
return
|
2014-12-06 00:50:56 +00:00
|
|
|
} else if !strings.Contains(out, expected) {
|
2015-04-27 17:29:48 +00:00
|
|
|
done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
|
|
|
|
return
|
2014-12-06 00:50:56 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 17:29:48 +00:00
|
|
|
case err := <-done:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-12-06 00:50:56 +00:00
|
|
|
case <-time.After(attachWait):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("attach is running but should have failed")
|
2014-12-06 00:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:35:54 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIAttachSuite) TestAttachDisconnect(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 16:46:47 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
|
2015-04-11 03:35:54 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", id)
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:35:54 +00:00
|
|
|
}
|
|
|
|
defer stdin.Close()
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-11 03:35:54 +00:00
|
|
|
defer stdout.Close()
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, cmd.Start() == nil)
|
2017-09-22 13:52:41 +00:00
|
|
|
defer func() {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
|
|
|
}()
|
2015-04-11 03:35:54 +00:00
|
|
|
|
2015-10-11 07:33:24 +00:00
|
|
|
_, err = stdin.Write([]byte("hello\n"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-11 03:35:54 +00:00
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, strings.TrimSpace(out), "hello")
|
2015-04-11 03:35:54 +00:00
|
|
|
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, stdin.Close() == nil)
|
2015-04-11 03:35:54 +00:00
|
|
|
|
|
|
|
// Expect container to still be running after stdin is closed
|
2016-01-28 14:19:25 +00:00
|
|
|
running := inspectField(c, id, "State.Running")
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, running, "true")
|
2015-04-11 03:35:54 +00:00
|
|
|
}
|
2015-10-29 01:00:09 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIAttachSuite) TestAttachPausedContainer(c *testing.T) {
|
2016-09-09 00:31:04 +00:00
|
|
|
testRequires(c, IsPausable)
|
|
|
|
runSleepingContainer(c, "-d", "--name=test")
|
2015-10-29 01:00:09 +00:00
|
|
|
dockerCmd(c, "pause", "test")
|
2016-08-04 16:57:34 +00:00
|
|
|
|
|
|
|
result := dockerCmdWithResult("attach", "test")
|
2017-08-23 21:01:29 +00:00
|
|
|
result.Assert(c, icmd.Expected{
|
2016-08-04 16:57:34 +00:00
|
|
|
Error: "exit status 1",
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "You cannot attach to a paused container, unpause it first",
|
|
|
|
})
|
2015-10-29 01:00:09 +00:00
|
|
|
}
|