-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_cli_attach_test.go
163 lines (131 loc) · 3.68 KB
/
docker_cli_attach_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
"sync"
"time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
const attachWait = 5 * time.Second
func (s *DockerSuite) TestAttachMultipleAndRestart(c *check.C) {
testRequires(c, DaemonIsLinux)
endGroup := &sync.WaitGroup{}
startGroup := &sync.WaitGroup{}
endGroup.Add(3)
startGroup.Add(3)
err := waitForContainer("attacher", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 1; echo hello; done")
c.Assert(err, check.IsNil)
startDone := make(chan struct{})
endDone := make(chan struct{})
go func() {
endGroup.Wait()
close(endDone)
}()
go func() {
startGroup.Wait()
close(startDone)
}()
for i := 0; i < 3; i++ {
go func() {
cmd := exec.Command(dockerBinary, "attach", "attacher")
defer func() {
cmd.Wait()
endGroup.Done()
}()
out, err := cmd.StdoutPipe()
if err != nil {
c.Fatal(err)
}
if err := cmd.Start(); err != nil {
c.Fatal(err)
}
buf := make([]byte, 1024)
if _, err := out.Read(buf); err != nil && err != io.EOF {
c.Fatal(err)
}
startGroup.Done()
if !strings.Contains(string(buf), "hello") {
c.Fatalf("unexpected output %s expected hello\n", string(buf))
}
}()
}
select {
case <-startDone:
case <-time.After(attachWait):
c.Fatalf("Attaches did not initialize properly")
}
dockerCmd(c, "kill", "attacher")
select {
case <-endDone:
case <-time.After(attachWait):
c.Fatalf("Attaches did not finish properly")
}
}
func (s *DockerSuite) TestAttachTtyWithoutStdin(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
id := strings.TrimSpace(out)
c.Assert(waitRun(id), check.IsNil)
done := make(chan error)
go func() {
defer close(done)
cmd := exec.Command(dockerBinary, "attach", id)
if _, err := cmd.StdinPipe(); err != nil {
done <- err
return
}
expected := "cannot enable tty mode"
if out, _, err := runCommandWithOutput(cmd); err == nil {
done <- fmt.Errorf("attach should have failed")
return
} else if !strings.Contains(out, expected) {
done <- fmt.Errorf("attach failed with error %q: expected %q", out, expected)
return
}
}()
select {
case err := <-done:
c.Assert(err, check.IsNil)
case <-time.After(attachWait):
c.Fatal("attach is running but should have failed")
}
}
func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
id := strings.TrimSpace(out)
cmd := exec.Command(dockerBinary, "attach", id)
stdin, err := cmd.StdinPipe()
if err != nil {
c.Fatal(err)
}
defer stdin.Close()
stdout, err := cmd.StdoutPipe()
c.Assert(err, check.IsNil)
defer stdout.Close()
c.Assert(cmd.Start(), check.IsNil)
defer cmd.Process.Kill()
_, err = stdin.Write([]byte("hello\n"))
c.Assert(err, check.IsNil)
out, err = bufio.NewReader(stdout).ReadString('\n')
c.Assert(err, check.IsNil)
c.Assert(strings.TrimSpace(out), check.Equals, "hello")
c.Assert(stdin.Close(), check.IsNil)
// Expect container to still be running after stdin is closed
running, err := inspectField(id, "State.Running")
c.Assert(err, check.IsNil)
c.Assert(running, check.Equals, "true")
}
func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
testRequires(c, DaemonIsLinux) // Containers cannot be paused on Windows
defer unpauseAllContainers()
dockerCmd(c, "run", "-d", "--name=test", "busybox", "top")
dockerCmd(c, "pause", "test")
out, _, err := dockerCmdWithError("attach", "test")
c.Assert(err, checker.NotNil, check.Commentf(out))
c.Assert(out, checker.Contains, "You cannot attach to a paused container, unpause it first")
}