72f49e55 |
package main
import ( |
e4cfd9b3 |
"bufio" |
4203230c |
"fmt" |
01094c15 |
"io" |
72f49e55 |
"os/exec" |
f7541b00 |
"runtime" |
72f49e55 |
"strings"
"sync" |
59e55dcd |
"testing" |
72f49e55 |
"time" |
dc944ea7 |
|
db35c2a5 |
"github.com/docker/docker/integration-cli/cli" |
9f0b3f56 |
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd" |
72f49e55 |
)
|
01094c15 |
const attachWait = 5 * time.Second
|
1d92789b |
func (s *DockerSuite) TestAttachMultipleAndRestart(c *testing.T) { |
01094c15 |
endGroup := &sync.WaitGroup{}
startGroup := &sync.WaitGroup{}
endGroup.Add(3)
startGroup.Add(3)
|
db35c2a5 |
cli.DockerCmd(c, "run", "--name", "attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
cli.WaitRun(c, "attacher") |
01094c15 |
startDone := make(chan struct{})
endDone := make(chan struct{})
go func() {
endGroup.Wait()
close(endDone) |
eb971633 |
}()
|
72f49e55 |
go func() { |
01094c15 |
startGroup.Wait()
close(startDone) |
72f49e55 |
}()
for i := 0; i < 3; i++ {
go func() { |
dc944ea7 |
cmd := exec.Command(dockerBinary, "attach", "attacher") |
72f49e55 |
|
01094c15 |
defer func() { |
dc944ea7 |
cmd.Wait() |
01094c15 |
endGroup.Done()
}()
|
dc944ea7 |
out, err := cmd.StdoutPipe() |
72f49e55 |
if err != nil { |
cbc3cfe1 |
c.Error(err) |
01094c15 |
} |
256310e9 |
defer out.Close() |
01094c15 |
|
dc944ea7 |
if err := cmd.Start(); err != nil { |
cbc3cfe1 |
c.Error(err) |
72f49e55 |
} |
01094c15 |
buf := make([]byte, 1024)
if _, err := out.Read(buf); err != nil && err != io.EOF { |
cbc3cfe1 |
c.Error(err) |
01094c15 |
}
startGroup.Done()
if !strings.Contains(string(buf), "hello") { |
cbc3cfe1 |
c.Errorf("unexpected output %s expected hello\n", string(buf)) |
72f49e55 |
}
}()
}
|
01094c15 |
select {
case <-startDone:
case <-time.After(attachWait): |
dc944ea7 |
c.Fatalf("Attaches did not initialize properly") |
01094c15 |
}
|
db35c2a5 |
cli.DockerCmd(c, "kill", "attacher") |
01094c15 |
select {
case <-endDone:
case <-time.After(attachWait): |
dc944ea7 |
c.Fatalf("Attaches did not finish properly") |
01094c15 |
} |
72f49e55 |
} |
67e3ddb7 |
|
1d92789b |
func (s *DockerSuite) TestAttachTTYWithoutStdin(c *testing.T) { |
89884487 |
// TODO: Figure out how to get this running again reliable on Windows. |
e4ec9195 |
// 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) |
5c295460 |
out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox") |
67e3ddb7 |
id := strings.TrimSpace(out) |
6345208b |
assert.NilError(c, waitRun(id)) |
67e3ddb7 |
|
c322af80 |
done := make(chan error, 1) |
67e3ddb7 |
go func() {
defer close(done)
cmd := exec.Command(dockerBinary, "attach", id)
if _, err := cmd.StdinPipe(); err != nil { |
4203230c |
done <- err
return |
67e3ddb7 |
}
|
f7541b00 |
expected := "the input device is not a TTY"
if runtime.GOOS == "windows" {
expected += ". If you are using mintty, try prefixing the command with 'winpty'"
} |
67e3ddb7 |
if out, _, err := runCommandWithOutput(cmd); err == nil { |
4203230c |
done <- fmt.Errorf("attach should have failed")
return |
67e3ddb7 |
} else if !strings.Contains(out, expected) { |
4203230c |
done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
return |
67e3ddb7 |
}
}()
select { |
4203230c |
case err := <-done: |
6345208b |
assert.NilError(c, err) |
67e3ddb7 |
case <-time.After(attachWait): |
dc944ea7 |
c.Fatal("attach is running but should have failed") |
67e3ddb7 |
}
} |
e4cfd9b3 |
|
1d92789b |
func (s *DockerSuite) TestAttachDisconnect(c *testing.T) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
dc944ea7 |
out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat") |
e4cfd9b3 |
id := strings.TrimSpace(out)
cmd := exec.Command(dockerBinary, "attach", id)
stdin, err := cmd.StdinPipe()
if err != nil { |
dc944ea7 |
c.Fatal(err) |
e4cfd9b3 |
}
defer stdin.Close()
stdout, err := cmd.StdoutPipe() |
6345208b |
assert.NilError(c, err) |
e4cfd9b3 |
defer stdout.Close() |
2743e2d8 |
assert.Assert(c, cmd.Start() == nil) |
ddae20c0 |
defer func() {
cmd.Process.Kill()
cmd.Wait()
}() |
e4cfd9b3 |
|
a1d4b7dd |
_, err = stdin.Write([]byte("hello\n")) |
6345208b |
assert.NilError(c, err) |
e4cfd9b3 |
out, err = bufio.NewReader(stdout).ReadString('\n') |
6345208b |
assert.NilError(c, err) |
6dc7f4c1 |
assert.Equal(c, strings.TrimSpace(out), "hello") |
e4cfd9b3 |
|
2743e2d8 |
assert.Assert(c, stdin.Close() == nil) |
e4cfd9b3 |
// Expect container to still be running after stdin is closed |
62a856e9 |
running := inspectField(c, id, "State.Running") |
6dc7f4c1 |
assert.Equal(c, running, "true") |
e4cfd9b3 |
} |
de1d6119 |
|
1d92789b |
func (s *DockerSuite) TestAttachPausedContainer(c *testing.T) { |
69985e85 |
testRequires(c, IsPausable)
runSleepingContainer(c, "-d", "--name=test") |
de1d6119 |
dockerCmd(c, "pause", "test") |
d7022f2b |
result := dockerCmdWithResult("attach", "test") |
92427b3a |
result.Assert(c, icmd.Expected{ |
d7022f2b |
Error: "exit status 1",
ExitCode: 1,
Err: "You cannot attach to a paused container, unpause it first",
}) |
de1d6119 |
} |