1
0
mirror of https://github.com/moby/moby.git synced 2025-08-01 05:47:11 +03:00

Add a new request package in integration-cli

The goal is to remove function from `docker_utils.go` and setup
simple, one-responsability package that can be well tested ; and to
ease writing request.

This moves all the calls to `sockRequest` (and similar methods) to
their counterpart in the `request` package.

This introduce `request.Do` to write easier request (with functional
argument to easily augment the request) with some pre-defined function
for the most used http method (i.e. `request.Get`, `request.Post` and
`request.Delete`).

Few of the `sockRequest` call have been moved to `request.Do` (and
`Get`, etc.) to showcase the usage of the package. There is still a
whole lot to do.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester
2016-12-30 10:49:36 +01:00
parent de709ebfd8
commit d69d4799a3
32 changed files with 536 additions and 426 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/pkg/testutil"
"github.com/go-check/check"
@ -23,7 +24,7 @@ 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), "")
rwc, err := request.SockConn(time.Duration(10*time.Second), daemonHost())
c.Assert(err, checker.IsNil)
cleanedContainerID := strings.TrimSpace(out)
@ -73,22 +74,20 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
// regression gh14320
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
req, client, err := newRequestClient("POST", "/containers/doesnotexist/attach", nil, "", "")
client, err := request.NewClient(daemonHost())
c.Assert(err, checker.IsNil)
req, err := request.New(daemonHost(), "/containers/doesnotexist/attach", request.Method(http.MethodPost))
resp, err := client.Do(req)
// connection will shutdown, err should be "persistent connection closed"
c.Assert(err, checker.NotNil) // Server shutdown connection
body, err := testutil.ReadBody(resp.Body)
c.Assert(err, checker.IsNil)
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound)
content, err := testutil.ReadBody(resp.Body)
c.Assert(err, checker.IsNil)
expected := "No such container: doesnotexist\r\n"
c.Assert(string(body), checker.Equals, expected)
c.Assert(string(content), checker.Equals, expected)
}
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
status, body, err := request.SockRequest("GET", "/containers/doesnotexist/attach/ws", nil, daemonHost())
c.Assert(status, checker.Equals, http.StatusNotFound)
c.Assert(err, checker.IsNil)
expected := "No such container: doesnotexist"
@ -140,12 +139,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
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")
conn, br, err := request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", daemonHost())
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")
conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", daemonHost())
c.Assert(err, checker.IsNil)
// Since the container only emits stdout, attaching to stderr should return nothing.
expectTimeout(conn, br, "stdout")
@ -153,10 +152,10 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
// 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")
conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", daemonHost())
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")
conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", daemonHost())
c.Assert(err, checker.IsNil)
expectTimeout(conn, br, "stderr")
@ -164,12 +163,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
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")
conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", daemonHost())
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")
conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", daemonHost())
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.