Skip to content

Commit

Permalink
Ensure body is closed after error is checked
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <[email protected]>
  • Loading branch information
runcom committed Jul 23, 2015
1 parent d593130 commit 18faf6f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
12 changes: 8 additions & 4 deletions integration-cli/docker_api_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,10 @@ func (s *DockerSuite) TestBuildApiRemoteTarballContext(c *check.C) {

defer server.Close()

res, _, err := sockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar")
res, b, err := sockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar")
c.Assert(err, check.IsNil)
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
b.Close()
}

func (s *DockerSuite) TestBuildApiRemoteTarballContextWithCustomDockerfile(c *check.C) {
Expand Down Expand Up @@ -1648,9 +1649,10 @@ func (s *DockerSuite) TestPostContainersStartWithoutLinksInHostConfig(c *check.C
c.Assert(err, check.IsNil)
config := `{"HostConfig":` + hc + `}`

res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
c.Assert(err, check.IsNil)
c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
b.Close()
}

// #14640
Expand All @@ -1663,9 +1665,10 @@ func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfig(c *check.C) {
c.Assert(err, check.IsNil)
config := `{"HostConfig":` + hc + `}`

res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
c.Assert(err, check.IsNil)
c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
b.Close()
}

// #14640
Expand All @@ -1679,7 +1682,8 @@ func (s *DockerSuite) TestPostContainersStartWithLinksInHostConfigIdLinked(c *ch
c.Assert(err, check.IsNil)
config := `{"HostConfig":` + hc + `}`

res, _, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
res, b, err := sockRequestRaw("POST", "/containers/"+name+"/start", strings.NewReader(config), "application/json")
c.Assert(err, check.IsNil)
c.Assert(res.StatusCode, check.Equals, http.StatusNoContent)
b.Close()
}
2 changes: 1 addition & 1 deletion integration-cli/docker_api_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) {
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})
c.Assert(status, check.Equals, http.StatusInternalServerError)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusInternalServerError)

if !bytes.Contains(body, []byte("No exec command specified")) {
c.Fatalf("Expected message when creating exec command with no Cmd specified")
Expand Down
10 changes: 5 additions & 5 deletions integration-cli/docker_api_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (s *DockerSuite) TestApiImagesFilter(c *check.C) {
v := url.Values{}
v.Set("filter", filter)
status, b, err := sockRequest("GET", "/images/json?"+v.Encode(), nil)
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)

var images []image
if err := json.Unmarshal(b, &images); err != nil {
Expand Down Expand Up @@ -88,16 +88,16 @@ func (s *DockerSuite) TestApiImagesDelete(c *check.C) {
dockerCmd(c, "tag", name, "test:tag1")

status, _, err := sockRequest("DELETE", "/images/"+id, nil)
c.Assert(status, check.Equals, http.StatusConflict)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusConflict)

status, _, err = sockRequest("DELETE", "/images/test:noexist", nil)
c.Assert(status, check.Equals, http.StatusNotFound) //Status Codes:404 – no such image
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusNotFound) //Status Codes:404 – no such image

status, _, err = sockRequest("DELETE", "/images/test:tag1", nil)
c.Assert(status, check.Equals, http.StatusOK)
c.Assert(err, check.IsNil)
c.Assert(status, check.Equals, http.StatusOK)
}

func (s *DockerSuite) TestApiImagesHistory(c *check.C) {
Expand Down Expand Up @@ -126,8 +126,8 @@ func (s *DockerSuite) TestApiImagesSearchJSONContentType(c *check.C) {
testRequires(c, Network)

res, b, err := sockRequestRaw("GET", "/images/search?term=test", nil, "application/json")
b.Close()
c.Assert(err, check.IsNil)
b.Close()
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
c.Assert(res.Header.Get("Content-Type"), check.Equals, "application/json")
}
15 changes: 11 additions & 4 deletions integration-cli/docker_api_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ func (s *DockerSuite) TestLogsApiWithStdout(c *check.C) {

go func() {
res, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&timestamps=1", id), nil, "")
out, _ := bufio.NewReader(body).ReadString('\n')
if err != nil {
chLog <- logOut{"", nil, err}
return
}
defer body.Close()
out, err := bufio.NewReader(body).ReadString('\n')
if err != nil {
chLog <- logOut{"", nil, err}
return
}
chLog <- logOut{strings.TrimSpace(out), res, err}
}()

Expand Down Expand Up @@ -65,10 +74,8 @@ func (s *DockerSuite) TestLogsApiFollowEmptyOutput(c *check.C) {

_, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/logs?follow=1&stdout=1&stderr=1&tail=all", name), bytes.NewBuffer(nil), "")
t1 := time.Now()
c.Assert(err, check.IsNil)
body.Close()
if err != nil {
c.Fatal(err)
}
elapsed := t1.Sub(t0).Seconds()
if elapsed > 5.0 {
c.Fatalf("HTTP response was not immediate (elapsed %.1fs)", elapsed)
Expand Down
2 changes: 2 additions & 0 deletions integration-cli/docker_api_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) {
var v *types.Stats
err = json.NewDecoder(body).Decode(&v)
c.Assert(err, check.IsNil)
body.Close()

var cpuPercent = 0.0
cpuDelta := float64(v.CpuStats.CpuUsage.TotalUsage - v.PreCpuStats.CpuUsage.TotalUsage)
Expand Down Expand Up @@ -113,6 +114,7 @@ func getNetworkStats(c *check.C, id string) types.Network {

err = json.NewDecoder(body).Decode(&st)
c.Assert(err, check.IsNil)
body.Close()

return st.Network
}
2 changes: 1 addition & 1 deletion integration-cli/docker_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func (s *DockerSuite) TestApiOptionsRoute(c *check.C) {

func (s *DockerSuite) TestApiGetEnabledCors(c *check.C) {
res, body, err := sockRequestRaw("GET", "/version", nil, "")
body.Close()
c.Assert(err, check.IsNil)
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
body.Close()
// TODO: @runcom incomplete tests, why old integration tests had this headers
// and here none of the headers below are in the response?
//c.Log(res.Header)
Expand Down

0 comments on commit 18faf6f

Please sign in to comment.