summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Goff <cpuguy83@gmail.com>2015-04-13 22:53:54 -0400
committerBrian Goff <cpuguy83@gmail.com>2015-04-16 13:40:49 -0400
commit8232cc777e329a47e123dbdc42411dae65288a80 (patch)
tree24b60f183f123cdd539db8ab8dfbb0b902b5ed5b
parent5224151da9cab6af4412d1dcdd81cf0cf76763f7 (diff)
downloaddocker-8232cc777e329a47e123dbdc42411dae65288a80.tar.gz
Make sockRequestRaw return reader, not []byte
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
-rw-r--r--integration-cli/docker_api_containers_test.go40
-rw-r--r--integration-cli/docker_utils.go28
2 files changed, 55 insertions, 13 deletions
diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go
index d48b945724..68561a587d 100644
--- a/integration-cli/docker_api_containers_test.go
+++ b/integration-cli/docker_api_containers_test.go
@@ -369,10 +369,15 @@ func TestBuildApiDockerfilePath(t *testing.T) {
t.Fatalf("failed to close tar archive: %v", err)
}
- _, out, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
+ _, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
if err == nil {
+ out, _ := readBody(body)
t.Fatalf("Build was supposed to fail: %s", out)
}
+ out, err := readBody(body)
+ if err != nil {
+ t.Fatal(err)
+ }
if !strings.Contains(string(out), "must be within the build context") {
t.Fatalf("Didn't complain about leaving build context: %s", out)
@@ -393,10 +398,14 @@ RUN find /tmp/`,
}
defer server.Close()
- _, buf, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
+ _, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
if err != nil {
t.Fatalf("Build failed: %s", err)
}
+ buf, err := readBody(body)
+ if err != nil {
+ t.Fatal(err)
+ }
// Make sure Dockerfile exists.
// Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
@@ -419,10 +428,15 @@ RUN echo from dockerfile`,
}
defer git.Close()
- _, buf, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
+ _, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
if err != nil {
+ buf, _ := readBody(body)
t.Fatalf("Build failed: %s\n%q", err, buf)
}
+ buf, err := readBody(body)
+ if err != nil {
+ t.Fatal(err)
+ }
out := string(buf)
if !strings.Contains(out, "from dockerfile") {
@@ -445,10 +459,15 @@ RUN echo from Dockerfile`,
defer git.Close()
// Make sure it tries to 'dockerfile' query param value
- _, buf, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
+ _, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
if err != nil {
+ buf, _ := readBody(body)
t.Fatalf("Build failed: %s\n%q", err, buf)
}
+ buf, err := readBody(body)
+ if err != nil {
+ t.Fatal(err)
+ }
out := string(buf)
if !strings.Contains(out, "from baz") {
@@ -472,10 +491,14 @@ RUN echo from dockerfile`,
defer git.Close()
// Make sure it tries to 'dockerfile' query param value
- _, buf, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
+ _, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
if err != nil {
t.Fatalf("Build failed: %s", err)
}
+ buf, err := readBody(body)
+ if err != nil {
+ t.Fatal(err)
+ }
out := string(buf)
if !strings.Contains(out, "from Dockerfile") {
@@ -503,10 +526,15 @@ func TestBuildApiDockerfileSymlink(t *testing.T) {
t.Fatalf("failed to close tar archive: %v", err)
}
- _, out, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
+ _, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
if err == nil {
+ out, _ := readBody(body)
t.Fatalf("Build was supposed to fail: %s", out)
}
+ out, err := readBody(body)
+ if err != nil {
+ t.Fatal(err)
+ }
// The reason the error is "Cannot locate specified Dockerfile" is because
// in the builder, the symlink is resolved within the context, therefore
diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go
index 4984b578bc..ab200e1248 100644
--- a/integration-cli/docker_utils.go
+++ b/integration-cli/docker_utils.go
@@ -22,6 +22,7 @@ import (
"time"
"github.com/docker/docker/api"
+ "github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/stringutils"
)
@@ -304,20 +305,27 @@ func sockRequest(method, endpoint string, data interface{}) (int, []byte, error)
return -1, nil, err
}
- return sockRequestRaw(method, endpoint, jsonData, "application/json")
+ status, body, err := sockRequestRaw(method, endpoint, jsonData, "application/json")
+ if err != nil {
+ b, _ := ioutil.ReadAll(body)
+ return status, b, err
+ }
+ var b []byte
+ b, err = readBody(body)
+ return status, b, err
}
-func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, []byte, error) {
+func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, 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)
}
client := httputil.NewClientConn(c, nil)
- defer client.Close()
req, err := http.NewRequest(method, endpoint, data)
if err != nil {
+ client.Close()
return -1, nil, fmt.Errorf("could not create new request: %v", err)
}
@@ -328,17 +336,23 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, []
resp, err := client.Do(req)
if err != nil {
+ client.Close()
return -1, nil, fmt.Errorf("could not perform request: %v", err)
}
- defer resp.Body.Close()
+ body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
+ defer client.Close()
+ return resp.Body.Close()
+ })
if resp.StatusCode != http.StatusOK {
- body, _ := ioutil.ReadAll(resp.Body)
return resp.StatusCode, body, fmt.Errorf("received status != 200 OK: %s", resp.Status)
}
- b, err := ioutil.ReadAll(resp.Body)
+ return resp.StatusCode, body, err
+}
- return resp.StatusCode, b, err
+func readBody(b io.ReadCloser) ([]byte, error) {
+ defer b.Close()
+ return ioutil.ReadAll(b)
}
func deleteContainer(container string) error {