[test-integration] convert request.SockRequestRaw to appropriate methods
| ... | ... |
@@ -623,6 +623,7 @@ func (d *Daemon) SockRequest(method, endpoint string, data interface{}) (int, []
|
| 623 | 623 |
|
| 624 | 624 |
// SockRequestRaw executes a socket request on a daemon and returns an http |
| 625 | 625 |
// response and a reader for the output data. |
| 626 |
+// Deprecated: use request package instead |
|
| 626 | 627 |
func (d *Daemon) SockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
|
| 627 | 628 |
return request.SockRequestRaw(method, endpoint, data, ct, d.Sock()) |
| 628 | 629 |
} |
| ... | ... |
@@ -714,7 +715,7 @@ func (d *Daemon) ReloadConfig() error {
|
| 714 | 714 |
errCh := make(chan error) |
| 715 | 715 |
started := make(chan struct{})
|
| 716 | 716 |
go func() {
|
| 717 |
- _, body, err := request.SockRequestRaw("GET", "/events", nil, "", d.Sock())
|
|
| 717 |
+ _, body, err := request.Get(d.Sock(), "/events") |
|
| 718 | 718 |
close(started) |
| 719 | 719 |
if err != nil {
|
| 720 | 720 |
errCh <- err |
| ... | ... |
@@ -3,6 +3,7 @@ package main |
| 3 | 3 |
import ( |
| 4 | 4 |
"archive/tar" |
| 5 | 5 |
"bytes" |
| 6 |
+ "io/ioutil" |
|
| 6 | 7 |
"net/http" |
| 7 | 8 |
"regexp" |
| 8 | 9 |
"strings" |
| ... | ... |
@@ -31,7 +32,7 @@ RUN find /tmp/` |
| 31 | 31 |
server := fakeStorage(c, map[string]string{"testD": testD})
|
| 32 | 32 |
defer server.Close() |
| 33 | 33 |
|
| 34 |
- res, body, err := request.SockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json", daemonHost())
|
|
| 34 |
+ res, body, err := request.Post(daemonHost(), "/build?dockerfile=baz&remote="+server.URL()+"/testD", request.JSON) |
|
| 35 | 35 |
c.Assert(err, checker.IsNil) |
| 36 | 36 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 37 | 37 |
|
| ... | ... |
@@ -70,7 +71,7 @@ func (s *DockerSuite) TestBuildAPIRemoteTarballContext(c *check.C) {
|
| 70 | 70 |
}) |
| 71 | 71 |
defer server.Close() |
| 72 | 72 |
|
| 73 |
- res, b, err := request.SockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar", daemonHost())
|
|
| 73 |
+ res, b, err := request.Post(daemonHost(), "/build?remote="+server.URL()+"/testT.tar", request.ContentType("application/tar"))
|
|
| 74 | 74 |
c.Assert(err, checker.IsNil) |
| 75 | 75 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 76 | 76 |
b.Close() |
| ... | ... |
@@ -118,7 +119,7 @@ RUN echo 'right' |
| 118 | 118 |
defer server.Close() |
| 119 | 119 |
|
| 120 | 120 |
url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar" |
| 121 |
- res, body, err := request.SockRequestRaw("POST", url, nil, "application/tar", daemonHost())
|
|
| 121 |
+ res, body, err := request.Post(daemonHost(), url, request.ContentType("application/tar"))
|
|
| 122 | 122 |
c.Assert(err, checker.IsNil) |
| 123 | 123 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 124 | 124 |
|
| ... | ... |
@@ -137,7 +138,7 @@ RUN echo from dockerfile`, |
| 137 | 137 |
}, false) |
| 138 | 138 |
defer git.Close() |
| 139 | 139 |
|
| 140 |
- res, body, err := request.SockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json", daemonHost())
|
|
| 140 |
+ res, body, err := request.Post(daemonHost(), "/build?remote="+git.RepoURL, request.JSON) |
|
| 141 | 141 |
c.Assert(err, checker.IsNil) |
| 142 | 142 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 143 | 143 |
|
| ... | ... |
@@ -158,7 +159,7 @@ RUN echo from Dockerfile`, |
| 158 | 158 |
defer git.Close() |
| 159 | 159 |
|
| 160 | 160 |
// Make sure it tries to 'dockerfile' query param value |
| 161 |
- res, body, err := request.SockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json", daemonHost())
|
|
| 161 |
+ res, body, err := request.Post(daemonHost(), "/build?dockerfile=baz&remote="+git.RepoURL, request.JSON) |
|
| 162 | 162 |
c.Assert(err, checker.IsNil) |
| 163 | 163 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 164 | 164 |
|
| ... | ... |
@@ -180,7 +181,7 @@ RUN echo from dockerfile`, |
| 180 | 180 |
defer git.Close() |
| 181 | 181 |
|
| 182 | 182 |
// Make sure it tries to 'dockerfile' query param value |
| 183 |
- res, body, err := request.SockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json", daemonHost())
|
|
| 183 |
+ res, body, err := request.Post(daemonHost(), "/build?remote="+git.RepoURL, request.JSON) |
|
| 184 | 184 |
c.Assert(err, checker.IsNil) |
| 185 | 185 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 186 | 186 |
|
| ... | ... |
@@ -227,7 +228,7 @@ func (s *DockerSuite) TestBuildAPIUnnormalizedTarPaths(c *check.C) {
|
| 227 | 227 |
// failed to close tar archive |
| 228 | 228 |
c.Assert(tw.Close(), checker.IsNil) |
| 229 | 229 |
|
| 230 |
- res, body, err := request.SockRequestRaw("POST", "/build", buffer, "application/x-tar", daemonHost())
|
|
| 230 |
+ res, body, err := request.Post(daemonHost(), "/build", request.RawContent(ioutil.NopCloser(buffer)), request.ContentType("application/x-tar"))
|
|
| 231 | 231 |
c.Assert(err, checker.IsNil) |
| 232 | 232 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 233 | 233 |
|
| ... | ... |
@@ -213,10 +213,10 @@ func (s *DockerSuite) TestGetContainerStatsRmRunning(c *check.C) {
|
| 213 | 213 |
out, _ := runSleepingContainer(c) |
| 214 | 214 |
id := strings.TrimSpace(out) |
| 215 | 215 |
|
| 216 |
- buf := &testutil.ChannelBuffer{make(chan []byte, 1)}
|
|
| 216 |
+ buf := &testutil.ChannelBuffer{C: make(chan []byte, 1)}
|
|
| 217 | 217 |
defer buf.Close() |
| 218 | 218 |
|
| 219 |
- _, body, err := request.SockRequestRaw("GET", "/containers/"+id+"/stats?stream=1", nil, "application/json", daemonHost())
|
|
| 219 |
+ _, body, err := request.Get(daemonHost(), "/containers/"+id+"/stats?stream=1", request.JSON) |
|
| 220 | 220 |
c.Assert(err, checker.IsNil) |
| 221 | 221 |
defer body.Close() |
| 222 | 222 |
|
| ... | ... |
@@ -250,13 +250,13 @@ func (s *DockerSuite) TestGetContainerStatsStream(c *check.C) {
|
| 250 | 250 |
|
| 251 | 251 |
type b struct {
|
| 252 | 252 |
status int |
| 253 |
- body []byte |
|
| 253 |
+ body io.ReadCloser |
|
| 254 | 254 |
err error |
| 255 | 255 |
} |
| 256 | 256 |
bc := make(chan b, 1) |
| 257 | 257 |
go func() {
|
| 258 |
- status, body, err := request.SockRequest("GET", "/containers/"+name+"/stats", nil, daemonHost())
|
|
| 259 |
- bc <- b{status, body, err}
|
|
| 258 |
+ status, body, err := request.Get(daemonHost(), "/containers/"+name+"/stats") |
|
| 259 |
+ bc <- b{status.StatusCode, body, err}
|
|
| 260 | 260 |
}() |
| 261 | 261 |
|
| 262 | 262 |
// allow some time to stream the stats from the container |
| ... | ... |
@@ -272,7 +272,9 @@ func (s *DockerSuite) TestGetContainerStatsStream(c *check.C) {
|
| 272 | 272 |
c.Assert(sr.err, checker.IsNil) |
| 273 | 273 |
c.Assert(sr.status, checker.Equals, http.StatusOK) |
| 274 | 274 |
|
| 275 |
- s := string(sr.body) |
|
| 275 |
+ b, err := ioutil.ReadAll(sr.body) |
|
| 276 |
+ c.Assert(err, checker.IsNil) |
|
| 277 |
+ s := string(b) |
|
| 276 | 278 |
// count occurrences of "read" of types.Stats |
| 277 | 279 |
if l := strings.Count(s, "read"); l < 2 {
|
| 278 | 280 |
c.Fatalf("Expected more than one stat streamed, got %d", l)
|
| ... | ... |
@@ -327,7 +329,7 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
|
| 327 | 327 |
// We expect an immediate response, but if it's not immediate, the test would hang, so put it in a goroutine |
| 328 | 328 |
// below we'll check this on a timeout. |
| 329 | 329 |
go func() {
|
| 330 |
- resp, body, err := request.SockRequestRaw("GET", "/containers/"+name+"/stats", nil, "", daemonHost())
|
|
| 330 |
+ resp, body, err := request.Get(daemonHost(), "/containers/"+name+"/stats") |
|
| 331 | 331 |
body.Close() |
| 332 | 332 |
chResp <- stats{resp.StatusCode, err}
|
| 333 | 333 |
}() |
| ... | ... |
@@ -659,7 +661,7 @@ func (s *DockerSuite) TestContainerAPIVerifyHeader(c *check.C) {
|
| 659 | 659 |
create := func(ct string) (*http.Response, io.ReadCloser, error) {
|
| 660 | 660 |
jsonData := bytes.NewBuffer(nil) |
| 661 | 661 |
c.Assert(json.NewEncoder(jsonData).Encode(config), checker.IsNil) |
| 662 |
- return request.SockRequestRaw("POST", "/containers/create", jsonData, ct, daemonHost())
|
|
| 662 |
+ return request.Post(daemonHost(), "/containers/create", request.RawContent(ioutil.NopCloser(jsonData)), request.ContentType(ct)) |
|
| 663 | 663 |
} |
| 664 | 664 |
|
| 665 | 665 |
// Try with no content-type |
| ... | ... |
@@ -695,7 +697,7 @@ func (s *DockerSuite) TestContainerAPIInvalidPortSyntax(c *check.C) {
|
| 695 | 695 |
} |
| 696 | 696 |
}` |
| 697 | 697 |
|
| 698 |
- res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 698 |
+ res, body, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 699 | 699 |
c.Assert(err, checker.IsNil) |
| 700 | 700 |
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) |
| 701 | 701 |
|
| ... | ... |
@@ -715,7 +717,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyInvalidPolicyName(c *check.C) |
| 715 | 715 |
} |
| 716 | 716 |
}` |
| 717 | 717 |
|
| 718 |
- res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 718 |
+ res, body, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 719 | 719 |
c.Assert(err, checker.IsNil) |
| 720 | 720 |
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) |
| 721 | 721 |
|
| ... | ... |
@@ -735,7 +737,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyRetryMismatch(c *check.C) {
|
| 735 | 735 |
} |
| 736 | 736 |
}` |
| 737 | 737 |
|
| 738 |
- res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 738 |
+ res, body, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 739 | 739 |
c.Assert(err, checker.IsNil) |
| 740 | 740 |
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) |
| 741 | 741 |
|
| ... | ... |
@@ -755,7 +757,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyNegativeRetryCount(c *check.C |
| 755 | 755 |
} |
| 756 | 756 |
}` |
| 757 | 757 |
|
| 758 |
- res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 758 |
+ res, body, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 759 | 759 |
c.Assert(err, checker.IsNil) |
| 760 | 760 |
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) |
| 761 | 761 |
|
| ... | ... |
@@ -775,7 +777,7 @@ func (s *DockerSuite) TestContainerAPIRestartPolicyDefaultRetryCount(c *check.C) |
| 775 | 775 |
} |
| 776 | 776 |
}` |
| 777 | 777 |
|
| 778 |
- res, _, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 778 |
+ res, _, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 779 | 779 |
c.Assert(err, checker.IsNil) |
| 780 | 780 |
c.Assert(res.StatusCode, checker.Equals, http.StatusCreated) |
| 781 | 781 |
} |
| ... | ... |
@@ -806,7 +808,7 @@ func (s *DockerSuite) TestContainerAPIPostCreateNull(c *check.C) {
|
| 806 | 806 |
"NetworkDisabled":false, |
| 807 | 807 |
"OnBuild":null}` |
| 808 | 808 |
|
| 809 |
- res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 809 |
+ res, body, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 810 | 810 |
c.Assert(err, checker.IsNil) |
| 811 | 811 |
c.Assert(res.StatusCode, checker.Equals, http.StatusCreated) |
| 812 | 812 |
|
| ... | ... |
@@ -837,7 +839,7 @@ func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *check.C) {
|
| 837 | 837 |
"Memory": 524287 |
| 838 | 838 |
}` |
| 839 | 839 |
|
| 840 |
- res, body, err := request.SockRequestRaw("POST", "/containers/create", strings.NewReader(config), "application/json", daemonHost())
|
|
| 840 |
+ res, body, err := request.Post(daemonHost(), "/containers/create", request.RawString(config), request.JSON) |
|
| 841 | 841 |
c.Assert(err, checker.IsNil) |
| 842 | 842 |
b, err2 := testutil.ReadBody(body) |
| 843 | 843 |
c.Assert(err2, checker.IsNil) |
| ... | ... |
@@ -22,7 +22,7 @@ func (s *DockerSuite) TestEventsAPIEmptyOutput(c *check.C) {
|
| 22 | 22 |
} |
| 23 | 23 |
chResp := make(chan *apiResp) |
| 24 | 24 |
go func() {
|
| 25 |
- resp, body, err := request.SockRequestRaw("GET", "/events", nil, "", daemonHost())
|
|
| 25 |
+ resp, body, err := request.Get(daemonHost(), "/events") |
|
| 26 | 26 |
body.Close() |
| 27 | 27 |
chResp <- &apiResp{resp, err}
|
| 28 | 28 |
}() |
| ... | ... |
@@ -47,7 +47,7 @@ func (s *DockerSuite) TestEventsAPIBackwardsCompatible(c *check.C) {
|
| 47 | 47 |
q := url.Values{}
|
| 48 | 48 |
q.Set("since", ts)
|
| 49 | 49 |
|
| 50 |
- _, body, err := request.SockRequestRaw("GET", "/events?"+q.Encode(), nil, "", daemonHost())
|
|
| 50 |
+ _, body, err := request.Get(daemonHost(), "/events?"+q.Encode()) |
|
| 51 | 51 |
c.Assert(err, checker.IsNil) |
| 52 | 52 |
defer body.Close() |
| 53 | 53 |
|
| ... | ... |
@@ -62,7 +62,7 @@ func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) {
|
| 62 | 62 |
} |
| 63 | 63 |
defer conn.Close() |
| 64 | 64 |
|
| 65 |
- _, rc, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), nil, "text/plain", daemonHost())
|
|
| 65 |
+ _, rc, err := request.Post(daemonHost(), fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), request.ContentType("text/plain"))
|
|
| 66 | 66 |
// It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned. |
| 67 | 67 |
if err == io.ErrUnexpectedEOF {
|
| 68 | 68 |
return fmt.Errorf("The daemon might have crashed.")
|
| ... | ... |
@@ -6,8 +6,8 @@ import ( |
| 6 | 6 |
"bytes" |
| 7 | 7 |
"encoding/json" |
| 8 | 8 |
"fmt" |
| 9 |
+ "io/ioutil" |
|
| 9 | 10 |
"net/http" |
| 10 |
- "strings" |
|
| 11 | 11 |
"time" |
| 12 | 12 |
|
| 13 | 13 |
"github.com/docker/docker/integration-cli/checker" |
| ... | ... |
@@ -38,7 +38,7 @@ func (s *DockerSuite) TestExecAPICreateNoValidContentType(c *check.C) {
|
| 38 | 38 |
c.Fatalf("Can not encode data to json %s", err)
|
| 39 | 39 |
} |
| 40 | 40 |
|
| 41 |
- res, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain", daemonHost())
|
|
| 41 |
+ res, body, err := request.Post(daemonHost(), fmt.Sprintf("/containers/%s/exec", name), request.RawContent(ioutil.NopCloser(jsonData)), request.ContentType("test/plain"))
|
|
| 42 | 42 |
c.Assert(err, checker.IsNil) |
| 43 | 43 |
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError) |
| 44 | 44 |
|
| ... | ... |
@@ -96,7 +96,7 @@ func (s *DockerSuite) TestExecAPIStartEnsureHeaders(c *check.C) {
|
| 96 | 96 |
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top") |
| 97 | 97 |
|
| 98 | 98 |
id := createExec(c, "test") |
| 99 |
- resp, _, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json", daemonHost())
|
|
| 99 |
+ resp, _, err := request.Post(daemonHost(), fmt.Sprintf("/exec/%s/start", id), request.RawString(`{"Detach": true}`), request.JSON)
|
|
| 100 | 100 |
c.Assert(err, checker.IsNil) |
| 101 | 101 |
c.Assert(resp.Header.Get("Server"), checker.Not(checker.Equals), "")
|
| 102 | 102 |
} |
| ... | ... |
@@ -106,7 +106,7 @@ func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
|
| 106 | 106 |
runSleepingContainer(c, "-d", "--name", "test") |
| 107 | 107 |
id := createExec(c, "test") |
| 108 | 108 |
|
| 109 |
- resp, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain", daemonHost())
|
|
| 109 |
+ resp, body, err := request.Post(daemonHost(), fmt.Sprintf("/v1.20/exec/%s/start", id), request.RawString(`{"Detach": true}`), request.ContentType("text/plain"))
|
|
| 110 | 110 |
c.Assert(err, checker.IsNil) |
| 111 | 111 |
|
| 112 | 112 |
b, err := testutil.ReadBody(body) |
| ... | ... |
@@ -141,14 +141,14 @@ func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) {
|
| 141 | 141 |
}{}
|
| 142 | 142 |
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b))) |
| 143 | 143 |
|
| 144 |
- _, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json", daemonHost())
|
|
| 144 |
+ _, body, err := request.Post(daemonHost(), fmt.Sprintf("/exec/%s/start", createResp.ID), request.RawString(`{"Detach": true}`), request.JSON)
|
|
| 145 | 145 |
c.Assert(err, checker.IsNil) |
| 146 | 146 |
|
| 147 | 147 |
b, err = testutil.ReadBody(body) |
| 148 | 148 |
comment := check.Commentf("response body: %s", b)
|
| 149 | 149 |
c.Assert(err, checker.IsNil, comment) |
| 150 | 150 |
|
| 151 |
- resp, _, err := request.SockRequestRaw("GET", "/_ping", nil, "", daemonHost())
|
|
| 151 |
+ resp, _, err := request.Get(daemonHost(), "/_ping") |
|
| 152 | 152 |
c.Assert(err, checker.IsNil) |
| 153 | 153 |
if resp.StatusCode != http.StatusOK {
|
| 154 | 154 |
c.Fatal("daemon is down, it should alive")
|
| ... | ... |
@@ -191,9 +191,11 @@ func createExec(c *check.C, name string) string {
|
| 191 | 191 |
} |
| 192 | 192 |
|
| 193 | 193 |
func createExecCmd(c *check.C, name string, cmd string) string {
|
| 194 |
- _, b, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{cmd}}, daemonHost())
|
|
| 195 |
- c.Assert(err, checker.IsNil, check.Commentf(string(b))) |
|
| 196 |
- |
|
| 194 |
+ _, reader, err := request.Post(daemonHost(), fmt.Sprintf("/containers/%s/exec", name), request.JSONBody(map[string]interface{}{"Cmd": []string{cmd}}))
|
|
| 195 |
+ c.Assert(err, checker.IsNil) |
|
| 196 |
+ b, err := ioutil.ReadAll(reader) |
|
| 197 |
+ c.Assert(err, checker.IsNil) |
|
| 198 |
+ defer reader.Close() |
|
| 197 | 199 |
createResp := struct {
|
| 198 | 200 |
ID string `json:"Id"` |
| 199 | 201 |
}{}
|
| ... | ... |
@@ -202,7 +204,7 @@ func createExecCmd(c *check.C, name string, cmd string) string {
|
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
func startExec(c *check.C, id string, code int) {
|
| 205 |
- resp, body, err := request.SockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json", daemonHost())
|
|
| 205 |
+ resp, body, err := request.Post(daemonHost(), fmt.Sprintf("/exec/%s/start", id), request.RawString(`{"Detach": true}`), request.JSON)
|
|
| 206 | 206 |
c.Assert(err, checker.IsNil) |
| 207 | 207 |
|
| 208 | 208 |
b, err := testutil.ReadBody(body) |
| ... | ... |
@@ -212,7 +214,7 @@ func startExec(c *check.C, id string, code int) {
|
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 | 214 |
func inspectExec(c *check.C, id string, out interface{}) {
|
| 215 |
- resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "", daemonHost())
|
|
| 215 |
+ resp, body, err := request.Get(daemonHost(), fmt.Sprintf("/exec/%s/json", id))
|
|
| 216 | 216 |
c.Assert(err, checker.IsNil) |
| 217 | 217 |
defer body.Close() |
| 218 | 218 |
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
| ... | ... |
@@ -238,7 +240,7 @@ func waitForExec(c *check.C, id string) {
|
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 | 240 |
func inspectContainer(c *check.C, id string, out interface{}) {
|
| 241 |
- resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/json", id), nil, "", daemonHost())
|
|
| 241 |
+ resp, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/json", id))
|
|
| 242 | 242 |
c.Assert(err, checker.IsNil) |
| 243 | 243 |
defer body.Close() |
| 244 | 244 |
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
| ... | ... |
@@ -56,14 +56,14 @@ func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *check.C) {
|
| 56 | 56 |
buildImageSuccessfully(c, "saveandload", withDockerfile("FROM busybox\nENV FOO bar"))
|
| 57 | 57 |
id := getIDByName(c, "saveandload") |
| 58 | 58 |
|
| 59 |
- res, body, err := request.SockRequestRaw("GET", "/images/"+id+"/get", nil, "", daemonHost())
|
|
| 59 |
+ res, body, err := request.Get(daemonHost(), "/images/"+id+"/get") |
|
| 60 | 60 |
c.Assert(err, checker.IsNil) |
| 61 | 61 |
defer body.Close() |
| 62 | 62 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 63 | 63 |
|
| 64 | 64 |
dockerCmd(c, "rmi", id) |
| 65 | 65 |
|
| 66 |
- res, loadBody, err := request.SockRequestRaw("POST", "/images/load", body, "application/x-tar", daemonHost())
|
|
| 66 |
+ res, loadBody, err := request.Post(daemonHost(), "/images/load", request.RawContent(body), request.ContentType("application/x-tar"))
|
|
| 67 | 67 |
c.Assert(err, checker.IsNil) |
| 68 | 68 |
defer loadBody.Close() |
| 69 | 69 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| ... | ... |
@@ -119,7 +119,7 @@ func (s *DockerSuite) TestAPIImagesHistory(c *check.C) {
|
| 119 | 119 |
func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *check.C) {
|
| 120 | 120 |
testRequires(c, Network) |
| 121 | 121 |
|
| 122 |
- res, b, err := request.SockRequestRaw("GET", "/images/search?term=test", nil, "application/json", daemonHost())
|
|
| 122 |
+ res, b, err := request.Get(daemonHost(), "/images/search?term=test", request.JSON) |
|
| 123 | 123 |
c.Assert(err, check.IsNil) |
| 124 | 124 |
b.Close() |
| 125 | 125 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| ... | ... |
@@ -2,7 +2,6 @@ package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"bufio" |
| 5 |
- "bytes" |
|
| 6 | 5 |
"fmt" |
| 7 | 6 |
"net/http" |
| 8 | 7 |
"strings" |
| ... | ... |
@@ -26,7 +25,7 @@ func (s *DockerSuite) TestLogsAPIWithStdout(c *check.C) {
|
| 26 | 26 |
chLog := make(chan logOut) |
| 27 | 27 |
|
| 28 | 28 |
go func() {
|
| 29 |
- res, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id), nil, "", daemonHost())
|
|
| 29 |
+ res, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1×tamps=1", id))
|
|
| 30 | 30 |
if err != nil {
|
| 31 | 31 |
chLog <- logOut{"", nil, err}
|
| 32 | 32 |
return |
| ... | ... |
@@ -70,7 +69,7 @@ func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) {
|
| 70 | 70 |
t0 := time.Now() |
| 71 | 71 |
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "sleep", "10") |
| 72 | 72 |
|
| 73 |
- _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "", daemonHost())
|
|
| 73 |
+ _, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
|
| 74 | 74 |
t1 := time.Now() |
| 75 | 75 |
c.Assert(err, checker.IsNil) |
| 76 | 76 |
body.Close() |
| ... | ... |
@@ -82,7 +81,7 @@ func (s *DockerSuite) TestLogsAPIFollowEmptyOutput(c *check.C) {
|
| 82 | 82 |
|
| 83 | 83 |
func (s *DockerSuite) TestLogsAPIContainerNotFound(c *check.C) {
|
| 84 | 84 |
name := "nonExistentContainer" |
| 85 |
- resp, _, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "", daemonHost())
|
|
| 85 |
+ resp, _, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name))
|
|
| 86 | 86 |
c.Assert(err, checker.IsNil) |
| 87 | 87 |
c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound) |
| 88 | 88 |
} |
| ... | ... |
@@ -26,7 +26,7 @@ func (s *DockerSuite) TestAPIStatsNoStreamGetCpu(c *check.C) {
|
| 26 | 26 |
id := strings.TrimSpace(out) |
| 27 | 27 |
c.Assert(waitRun(id), checker.IsNil) |
| 28 | 28 |
|
| 29 |
- resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "", daemonHost())
|
|
| 29 |
+ resp, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/stats?stream=false", id))
|
|
| 30 | 30 |
c.Assert(err, checker.IsNil) |
| 31 | 31 |
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
| 32 | 32 |
c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
|
| ... | ... |
@@ -65,7 +65,7 @@ func (s *DockerSuite) TestAPIStatsStoppedContainerInGoroutines(c *check.C) {
|
| 65 | 65 |
id := strings.TrimSpace(out) |
| 66 | 66 |
|
| 67 | 67 |
getGoRoutines := func() int {
|
| 68 |
- _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/info"), nil, "", daemonHost())
|
|
| 68 |
+ _, body, err := request.Get(daemonHost(), fmt.Sprintf("/info"))
|
|
| 69 | 69 |
c.Assert(err, checker.IsNil) |
| 70 | 70 |
info := types.Info{}
|
| 71 | 71 |
err = json.NewDecoder(body).Decode(&info) |
| ... | ... |
@@ -76,7 +76,7 @@ func (s *DockerSuite) TestAPIStatsStoppedContainerInGoroutines(c *check.C) {
|
| 76 | 76 |
|
| 77 | 77 |
// When the HTTP connection is closed, the number of goroutines should not increase. |
| 78 | 78 |
routines := getGoRoutines() |
| 79 |
- _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "", daemonHost())
|
|
| 79 |
+ _, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/stats", id))
|
|
| 80 | 80 |
c.Assert(err, checker.IsNil) |
| 81 | 81 |
body.Close() |
| 82 | 82 |
|
| ... | ... |
@@ -192,7 +192,7 @@ func (s *DockerSuite) TestAPIStatsNetworkStatsVersioning(c *check.C) {
|
| 192 | 192 |
func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
|
| 193 | 193 |
var st *types.StatsJSON |
| 194 | 194 |
|
| 195 |
- _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "", daemonHost())
|
|
| 195 |
+ _, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/stats?stream=false", id))
|
|
| 196 | 196 |
c.Assert(err, checker.IsNil) |
| 197 | 197 |
|
| 198 | 198 |
err = json.NewDecoder(body).Decode(&st) |
| ... | ... |
@@ -209,7 +209,7 @@ func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
|
| 209 | 209 |
func getVersionedStats(c *check.C, id string, apiVersion string) map[string]interface{} {
|
| 210 | 210 |
stats := make(map[string]interface{})
|
| 211 | 211 |
|
| 212 |
- _, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id), nil, "", daemonHost())
|
|
| 212 |
+ _, body, err := request.Get(daemonHost(), fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id))
|
|
| 213 | 213 |
c.Assert(err, checker.IsNil) |
| 214 | 214 |
defer body.Close() |
| 215 | 215 |
|
| ... | ... |
@@ -284,7 +284,7 @@ func (s *DockerSuite) TestAPIStatsNoStreamConnectedContainers(c *check.C) {
|
| 284 | 284 |
|
| 285 | 285 |
ch := make(chan error) |
| 286 | 286 |
go func() {
|
| 287 |
- resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id2), nil, "", daemonHost())
|
|
| 287 |
+ resp, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/stats?stream=false", id2))
|
|
| 288 | 288 |
defer body.Close() |
| 289 | 289 |
if err != nil {
|
| 290 | 290 |
ch <- err |
| ... | ... |
@@ -16,7 +16,7 @@ import ( |
| 16 | 16 |
func (s *DockerSuite) TestAPIStatsContainerGetMemoryLimit(c *check.C) {
|
| 17 | 17 |
testRequires(c, DaemonIsLinux, memoryLimitSupport) |
| 18 | 18 |
|
| 19 |
- resp, body, err := request.SockRequestRaw("GET", "/info", nil, "application/json", daemonHost())
|
|
| 19 |
+ resp, body, err := request.Get(daemonHost(), "/info", request.JSON) |
|
| 20 | 20 |
c.Assert(err, checker.IsNil) |
| 21 | 21 |
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
| 22 | 22 |
var info types.Info |
| ... | ... |
@@ -29,7 +29,7 @@ func (s *DockerSuite) TestAPIStatsContainerGetMemoryLimit(c *check.C) {
|
| 29 | 29 |
dockerCmd(c, "run", "-d", "--name", conName, "busybox", "top") |
| 30 | 30 |
c.Assert(waitRun(conName), checker.IsNil) |
| 31 | 31 |
|
| 32 |
- resp, body, err = request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", conName), nil, "", daemonHost())
|
|
| 32 |
+ resp, body, err = request.Get(daemonHost(), fmt.Sprintf("/containers/%s/stats?stream=false", conName))
|
|
| 33 | 33 |
c.Assert(err, checker.IsNil) |
| 34 | 34 |
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) |
| 35 | 35 |
c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
|
| ... | ... |
@@ -24,7 +24,7 @@ func (s *DockerSuite) TestAPIOptionsRoute(c *check.C) {
|
| 24 | 24 |
} |
| 25 | 25 |
|
| 26 | 26 |
func (s *DockerSuite) TestAPIGetEnabledCORS(c *check.C) {
|
| 27 |
- res, body, err := request.SockRequestRaw("GET", "/version", nil, "", daemonHost())
|
|
| 27 |
+ res, body, err := request.Get(daemonHost(), "/version") |
|
| 28 | 28 |
c.Assert(err, checker.IsNil) |
| 29 | 29 |
c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 30 | 30 |
body.Close() |
| ... | ... |
@@ -220,7 +220,7 @@ func (s *DockerSuite) TestUpdateStats(c *check.C) {
|
| 220 | 220 |
c.Assert(waitRun(name), checker.IsNil) |
| 221 | 221 |
|
| 222 | 222 |
getMemLimit := func(id string) uint64 {
|
| 223 |
- resp, body, err := request.SockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "", daemonHost())
|
|
| 223 |
+ resp, body, err := request.Get(daemonHost(), fmt.Sprintf("/containers/%s/stats?stream=false", id))
|
|
| 224 | 224 |
c.Assert(err, checker.IsNil) |
| 225 | 225 |
c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
|
| 226 | 226 |
|
| ... | ... |
@@ -150,7 +150,7 @@ func (s *DockerSuite) TestDeprecatedStartWithTooLowMemoryLimit(c *check.C) {
|
| 150 | 150 |
"Memory": 524287 |
| 151 | 151 |
}` |
| 152 | 152 |
|
| 153 |
- res, body, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+containerID+"/start"), strings.NewReader(config), "application/json", daemonHost())
|
|
| 153 |
+ res, body, err := request.Post(daemonHost(), formatV123StartAPIURL("/containers/"+containerID+"/start"), request.RawString(config), request.JSON)
|
|
| 154 | 154 |
c.Assert(err, checker.IsNil) |
| 155 | 155 |
b, err2 := testutil.ReadBody(body) |
| 156 | 156 |
c.Assert(err2, checker.IsNil) |
| ... | ... |
@@ -169,7 +169,7 @@ func (s *DockerSuite) TestDeprecatedPostContainersStartWithoutLinksInHostConfig( |
| 169 | 169 |
hc := inspectFieldJSON(c, name, "HostConfig") |
| 170 | 170 |
config := `{"HostConfig":` + hc + `}`
|
| 171 | 171 |
|
| 172 |
- res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json", daemonHost())
|
|
| 172 |
+ res, b, err := request.Post(daemonHost(), formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
|
|
| 173 | 173 |
c.Assert(err, checker.IsNil) |
| 174 | 174 |
c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) |
| 175 | 175 |
b.Close() |
| ... | ... |
@@ -187,7 +187,7 @@ func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfig(c * |
| 187 | 187 |
hc := inspectFieldJSON(c, name, "HostConfig") |
| 188 | 188 |
config := `{"HostConfig":` + hc + `}`
|
| 189 | 189 |
|
| 190 |
- res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json", daemonHost())
|
|
| 190 |
+ res, b, err := request.Post(daemonHost(), formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
|
|
| 191 | 191 |
c.Assert(err, checker.IsNil) |
| 192 | 192 |
c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) |
| 193 | 193 |
b.Close() |
| ... | ... |
@@ -205,7 +205,7 @@ func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfigIdLi |
| 205 | 205 |
hc := inspectFieldJSON(c, name, "HostConfig") |
| 206 | 206 |
config := `{"HostConfig":` + hc + `}`
|
| 207 | 207 |
|
| 208 |
- res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+name+"/start"), strings.NewReader(config), "application/json", daemonHost())
|
|
| 208 |
+ res, b, err := request.Post(daemonHost(), formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
|
|
| 209 | 209 |
c.Assert(err, checker.IsNil) |
| 210 | 210 |
c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) |
| 211 | 211 |
b.Close() |
| ... | ... |
@@ -219,7 +219,7 @@ func (s *DockerSuite) TestDeprecatedStartWithNilDNS(c *check.C) {
|
| 219 | 219 |
|
| 220 | 220 |
config := `{"HostConfig": {"Dns": null}}`
|
| 221 | 221 |
|
| 222 |
- res, b, err := request.SockRequestRaw("POST", formatV123StartAPIURL("/containers/"+containerID+"/start"), strings.NewReader(config), "application/json", daemonHost())
|
|
| 222 |
+ res, b, err := request.Post(daemonHost(), formatV123StartAPIURL("/containers/"+containerID+"/start"), request.RawString(config), request.JSON)
|
|
| 223 | 223 |
c.Assert(err, checker.IsNil) |
| 224 | 224 |
c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) |
| 225 | 225 |
b.Close() |
| ... | ... |
@@ -14,6 +14,7 @@ import ( |
| 14 | 14 |
"net/url" |
| 15 | 15 |
"os" |
| 16 | 16 |
"path/filepath" |
| 17 |
+ "strings" |
|
| 17 | 18 |
"time" |
| 18 | 19 |
|
| 19 | 20 |
dclient "github.com/docker/docker/client" |
| ... | ... |
@@ -32,10 +33,30 @@ func Method(method string) func(*http.Request) error {
|
| 32 | 32 |
} |
| 33 | 33 |
} |
| 34 | 34 |
|
| 35 |
+// RawString sets the specified string as body for the request |
|
| 36 |
+func RawString(content string) func(*http.Request) error {
|
|
| 37 |
+ return RawContent(ioutil.NopCloser(strings.NewReader(content))) |
|
| 38 |
+} |
|
| 39 |
+ |
|
| 40 |
+// RawContent sets the specified reader as body for the request |
|
| 41 |
+func RawContent(reader io.ReadCloser) func(*http.Request) error {
|
|
| 42 |
+ return func(req *http.Request) error {
|
|
| 43 |
+ req.Body = reader |
|
| 44 |
+ return nil |
|
| 45 |
+ } |
|
| 46 |
+} |
|
| 47 |
+ |
|
| 48 |
+// ContentType sets the specified Content-Type request header |
|
| 49 |
+func ContentType(contentType string) func(*http.Request) error {
|
|
| 50 |
+ return func(req *http.Request) error {
|
|
| 51 |
+ req.Header.Set("Content-Type", contentType)
|
|
| 52 |
+ return nil |
|
| 53 |
+ } |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 35 | 56 |
// JSON sets the Content-Type request header to json |
| 36 | 57 |
func JSON(req *http.Request) error {
|
| 37 |
- req.Header.Set("Content-Type", "application/json")
|
|
| 38 |
- return nil |
|
| 58 |
+ return ContentType("application/json")(req)
|
|
| 39 | 59 |
} |
| 40 | 60 |
|
| 41 | 61 |
// JSONBody creates a modifier that encodes the specified data to a JSON string and set it as request body. It also sets |