mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #12820 from runcom/yet-another-sockRequest-refactor
Expose whole Response struct in sockRequestRaw
This commit is contained in:
commit
cfa3a080c6
4 changed files with 43 additions and 43 deletions
|
@ -339,8 +339,8 @@ func (s *DockerSuite) TestBuildApiDockerfilePath(c *check.C) {
|
|||
c.Fatalf("failed to close tar archive: %v", err)
|
||||
}
|
||||
|
||||
status, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
|
||||
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
||||
res, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
out, err := readBody(body)
|
||||
|
@ -365,8 +365,8 @@ RUN find /tmp/`,
|
|||
}
|
||||
defer server.Close()
|
||||
|
||||
status, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
buf, err := readBody(body)
|
||||
|
@ -393,8 +393,8 @@ RUN echo from dockerfile`,
|
|||
}
|
||||
defer git.Close()
|
||||
|
||||
status, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
buf, err := readBody(body)
|
||||
|
@ -421,8 +421,8 @@ RUN echo from Dockerfile`,
|
|||
defer git.Close()
|
||||
|
||||
// Make sure it tries to 'dockerfile' query param value
|
||||
status, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
buf, err := readBody(body)
|
||||
|
@ -450,8 +450,8 @@ RUN echo from dockerfile`,
|
|||
defer git.Close()
|
||||
|
||||
// Make sure it tries to 'dockerfile' query param value
|
||||
status, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
buf, err := readBody(body)
|
||||
|
@ -483,8 +483,8 @@ func (s *DockerSuite) TestBuildApiDockerfileSymlink(c *check.C) {
|
|||
c.Fatalf("failed to close tar archive: %v", err)
|
||||
}
|
||||
|
||||
status, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
|
||||
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
||||
res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
out, err := readBody(body)
|
||||
|
@ -720,7 +720,7 @@ func (s *DockerSuite) TestContainerApiVerifyHeader(c *check.C) {
|
|||
"Image": "busybox",
|
||||
}
|
||||
|
||||
create := func(ct string) (int, io.ReadCloser, error) {
|
||||
create := func(ct string) (*http.Response, io.ReadCloser, error) {
|
||||
jsonData := bytes.NewBuffer(nil)
|
||||
if err := json.NewEncoder(jsonData).Encode(config); err != nil {
|
||||
c.Fatal(err)
|
||||
|
@ -729,21 +729,21 @@ func (s *DockerSuite) TestContainerApiVerifyHeader(c *check.C) {
|
|||
}
|
||||
|
||||
// Try with no content-type
|
||||
status, body, err := create("")
|
||||
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
||||
res, body, err := create("")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
body.Close()
|
||||
|
||||
// Try with wrong content-type
|
||||
status, body, err = create("application/xml")
|
||||
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
||||
res, body, err = create("application/xml")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
body.Close()
|
||||
|
||||
// now application/json
|
||||
status, body, err = create("application/json")
|
||||
c.Assert(status, check.Equals, http.StatusCreated)
|
||||
res, body, err = create("application/json")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusCreated)
|
||||
body.Close()
|
||||
}
|
||||
|
||||
|
@ -774,8 +774,8 @@ func (s *DockerSuite) TestContainerApiPostCreateNull(c *check.C) {
|
|||
"NetworkDisabled":false,
|
||||
"OnBuild":null}`
|
||||
|
||||
status, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
|
||||
c.Assert(status, check.Equals, http.StatusCreated)
|
||||
res, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusCreated)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
b, err := readBody(body)
|
||||
|
@ -808,13 +808,13 @@ func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *check.C) {
|
|||
"Memory": 524287
|
||||
}`
|
||||
|
||||
status, body, _ := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
|
||||
res, body, _ := sockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json")
|
||||
b, err2 := readBody(body)
|
||||
if err2 != nil {
|
||||
c.Fatal(err2)
|
||||
}
|
||||
|
||||
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
c.Assert(strings.Contains(string(b), "Minimum memory limit allowed is 4MB"), check.Equals, true)
|
||||
}
|
||||
|
||||
|
@ -831,13 +831,13 @@ func (s *DockerSuite) TestStartWithTooLowMemoryLimit(c *check.C) {
|
|||
"Memory": 524287
|
||||
}`
|
||||
|
||||
status, body, _ := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
|
||||
res, body, _ := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json")
|
||||
b, err2 := readBody(body)
|
||||
if err2 != nil {
|
||||
c.Fatal(err2)
|
||||
}
|
||||
|
||||
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
c.Assert(strings.Contains(string(b), "Minimum memory limit allowed is 4MB"), check.Equals, true)
|
||||
}
|
||||
|
||||
|
|
|
@ -74,9 +74,9 @@ func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) {
|
|||
}
|
||||
id := strings.TrimSpace(out)
|
||||
|
||||
status, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "")
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
res, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
||||
|
||||
defer body.Close()
|
||||
|
||||
|
@ -84,9 +84,9 @@ func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) {
|
|||
c.Fatal(err, out)
|
||||
}
|
||||
|
||||
status, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar")
|
||||
c.Assert(status, check.Equals, http.StatusOK)
|
||||
res, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
||||
|
||||
defer loadBody.Close()
|
||||
|
||||
|
|
|
@ -20,22 +20,22 @@ func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {
|
|||
}
|
||||
|
||||
type logOut struct {
|
||||
out string
|
||||
status int
|
||||
err error
|
||||
out string
|
||||
res *http.Response
|
||||
err error
|
||||
}
|
||||
chLog := make(chan logOut)
|
||||
|
||||
go func() {
|
||||
statusCode, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id), nil, "")
|
||||
res, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id), nil, "")
|
||||
out, _ := bufio.NewReader(body).ReadString('\n')
|
||||
chLog <- logOut{strings.TrimSpace(out), statusCode, err}
|
||||
chLog <- logOut{strings.TrimSpace(out), res, err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case l := <-chLog:
|
||||
c.Assert(l.status, check.Equals, http.StatusOK)
|
||||
c.Assert(l.err, check.IsNil)
|
||||
c.Assert(l.res.StatusCode, check.Equals, http.StatusOK)
|
||||
if !strings.HasSuffix(l.out, "hello") {
|
||||
c.Fatalf("expected log output to container 'hello', but it does not")
|
||||
}
|
||||
|
|
|
@ -313,20 +313,20 @@ func sockRequest(method, endpoint string, data interface{}) (int, []byte, error)
|
|||
return -1, nil, err
|
||||
}
|
||||
|
||||
status, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
|
||||
res, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
|
||||
if err != nil {
|
||||
b, _ := ioutil.ReadAll(body)
|
||||
return status, b, err
|
||||
return -1, b, err
|
||||
}
|
||||
var b []byte
|
||||
b, err = readBody(body)
|
||||
return status, b, err
|
||||
return res.StatusCode, b, err
|
||||
}
|
||||
|
||||
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io.ReadCloser, error) {
|
||||
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
|
||||
c, err := sockConn(time.Duration(10 * time.Second))
|
||||
if err != nil {
|
||||
return -1, nil, fmt.Errorf("could not dial docker daemon: %v", err)
|
||||
return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
|
||||
}
|
||||
|
||||
client := httputil.NewClientConn(c, nil)
|
||||
|
@ -334,7 +334,7 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io
|
|||
req, err := http.NewRequest(method, endpoint, data)
|
||||
if err != nil {
|
||||
client.Close()
|
||||
return -1, nil, fmt.Errorf("could not create new request: %v", err)
|
||||
return nil, nil, fmt.Errorf("could not create new request: %v", err)
|
||||
}
|
||||
|
||||
if ct != "" {
|
||||
|
@ -344,14 +344,14 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io
|
|||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
client.Close()
|
||||
return -1, nil, fmt.Errorf("could not perform request: %v", err)
|
||||
return nil, nil, fmt.Errorf("could not perform request: %v", err)
|
||||
}
|
||||
body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
||||
defer client.Close()
|
||||
return resp.Body.Close()
|
||||
})
|
||||
|
||||
return resp.StatusCode, body, err
|
||||
return resp, body, nil
|
||||
}
|
||||
|
||||
func readBody(b io.ReadCloser) ([]byte, error) {
|
||||
|
|
Loading…
Reference in a new issue