From 18faf6f94e0995ac3fab703d22ca82baf4a65676 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Thu, 23 Jul 2015 13:24:14 +0200 Subject: [PATCH] Ensure body is closed after error is checked Signed-off-by: Antonio Murdaca --- integration-cli/docker_api_containers_test.go | 12 ++++++++---- integration-cli/docker_api_exec_test.go | 2 +- integration-cli/docker_api_images_test.go | 10 +++++----- integration-cli/docker_api_logs_test.go | 15 +++++++++++---- integration-cli/docker_api_stats_test.go | 2 ++ integration-cli/docker_api_test.go | 2 +- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 0bc25de30ea62..b0e9b0eed78b9 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -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) { @@ -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 @@ -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 @@ -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() } diff --git a/integration-cli/docker_api_exec_test.go b/integration-cli/docker_api_exec_test.go index c8cbbe709ed0f..3d99fe6b8e123 100644 --- a/integration-cli/docker_api_exec_test.go +++ b/integration-cli/docker_api_exec_test.go @@ -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") diff --git a/integration-cli/docker_api_images_test.go b/integration-cli/docker_api_images_test.go index 1536c0cd6549e..339a2f489e066 100644 --- a/integration-cli/docker_api_images_test.go +++ b/integration-cli/docker_api_images_test.go @@ -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 { @@ -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) { @@ -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") } diff --git a/integration-cli/docker_api_logs_test.go b/integration-cli/docker_api_logs_test.go index 7d2bb5a55b1bd..d47844757a99f 100644 --- a/integration-cli/docker_api_logs_test.go +++ b/integration-cli/docker_api_logs_test.go @@ -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×tamps=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} }() @@ -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) diff --git a/integration-cli/docker_api_stats_test.go b/integration-cli/docker_api_stats_test.go index 54d3331a8e198..f019e00dfb6d5 100644 --- a/integration-cli/docker_api_stats_test.go +++ b/integration-cli/docker_api_stats_test.go @@ -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) @@ -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 } diff --git a/integration-cli/docker_api_test.go b/integration-cli/docker_api_test.go index f3e3b1d9543f5..6cbf301955f39 100644 --- a/integration-cli/docker_api_test.go +++ b/integration-cli/docker_api_test.go @@ -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)