diff --git a/integration-cli/daemon/daemon.go b/integration-cli/daemon/daemon.go index 5890dd2cb2..67e6066f5b 100644 --- a/integration-cli/daemon/daemon.go +++ b/integration-cli/daemon/daemon.go @@ -2,15 +2,11 @@ package daemon import ( "bytes" - "crypto/tls" "encoding/json" "fmt" "io" "io/ioutil" - "net" "net/http" - "net/http/httputil" - "net/url" "os" "os/exec" "path/filepath" @@ -20,6 +16,7 @@ import ( "github.com/docker/docker/api/types/events" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/stringid" @@ -624,7 +621,7 @@ func (d *Daemon) SockRequest(method, endpoint string, data interface{}) (int, [] // SockRequestRaw executes a socket request on a daemon and returns an http // response and a reader for the output data. func (d *Daemon) SockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) { - return SockRequestRawToDaemon(method, endpoint, data, ct, d.Sock()) + return request.SockRequestRaw(method, endpoint, data, ct, d.Sock()) } // LogFileName returns the path the daemon's log file @@ -714,7 +711,7 @@ func (d *Daemon) ReloadConfig() error { errCh := make(chan error) started := make(chan struct{}) go func() { - _, body, err := SockRequestRawToDaemon("GET", "/events", nil, "", d.Sock()) + _, body, err := request.SockRequestRaw("GET", "/events", nil, "", d.Sock()) close(started) if err != nil { errCh <- err @@ -791,96 +788,6 @@ func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time return nil } -// SockRequestRawToDaemon creates an http request against the specified daemon socket -// FIXME(vdemeester) attach this to daemon ? -func SockRequestRawToDaemon(method, endpoint string, data io.Reader, ct, daemon string) (*http.Response, io.ReadCloser, error) { - req, client, err := newRequestClient(method, endpoint, data, ct, daemon) - if err != nil { - return nil, nil, err - } - - resp, err := client.Do(req) - if err != nil { - client.Close() - return nil, nil, err - } - body := ioutils.NewReadCloserWrapper(resp.Body, func() error { - defer resp.Body.Close() - return client.Close() - }) - - return resp, body, nil -} - -func getTLSConfig() (*tls.Config, error) { - dockerCertPath := os.Getenv("DOCKER_CERT_PATH") - - if dockerCertPath == "" { - return nil, errors.New("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable") - } - - option := &tlsconfig.Options{ - CAFile: filepath.Join(dockerCertPath, "ca.pem"), - CertFile: filepath.Join(dockerCertPath, "cert.pem"), - KeyFile: filepath.Join(dockerCertPath, "key.pem"), - } - tlsConfig, err := tlsconfig.Client(*option) - if err != nil { - return nil, err - } - - return tlsConfig, nil -} - -// SockConn opens a connection on the specified socket -func SockConn(timeout time.Duration, daemon string) (net.Conn, error) { - daemonURL, err := url.Parse(daemon) - if err != nil { - return nil, errors.Wrapf(err, "could not parse url %q", daemon) - } - - var c net.Conn - switch daemonURL.Scheme { - case "npipe": - return npipeDial(daemonURL.Path, timeout) - case "unix": - return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout) - case "tcp": - if os.Getenv("DOCKER_TLS_VERIFY") != "" { - // Setup the socket TLS configuration. - tlsConfig, err := getTLSConfig() - if err != nil { - return nil, err - } - dialer := &net.Dialer{Timeout: timeout} - return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig) - } - return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout) - default: - return c, errors.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon) - } -} - -func newRequestClient(method, endpoint string, data io.Reader, ct, daemon string) (*http.Request, *httputil.ClientConn, error) { - c, err := SockConn(time.Duration(10*time.Second), daemon) - if err != nil { - return nil, nil, errors.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, errors.Errorf("could not create new request: %v", err) - } - - if ct != "" { - req.Header.Set("Content-Type", ct) - } - return req, client, nil -} - // BuildImageCmdWithHost create a build command with the specified arguments. // FIXME(vdemeester) move this away func BuildImageCmdWithHost(dockerBinary, name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd { diff --git a/integration-cli/docker_api_attach_test.go b/integration-cli/docker_api_attach_test.go index d9d63d3ead..88dba3e974 100644 --- a/integration-cli/docker_api_attach_test.go +++ b/integration-cli/docker_api_attach_test.go @@ -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. diff --git a/integration-cli/docker_api_auth_test.go b/integration-cli/docker_api_auth_test.go index 763e56f273..cc903c01ff 100644 --- a/integration-cli/docker_api_auth_test.go +++ b/integration-cli/docker_api_auth_test.go @@ -5,6 +5,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -17,7 +18,7 @@ func (s *DockerSuite) TestAuthAPI(c *check.C) { } expected := "Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password" - status, body, err := sockRequest("POST", "/auth", config) + status, body, err := request.SockRequest("POST", "/auth", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusUnauthorized) msg := getErrorMessage(c, body) diff --git a/integration-cli/docker_api_build_test.go b/integration-cli/docker_api_build_test.go index 3ff9d18818..68eadca88e 100644 --- a/integration-cli/docker_api_build_test.go +++ b/integration-cli/docker_api_build_test.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/testutil" "github.com/go-check/check" ) @@ -31,7 +32,7 @@ RUN find /tmp/` c.Assert(err, checker.IsNil) defer server.Close() - res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json") + res, body, err := request.SockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) @@ -72,7 +73,7 @@ func (s *DockerSuite) TestBuildAPIRemoteTarballContext(c *check.C) { defer server.Close() - res, b, err := sockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar") + res, b, err := request.SockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) b.Close() @@ -121,7 +122,7 @@ RUN echo 'right' defer server.Close() url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar" - res, body, err := sockRequestRaw("POST", url, nil, "application/tar") + res, body, err := request.SockRequestRaw("POST", url, nil, "application/tar", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) @@ -141,7 +142,7 @@ RUN echo from dockerfile`, c.Assert(err, checker.IsNil) defer git.Close() - res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json") + res, body, err := request.SockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) @@ -163,7 +164,7 @@ RUN echo from Dockerfile`, defer git.Close() // Make sure it tries to 'dockerfile' query param value - res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json") + res, body, err := request.SockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) @@ -186,7 +187,7 @@ RUN echo from dockerfile`, defer git.Close() // Make sure it tries to 'dockerfile' query param value - res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json") + res, body, err := request.SockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) @@ -233,7 +234,7 @@ func (s *DockerSuite) TestBuildAPIUnnormalizedTarPaths(c *check.C) { // failed to close tar archive c.Assert(tw.Close(), checker.IsNil) - res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar") + res, body, err := request.SockRequestRaw("POST", "/build", buffer, "application/x-tar", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index c02c48f981..359310a9da 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -8,7 +8,6 @@ import ( "io" "io/ioutil" "net/http" - "net/http/httputil" "net/url" "os" "path/filepath" @@ -22,6 +21,7 @@ import ( mounttypes "github.com/docker/docker/api/types/mount" networktypes "github.com/docker/docker/api/types/network" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/stringid" @@ -37,7 +37,7 @@ func (s *DockerSuite) TestContainerAPIGetAll(c *check.C) { name := "getall" dockerCmd(c, "run", "--name", name, "busybox", "true") - status, body, err := sockRequest("GET", "/containers/json?all=1", nil) + status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -57,7 +57,7 @@ func (s *DockerSuite) TestContainerAPIGetAll(c *check.C) { func (s *DockerSuite) TestContainerAPIGetJSONNoFieldsOmitted(c *check.C) { dockerCmd(c, "run", "busybox", "true") - status, body, err := sockRequest("GET", "/containers/json?all=1", nil) + status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -98,7 +98,7 @@ func (s *DockerSuite) TestContainerAPIPsOmitFields(c *check.C) { port := 80 runSleepingContainer(c, "--name", name, "--expose", strconv.Itoa(port)) - status, body, err := sockRequest("GET", "/containers/json?all=1", nil) + status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -130,7 +130,7 @@ func (s *DockerSuite) TestContainerAPIGetExport(c *check.C) { name := "exportcontainer" dockerCmd(c, "run", "--name", name, "busybox", "touch", "/test") - status, body, err := sockRequest("GET", "/containers/"+name+"/export", nil) + status, body, err := request.SockRequest("GET", "/containers/"+name+"/export", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -154,7 +154,7 @@ func (s *DockerSuite) TestContainerAPIGetChanges(c *check.C) { name := "changescontainer" dockerCmd(c, "run", "--name", name, "busybox", "rm", "/etc/passwd") - status, body, err := sockRequest("GET", "/containers/"+name+"/changes", nil) + status, body, err := request.SockRequest("GET", "/containers/"+name+"/changes", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -187,7 +187,7 @@ func (s *DockerSuite) TestGetContainerStats(c *check.C) { } bc := make(chan b, 1) go func() { - status, body, err := sockRequest("GET", "/containers/"+name+"/stats", nil) + status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats", nil, daemonHost()) bc <- b{status, body, err} }() @@ -218,7 +218,7 @@ func (s *DockerSuite) TestGetContainerStatsRmRunning(c *check.C) { buf := &testutil.ChannelBuffer{make(chan []byte, 1)} defer buf.Close() - _, body, err := sockRequestRaw("GET", "/containers/"+id+"/stats?stream=1", nil, "application/json") + _, body, err := request.SockRequestRaw("GET", "/containers/"+id+"/stats?stream=1", nil, "application/json", daemonHost()) c.Assert(err, checker.IsNil) defer body.Close() @@ -257,7 +257,7 @@ func (s *DockerSuite) TestGetContainerStatsStream(c *check.C) { } bc := make(chan b, 1) go func() { - status, body, err := sockRequest("GET", "/containers/"+name+"/stats", nil) + status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats", nil, daemonHost()) bc <- b{status, body, err} }() @@ -293,7 +293,7 @@ func (s *DockerSuite) TestGetContainerStatsNoStream(c *check.C) { } bc := make(chan b, 1) go func() { - status, body, err := sockRequest("GET", "/containers/"+name+"/stats?stream=0", nil) + status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats?stream=0", nil, daemonHost()) bc <- b{status, body, err} }() @@ -329,7 +329,7 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) { // We expect an immediate response, but if it's not immediate, the test would hang, so put it in a goroutine // below we'll check this on a timeout. go func() { - resp, body, err := sockRequestRaw("GET", "/containers/"+name+"/stats", nil, "") + resp, body, err := request.SockRequestRaw("GET", "/containers/"+name+"/stats", nil, "", daemonHost()) body.Close() chResp <- stats{resp.StatusCode, err} }() @@ -350,7 +350,7 @@ func (s *DockerSuite) TestContainerAPIPause(c *check.C) { out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "30") ContainerID := strings.TrimSpace(out) - status, _, err := sockRequest("POST", "/containers/"+ContainerID+"/pause", nil) + status, _, err := request.SockRequest("POST", "/containers/"+ContainerID+"/pause", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -361,7 +361,7 @@ func (s *DockerSuite) TestContainerAPIPause(c *check.C) { c.Fatalf("there should be one paused container and not %d", len(pausedContainers)) } - status, _, err = sockRequest("POST", "/containers/"+ContainerID+"/unpause", nil) + status, _, err = request.SockRequest("POST", "/containers/"+ContainerID+"/unpause", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -381,7 +381,7 @@ func (s *DockerSuite) TestContainerAPITop(c *check.C) { Processes [][]string } var top topResp - status, b, err := sockRequest("GET", "/containers/"+id+"/top?ps_args=aux", nil) + status, b, err := request.SockRequest("GET", "/containers/"+id+"/top?ps_args=aux", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(json.Unmarshal(b, &top), checker.IsNil) @@ -406,7 +406,7 @@ func (s *DockerSuite) TestContainerAPITopWindows(c *check.C) { Processes [][]string } var top topResp - status, b, err := sockRequest("GET", "/containers/"+id+"/top", nil) + status, b, err := request.SockRequest("GET", "/containers/"+id+"/top", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(json.Unmarshal(b, &top), checker.IsNil) @@ -434,7 +434,7 @@ func (s *DockerSuite) TestContainerAPICommit(c *check.C) { dockerCmd(c, "run", "--name="+cName, "busybox", "/bin/sh", "-c", "touch /test") name := "testcontainerapicommit" - status, b, err := sockRequest("POST", "/commit?repo="+name+"&testtag=tag&container="+cName, nil) + status, b, err := request.SockRequest("POST", "/commit?repo="+name+"&testtag=tag&container="+cName, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) @@ -460,7 +460,7 @@ func (s *DockerSuite) TestContainerAPICommitWithLabelInConfig(c *check.C) { } name := "testcontainerapicommitwithconfig" - status, b, err := sockRequest("POST", "/commit?repo="+name+"&container="+cName, config) + status, b, err := request.SockRequest("POST", "/commit?repo="+name+"&container="+cName, config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) @@ -502,7 +502,7 @@ func (s *DockerSuite) TestContainerAPIBadPort(c *check.C) { jsonData := bytes.NewBuffer(nil) json.NewEncoder(jsonData).Encode(config) - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) c.Assert(getErrorMessage(c, body), checker.Equals, `invalid port specification: "aa80"`, check.Commentf("Incorrect error msg: %s", body)) @@ -514,7 +514,7 @@ func (s *DockerSuite) TestContainerAPICreate(c *check.C) { "Cmd": []string{"/bin/sh", "-c", "touch /test && ls /test"}, } - status, b, err := sockRequest("POST", "/containers/create", config) + status, b, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) @@ -531,7 +531,7 @@ func (s *DockerSuite) TestContainerAPICreate(c *check.C) { func (s *DockerSuite) TestContainerAPICreateEmptyConfig(c *check.C) { config := map[string]interface{}{} - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) @@ -552,7 +552,7 @@ func (s *DockerSuite) TestContainerAPICreateMultipleNetworksConfig(c *check.C) { }, } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusBadRequest) msg := getErrorMessage(c, body) @@ -570,14 +570,14 @@ func (s *DockerSuite) TestContainerAPICreateWithHostName(c *check.C) { "Hostname": hostName, } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), checker.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -593,14 +593,14 @@ func (s *DockerSuite) TestContainerAPICreateWithDomainName(c *check.C) { "Domainname": domainName, } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), checker.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -628,14 +628,14 @@ func UtilCreateNetworkMode(c *check.C, networkMode string) { "HostConfig": map[string]interface{}{"NetworkMode": networkMode}, } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), checker.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -653,14 +653,14 @@ func (s *DockerSuite) TestContainerAPICreateWithCpuSharesCpuset(c *check.C) { "CpusetCpus": "0", } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), checker.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -683,7 +683,7 @@ func (s *DockerSuite) TestContainerAPIVerifyHeader(c *check.C) { create := func(ct string) (*http.Response, io.ReadCloser, error) { jsonData := bytes.NewBuffer(nil) c.Assert(json.NewEncoder(jsonData).Encode(config), checker.IsNil) - return sockRequestRaw("POST", "/containers/create", jsonData, ct) + return request.SockRequestRaw("POST", "/containers/create", jsonData, ct, daemonHost()) } // Try with no content-type @@ -719,7 +719,7 @@ func (s *DockerSuite) TestContainerAPIInvalidPortSyntax(c *check.C) { } }` - res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) @@ -739,7 +739,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyInvalidPolicyName(c *check.C) } }` - res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) @@ -759,7 +759,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyRetryMismatch(c *check.C) { } }` - res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) @@ -779,7 +779,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyNegativeRetryCount(c *check.C } }` - res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) @@ -799,7 +799,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyDefaultRetryCount(c *check.C) } }` - res, _, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, _, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusCreated) } @@ -830,7 +830,7 @@ func (s *DockerSuite) TestContainerAPIPostCreateNull(c *check.C) { "NetworkDisabled":false, "OnBuild":null}` - res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusCreated) @@ -861,7 +861,7 @@ func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *check.C) { "Memory": 524287 }` - res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) b, err2 := testutil.ReadBody(body) c.Assert(err2, checker.IsNil) @@ -875,7 +875,7 @@ func (s *DockerSuite) TestContainerAPIRename(c *check.C) { containerID := strings.TrimSpace(out) newName := "TestContainerAPIRenameNew" - statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/rename?name="+newName, nil) + statusCode, _, err := request.SockRequest("POST", "/containers/"+containerID+"/rename?name="+newName, nil, daemonHost()) c.Assert(err, checker.IsNil) // 204 No Content is expected, not 200 c.Assert(statusCode, checker.Equals, http.StatusNoContent) @@ -888,7 +888,7 @@ func (s *DockerSuite) TestContainerAPIKill(c *check.C) { name := "test-api-kill" runSleepingContainer(c, "-i", "--name", name) - status, _, err := sockRequest("POST", "/containers/"+name+"/kill", nil) + status, _, err := request.SockRequest("POST", "/containers/"+name+"/kill", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -900,7 +900,7 @@ func (s *DockerSuite) TestContainerAPIRestart(c *check.C) { name := "test-api-restart" runSleepingContainer(c, "-di", "--name", name) - status, _, err := sockRequest("POST", "/containers/"+name+"/restart?t=1", nil) + status, _, err := request.SockRequest("POST", "/containers/"+name+"/restart?t=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) c.Assert(waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second), checker.IsNil) @@ -912,7 +912,7 @@ func (s *DockerSuite) TestContainerAPIRestartNotimeoutParam(c *check.C) { id := strings.TrimSpace(out) c.Assert(waitRun(id), checker.IsNil) - status, _, err := sockRequest("POST", "/containers/"+name+"/restart", nil) + status, _, err := request.SockRequest("POST", "/containers/"+name+"/restart", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) c.Assert(waitInspect(name, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second), checker.IsNil) @@ -926,16 +926,16 @@ func (s *DockerSuite) TestContainerAPIStart(c *check.C) { "OpenStdin": true, } - status, _, err := sockRequest("POST", "/containers/create?name="+name, config) + status, _, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) - status, _, err = sockRequest("POST", "/containers/"+name+"/start", nil) + status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) // second call to start should give 304 - status, _, err = sockRequest("POST", "/containers/"+name+"/start", nil) + status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost()) c.Assert(err, checker.IsNil) // TODO(tibor): figure out why this doesn't work on windows @@ -948,13 +948,13 @@ func (s *DockerSuite) TestContainerAPIStop(c *check.C) { name := "test-api-stop" runSleepingContainer(c, "-i", "--name", name) - status, _, err := sockRequest("POST", "/containers/"+name+"/stop?t=30", nil) + status, _, err := request.SockRequest("POST", "/containers/"+name+"/stop?t=30", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) c.Assert(waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second), checker.IsNil) // second call to start should give 304 - status, _, err = sockRequest("POST", "/containers/"+name+"/stop?t=30", nil) + status, _, err = request.SockRequest("POST", "/containers/"+name+"/stop?t=30", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotModified) } @@ -968,7 +968,7 @@ func (s *DockerSuite) TestContainerAPIWait(c *check.C) { } dockerCmd(c, "run", "--name", name, "busybox", sleepCmd, "2") - status, body, err := sockRequest("POST", "/containers/"+name+"/wait", nil) + status, body, err := request.SockRequest("POST", "/containers/"+name+"/wait", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(waitInspect(name, "{{ .State.Running }}", "false", 60*time.Second), checker.IsNil) @@ -986,7 +986,7 @@ func (s *DockerSuite) TestContainerAPICopyNotExistsAnyMore(c *check.C) { Resource: "/test.txt", } - status, _, err := sockRequest("POST", "/containers/"+name+"/copy", postData) + status, _, err := request.SockRequest("POST", "/containers/"+name+"/copy", postData, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound) } @@ -1000,7 +1000,7 @@ func (s *DockerSuite) TestContainerAPICopyPre124(c *check.C) { Resource: "/test.txt", } - status, body, err := sockRequest("POST", "/v1.23/containers/"+name+"/copy", postData) + status, body, err := request.SockRequest("POST", "/v1.23/containers/"+name+"/copy", postData, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -1030,7 +1030,7 @@ func (s *DockerSuite) TestContainerAPICopyResourcePathEmptyPr124(c *check.C) { Resource: "", } - status, body, err := sockRequest("POST", "/v1.23/containers/"+name+"/copy", postData) + status, body, err := request.SockRequest("POST", "/v1.23/containers/"+name+"/copy", postData, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) c.Assert(string(body), checker.Matches, "Path cannot be empty\n") @@ -1045,7 +1045,7 @@ func (s *DockerSuite) TestContainerAPICopyResourcePathNotFoundPre124(c *check.C) Resource: "/notexist", } - status, body, err := sockRequest("POST", "/v1.23/containers/"+name+"/copy", postData) + status, body, err := request.SockRequest("POST", "/v1.23/containers/"+name+"/copy", postData, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) c.Assert(string(body), checker.Matches, "Could not find the file /notexist in container "+name+"\n") @@ -1057,7 +1057,7 @@ func (s *DockerSuite) TestContainerAPICopyContainerNotFoundPr124(c *check.C) { Resource: "/something", } - status, _, err := sockRequest("POST", "/v1.23/containers/notexists/copy", postData) + status, _, err := request.SockRequest("POST", "/v1.23/containers/notexists/copy", postData, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound) } @@ -1070,13 +1070,13 @@ func (s *DockerSuite) TestContainerAPIDelete(c *check.C) { dockerCmd(c, "stop", id) - status, _, err := sockRequest("DELETE", "/containers/"+id, nil) + status, _, err := request.SockRequest("DELETE", "/containers/"+id, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) } func (s *DockerSuite) TestContainerAPIDeleteNotExist(c *check.C) { - status, body, err := sockRequest("DELETE", "/containers/doesnotexist", nil) + status, body, err := request.SockRequest("DELETE", "/containers/doesnotexist", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound) c.Assert(getErrorMessage(c, body), checker.Matches, "No such container: doesnotexist") @@ -1088,7 +1088,7 @@ func (s *DockerSuite) TestContainerAPIDeleteForce(c *check.C) { id := strings.TrimSpace(out) c.Assert(waitRun(id), checker.IsNil) - status, _, err := sockRequest("DELETE", "/containers/"+id+"?force=1", nil) + status, _, err := request.SockRequest("DELETE", "/containers/"+id+"?force=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) } @@ -1109,7 +1109,7 @@ func (s *DockerSuite) TestContainerAPIDeleteRemoveLinks(c *check.C) { links := inspectFieldJSON(c, id2, "HostConfig.Links") c.Assert(links, checker.Equals, "[\"/tlink1:/tlink2/tlink1\"]", check.Commentf("expected to have links between containers")) - status, b, err := sockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil) + status, b, err := request.SockRequest("DELETE", "/containers/tlink2/tlink1?link=1", nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusNoContent, check.Commentf(string(b))) @@ -1123,7 +1123,7 @@ func (s *DockerSuite) TestContainerAPIDeleteConflict(c *check.C) { id := strings.TrimSpace(out) c.Assert(waitRun(id), checker.IsNil) - status, _, err := sockRequest("DELETE", "/containers/"+id, nil) + status, _, err := request.SockRequest("DELETE", "/containers/"+id, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusConflict) } @@ -1145,7 +1145,7 @@ func (s *DockerSuite) TestContainerAPIDeleteRemoveVolume(c *check.C) { _, err = os.Stat(source) c.Assert(err, checker.IsNil) - status, _, err := sockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil) + status, _, err := request.SockRequest("DELETE", "/containers/"+id+"?v=1&force=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) _, err = os.Stat(source) @@ -1154,30 +1154,21 @@ func (s *DockerSuite) TestContainerAPIDeleteRemoveVolume(c *check.C) { // Regression test for https://github.com/docker/docker/issues/6231 func (s *DockerSuite) TestContainerAPIChunkedEncoding(c *check.C) { - conn, err := sockConn(time.Duration(10*time.Second), "") - c.Assert(err, checker.IsNil) - client := httputil.NewClientConn(conn, nil) - defer client.Close() config := map[string]interface{}{ "Image": "busybox", "Cmd": append([]string{"/bin/sh", "-c"}, sleepCommandForDaemonPlatform()...), "OpenStdin": true, } - b, err := json.Marshal(config) - c.Assert(err, checker.IsNil) - req, err := http.NewRequest("POST", "/containers/create", bytes.NewBuffer(b)) - c.Assert(err, checker.IsNil) - req.Header.Set("Content-Type", "application/json") - // This is a cheat to make the http request do chunked encoding - // Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite - // https://golang.org/src/pkg/net/http/request.go?s=11980:12172 - req.ContentLength = -1 - - resp, err := client.Do(req) + resp, _, err := request.Post(daemonHost(), "/containers/create", request.JSONBody(config), func(req *http.Request) error { + // This is a cheat to make the http request do chunked encoding + // Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite + // https://golang.org/src/pkg/net/http/request.go?s=11980:12172 + req.ContentLength = -1 + return nil + }) c.Assert(err, checker.IsNil, check.Commentf("error creating container with chunked encoding")) - resp.Body.Close() c.Assert(resp.StatusCode, checker.Equals, http.StatusCreated) } @@ -1187,7 +1178,7 @@ func (s *DockerSuite) TestContainerAPIPostContainerStop(c *check.C) { containerID := strings.TrimSpace(out) c.Assert(waitRun(containerID), checker.IsNil) - statusCode, _, err := sockRequest("POST", "/containers/"+containerID+"/stop", nil) + statusCode, _, err := request.SockRequest("POST", "/containers/"+containerID+"/stop", nil, daemonHost()) c.Assert(err, checker.IsNil) // 204 No Content is expected, not 200 c.Assert(statusCode, checker.Equals, http.StatusNoContent) @@ -1201,7 +1192,7 @@ func (s *DockerSuite) TestPostContainerAPICreateWithStringOrSliceEntrypoint(c *c Entrypoint string Cmd []string }{"busybox", "echo", []string{"hello", "world"}} - _, _, err := sockRequest("POST", "/containers/create?name=echotest", config) + _, _, err := request.SockRequest("POST", "/containers/create?name=echotest", config, daemonHost()) c.Assert(err, checker.IsNil) out, _ := dockerCmd(c, "start", "-a", "echotest") c.Assert(strings.TrimSpace(out), checker.Equals, "hello world") @@ -1211,7 +1202,7 @@ func (s *DockerSuite) TestPostContainerAPICreateWithStringOrSliceEntrypoint(c *c Entrypoint []string Cmd []string }{"busybox", []string{"echo"}, []string{"hello", "world"}} - _, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2) + _, _, err = request.SockRequest("POST", "/containers/create?name=echotest2", config2, daemonHost()) c.Assert(err, checker.IsNil) out, _ = dockerCmd(c, "start", "-a", "echotest2") c.Assert(strings.TrimSpace(out), checker.Equals, "hello world") @@ -1224,7 +1215,7 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *check.C) { Entrypoint string Cmd string }{"busybox", "echo", "hello world"} - _, _, err := sockRequest("POST", "/containers/create?name=echotest", config) + _, _, err := request.SockRequest("POST", "/containers/create?name=echotest", config, daemonHost()) c.Assert(err, checker.IsNil) out, _ := dockerCmd(c, "start", "-a", "echotest") c.Assert(strings.TrimSpace(out), checker.Equals, "hello world") @@ -1233,7 +1224,7 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *check.C) { Image string Cmd []string }{"busybox", []string{"echo", "hello", "world"}} - _, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2) + _, _, err = request.SockRequest("POST", "/containers/create?name=echotest2", config2, daemonHost()) c.Assert(err, checker.IsNil) out, _ = dockerCmd(c, "start", "-a", "echotest2") c.Assert(strings.TrimSpace(out), checker.Equals, "hello world") @@ -1248,7 +1239,7 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *che CapAdd string CapDrop string }{"busybox", "NET_ADMIN", "SYS_ADMIN"} - status, _, err := sockRequest("POST", "/containers/create?name=capaddtest0", config) + status, _, err := request.SockRequest("POST", "/containers/create?name=capaddtest0", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) @@ -1257,7 +1248,7 @@ func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCapAddDrop(c *che CapAdd []string CapDrop []string }{"busybox", []string{"NET_ADMIN", "SYS_ADMIN"}, []string{"SETGID"}} - status, _, err = sockRequest("POST", "/containers/create?name=capaddtest1", config2) + status, _, err = request.SockRequest("POST", "/containers/create?name=capaddtest1", config2, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) } @@ -1268,7 +1259,7 @@ func (s *DockerSuite) TestContainerAPICreateNoHostConfig118(c *check.C) { config := struct { Image string }{"busybox"} - status, _, err := sockRequest("POST", "/v1.18/containers/create", config) + status, _, err := request.SockRequest("POST", "/v1.18/containers/create", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) } @@ -1300,7 +1291,7 @@ func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs( query.Set("path", "/vol2/symlinkToAbsDir") urlPath := fmt.Sprintf("/v1.20/containers/%s/archive?%s", cID, query.Encode()) - statusCode, body, err := sockRequest("PUT", urlPath, nil) + statusCode, body, err := request.SockRequest("PUT", urlPath, nil, daemonHost()) c.Assert(err, checker.IsNil) if !isCpCannotCopyReadOnly(fmt.Errorf(string(body))) { @@ -1309,7 +1300,7 @@ func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs( } func (s *DockerSuite) TestContainerAPIGetContainersJSONEmpty(c *check.C) { - status, body, err := sockRequest("GET", "/containers/json?all=1", nil) + status, body, err := request.SockRequest("GET", "/containers/json?all=1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(string(body), checker.Equals, "[]\n") @@ -1324,7 +1315,7 @@ func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *check.C) CpusetCpus string }{"busybox", "1-42,,"} name := "wrong-cpuset-cpus" - status, body, err := sockRequest("POST", "/containers/create?name="+name, c1) + status, body, err := request.SockRequest("POST", "/containers/create?name="+name, c1, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) expected := "Invalid value 1-42,, for cpuset cpus" @@ -1335,7 +1326,7 @@ func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *check.C) CpusetMems string }{"busybox", "42-3,1--"} name = "wrong-cpuset-mems" - status, body, err = sockRequest("POST", "/containers/create?name="+name, c2) + status, body, err = request.SockRequest("POST", "/containers/create?name="+name, c2, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) expected = "Invalid value 42-3,1-- for cpuset mems" @@ -1350,7 +1341,7 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeNegative(c *check.C) { "HostConfig": map[string]interface{}{"ShmSize": -1}, } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusInternalServerError) c.Assert(getErrorMessage(c, body), checker.Contains, "SHM size can not be less than 0") @@ -1365,14 +1356,14 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeHostConfigOmitted(c *check. "Cmd": "mount", } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), check.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusOK) @@ -1397,14 +1388,14 @@ func (s *DockerSuite) TestPostContainersCreateShmSizeOmitted(c *check.C) { "Cmd": "mount", } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), check.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusOK) @@ -1429,14 +1420,14 @@ func (s *DockerSuite) TestPostContainersCreateWithShmSize(c *check.C) { "HostConfig": map[string]interface{}{"ShmSize": 1073741824}, } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), check.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusOK) @@ -1459,14 +1450,14 @@ func (s *DockerSuite) TestPostContainersCreateMemorySwappinessHostConfigOmitted( "Image": "busybox", } - status, body, err := sockRequest("POST", "/containers/create", config) + status, body, err := request.SockRequest("POST", "/containers/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated) var container containertypes.ContainerCreateCreatedBody c.Assert(json.Unmarshal(body, &container), check.IsNil) - status, body, err = sockRequest("GET", "/containers/"+container.ID+"/json", nil) + status, body, err = request.SockRequest("GET", "/containers/"+container.ID+"/json", nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusOK) @@ -1486,7 +1477,7 @@ func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *che OomScoreAdj int }{"busybox", 1001} name := "oomscoreadj-over" - status, b, err := sockRequest("POST", "/containers/create?name="+name, config) + status, b, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusInternalServerError) @@ -1501,7 +1492,7 @@ func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *che OomScoreAdj int }{"busybox", -1001} name = "oomscoreadj-low" - status, b, err = sockRequest("POST", "/containers/create?name="+name, config) + status, b, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusInternalServerError) expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]" @@ -1513,7 +1504,7 @@ func (s *DockerSuite) TestPostContainersCreateWithOomScoreAdjInvalidRange(c *che // test case for #22210 where an empty container name caused panic. func (s *DockerSuite) TestContainerAPIDeleteWithEmptyName(c *check.C) { - status, out, err := sockRequest("DELETE", "/containers/", nil) + status, out, err := request.SockRequest("DELETE", "/containers/", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusBadRequest) c.Assert(string(out), checker.Contains, "No container name or ID supplied") @@ -1530,11 +1521,11 @@ func (s *DockerSuite) TestContainerAPIStatsWithNetworkDisabled(c *check.C) { "NetworkDisabled": true, } - status, _, err := sockRequest("POST", "/containers/create?name="+name, config) + status, _, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) - status, _, err = sockRequest("POST", "/containers/"+name+"/start", nil) + status, _, err = request.SockRequest("POST", "/containers/"+name+"/start", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -1547,7 +1538,7 @@ func (s *DockerSuite) TestContainerAPIStatsWithNetworkDisabled(c *check.C) { } bc := make(chan b, 1) go func() { - status, body, err := sockRequest("GET", "/containers/"+name+"/stats", nil) + status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats", nil, daemonHost()) bc <- b{status, body, err} }() @@ -1755,7 +1746,7 @@ func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *check.C) { for i, x := range cases { c.Logf("case %d", i) - status, b, err := sockRequest("POST", "/containers/create", x.config) + status, b, err := request.SockRequest("POST", "/containers/create", x.config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, x.status, check.Commentf("%s\n%v", string(b), cases[i].config)) if len(x.msg) > 0 { @@ -1780,7 +1771,7 @@ func (s *DockerSuite) TestContainerAPICreateMountsBindRead(c *check.C) { "Cmd": []string{"/bin/sh", "-c", "cat /foo/bar"}, "HostConfig": map[string]interface{}{"Mounts": []map[string]interface{}{{"Type": "bind", "Source": tmpDir, "Target": destPath}}}, } - status, resp, err := sockRequest("POST", "/containers/create?name=test", data) + status, resp, err := request.SockRequest("POST", "/containers/create?name=test", data, daemonHost()) c.Assert(err, checker.IsNil, check.Commentf(string(resp))) c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(resp))) @@ -1868,7 +1859,7 @@ func (s *DockerSuite) TestContainersAPICreateMountsCreate(c *check.C) { } for i, x := range cases { c.Logf("case %d - config: %v", i, x.cfg) - status, data, err := sockRequest("POST", "/containers/create", wrapper{containertypes.Config{Image: testImg}, containertypes.HostConfig{Mounts: []mounttypes.Mount{x.cfg}}}) + status, data, err := request.SockRequest("POST", "/containers/create", wrapper{containertypes.Config{Image: testImg}, containertypes.HostConfig{Mounts: []mounttypes.Mount{x.cfg}}}, daemonHost()) c.Assert(err, checker.IsNil, check.Commentf(string(data))) c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(data))) @@ -1950,7 +1941,7 @@ func (s *DockerSuite) TestContainersAPICreateMountsTmpfs(c *check.C) { fmt.Sprintf("mount | grep 'tmpfs on %s'", target)}, "HostConfig": map[string]interface{}{"Mounts": []map[string]interface{}{x.cfg}}, } - status, resp, err := sockRequest("POST", "/containers/create?name="+cName, data) + status, resp, err := request.SockRequest("POST", "/containers/create?name="+cName, data, daemonHost()) c.Assert(err, checker.IsNil, check.Commentf(string(resp))) c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(resp))) out, _ := dockerCmd(c, "start", "-a", cName) diff --git a/integration-cli/docker_api_create_test.go b/integration-cli/docker_api_create_test.go index 7718b487a5..c2616c2e73 100644 --- a/integration-cli/docker_api_create_test.go +++ b/integration-cli/docker_api_create_test.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -14,7 +15,7 @@ func (s *DockerSuite) TestAPICreateWithNotExistImage(c *check.C) { "Volumes": map[string]struct{}{"/tmp": {}}, } - status, body, err := sockRequest("POST", "/containers/create?name="+name, config) + status, body, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusNotFound) expected := "No such image: test456:v1" @@ -25,7 +26,7 @@ func (s *DockerSuite) TestAPICreateWithNotExistImage(c *check.C) { "Volumes": map[string]struct{}{"/tmp": {}}, } - status, body, err = sockRequest("POST", "/containers/create?name="+name, config2) + status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config2, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusNotFound) expected = "No such image: test456:latest" @@ -35,7 +36,7 @@ func (s *DockerSuite) TestAPICreateWithNotExistImage(c *check.C) { "Image": "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa", } - status, body, err = sockRequest("POST", "/containers/create?name="+name, config3) + status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config3, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusNotFound) expected = "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa" @@ -52,7 +53,7 @@ func (s *DockerSuite) TestAPICreateEmptyEnv(c *check.C) { "Cmd": []string{"true"}, } - status, body, err := sockRequest("POST", "/containers/create?name="+name, config) + status, body, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusInternalServerError) expected := "invalid environment variable:" @@ -64,7 +65,7 @@ func (s *DockerSuite) TestAPICreateEmptyEnv(c *check.C) { "Env": []string{"=", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, "Cmd": []string{"true"}, } - status, body, err = sockRequest("POST", "/containers/create?name="+name, config) + status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusInternalServerError) expected = "invalid environment variable: =" @@ -76,7 +77,7 @@ func (s *DockerSuite) TestAPICreateEmptyEnv(c *check.C) { "Env": []string{"=foo", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}, "Cmd": []string{"true"}, } - status, body, err = sockRequest("POST", "/containers/create?name="+name, config) + status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusInternalServerError) expected = "invalid environment variable: =foo" diff --git a/integration-cli/docker_api_events_test.go b/integration-cli/docker_api_events_test.go index a56d98c85e..45de688c63 100644 --- a/integration-cli/docker_api_events_test.go +++ b/integration-cli/docker_api_events_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/jsonmessage" "github.com/go-check/check" ) @@ -21,7 +22,7 @@ func (s *DockerSuite) TestEventsAPIEmptyOutput(c *check.C) { } chResp := make(chan *apiResp) go func() { - resp, body, err := sockRequestRaw("GET", "/events", nil, "") + resp, body, err := request.SockRequestRaw("GET", "/events", nil, "", daemonHost()) body.Close() chResp <- &apiResp{resp, err} }() @@ -46,7 +47,7 @@ func (s *DockerSuite) TestEventsAPIBackwardsCompatible(c *check.C) { q := url.Values{} q.Set("since", ts) - _, body, err := sockRequestRaw("GET", "/events?"+q.Encode(), nil, "") + _, body, err := request.SockRequestRaw("GET", "/events?"+q.Encode(), nil, "", daemonHost()) c.Assert(err, checker.IsNil) defer body.Close() diff --git a/integration-cli/docker_api_exec_resize_test.go b/integration-cli/docker_api_exec_resize_test.go index 28ccd5e673..94ced6e0d4 100644 --- a/integration-cli/docker_api_exec_resize_test.go +++ b/integration-cli/docker_api_exec_resize_test.go @@ -10,6 +10,7 @@ import ( "sync" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -19,7 +20,7 @@ func (s *DockerSuite) TestExecResizeAPIHeightWidthNoInt(c *check.C) { cleanedContainerID := strings.TrimSpace(out) endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar" - status, _, err := sockRequest("POST", endpoint, nil) + status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) } @@ -35,7 +36,7 @@ func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) { "Cmd": []string{"/bin/sh"}, } uri := fmt.Sprintf("/containers/%s/exec", name) - status, body, err := sockRequest("POST", uri, data) + status, body, err := request.SockRequest("POST", uri, data, daemonHost()) if err != nil { return err } @@ -55,13 +56,13 @@ func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) { } payload := bytes.NewBufferString(`{"Tty":true}`) - conn, _, err := sockRequestHijack("POST", fmt.Sprintf("/exec/%s/start", execID), payload, "application/json") + conn, _, err := request.SockRequestHijack("POST", fmt.Sprintf("/exec/%s/start", execID), payload, "application/json", daemonHost()) if err != nil { return fmt.Errorf("Failed to start the exec: %q", err.Error()) } defer conn.Close() - _, rc, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), nil, "text/plain") + _, rc, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), nil, "text/plain", daemonHost()) // It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned. if err == io.ErrUnexpectedEOF { return fmt.Errorf("The daemon might have crashed.") diff --git a/integration-cli/docker_api_exec_test.go b/integration-cli/docker_api_exec_test.go index 5ca4da782b..422438c447 100644 --- a/integration-cli/docker_api_exec_test.go +++ b/integration-cli/docker_api_exec_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/testutil" "github.com/go-check/check" ) @@ -20,7 +21,7 @@ func (s *DockerSuite) TestExecAPICreateNoCmd(c *check.C) { name := "exec_test" dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") - status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil}) + status, body, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil}, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) @@ -37,7 +38,7 @@ func (s *DockerSuite) TestExecAPICreateNoValidContentType(c *check.C) { c.Fatalf("Can not encode data to json %s", err) } - res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain") + res, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) @@ -55,7 +56,7 @@ func (s *DockerSuite) TestExecAPICreateContainerPaused(c *check.C) { dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") dockerCmd(c, "pause", name) - status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}) + status, body, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusConflict) @@ -95,7 +96,7 @@ func (s *DockerSuite) TestExecAPIStartEnsureHeaders(c *check.C) { dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top") id := createExec(c, "test") - resp, _, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json") + resp, _, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(resp.Header.Get("Server"), checker.Not(checker.Equals), "") } @@ -105,7 +106,7 @@ func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) { runSleepingContainer(c, "-d", "--name", "test") id := createExec(c, "test") - resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain") + resp, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain", daemonHost()) c.Assert(err, checker.IsNil) b, err := testutil.ReadBody(body) @@ -146,7 +147,7 @@ func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) { "cmd": []string{"true"}, "AttachStdin": true, } - _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data) + _, b, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data, daemonHost()) c.Assert(err, checker.IsNil, check.Commentf(string(b))) createResp := struct { @@ -154,14 +155,14 @@ func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) { }{} c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b))) - _, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json") + _, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json", daemonHost()) c.Assert(err, checker.IsNil) b, err = testutil.ReadBody(body) comment := check.Commentf("response body: %s", b) c.Assert(err, checker.IsNil, comment) - resp, _, err := sockRequestRaw("GET", "/_ping", nil, "") + resp, _, err := request.SockRequestRaw("GET", "/_ping", nil, "", daemonHost()) c.Assert(err, checker.IsNil) if resp.StatusCode != http.StatusOK { c.Fatal("daemon is down, it should alive") @@ -169,7 +170,7 @@ func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) { } func createExec(c *check.C, name string) string { - _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}) + _, b, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}, daemonHost()) c.Assert(err, checker.IsNil, check.Commentf(string(b))) createResp := struct { @@ -180,7 +181,7 @@ func createExec(c *check.C, name string) string { } func startExec(c *check.C, id string, code int) { - resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json") + resp, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json", daemonHost()) c.Assert(err, checker.IsNil) b, err := testutil.ReadBody(body) @@ -190,7 +191,7 @@ func startExec(c *check.C, id string, code int) { } func inspectExec(c *check.C, id string, out interface{}) { - resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "") + resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "", daemonHost()) c.Assert(err, checker.IsNil) defer body.Close() c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) diff --git a/integration-cli/docker_api_images_test.go b/integration-cli/docker_api_images_test.go index 6b24a39122..ff53f8e3ee 100644 --- a/integration-cli/docker_api_images_test.go +++ b/integration-cli/docker_api_images_test.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -22,7 +23,7 @@ func (s *DockerSuite) TestAPIImagesFilter(c *check.C) { getImages := func(filter string) []image { v := url.Values{} v.Set("filter", filter) - status, b, err := sockRequest("GET", "/images/json?"+v.Encode(), nil) + status, b, err := request.SockRequest("GET", "/images/json?"+v.Encode(), nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -55,14 +56,14 @@ func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *check.C) { c.Assert(err, checker.IsNil) id := strings.TrimSpace(out) - res, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "") + res, body, err := request.SockRequestRaw("GET", "/images/"+id+"/get", nil, "", daemonHost()) c.Assert(err, checker.IsNil) defer body.Close() c.Assert(res.StatusCode, checker.Equals, http.StatusOK) dockerCmd(c, "rmi", id) - res, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar") + res, loadBody, err := request.SockRequestRaw("POST", "/images/load", body, "application/x-tar", daemonHost()) c.Assert(err, checker.IsNil) defer loadBody.Close() c.Assert(res.StatusCode, checker.Equals, http.StatusOK) @@ -82,15 +83,15 @@ func (s *DockerSuite) TestAPIImagesDelete(c *check.C) { dockerCmd(c, "tag", name, "test:tag1") - status, _, err := sockRequest("DELETE", "/images/"+id, nil) + status, _, err := request.SockRequest("DELETE", "/images/"+id, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusConflict) - status, _, err = sockRequest("DELETE", "/images/test:noexist", nil) + status, _, err = request.SockRequest("DELETE", "/images/test:noexist", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound) //Status Codes:404 – no such image - status, _, err = sockRequest("DELETE", "/images/test:tag1", nil) + status, _, err = request.SockRequest("DELETE", "/images/test:tag1", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) } @@ -105,7 +106,7 @@ func (s *DockerSuite) TestAPIImagesHistory(c *check.C) { id := strings.TrimSpace(out) - status, body, err := sockRequest("GET", "/images/"+id+"/history", nil) + status, body, err := request.SockRequest("GET", "/images/"+id+"/history", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -121,7 +122,7 @@ func (s *DockerSuite) TestAPIImagesHistory(c *check.C) { func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *check.C) { testRequires(c, Network) - res, b, err := sockRequestRaw("GET", "/images/search?term=test", nil, "application/json") + res, b, err := request.SockRequestRaw("GET", "/images/search?term=test", nil, "application/json", daemonHost()) c.Assert(err, check.IsNil) b.Close() c.Assert(res.StatusCode, checker.Equals, http.StatusOK) diff --git a/integration-cli/docker_api_info_test.go b/integration-cli/docker_api_info_test.go index 421b5fe3f3..57dac36e18 100644 --- a/integration-cli/docker_api_info_test.go +++ b/integration-cli/docker_api_info_test.go @@ -4,13 +4,14 @@ import ( "net/http" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) func (s *DockerSuite) TestInfoAPI(c *check.C) { endpoint := "/info" - status, body, err := sockRequest("GET", endpoint, nil) + status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost()) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) @@ -43,7 +44,7 @@ func (s *DockerSuite) TestInfoAPIVersioned(c *check.C) { testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later endpoint := "/v1.20/info" - status, body, err := sockRequest("GET", endpoint, nil) + status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost()) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) diff --git a/integration-cli/docker_api_inspect_test.go b/integration-cli/docker_api_inspect_test.go index 8785dd73f2..30ba1aa09f 100644 --- a/integration-cli/docker_api_inspect_test.go +++ b/integration-cli/docker_api_inspect_test.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/versions/v1p20" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/stringutils" "github.com/go-check/check" ) @@ -107,7 +108,7 @@ func (s *DockerSuite) TestInspectAPIImageResponse(c *check.C) { dockerCmd(c, "tag", "busybox:latest", "busybox:mytag") endpoint := "/images/busybox/json" - status, body, err := sockRequest("GET", endpoint, nil) + status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) diff --git a/integration-cli/docker_api_inspect_unix_test.go b/integration-cli/docker_api_inspect_unix_test.go index cfa6781a9d..f7731f3d97 100644 --- a/integration-cli/docker_api_inspect_unix_test.go +++ b/integration-cli/docker_api_inspect_unix_test.go @@ -8,6 +8,7 @@ import ( "net/http" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -19,7 +20,7 @@ func (s *DockerSuite) TestInspectAPICpusetInConfigPre120(c *check.C) { name := "cpusetinconfig-pre120" dockerCmd(c, "run", "--name", name, "--cpuset-cpus", "0", "busybox", "true") - status, body, err := sockRequest("GET", fmt.Sprintf("/v1.19/containers/%s/json", name), nil) + status, body, err := request.SockRequest("GET", fmt.Sprintf("/v1.19/containers/%s/json", name), nil, daemonHost()) c.Assert(status, check.Equals, http.StatusOK) c.Assert(err, check.IsNil) diff --git a/integration-cli/docker_api_logs_test.go b/integration-cli/docker_api_logs_test.go index 5bc58f3e7c..94954adf0e 100644 --- a/integration-cli/docker_api_logs_test.go +++ b/integration-cli/docker_api_logs_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -25,7 +26,7 @@ func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) { chLog := make(chan logOut) go func() { - res, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id), nil, "") + res, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id), nil, "", daemonHost()) if err != nil { chLog <- logOut{"", nil, err} return @@ -55,7 +56,7 @@ func (s *DockerSuite) TestLogsAPINoStdoutNorStderr(c *check.C) { name := "logs_test" dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") - status, body, err := sockRequest("GET", fmt.Sprintf("/containers/%s/logs", name), nil) + status, body, err := request.SockRequest("GET", fmt.Sprintf("/containers/%s/logs", name), nil, daemonHost()) c.Assert(status, checker.Equals, http.StatusBadRequest) c.Assert(err, checker.IsNil) @@ -69,7 +70,7 @@ func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) { t0 := time.Now() dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "sleep", "10") - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "") + _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "", daemonHost()) t1 := time.Now() c.Assert(err, checker.IsNil) body.Close() @@ -81,7 +82,7 @@ func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) { func (s *DockerSuite) TestLogsAPIContainerNotFound(c *check.C) { name := "nonExistentContainer" - resp, _, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "") + resp, _, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound) } diff --git a/integration-cli/docker_api_network_test.go b/integration-cli/docker_api_network_test.go index fb30d12807..b8cbb8dedb 100644 --- a/integration-cli/docker_api_network_test.go +++ b/integration-cli/docker_api_network_test.go @@ -12,6 +12,7 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/network" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -256,12 +257,12 @@ func createDeletePredefinedNetwork(c *check.C, name string) { } func isNetworkAvailable(c *check.C, name string) bool { - status, body, err := sockRequest("GET", "/networks", nil) - c.Assert(status, checker.Equals, http.StatusOK) + resp, body, err := request.Get(daemonHost(), "/networks") + c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) nJSON := []types.NetworkResource{} - err = json.Unmarshal(body, &nJSON) + err = json.NewDecoder(body).Decode(&nJSON) c.Assert(err, checker.IsNil) for _, n := range nJSON { @@ -282,12 +283,12 @@ func getNetworkIDByName(c *check.C, name string) string { c.Assert(err, checker.IsNil) v.Set("filters", filterJSON) - status, body, err := sockRequest("GET", "/networks?"+v.Encode(), nil) - c.Assert(status, checker.Equals, http.StatusOK) + resp, body, err := request.Get(daemonHost(), "/networks?"+v.Encode()) + c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) nJSON := []types.NetworkResource{} - err = json.Unmarshal(body, &nJSON) + err = json.NewDecoder(body).Decode(&nJSON) c.Assert(err, checker.IsNil) c.Assert(len(nJSON), checker.Equals, 1) @@ -295,28 +296,28 @@ func getNetworkIDByName(c *check.C, name string) string { } func getNetworkResource(c *check.C, id string) *types.NetworkResource { - _, obj, err := sockRequest("GET", "/networks/"+id, nil) + _, obj, err := request.Get(daemonHost(), "/networks/"+id) c.Assert(err, checker.IsNil) nr := types.NetworkResource{} - err = json.Unmarshal(obj, &nr) + err = json.NewDecoder(obj).Decode(&nr) c.Assert(err, checker.IsNil) return &nr } func createNetwork(c *check.C, config types.NetworkCreateRequest, shouldSucceed bool) string { - status, resp, err := sockRequest("POST", "/networks/create", config) + resp, body, err := request.Post(daemonHost(), "/networks/create", request.JSONBody(config)) if !shouldSucceed { - c.Assert(status, checker.Not(checker.Equals), http.StatusCreated) + c.Assert(resp.StatusCode, checker.Not(checker.Equals), http.StatusCreated) return "" } c.Assert(err, checker.IsNil) - c.Assert(status, checker.Equals, http.StatusCreated) + c.Assert(resp.StatusCode, checker.Equals, http.StatusCreated) var nr types.NetworkCreateResponse - err = json.Unmarshal(resp, &nr) + err = json.NewDecoder(body).Decode(&nr) c.Assert(err, checker.IsNil) return nr.ID @@ -327,8 +328,8 @@ func connectNetwork(c *check.C, nid, cid string) { Container: cid, } - status, _, err := sockRequest("POST", "/networks/"+nid+"/connect", config) - c.Assert(status, checker.Equals, http.StatusOK) + resp, _, err := request.Post(daemonHost(), "/networks/"+nid+"/connect", request.JSONBody(config)) + c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) } @@ -337,17 +338,17 @@ func disconnectNetwork(c *check.C, nid, cid string) { Container: cid, } - status, _, err := sockRequest("POST", "/networks/"+nid+"/disconnect", config) - c.Assert(status, checker.Equals, http.StatusOK) + resp, _, err := request.Post(daemonHost(), "/networks/"+nid+"/disconnect", request.JSONBody(config)) + c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) } func deleteNetwork(c *check.C, id string, shouldSucceed bool) { - status, _, err := sockRequest("DELETE", "/networks/"+id, nil) + resp, _, err := request.Delete(daemonHost(), "/networks/"+id) if !shouldSucceed { - c.Assert(status, checker.Not(checker.Equals), http.StatusOK) + c.Assert(resp.StatusCode, checker.Not(checker.Equals), http.StatusOK) return } - c.Assert(status, checker.Equals, http.StatusNoContent) + c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent) c.Assert(err, checker.IsNil) } diff --git a/integration-cli/docker_api_resize_test.go b/integration-cli/docker_api_resize_test.go index fee2d16442..682f315a53 100644 --- a/integration-cli/docker_api_resize_test.go +++ b/integration-cli/docker_api_resize_test.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -13,7 +14,7 @@ func (s *DockerSuite) TestResizeAPIResponse(c *check.C) { cleanedContainerID := strings.TrimSpace(out) endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40" - status, _, err := sockRequest("POST", endpoint, nil) + status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost()) c.Assert(status, check.Equals, http.StatusOK) c.Assert(err, check.IsNil) } @@ -23,7 +24,7 @@ func (s *DockerSuite) TestResizeAPIHeightWidthNoInt(c *check.C) { cleanedContainerID := strings.TrimSpace(out) endpoint := "/containers/" + cleanedContainerID + "/resize?h=foo&w=bar" - status, _, err := sockRequest("POST", endpoint, nil) + status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost()) c.Assert(status, check.Equals, http.StatusInternalServerError) c.Assert(err, check.IsNil) } @@ -36,7 +37,7 @@ func (s *DockerSuite) TestResizeAPIResponseWhenContainerNotStarted(c *check.C) { dockerCmd(c, "wait", cleanedContainerID) endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40" - status, body, err := sockRequest("POST", endpoint, nil) + status, body, err := request.SockRequest("POST", endpoint, nil, daemonHost()) c.Assert(status, check.Equals, http.StatusInternalServerError) c.Assert(err, check.IsNil) diff --git a/integration-cli/docker_api_stats_test.go b/integration-cli/docker_api_stats_test.go index 5c81670f6d..3851be260e 100644 --- a/integration-cli/docker_api_stats_test.go +++ b/integration-cli/docker_api_stats_test.go @@ -14,6 +14,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/versions" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -25,7 +26,7 @@ func (s *DockerSuite) TestAPIStatsNoStreamGetCpu(c *check.C) { id := strings.TrimSpace(out) c.Assert(waitRun(id), checker.IsNil) - resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "") + resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json") @@ -64,7 +65,7 @@ func (s *DockerSuite) TestAPIStatsStoppedContainerInGoroutines(c *check.C) { id := strings.TrimSpace(out) getGoRoutines := func() int { - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/info"), nil, "") + _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/info"), nil, "", daemonHost()) c.Assert(err, checker.IsNil) info := types.Info{} err = json.NewDecoder(body).Decode(&info) @@ -75,7 +76,7 @@ func (s *DockerSuite) TestAPIStatsStoppedContainerInGoroutines(c *check.C) { // When the HTTP connection is closed, the number of goroutines should not increase. routines := getGoRoutines() - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "") + _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "", daemonHost()) c.Assert(err, checker.IsNil) body.Close() @@ -191,7 +192,7 @@ func (s *DockerSuite) TestAPIStatsNetworkStatsVersioning(c *check.C) { func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats { var st *types.StatsJSON - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "") + _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "", daemonHost()) c.Assert(err, checker.IsNil) err = json.NewDecoder(body).Decode(&st) @@ -208,7 +209,7 @@ func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats { func getVersionedStats(c *check.C, id string, apiVersion string) map[string]interface{} { stats := make(map[string]interface{}) - _, body, err := sockRequestRaw("GET", fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id), nil, "") + _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id), nil, "", daemonHost()) c.Assert(err, checker.IsNil) defer body.Close() @@ -261,11 +262,11 @@ func jsonBlobHasGTE121NetworkStats(blob map[string]interface{}) bool { func (s *DockerSuite) TestAPIStatsContainerNotFound(c *check.C) { testRequires(c, DaemonIsLinux) - status, _, err := sockRequest("GET", "/containers/nonexistent/stats", nil) + status, _, err := request.SockRequest("GET", "/containers/nonexistent/stats", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound) - status, _, err = sockRequest("GET", "/containers/nonexistent/stats?stream=0", nil) + status, _, err = request.SockRequest("GET", "/containers/nonexistent/stats?stream=0", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound) } @@ -283,7 +284,7 @@ func (s *DockerSuite) TestAPIStatsNoStreamConnectedContainers(c *check.C) { ch := make(chan error) go func() { - resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id2), nil, "") + resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id2), nil, "", daemonHost()) defer body.Close() if err != nil { ch <- err diff --git a/integration-cli/docker_api_stats_unix_test.go b/integration-cli/docker_api_stats_unix_test.go index 95009a6d2d..a367a6e13e 100644 --- a/integration-cli/docker_api_stats_unix_test.go +++ b/integration-cli/docker_api_stats_unix_test.go @@ -9,13 +9,14 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) func (s *DockerSuite) TestAPIStatsContainerGetMemoryLimit(c *check.C) { testRequires(c, DaemonIsLinux, memoryLimitSupport) - resp, body, err := sockRequestRaw("GET", "/info", nil, "application/json") + resp, body, err := request.SockRequestRaw("GET", "/info", nil, "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) var info types.Info @@ -28,7 +29,7 @@ func (s *DockerSuite) TestAPIStatsContainerGetMemoryLimit(c *check.C) { dockerCmd(c, "run", "-d", "--name", conName, "busybox", "top") c.Assert(waitRun(conName), checker.IsNil) - resp, body, err = sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", conName), nil, "") + resp, body, err = request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", conName), nil, "", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json") diff --git a/integration-cli/docker_api_test.go b/integration-cli/docker_api_test.go index c0f15c09fd..a512c02c19 100644 --- a/integration-cli/docker_api_test.go +++ b/integration-cli/docker_api_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io/ioutil" "net/http" "net/http/httptest" "runtime" @@ -10,19 +11,20 @@ import ( "github.com/docker/docker/api" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/testutil" icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" ) func (s *DockerSuite) TestAPIOptionsRoute(c *check.C) { - status, _, err := sockRequest("OPTIONS", "/", nil) + resp, _, err := request.Do(daemonHost(), "/", request.Method(http.MethodOptions)) c.Assert(err, checker.IsNil) - c.Assert(status, checker.Equals, http.StatusOK) + c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) } func (s *DockerSuite) TestAPIGetEnabledCORS(c *check.C) { - res, body, err := sockRequestRaw("GET", "/version", nil, "") + res, body, err := request.SockRequestRaw("GET", "/version", nil, "", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusOK) body.Close() @@ -47,11 +49,14 @@ func (s *DockerSuite) TestAPIClientVersionOldNotSupported(c *check.C) { v[1] = strconv.Itoa(vMinInt) version := strings.Join(v, ".") - status, body, err := sockRequest("GET", "/v"+version+"/version", nil) + resp, body, err := request.Get(daemonHost(), "/v"+version+"/version") c.Assert(err, checker.IsNil) - c.Assert(status, checker.Equals, http.StatusBadRequest) + defer body.Close() + c.Assert(resp.StatusCode, checker.Equals, http.StatusBadRequest) expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, api.MinVersion) - c.Assert(strings.TrimSpace(string(body)), checker.Contains, expected) + content, err := ioutil.ReadAll(body) + c.Assert(err, checker.IsNil) + c.Assert(strings.TrimSpace(string(content)), checker.Contains, expected) } func (s *DockerSuite) TestAPIDockerAPIVersion(c *check.C) { @@ -75,7 +80,7 @@ func (s *DockerSuite) TestAPIDockerAPIVersion(c *check.C) { } func (s *DockerSuite) TestAPIErrorJSON(c *check.C) { - httpResp, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(`{}`), "application/json") + httpResp, body, err := request.Post(daemonHost(), "/containers/create", request.JSONBody(struct{}{})) c.Assert(err, checker.IsNil) c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError) c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json") @@ -88,7 +93,7 @@ func (s *DockerSuite) TestAPIErrorPlainText(c *check.C) { // Windows requires API 1.25 or later. This test is validating a behaviour which was present // in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors testRequires(c, DaemonIsLinux) - httpResp, body, err := sockRequestRaw("POST", "/v1.23/containers/create", strings.NewReader(`{}`), "application/json") + httpResp, body, err := request.Post(daemonHost(), "/v1.23/containers/create", request.JSONBody(struct{}{})) c.Assert(err, checker.IsNil) c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError) c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain") @@ -99,7 +104,7 @@ func (s *DockerSuite) TestAPIErrorPlainText(c *check.C) { func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *check.C) { // 404 is a different code path to normal errors, so test separately - httpResp, body, err := sockRequestRaw("GET", "/notfound", nil, "application/json") + httpResp, body, err := request.Get(daemonHost(), "/notfound", request.JSON) c.Assert(err, checker.IsNil) c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound) c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json") @@ -109,7 +114,7 @@ func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *check.C) { } func (s *DockerSuite) TestAPIErrorNotFoundPlainText(c *check.C) { - httpResp, body, err := sockRequestRaw("GET", "/v1.23/notfound", nil, "application/json") + httpResp, body, err := request.Get(daemonHost(), "/v1.23/notfound", request.JSON) c.Assert(err, checker.IsNil) c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound) c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain") diff --git a/integration-cli/docker_api_update_unix_test.go b/integration-cli/docker_api_update_unix_test.go index 6b17ea9d4d..1af6ed1e7c 100644 --- a/integration-cli/docker_api_update_unix_test.go +++ b/integration-cli/docker_api_update_unix_test.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -20,7 +21,7 @@ func (s *DockerSuite) TestAPIUpdateContainer(c *check.C) { "MemorySwap": 524288000, } dockerCmd(c, "run", "-d", "--name", name, "-m", "200M", "busybox", "top") - _, _, err := sockRequest("POST", "/containers/"+name+"/update", hostConfig) + _, _, err := request.SockRequest("POST", "/containers/"+name+"/update", hostConfig, daemonHost()) c.Assert(err, check.IsNil) c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "314572800") diff --git a/integration-cli/docker_api_version_test.go b/integration-cli/docker_api_version_test.go index 1374232518..5f919deb75 100644 --- a/integration-cli/docker_api_version_test.go +++ b/integration-cli/docker_api_version_test.go @@ -7,11 +7,12 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/dockerversion" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) func (s *DockerSuite) TestGetVersion(c *check.C) { - status, body, err := sockRequest("GET", "/version", nil) + status, body, err := request.SockRequest("GET", "/version", nil, daemonHost()) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) diff --git a/integration-cli/docker_api_volumes_test.go b/integration-cli/docker_api_volumes_test.go index b3994ff3f9..3cc03dfb36 100644 --- a/integration-cli/docker_api_volumes_test.go +++ b/integration-cli/docker_api_volumes_test.go @@ -8,6 +8,7 @@ import ( "github.com/docker/docker/api/types" volumetypes "github.com/docker/docker/api/types/volume" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -15,7 +16,7 @@ func (s *DockerSuite) TestVolumesAPIList(c *check.C) { prefix, _ := getPrefixAndSlashFromDaemonPlatform() dockerCmd(c, "run", "-v", prefix+"/foo", "busybox") - status, b, err := sockRequest("GET", "/volumes", nil) + status, b, err := request.SockRequest("GET", "/volumes", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -29,7 +30,7 @@ func (s *DockerSuite) TestVolumesAPICreate(c *check.C) { config := volumetypes.VolumesCreateBody{ Name: "test", } - status, b, err := sockRequest("POST", "/volumes/create", config) + status, b, err := request.SockRequest("POST", "/volumes/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated, check.Commentf(string(b))) @@ -44,7 +45,7 @@ func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) { prefix, _ := getPrefixAndSlashFromDaemonPlatform() dockerCmd(c, "run", "-v", prefix+"/foo", "--name=test", "busybox") - status, b, err := sockRequest("GET", "/volumes", nil) + status, b, err := request.SockRequest("GET", "/volumes", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK) @@ -53,12 +54,12 @@ func (s *DockerSuite) TestVolumesAPIRemove(c *check.C) { c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes)) v := volumes.Volumes[0] - status, _, err = sockRequest("DELETE", "/volumes/"+v.Name, nil) + status, _, err = request.SockRequest("DELETE", "/volumes/"+v.Name, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusConflict, check.Commentf("Should not be able to remove a volume that is in use")) dockerCmd(c, "rm", "-f", "test") - status, data, err := sockRequest("DELETE", "/volumes/"+v.Name, nil) + status, data, err := request.SockRequest("DELETE", "/volumes/"+v.Name, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent, check.Commentf(string(data))) @@ -68,11 +69,11 @@ func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) { config := volumetypes.VolumesCreateBody{ Name: "test", } - status, b, err := sockRequest("POST", "/volumes/create", config) + status, b, err := request.SockRequest("POST", "/volumes/create", config, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusCreated, check.Commentf(string(b))) - status, b, err = sockRequest("GET", "/volumes", nil) + status, b, err = request.SockRequest("GET", "/volumes", nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(b))) @@ -81,7 +82,7 @@ func (s *DockerSuite) TestVolumesAPIInspect(c *check.C) { c.Assert(len(volumes.Volumes), checker.Equals, 1, check.Commentf("\n%v", volumes.Volumes)) var vol types.Volume - status, b, err = sockRequest("GET", "/volumes/"+config.Name, nil) + status, b, err = request.SockRequest("GET", "/volumes/"+config.Name, nil, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(b))) c.Assert(json.Unmarshal(b, &vol), checker.IsNil) diff --git a/integration-cli/docker_cli_events_test.go b/integration-cli/docker_cli_events_test.go index dc9828ef8d..013942ca24 100644 --- a/integration-cli/docker_cli_events_test.go +++ b/integration-cli/docker_cli_events_test.go @@ -15,6 +15,7 @@ import ( eventtypes "github.com/docker/docker/api/types/events" eventstestutils "github.com/docker/docker/daemon/events/testutils" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/testutil" icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" @@ -494,7 +495,7 @@ func (s *DockerSuite) TestEventsResize(c *check.C) { c.Assert(waitRun(cID), checker.IsNil) endpoint := "/containers/" + cID + "/resize?h=80&w=24" - status, _, err := sockRequest("POST", endpoint, nil) + status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost()) c.Assert(status, checker.Equals, http.StatusOK) c.Assert(err, checker.IsNil) diff --git a/integration-cli/docker_cli_exec_test.go b/integration-cli/docker_cli_exec_test.go index bd72eaf925..bffd60b696 100644 --- a/integration-cli/docker_cli_exec_test.go +++ b/integration-cli/docker_cli_exec_test.go @@ -16,6 +16,7 @@ import ( "time" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" ) @@ -355,14 +356,14 @@ func (s *DockerSuite) TestExecInspectID(c *check.C) { } // But we should still be able to query the execID - sc, body, err := sockRequest("GET", "/exec/"+execID+"/json", nil) + sc, body, err := request.SockRequest("GET", "/exec/"+execID+"/json", nil, daemonHost()) c.Assert(sc, checker.Equals, http.StatusOK, check.Commentf("received status != 200 OK: %d\n%s", sc, body)) // Now delete the container and then an 'inspect' on the exec should // result in a 404 (not 'container not running') out, ec := dockerCmd(c, "rm", "-f", id) c.Assert(ec, checker.Equals, 0, check.Commentf("error removing container: %s", out)) - sc, body, err = sockRequest("GET", "/exec/"+execID+"/json", nil) + sc, body, err = request.SockRequest("GET", "/exec/"+execID+"/json", nil, daemonHost()) c.Assert(sc, checker.Equals, http.StatusNotFound, check.Commentf("received status != 404: %d\n%s", sc, body)) } diff --git a/integration-cli/docker_cli_kill_test.go b/integration-cli/docker_cli_kill_test.go index f2c42f1c3f..db36963574 100644 --- a/integration-cli/docker_cli_kill_test.go +++ b/integration-cli/docker_cli_kill_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -128,7 +129,7 @@ func (s *DockerSuite) TestKillStoppedContainerAPIPre120(c *check.C) { runSleepingContainer(c, "--name", "docker-kill-test-api", "-d") dockerCmd(c, "stop", "docker-kill-test-api") - status, _, err := sockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil) + status, _, err := request.SockRequest("POST", fmt.Sprintf("/v1.19/containers/%s/kill", "docker-kill-test-api"), nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusNoContent) } diff --git a/integration-cli/docker_cli_update_unix_test.go b/integration-cli/docker_cli_update_unix_test.go index 99fdba2eeb..37885d0179 100644 --- a/integration-cli/docker_cli_update_unix_test.go +++ b/integration-cli/docker_cli_update_unix_test.go @@ -5,15 +5,16 @@ package main import ( "encoding/json" "fmt" - "github.com/kr/pty" "os/exec" "strings" "time" "github.com/docker/docker/api/types" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/parsers/kernel" "github.com/go-check/check" + "github.com/kr/pty" ) func (s *DockerSuite) TestUpdateRunningContainer(c *check.C) { @@ -219,7 +220,7 @@ func (s *DockerSuite) TestUpdateStats(c *check.C) { c.Assert(waitRun(name), checker.IsNil) getMemLimit := func(id string) uint64 { - resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "") + resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json") diff --git a/integration-cli/docker_deprecated_api_v124_test.go b/integration-cli/docker_deprecated_api_v124_test.go index 09e4b61de0..a29d3f4631 100644 --- a/integration-cli/docker_deprecated_api_v124_test.go +++ b/integration-cli/docker_deprecated_api_v124_test.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/pkg/testutil" "github.com/go-check/check" ) @@ -22,7 +23,7 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartHostConfig(c *check.C) { config := map[string]interface{}{ "Binds": []string{"/aa:/bb"}, } - status, body, err := sockRequest("POST", "/containers/"+name+"/start", config) + status, body, err := request.SockRequest("POST", "/containers/"+name+"/start", config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusBadRequest) c.Assert(string(body), checker.Contains, "was deprecated since v1.10") @@ -41,7 +42,7 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumeBinds(c *check.C) { "Volumes": map[string]struct{}{path: {}}, } - status, _, err := sockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config) + status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) @@ -49,7 +50,7 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumeBinds(c *check.C) { config = map[string]interface{}{ "Binds": []string{bindPath + ":" + path}, } - status, _, err = sockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config) + status, _, err = request.SockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -68,7 +69,7 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C) "Volumes": map[string]struct{}{"/tmp": {}}, } - status, _, err := sockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config) + status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) @@ -78,7 +79,7 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C) config = map[string]interface{}{ "Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"}, } - status, body, err := sockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config) + status, body, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusInternalServerError) c.Assert(string(body), checker.Contains, "Duplicate mount point", check.Commentf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(body), err)) @@ -98,14 +99,14 @@ func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumesFrom(c *check.C) { "Volumes": map[string]struct{}{volPath: {}}, } - status, _, err := sockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config) + status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/create?name="+name), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusCreated) config = map[string]interface{}{ "VolumesFrom": []string{volName}, } - status, _, err = sockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config) + status, _, err = request.SockRequest("POST", formatV123StartAPIURL("/containers/"+name+"/start"), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -128,7 +129,7 @@ func (s *DockerSuite) TestDeprecatedPostContainerBindNormalVolume(c *check.C) { dockerCmd(c, "create", "-v", "/foo", "--name=two", "busybox") bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}} - status, _, err := sockRequest("POST", formatV123StartAPIURL("/containers/two/start"), bindSpec) + status, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/two/start"), bindSpec, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNoContent) @@ -149,7 +150,7 @@ func (s *DockerSuite) TestDeprecatedStartWithTooLowMemoryLimit(c *check.C) { "Memory": 524287 }` - res, body, err := sockRequestRaw("POST", formatV123StartAPIURL("/containers/"+containerID+"/start"), strings.NewReader(config), "application/json") + res, body, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+containerID+"/start"), strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) b, err2 := testutil.ReadBody(body) c.Assert(err2, checker.IsNil) @@ -168,7 +169,7 @@ func (s *DockerSuite) TestDeprecatedPostContainersStartWithoutLinksInHostConfig( hc := inspectFieldJSON(c, name, "HostConfig") config := `{"HostConfig":` + hc + `}` - res, b, err := sockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json") + res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) b.Close() @@ -186,7 +187,7 @@ func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfig(c * hc := inspectFieldJSON(c, name, "HostConfig") config := `{"HostConfig":` + hc + `}` - res, b, err := sockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json") + res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) b.Close() @@ -204,7 +205,7 @@ func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfigIdLi hc := inspectFieldJSON(c, name, "HostConfig") config := `{"HostConfig":` + hc + `}` - res, b, err := sockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json") + res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) b.Close() @@ -218,7 +219,7 @@ func (s *DockerSuite) TestDeprecatedStartWithNilDNS(c *check.C) { config := `{"HostConfig": {"Dns": null}}` - res, b, err := sockRequestRaw("POST", formatV123StartAPIURL("/containers/"+containerID+"/start"), strings.NewReader(config), "application/json") + res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+containerID+"/start"), strings.NewReader(config), "application/json", daemonHost()) c.Assert(err, checker.IsNil) c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) b.Close() diff --git a/integration-cli/docker_deprecated_api_v124_unix_test.go b/integration-cli/docker_deprecated_api_v124_unix_test.go index d50a521a94..5fc6c2ddfc 100644 --- a/integration-cli/docker_deprecated_api_v124_unix_test.go +++ b/integration-cli/docker_deprecated_api_v124_unix_test.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/docker/docker/integration-cli/checker" + "github.com/docker/docker/integration-cli/request" "github.com/go-check/check" ) @@ -21,7 +22,7 @@ func (s *DockerNetworkSuite) TestDeprecatedDockerNetworkStartAPIWithHostconfig(c "NetworkMode": netName, }, } - _, _, err := sockRequest("POST", formatV123StartAPIURL("/containers/"+conName+"/start"), config) + _, _, err := request.SockRequest("POST", formatV123StartAPIURL("/containers/"+conName+"/start"), config, daemonHost()) c.Assert(err, checker.IsNil) c.Assert(waitRun(conName), checker.IsNil) networks := inspectField(c, conName, "NetworkSettings.Networks") diff --git a/integration-cli/docker_utils_test.go b/integration-cli/docker_utils_test.go index f4d08931cc..c52e3d349f 100644 --- a/integration-cli/docker_utils_test.go +++ b/integration-cli/docker_utils_test.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "bytes" "encoding/json" "errors" @@ -11,7 +10,6 @@ import ( "net" "net/http" "net/http/httptest" - "net/http/httputil" "net/url" "os" "os/exec" @@ -26,10 +24,9 @@ import ( "github.com/docker/docker/integration-cli/checker" "github.com/docker/docker/integration-cli/daemon" "github.com/docker/docker/integration-cli/registry" + "github.com/docker/docker/integration-cli/request" "github.com/docker/docker/opts" - "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/stringutils" - "github.com/docker/docker/pkg/testutil" icmd "github.com/docker/docker/pkg/testutil/cmd" "github.com/go-check/check" ) @@ -42,82 +39,6 @@ func daemonHost() string { return daemonURLStr } -// FIXME(vdemeester) should probably completely move to daemon struct/methods -func sockConn(timeout time.Duration, daemonStr string) (net.Conn, error) { - if daemonStr == "" { - daemonStr = daemonHost() - } - return daemon.SockConn(timeout, daemonStr) -} - -func sockRequest(method, endpoint string, data interface{}) (int, []byte, error) { - jsonData := bytes.NewBuffer(nil) - if err := json.NewEncoder(jsonData).Encode(data); err != nil { - return -1, nil, err - } - - res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json") - if err != nil { - return -1, nil, err - } - b, err := testutil.ReadBody(body) - return res.StatusCode, b, err -} - -func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) { - return sockRequestRawToDaemon(method, endpoint, data, ct, "") -} - -func sockRequestRawToDaemon(method, endpoint string, data io.Reader, ct, daemon string) (*http.Response, io.ReadCloser, error) { - req, client, err := newRequestClient(method, endpoint, data, ct, daemon) - if err != nil { - return nil, nil, err - } - - resp, err := client.Do(req) - if err != nil { - client.Close() - return nil, nil, err - } - body := ioutils.NewReadCloserWrapper(resp.Body, func() error { - defer resp.Body.Close() - return client.Close() - }) - - return resp, body, nil -} - -func sockRequestHijack(method, endpoint string, data io.Reader, ct string) (net.Conn, *bufio.Reader, error) { - req, client, err := newRequestClient(method, endpoint, data, ct, "") - if err != nil { - return nil, nil, err - } - - client.Do(req) - conn, br := client.Hijack() - return conn, br, nil -} - -func newRequestClient(method, endpoint string, data io.Reader, ct, daemon string) (*http.Request, *httputil.ClientConn, error) { - c, err := sockConn(time.Duration(10*time.Second), daemon) - 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) - } - - if ct != "" { - req.Header.Set("Content-Type", ct) - } - return req, client, nil -} - // FIXME(vdemeester) move this away are remove ignoreNoSuchContainer bool func deleteContainer(ignoreNoSuchContainer bool, container ...string) error { result := icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...) @@ -163,7 +84,7 @@ func deleteAllNetworks(c *check.C) { // nat is a pre-defined network on Windows and cannot be removed continue } - status, b, err := sockRequest("DELETE", "/networks/"+n.Name, nil) + status, b, err := request.SockRequest("DELETE", "/networks/"+n.Name, nil, daemonHost()) if err != nil { errs = append(errs, err.Error()) continue @@ -177,7 +98,7 @@ func deleteAllNetworks(c *check.C) { func getAllNetworks() ([]types.NetworkResource, error) { var networks []types.NetworkResource - _, b, err := sockRequest("GET", "/networks", nil) + _, b, err := request.SockRequest("GET", "/networks", nil, daemonHost()) if err != nil { return nil, err } @@ -193,7 +114,7 @@ func deleteAllPlugins(c *check.C) { var errs []string for _, p := range plugins { pluginName := p.Name - status, b, err := sockRequest("DELETE", "/plugins/"+pluginName+"?force=1", nil) + status, b, err := request.SockRequest("DELETE", "/plugins/"+pluginName+"?force=1", nil, daemonHost()) if err != nil { errs = append(errs, err.Error()) continue @@ -207,7 +128,7 @@ func deleteAllPlugins(c *check.C) { func getAllPlugins() (types.PluginsListResponse, error) { var plugins types.PluginsListResponse - _, b, err := sockRequest("GET", "/plugins", nil) + _, b, err := request.SockRequest("GET", "/plugins", nil, daemonHost()) if err != nil { return nil, err } @@ -222,7 +143,7 @@ func deleteAllVolumes(c *check.C) { c.Assert(err, checker.IsNil) var errs []string for _, v := range volumes { - status, b, err := sockRequest("DELETE", "/volumes/"+v.Name, nil) + status, b, err := request.SockRequest("DELETE", "/volumes/"+v.Name, nil, daemonHost()) if err != nil { errs = append(errs, err.Error()) continue @@ -236,7 +157,7 @@ func deleteAllVolumes(c *check.C) { func getAllVolumes() ([]*types.Volume, error) { var volumes volumetypes.VolumesListOKBody - _, b, err := sockRequest("GET", "/volumes", nil) + _, b, err := request.SockRequest("GET", "/volumes", nil, daemonHost()) if err != nil { return nil, err } @@ -1066,7 +987,7 @@ func daemonTime(c *check.C) time.Time { return time.Now() } - status, body, err := sockRequest("GET", "/info", nil) + status, body, err := request.SockRequest("GET", "/info", nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusOK) @@ -1192,7 +1113,7 @@ func waitInspectWithArgs(name, expr, expected string, timeout time.Duration, arg func getInspectBody(c *check.C, version, id string) []byte { endpoint := fmt.Sprintf("/%s/containers/%s/json", version, id) - status, body, err := sockRequest("GET", endpoint, nil) + status, body, err := request.SockRequest("GET", endpoint, nil, daemonHost()) c.Assert(err, check.IsNil) c.Assert(status, check.Equals, http.StatusOK) return body @@ -1224,7 +1145,7 @@ func getGoroutineNumber() (int, error) { i := struct { NGoroutines int }{} - status, b, err := sockRequest("GET", "/info", nil) + status, b, err := request.SockRequest("GET", "/info", nil, daemonHost()) if err != nil { return 0, err } diff --git a/integration-cli/daemon/npipe.go b/integration-cli/request/npipe.go similarity index 91% rename from integration-cli/daemon/npipe.go rename to integration-cli/request/npipe.go index 4e164aae4b..e6ab03945e 100644 --- a/integration-cli/daemon/npipe.go +++ b/integration-cli/request/npipe.go @@ -1,6 +1,6 @@ // +build !windows -package daemon +package request import ( "net" diff --git a/integration-cli/daemon/npipe_windows.go b/integration-cli/request/npipe_windows.go similarity index 91% rename from integration-cli/daemon/npipe_windows.go rename to integration-cli/request/npipe_windows.go index d3c13be5d9..a268aac922 100644 --- a/integration-cli/daemon/npipe_windows.go +++ b/integration-cli/request/npipe_windows.go @@ -1,4 +1,4 @@ -package daemon +package request import ( "net" diff --git a/integration-cli/request/request.go b/integration-cli/request/request.go new file mode 100644 index 0000000000..72e15b73d0 --- /dev/null +++ b/integration-cli/request/request.go @@ -0,0 +1,263 @@ +package request + +import ( + "bufio" + "bytes" + "crypto/tls" + "encoding/json" + "fmt" + "io" + "io/ioutil" + "net" + "net/http" + "net/http/httputil" + "net/url" + "os" + "path/filepath" + "time" + + dclient "github.com/docker/docker/client" + "github.com/docker/docker/pkg/ioutils" + "github.com/docker/docker/pkg/testutil" + "github.com/docker/go-connections/sockets" + "github.com/docker/go-connections/tlsconfig" + "github.com/pkg/errors" +) + +// Method creates a modifier that sets the specified string as the request method +func Method(method string) func(*http.Request) error { + return func(req *http.Request) error { + req.Method = method + return nil + } +} + +// JSON sets the Content-Type request header to json +func JSON(req *http.Request) error { + req.Header.Set("Content-Type", "application/json") + return nil +} + +// JSONBody creates a modifier that encodes the specified data to a JSON string and set it as request body. It also sets +// the Content-Type header of the request. +func JSONBody(data interface{}) func(*http.Request) error { + return func(req *http.Request) error { + jsonData := bytes.NewBuffer(nil) + if err := json.NewEncoder(jsonData).Encode(data); err != nil { + return err + } + req.Body = ioutil.NopCloser(jsonData) + req.Header.Set("Content-Type", "application/json") + return nil + } +} + +// Post creates and execute a POST request on the specified host and endpoint, with the specified request modifiers +func Post(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) { + return Do(host, endpoint, append(modifiers, Method(http.MethodPost))...) +} + +// Delete creates and execute a DELETE request on the specified host and endpoint, with the specified request modifiers +func Delete(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) { + return Do(host, endpoint, append(modifiers, Method(http.MethodDelete))...) +} + +// Get creates and execute a GET request on the specified host and endpoint, with the specified request modifiers +func Get(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) { + return Do(host, endpoint, modifiers...) +} + +// Do creates and execute a request on the specified host and endpoint, with the specified request modifiers +func Do(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Response, io.ReadCloser, error) { + req, err := New(host, endpoint, modifiers...) + if err != nil { + return nil, nil, err + } + client, err := NewClient(host) + if err != nil { + return nil, nil, err + } + resp, err := client.Do(req) + var body io.ReadCloser + if resp != nil { + body = ioutils.NewReadCloserWrapper(resp.Body, func() error { + defer resp.Body.Close() + return nil + }) + } + return resp, body, err +} + +// New creates a new http Request to the specified host and endpoint, with the specified request modifiers +func New(host, endpoint string, modifiers ...func(*http.Request) error) (*http.Request, error) { + _, addr, _, err := dclient.ParseHost(host) + if err != nil { + return nil, err + } + if err != nil { + return nil, errors.Wrapf(err, "could not parse url %q", host) + } + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return nil, fmt.Errorf("could not create new request: %v", err) + } + + req.URL.Scheme = "http" + req.URL.Host = addr + + for _, config := range modifiers { + if err := config(req); err != nil { + return nil, err + } + } + return req, nil +} + +// NewClient creates an http client for the specific host +func NewClient(host string) (*http.Client, error) { + // FIXME(vdemeester) 10*time.Second timeout of SockRequest… ? + proto, addr, _, err := dclient.ParseHost(host) + if err != nil { + return nil, err + } + transport := new(http.Transport) + if proto == "tcp" && os.Getenv("DOCKER_TLS_VERIFY") != "" { + // Setup the socket TLS configuration. + tlsConfig, err := getTLSConfig() + if err != nil { + return nil, err + } + transport = &http.Transport{TLSClientConfig: tlsConfig} + } + err = sockets.ConfigureTransport(transport, proto, addr) + return &http.Client{ + Transport: transport, + }, err +} + +// 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) { + c, err := SockConn(time.Duration(10*time.Second), daemon) + 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 +} + +// SockRequest create a request against the specified host (with method, endpoint and other request modifier) and +// returns the status code, and the content as an byte slice +// Deprecated: use request.Do instead +func SockRequest(method, endpoint string, data interface{}, daemon string, modifiers ...func(*http.Request)) (int, []byte, error) { + jsonData := bytes.NewBuffer(nil) + if err := json.NewEncoder(jsonData).Encode(data); err != nil { + return -1, nil, err + } + + res, body, err := SockRequestRaw(method, endpoint, jsonData, "application/json", daemon, modifiers...) + if err != nil { + return -1, nil, err + } + b, err := testutil.ReadBody(body) + return res.StatusCode, b, err +} + +// SockRequestRaw create a request against the specified host (with method, endpoint and other request modifier) and +// returns the http response, the output as a io.ReadCloser +// Deprecated: use request.Do (or Get, Delete, Post) instead +func SockRequestRaw(method, endpoint string, data io.Reader, ct, daemon string, modifiers ...func(*http.Request)) (*http.Response, io.ReadCloser, error) { + req, client, err := newRequestClient(method, endpoint, data, ct, daemon, modifiers...) + if err != nil { + return nil, nil, err + } + + resp, err := client.Do(req) + body := ioutils.NewReadCloserWrapper(resp.Body, func() error { + defer resp.Body.Close() + return client.Close() + }) + if err != nil { + client.Close() + } + + return resp, body, err +} + +// 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 +} + +// SockConn opens a connection on the specified socket +func SockConn(timeout time.Duration, daemon string) (net.Conn, error) { + daemonURL, err := url.Parse(daemon) + if err != nil { + return nil, errors.Wrapf(err, "could not parse url %q", daemon) + } + + var c net.Conn + switch daemonURL.Scheme { + case "npipe": + return npipeDial(daemonURL.Path, timeout) + case "unix": + return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout) + case "tcp": + if os.Getenv("DOCKER_TLS_VERIFY") != "" { + // Setup the socket TLS configuration. + tlsConfig, err := getTLSConfig() + if err != nil { + return nil, err + } + dialer := &net.Dialer{Timeout: timeout} + return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig) + } + return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout) + default: + return c, errors.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon) + } +} + +func getTLSConfig() (*tls.Config, error) { + dockerCertPath := os.Getenv("DOCKER_CERT_PATH") + + if dockerCertPath == "" { + return nil, errors.New("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable") + } + + option := &tlsconfig.Options{ + CAFile: filepath.Join(dockerCertPath, "ca.pem"), + CertFile: filepath.Join(dockerCertPath, "cert.pem"), + KeyFile: filepath.Join(dockerCertPath, "key.pem"), + } + tlsConfig, err := tlsconfig.Client(*option) + if err != nil { + return nil, err + } + + return tlsConfig, nil +}