forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_api_attach_test.go
164 lines (141 loc) · 5.35 KB
/
docker_api_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
164
package main
import (
"bufio"
"io"
"net"
"net/http"
"strings"
"time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
"golang.org/x/net/websocket"
)
func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat")
rwc, err := sockConn(time.Duration(10 * time.Second))
c.Assert(err, checker.IsNil)
cleanedContainerID := strings.TrimSpace(out)
config, err := websocket.NewConfig(
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
"http://localhost",
)
c.Assert(err, checker.IsNil)
ws, err := websocket.NewClient(config, rwc)
c.Assert(err, checker.IsNil)
defer ws.Close()
expected := []byte("hello")
actual := make([]byte, len(expected))
outChan := make(chan error)
go func() {
_, err := ws.Read(actual)
outChan <- err
close(outChan)
}()
inChan := make(chan error)
go func() {
_, err := ws.Write(expected)
inChan <- err
close(inChan)
}()
select {
case err := <-inChan:
c.Assert(err, checker.IsNil)
case <-time.After(5 * time.Second):
c.Fatal("Timeout writing to ws")
}
select {
case err := <-outChan:
c.Assert(err, checker.IsNil)
case <-time.After(5 * time.Second):
c.Fatal("Timeout reading from ws")
}
c.Assert(actual, checker.DeepEquals, expected, check.Commentf("Websocket didn't return the expected data"))
}
// regression gh14320
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
status, body, err := sockRequest("POST", "/containers/doesnotexist/attach", nil)
c.Assert(status, checker.Equals, http.StatusNotFound)
c.Assert(err, checker.IsNil)
expected := "No such container: doesnotexist\n"
c.Assert(string(body), checker.Contains, expected)
}
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
c.Assert(status, checker.Equals, http.StatusNotFound)
c.Assert(err, checker.IsNil)
expected := "No such container: doesnotexist\n"
c.Assert(string(body), checker.Contains, expected)
}
func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
testRequires(c, DaemonIsLinux)
expectSuccess := func(conn net.Conn, br *bufio.Reader, stream string, tty bool) {
defer conn.Close()
expected := []byte("success")
_, err := conn.Write(expected)
c.Assert(err, checker.IsNil)
conn.SetReadDeadline(time.Now().Add(time.Second))
lenHeader := 0
if !tty {
lenHeader = 8
}
actual := make([]byte, len(expected)+lenHeader)
_, err = io.ReadFull(br, actual)
c.Assert(err, checker.IsNil)
if !tty {
fdMap := map[string]byte{
"stdin": 0,
"stdout": 1,
"stderr": 2,
}
c.Assert(actual[0], checker.Equals, fdMap[stream])
}
c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
}
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
defer conn.Close()
_, err := conn.Write([]byte{'t'})
c.Assert(err, checker.IsNil)
conn.SetReadDeadline(time.Now().Add(time.Second))
actual := make([]byte, 1)
_, err = io.ReadFull(br, actual)
opErr, ok := err.(*net.OpError)
c.Assert(ok, checker.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
c.Assert(opErr.Timeout(), checker.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
}
// Create a container that only emits stdout.
cid, _ := dockerCmd(c, "run", "-di", "busybox", "cat")
cid = strings.TrimSpace(cid)
// Attach to the container's stdout stream.
conn, br, err := sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
// Check if the data from stdout can be received.
expectSuccess(conn, br, "stdout", false)
// Attach to the container's stderr stream.
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
// Since the container only emits stdout, attaching to stderr should return nothing.
expectTimeout(conn, br, "stdout")
// Test the similar functions of the stderr stream.
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2")
cid = strings.TrimSpace(cid)
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
expectSuccess(conn, br, "stderr", false)
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
expectTimeout(conn, br, "stderr")
// Test with tty.
cid, _ = dockerCmd(c, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2")
cid = strings.TrimSpace(cid)
// Attach to stdout only.
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
expectSuccess(conn, br, "stdout", true)
// Attach without stdout stream.
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
// Nothing should be received because both the stdout and stderr of the container will be
// sent to the client as stdout when tty is enabled.
expectTimeout(conn, br, "stdout")
}