2015-01-17 02:52:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-11 15:39:44 +00:00
|
|
|
"bufio"
|
2016-09-19 18:55:52 +00:00
|
|
|
"bytes"
|
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"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-02-12 19:51:28 +00:00
|
|
|
"time"
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2016-09-19 18:55:52 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2023-08-25 18:10:15 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-09-19 18:55:52 +00:00
|
|
|
"github.com/docker/docker/client"
|
2023-07-27 11:13:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2016-09-19 18:55:52 +00:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2019-10-11 09:54:35 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
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-16 03:07:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-06-16 14:08:18 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-01-17 02:52:27 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestGetContainersAttachWebsocket(c *testing.T) {
|
2023-07-27 11:13:00 +00:00
|
|
|
cid := cli.DockerCmd(c, "run", "-di", "busybox", "cat").Stdout()
|
|
|
|
cid = strings.TrimSpace(cid)
|
2015-01-17 02:52:27 +00:00
|
|
|
|
2019-08-05 15:54:15 +00:00
|
|
|
rwc, err := request.SockConn(10*time.Second, request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-01-17 02:52:27 +00:00
|
|
|
|
|
|
|
config, err := websocket.NewConfig(
|
2023-07-27 11:13:00 +00:00
|
|
|
"/containers/"+cid+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
|
2015-01-17 02:52:27 +00:00
|
|
|
"http://localhost",
|
|
|
|
)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-01-17 02:52:27 +00:00
|
|
|
|
|
|
|
ws, err := websocket.NewClient(config, rwc)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
outChan := make(chan error, 1)
|
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
|
|
|
}()
|
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
inChan := make(chan error, 1)
|
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:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, is.DeepEqual(actual, expected), "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
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestPostContainersAttachContainerNotFound(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
resp, _, err := request.Post(ctx, "/containers/doesnotexist/attach")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-03-23 11:34:47 +00:00
|
|
|
// connection will shutdown, err should be "persistent connection closed"
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, resp.StatusCode, http.StatusNotFound)
|
2017-08-21 22:50:40 +00:00
|
|
|
content, err := request.ReadBody(resp.Body)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-03-23 11:34:47 +00:00
|
|
|
expected := "No such container: doesnotexist\r\n"
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, string(content), expected)
|
2015-07-01 16:10:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestGetContainersWsAttachContainerNotFound(c *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
res, body, err := request.Get(ctx, "/containers/doesnotexist/attach/ws")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusNotFound)
|
|
|
|
assert.NilError(c, err)
|
2017-05-24 03:56:26 +00:00
|
|
|
b, err := request.ReadBody(body)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-05-21 11:56:04 +00:00
|
|
|
expected := "No such container: doesnotexist"
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, strings.Contains(getErrorMessage(c, b), expected))
|
2015-07-01 16:10:31 +00:00
|
|
|
}
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestPostContainersAttach(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2019-10-11 09:54:35 +00:00
|
|
|
expectSuccess := func(wc io.WriteCloser, br *bufio.Reader, stream string, tty bool) {
|
|
|
|
defer wc.Close()
|
2015-10-11 15:39:44 +00:00
|
|
|
expected := []byte("success")
|
2019-10-11 09:54:35 +00:00
|
|
|
_, err := wc.Write(expected)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
lenHeader := 0
|
|
|
|
if !tty {
|
|
|
|
lenHeader = 8
|
|
|
|
}
|
|
|
|
actual := make([]byte, len(expected)+lenHeader)
|
2019-10-11 09:54:35 +00:00
|
|
|
_, err = readTimeout(br, actual, time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 15:39:44 +00:00
|
|
|
if !tty {
|
|
|
|
fdMap := map[string]byte{
|
|
|
|
"stdin": 0,
|
|
|
|
"stdout": 1,
|
|
|
|
"stderr": 2,
|
|
|
|
}
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, actual[0], fdMap[stream])
|
2015-10-11 15:39:44 +00:00
|
|
|
}
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, is.DeepEqual(actual[lenHeader:], expected), "Attach didn't return the expected data from %s", stream)
|
2015-07-09 18:49:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 09:54:35 +00:00
|
|
|
expectTimeout := func(wc io.WriteCloser, br *bufio.Reader, stream string) {
|
|
|
|
defer wc.Close()
|
|
|
|
_, err := wc.Write([]byte{'t'})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
actual := make([]byte, 1)
|
2019-10-11 09:54:35 +00:00
|
|
|
_, err = readTimeout(br, actual, time.Second)
|
|
|
|
assert.Assert(c, err.Error() == "Timeout", "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.
|
2023-07-27 11:13:00 +00:00
|
|
|
cid := cli.DockerCmd(c, "run", "-di", "busybox", "cat").Stdout()
|
2015-10-11 15:39:44 +00:00
|
|
|
cid = strings.TrimSpace(cid)
|
2019-10-11 09:54:35 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// Attach to the container's stdout stream.
|
2019-10-11 09:54:35 +00:00
|
|
|
wc, br, err := requestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 15:39:44 +00:00
|
|
|
// Check if the data from stdout can be received.
|
2019-10-11 09:54:35 +00:00
|
|
|
expectSuccess(wc, br, "stdout", false)
|
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// Attach to the container's stderr stream.
|
2019-10-11 09:54:35 +00:00
|
|
|
wc, br, err = requestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-11 15:39:44 +00:00
|
|
|
// Since the container only emits stdout, attaching to stderr should return nothing.
|
2019-10-11 09:54:35 +00:00
|
|
|
expectTimeout(wc, 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.
|
2023-07-27 11:13:00 +00:00
|
|
|
cid = cli.DockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2").Stdout()
|
2015-10-11 15:39:44 +00:00
|
|
|
cid = strings.TrimSpace(cid)
|
2019-10-11 09:54:35 +00:00
|
|
|
wc, br, err = requestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-10-11 09:54:35 +00:00
|
|
|
expectSuccess(wc, br, "stderr", false)
|
|
|
|
wc, br, err = requestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-10-11 09:54:35 +00:00
|
|
|
expectTimeout(wc, br, "stderr")
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// Test with tty.
|
2023-07-27 11:13:00 +00:00
|
|
|
cid = cli.DockerCmd(c, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2").Stdout()
|
2015-10-11 15:39:44 +00:00
|
|
|
cid = strings.TrimSpace(cid)
|
|
|
|
// Attach to stdout only.
|
2019-10-11 09:54:35 +00:00
|
|
|
wc, br, err = requestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-10-11 09:54:35 +00:00
|
|
|
expectSuccess(wc, br, "stdout", true)
|
2015-07-09 18:49:41 +00:00
|
|
|
|
2015-10-11 15:39:44 +00:00
|
|
|
// Attach without stdout stream.
|
2019-10-11 09:54:35 +00:00
|
|
|
wc, br, err = requestHijack(http.MethodPost, "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", request.DaemonHost())
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
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.
|
2019-10-11 09:54:35 +00:00
|
|
|
expectTimeout(wc, br, "stdout")
|
2016-09-19 18:55:52 +00:00
|
|
|
|
|
|
|
// Test the client API
|
2023-04-03 11:00:29 +00:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-04-03 11:00:29 +00:00
|
|
|
defer apiClient.Close()
|
2016-09-19 18:55:52 +00:00
|
|
|
|
2023-07-27 11:13:00 +00:00
|
|
|
cid = cli.DockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat").Stdout()
|
2016-09-19 18:55:52 +00:00
|
|
|
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-16 03:07:26 +00:00
|
|
|
// Make sure we don't see "hello" if Logs is false
|
2023-08-25 18:10:15 +00:00
|
|
|
attachOpts := container.AttachOptions{
|
2016-09-19 18:55:52 +00:00
|
|
|
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-16 03:07:26 +00:00
|
|
|
Stderr: true,
|
|
|
|
Logs: false,
|
2016-09-19 18:55:52 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, err := apiClient.ContainerAttach(testutil.GetContext(c), cid, attachOpts)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2019-08-28 11:46:32 +00:00
|
|
|
mediaType, b := resp.MediaType()
|
|
|
|
assert.Check(c, b)
|
|
|
|
assert.Equal(c, mediaType, types.MediaTypeMultiplexedStream)
|
2016-09-19 18:55:52 +00:00
|
|
|
expectSuccess(resp.Conn, resp.Reader, "stdout", false)
|
|
|
|
|
|
|
|
// Make sure we do see "hello" if Logs is true
|
|
|
|
attachOpts.Logs = true
|
2023-07-14 18:02:38 +00:00
|
|
|
resp, err = apiClient.ContainerAttach(testutil.GetContext(c), cid, attachOpts)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-09-19 18:55:52 +00:00
|
|
|
|
|
|
|
defer resp.Conn.Close()
|
|
|
|
resp.Conn.SetReadDeadline(time.Now().Add(time.Second))
|
|
|
|
|
|
|
|
_, err = resp.Conn.Write([]byte("success"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-09-19 18:55:52 +00: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-16 03:07:26 +00:00
|
|
|
var outBuf, errBuf bytes.Buffer
|
2020-04-17 10:01:01 +00:00
|
|
|
var nErr net.Error
|
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-16 03:07:26 +00:00
|
|
|
_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
|
2020-04-17 10:01:01 +00:00
|
|
|
if errors.As(err, &nErr) && nErr.Timeout() {
|
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-16 03:07:26 +00:00
|
|
|
// ignore the timeout error as it is expected
|
|
|
|
err = nil
|
|
|
|
}
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, errBuf.String(), "")
|
|
|
|
assert.Equal(c, outBuf.String(), "hello\nsuccess")
|
2015-07-09 18:49:41 +00:00
|
|
|
}
|
2017-09-19 20:12:29 +00:00
|
|
|
|
2019-10-11 09:54:35 +00:00
|
|
|
// requestHijack create a http requst to specified host with `Upgrade` header (with method
|
|
|
|
// , contenttype, …), if receive a successful "101 Switching Protocols" response return
|
|
|
|
// a `io.WriteCloser` and `bufio.Reader`
|
|
|
|
func requestHijack(method, endpoint string, data io.Reader, ct, daemon string, modifiers ...func(*http.Request)) (io.WriteCloser, *bufio.Reader, error) {
|
|
|
|
hostURL, err := client.ParseHostURL(daemon)
|
2017-09-19 20:12:29 +00:00
|
|
|
if err != nil {
|
2019-10-11 09:54:35 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "parse daemon host error")
|
2017-09-19 20:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, endpoint, data)
|
|
|
|
if err != nil {
|
2019-10-11 09:54:35 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "could not create new request")
|
2017-09-19 20:12:29 +00:00
|
|
|
}
|
2019-10-11 09:54:35 +00:00
|
|
|
req.URL.Scheme = "http"
|
|
|
|
req.URL.Host = hostURL.Host
|
2017-09-19 20:12:29 +00:00
|
|
|
|
2023-07-12 15:37:01 +00:00
|
|
|
if hostURL.Scheme == "unix" || hostURL.Scheme == "npipe" {
|
|
|
|
// Override host header for non-tcp connections.
|
|
|
|
req.Host = client.DummyHost
|
|
|
|
}
|
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
for _, opt := range modifiers {
|
|
|
|
opt(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ct != "" {
|
|
|
|
req.Header.Set("Content-Type", ct)
|
|
|
|
}
|
2019-10-11 09:54:35 +00:00
|
|
|
|
|
|
|
// must have Upgrade header
|
|
|
|
// server api return 101 Switching Protocols
|
|
|
|
req.Header.Set("Upgrade", "tcp")
|
|
|
|
|
|
|
|
// new client
|
|
|
|
// FIXME use testutil/request newHTTPClient
|
|
|
|
transport := &http.Transport{}
|
|
|
|
err = sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "configure Transport error")
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:00:29 +00:00
|
|
|
c := http.Client{
|
2019-10-11 09:54:35 +00:00
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:00:29 +00:00
|
|
|
resp, err := c.Do(req)
|
2019-10-11 09:54:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "client.Do")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bodyIsWritable(resp) {
|
|
|
|
return nil, nil, errors.New("response.Body not writable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Body.(io.WriteCloser), bufio.NewReader(resp.Body), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bodyIsWritable check Response.Body is writable
|
|
|
|
func bodyIsWritable(r *http.Response) bool {
|
|
|
|
_, ok := r.Body.(io.Writer)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// readTimeout read from io.Reader with timeout
|
|
|
|
func readTimeout(r io.Reader, buf []byte, timeout time.Duration) (n int, err error) {
|
2020-02-25 22:13:25 +00:00
|
|
|
ch := make(chan bool, 1)
|
2019-10-11 09:54:35 +00:00
|
|
|
go func() {
|
|
|
|
n, err = io.ReadFull(r, buf)
|
|
|
|
ch <- true
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return 0, errors.New("Timeout")
|
|
|
|
}
|
2017-09-19 20:12:29 +00:00
|
|
|
}
|