2015-01-16 21:52:27 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-11 11:39:44 -04:00
|
|
|
"bufio"
|
2016-09-19 14:55:52 -04:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2017-09-19 16:12:29 -04:00
|
|
|
"fmt"
|
2015-07-09 14:49:41 -04:00
|
|
|
"io"
|
2015-10-11 11:39:44 -04:00
|
|
|
"net"
|
2015-07-01 12:10:31 -04:00
|
|
|
"net/http"
|
2017-09-19 16:12:29 -04:00
|
|
|
"net/http/httputil"
|
2015-04-06 09:21:18 -04:00
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-02-12 14:51:28 -05:00
|
|
|
"time"
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2016-09-19 14:55:52 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/request"
|
TestPostContainerAttach: minor improvements
When this test fails, the error looks like this:
> FAIL: docker_api_attach_test.go:98: DockerSuite.TestPostContainersAttach
> docker_api_attach_test.go:211:
> c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout"))
> ... obtained []uint8 = []byte{0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... expected []uint8 = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... Attach didn't return the expected data from stdout
Let's use strings for comparisons to make the output more readable.
While at it,
- get the container's stderr as well, and make sure it's empty;
- check that stdcopy.StdCopy() did not return an error, except for
the timeout which is expected;
- move/remove comments, simplify var names.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-03-15 23:07:26 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-06-16 10:08:18 -04:00
|
|
|
"golang.org/x/net/websocket"
|
2019-04-04 09:23:19 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2015-01-16 21:52:27 -05:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestGetContainersAttachWebsocket(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat")
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
rwc, err := request.SockConn(10*time.Second, request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-01-16 21:52:27 -05:00
|
|
|
config, err := websocket.NewConfig(
|
|
|
|
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
|
|
|
|
"http://localhost",
|
|
|
|
)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-01-16 21:52:27 -05:00
|
|
|
|
|
|
|
ws, err := websocket.NewClient(config, rwc)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-01-16 21:52:27 -05:00
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
expected := []byte("hello")
|
|
|
|
actual := make([]byte, len(expected))
|
2015-04-27 07:56:55 -04:00
|
|
|
|
|
|
|
outChan := make(chan error)
|
2015-01-16 21:52:27 -05:00
|
|
|
go func() {
|
2016-03-31 14:31:48 -04:00
|
|
|
_, err := io.ReadFull(ws, actual)
|
2015-04-27 07:56:55 -04:00
|
|
|
outChan <- err
|
|
|
|
close(outChan)
|
2015-01-16 21:52:27 -05:00
|
|
|
}()
|
|
|
|
|
2015-04-27 07:56:55 -04:00
|
|
|
inChan := make(chan error)
|
2015-01-16 21:52:27 -05:00
|
|
|
go func() {
|
2015-04-27 07:56:55 -04:00
|
|
|
_, err := ws.Write(expected)
|
|
|
|
inChan <- err
|
|
|
|
close(inChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-inChan:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-27 07:56:55 -04:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout writing to ws")
|
|
|
|
}
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2015-04-27 07:56:55 -04:00
|
|
|
select {
|
|
|
|
case err := <-outChan:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-27 07:56:55 -04:00
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
c.Fatal("Timeout reading from ws")
|
|
|
|
}
|
2015-01-16 21:52:27 -05:00
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.DeepEqual(actual, expected), "Websocket didn't return the expected data")
|
2015-01-16 21:52:27 -05:00
|
|
|
}
|
2015-07-01 12:10:31 -04:00
|
|
|
|
|
|
|
// regression gh14320
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *testing.T) {
|
2017-09-19 16:12:29 -04:00
|
|
|
resp, _, err := request.Post("/containers/doesnotexist/attach")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-23 07:34:47 -04:00
|
|
|
// connection will shutdown, err should be "persistent connection closed"
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusNotFound)
|
2017-08-21 18:50:40 -04:00
|
|
|
content, err := request.ReadBody(resp.Body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-23 07:34:47 -04:00
|
|
|
expected := "No such container: doesnotexist\r\n"
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, string(content), expected)
|
2015-07-01 12:10:31 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *testing.T) {
|
2017-05-23 23:56:26 -04:00
|
|
|
res, body, err := request.Get("/containers/doesnotexist/attach/ws")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusNotFound)
|
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
b, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-05-21 07:56:04 -04:00
|
|
|
expected := "No such container: doesnotexist"
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(getErrorMessage(c, b), expected))
|
2015-07-01 12:10:31 -04:00
|
|
|
}
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestPostContainersAttach(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2015-10-11 11:39:44 -04:00
|
|
|
expectSuccess := func(conn net.Conn, br *bufio.Reader, stream string, tty bool) {
|
|
|
|
defer conn.Close()
|
|
|
|
expected := []byte("success")
|
|
|
|
_, err := conn.Write(expected)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2015-10-11 11:39:44 -04: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)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04:00
|
|
|
if !tty {
|
|
|
|
fdMap := map[string]byte{
|
|
|
|
"stdin": 0,
|
|
|
|
"stdout": 1,
|
|
|
|
"stderr": 2,
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, actual[0], fdMap[stream])
|
2015-10-11 11:39:44 -04:00
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.DeepEqual(actual[lenHeader:], expected), "Attach didn't return the expected data from %s", stream)
|
2015-07-09 14:49:41 -04:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:39:44 -04:00
|
|
|
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
|
|
|
|
defer conn.Close()
|
|
|
|
_, err := conn.Write([]byte{'t'})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2015-10-11 11:39:44 -04:00
|
|
|
conn.SetReadDeadline(time.Now().Add(time.Second))
|
|
|
|
actual := make([]byte, 1)
|
|
|
|
_, err = io.ReadFull(br, actual)
|
|
|
|
opErr, ok := err.(*net.OpError)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, ok, "Error is expected to be *net.OpError, got %v", err)
|
|
|
|
assert.Assert(c, opErr.Timeout(), "Read from %s is expected to timeout", stream)
|
2015-07-09 14:49:41 -04:00
|
|
|
}
|
|
|
|
|
2015-10-11 11:39:44 -04: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.
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, br, err := sockRequestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04:00
|
|
|
// Check if the data from stdout can be received.
|
|
|
|
expectSuccess(conn, br, "stdout", false)
|
|
|
|
// Attach to the container's stderr stream.
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, br, err = sockRequestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04:00
|
|
|
// Since the container only emits stdout, attaching to stderr should return nothing.
|
|
|
|
expectTimeout(conn, br, "stdout")
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Test the similar functions of the stderr stream.
|
2015-10-11 11:39:44 -04:00
|
|
|
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2")
|
|
|
|
cid = strings.TrimSpace(cid)
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, br, err = sockRequestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04:00
|
|
|
expectSuccess(conn, br, "stderr", false)
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, br, err = sockRequestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04:00
|
|
|
expectTimeout(conn, br, "stderr")
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2015-10-11 11:39:44 -04:00
|
|
|
// Test with tty.
|
|
|
|
cid, _ = dockerCmd(c, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2")
|
|
|
|
cid = strings.TrimSpace(cid)
|
|
|
|
// Attach to stdout only.
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, br, err = sockRequestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04:00
|
|
|
expectSuccess(conn, br, "stdout", true)
|
2015-07-09 14:49:41 -04:00
|
|
|
|
2015-10-11 11:39:44 -04:00
|
|
|
// Attach without stdout stream.
|
2019-10-12 14:42:19 -04:00
|
|
|
conn, br, err = sockRequestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 11:39:44 -04: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")
|
2016-09-19 14:55:52 -04:00
|
|
|
|
|
|
|
// Test the client API
|
2019-01-03 16:49:00 -05:00
|
|
|
client, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer client.Close()
|
2016-09-19 14:55:52 -04:00
|
|
|
|
|
|
|
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat")
|
|
|
|
cid = strings.TrimSpace(cid)
|
|
|
|
|
TestPostContainerAttach: minor improvements
When this test fails, the error looks like this:
> FAIL: docker_api_attach_test.go:98: DockerSuite.TestPostContainersAttach
> docker_api_attach_test.go:211:
> c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout"))
> ... obtained []uint8 = []byte{0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... expected []uint8 = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... Attach didn't return the expected data from stdout
Let's use strings for comparisons to make the output more readable.
While at it,
- get the container's stderr as well, and make sure it's empty;
- check that stdcopy.StdCopy() did not return an error, except for
the timeout which is expected;
- move/remove comments, simplify var names.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-03-15 23:07:26 -04:00
|
|
|
// Make sure we don't see "hello" if Logs is false
|
2016-09-19 14:55:52 -04:00
|
|
|
attachOpts := types.ContainerAttachOptions{
|
|
|
|
Stream: true,
|
|
|
|
Stdin: true,
|
|
|
|
Stdout: true,
|
TestPostContainerAttach: minor improvements
When this test fails, the error looks like this:
> FAIL: docker_api_attach_test.go:98: DockerSuite.TestPostContainersAttach
> docker_api_attach_test.go:211:
> c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout"))
> ... obtained []uint8 = []byte{0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... expected []uint8 = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... Attach didn't return the expected data from stdout
Let's use strings for comparisons to make the output more readable.
While at it,
- get the container's stderr as well, and make sure it's empty;
- check that stdcopy.StdCopy() did not return an error, except for
the timeout which is expected;
- move/remove comments, simplify var names.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-03-15 23:07:26 -04:00
|
|
|
Stderr: true,
|
|
|
|
Logs: false,
|
2016-09-19 14:55:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.ContainerAttach(context.Background(), cid, attachOpts)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-09-19 14:55:52 -04:00
|
|
|
expectSuccess(resp.Conn, resp.Reader, "stdout", false)
|
|
|
|
|
|
|
|
// Make sure we do see "hello" if Logs is true
|
|
|
|
attachOpts.Logs = true
|
|
|
|
resp, err = client.ContainerAttach(context.Background(), cid, attachOpts)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-09-19 14:55:52 -04:00
|
|
|
|
|
|
|
defer resp.Conn.Close()
|
|
|
|
resp.Conn.SetReadDeadline(time.Now().Add(time.Second))
|
|
|
|
|
|
|
|
_, err = resp.Conn.Write([]byte("success"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-09-19 14:55:52 -04:00
|
|
|
|
TestPostContainerAttach: minor improvements
When this test fails, the error looks like this:
> FAIL: docker_api_attach_test.go:98: DockerSuite.TestPostContainersAttach
> docker_api_attach_test.go:211:
> c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout"))
> ... obtained []uint8 = []byte{0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... expected []uint8 = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... Attach didn't return the expected data from stdout
Let's use strings for comparisons to make the output more readable.
While at it,
- get the container's stderr as well, and make sure it's empty;
- check that stdcopy.StdCopy() did not return an error, except for
the timeout which is expected;
- move/remove comments, simplify var names.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-03-15 23:07:26 -04:00
|
|
|
var outBuf, errBuf bytes.Buffer
|
|
|
|
_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
|
|
|
|
if err != nil && errors.Cause(err).(net.Error).Timeout() {
|
|
|
|
// ignore the timeout error as it is expected
|
|
|
|
err = nil
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, errBuf.String(), "")
|
|
|
|
assert.Equal(c, outBuf.String(), "hello\nsuccess")
|
2015-07-09 14:49:41 -04:00
|
|
|
}
|
2017-09-19 16:12:29 -04:00
|
|
|
|
|
|
|
// SockRequestHijack creates a connection to specified host (with method, contenttype, …) and returns a hijacked connection
|
|
|
|
// and the output as a `bufio.Reader`
|
|
|
|
func sockRequestHijack(method, endpoint string, data io.Reader, ct string, daemon string, modifiers ...func(*http.Request)) (net.Conn, *bufio.Reader, error) {
|
|
|
|
req, client, err := newRequestClient(method, endpoint, data, ct, daemon, modifiers...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client.Do(req)
|
|
|
|
conn, br := client.Hijack()
|
|
|
|
return conn, br, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(vdemeester) httputil.ClientConn is deprecated, use http.Client instead (closer to actual client)
|
|
|
|
// Deprecated: Use New instead of NewRequestClient
|
|
|
|
// Deprecated: use request.Do (or Get, Delete, Post) instead
|
|
|
|
func newRequestClient(method, endpoint string, data io.Reader, ct, daemon string, modifiers ...func(*http.Request)) (*http.Request, *httputil.ClientConn, error) {
|
2019-08-05 11:54:15 -04:00
|
|
|
c, err := request.SockConn(10*time.Second, daemon)
|
2017-09-19 16:12:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := httputil.NewClientConn(c, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, endpoint, data)
|
|
|
|
if err != nil {
|
|
|
|
client.Close()
|
|
|
|
return nil, nil, fmt.Errorf("could not create new request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range modifiers {
|
|
|
|
opt(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ct != "" {
|
|
|
|
req.Header.Set("Content-Type", ct)
|
|
|
|
}
|
|
|
|
return req, client, nil
|
|
|
|
}
|