2015-01-17 02:52:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-11 15:39:44 +00:00
|
|
|
"bufio"
|
2015-07-09 18:49:41 +00:00
|
|
|
"io"
|
2015-10-11 15:39:44 +00:00
|
|
|
"net"
|
2015-07-01 16:10:31 +00:00
|
|
|
"net/http"
|
2015-04-06 13:21:18 +00:00
|
|
|
"strings"
|
2015-02-12 19:51:28 +00:00
|
|
|
"time"
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2015-10-15 07:22:38 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2015-06-16 14:08:18 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2015-01-17 02:52:27 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 06:35:36 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat")
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2016-06-14 02:54:20 +00:00
|
|
|
rwc, err := sockConn(time.Duration(10*time.Second), "")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-01-17 02:52:27 +00:00
|
|
|
config, err := websocket.NewConfig(
|
|
|
|
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
|
|
|
|
"http://localhost",
|
|
|
|
)
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-01-17 02:52:27 +00:00
|
|
|
|
|
|
|
ws, err := websocket.NewClient(config, rwc)
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-01-17 02:52:27 +00:00
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
expected := []byte("hello")
|
|
|
|
actual := make([]byte, len(expected))
|
2015-04-27 11:56:55 +00:00
|
|
|
|
|
|
|
outChan := make(chan error)
|
2015-01-17 02:52:27 +00:00
|
|
|
go func() {
|
2016-03-31 18:31:48 +00:00
|
|
|
_, err := io.ReadFull(ws, actual)
|
2015-04-27 11:56:55 +00:00
|
|
|
outChan <- err
|
|
|
|
close(outChan)
|
2015-01-17 02:52:27 +00:00
|
|
|
}()
|
|
|
|
|
2015-04-27 11:56:55 +00:00
|
|
|
inChan := make(chan error)
|
2015-01-17 02:52:27 +00:00
|
|
|
go func() {
|
2015-04-27 11:56:55 +00:00
|
|
|
_, err := ws.Write(expected)
|
|
|
|
inChan <- err
|
|
|
|
close(inChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-inChan:
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-27 11:56:55 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout writing to ws")
|
|
|
|
}
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2015-04-27 11:56:55 +00:00
|
|
|
select {
|
|
|
|
case err := <-outChan:
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-04-27 11:56:55 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout reading from ws")
|
|
|
|
}
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(actual, checker.DeepEquals, expected, check.Commentf("Websocket didn't return the expected data"))
|
2015-01-17 02:52:27 +00:00
|
|
|
}
|
2015-07-01 16:10:31 +00:00
|
|
|
|
|
|
|
// regression gh14320
|
|
|
|
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
|
2016-06-14 02:54:20 +00:00
|
|
|
req, client, err := newRequestClient("POST", "/containers/doesnotexist/attach", nil, "", "")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-03-23 11:34:47 +00:00
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
// connection will shutdown, err should be "persistent connection closed"
|
|
|
|
c.Assert(err, checker.NotNil) // Server shutdown connection
|
|
|
|
|
|
|
|
body, err := readBody(resp.Body)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
|
|
|
|
expected := "No such container: doesnotexist\r\n"
|
|
|
|
c.Assert(string(body), checker.Equals, expected)
|
2015-07-01 16:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
|
|
|
|
status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(status, checker.Equals, http.StatusNotFound)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-05-21 11:56:04 +00:00
|
|
|
expected := "No such container: doesnotexist"
|
|
|
|
c.Assert(getErrorMessage(c, body), checker.Contains, expected)
|
2015-07-01 16:10:31 +00:00
|
|
|
}
|
2015-07-09 18:49:41 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
expectSuccess := func(conn net.Conn, br *bufio.Reader, stream string, tty bool) {
|
|
|
|
defer conn.Close()
|
|
|
|
expected := []byte("success")
|
|
|
|
_, err := conn.Write(expected)
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(time.Second))
|
|
|
|
lenHeader := 0
|
|
|
|
if !tty {
|
|
|
|
lenHeader = 8
|
|
|
|
}
|
|
|
|
actual := make([]byte, len(expected)+lenHeader)
|
|
|
|
_, err = io.ReadFull(br, actual)
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
if !tty {
|
|
|
|
fdMap := map[string]byte{
|
|
|
|
"stdin": 0,
|
|
|
|
"stdout": 1,
|
|
|
|
"stderr": 2,
|
|
|
|
}
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(actual[0], checker.Equals, fdMap[stream])
|
2015-10-11 15:39:44 +00:00
|
|
|
}
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
|
2015-07-09 18:49:41 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
|
|
|
|
defer conn.Close()
|
|
|
|
_, err := conn.Write([]byte{'t'})
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(time.Second))
|
|
|
|
actual := make([]byte, 1)
|
|
|
|
_, err = io.ReadFull(br, actual)
|
|
|
|
opErr, ok := err.(*net.OpError)
|
2015-10-15 07:22:38 +00:00
|
|
|
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))
|
2015-07-09 18:49:41 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// 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")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
// 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")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
// Since the container only emits stdout, attaching to stderr should return nothing.
|
|
|
|
expectTimeout(conn, br, "stdout")
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-12-13 16:00:39 +00:00
|
|
|
// Test the similar functions of the stderr stream.
|
2015-10-11 15:39:44 +00:00
|
|
|
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")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
expectSuccess(conn, br, "stderr", false)
|
|
|
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
expectTimeout(conn, br, "stderr")
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// 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")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
expectSuccess(conn, br, "stdout", true)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// Attach without stdout stream.
|
|
|
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
2015-10-15 07:22:38 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-11 15:39:44 +00:00
|
|
|
// 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")
|
2015-07-09 18:49:41 +00:00
|
|
|
}
|