Merge pull request #4006 from milosgajdos/enable-bodyclose

Enable bodyclose linter
This commit is contained in:
Milos Gajdos 2023-08-21 09:34:35 +01:00 committed by GitHub
commit 03d926737a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 87 additions and 15 deletions

View file

@ -9,6 +9,7 @@ linters:
- vet - vet
- unused - unused
- misspell - misspell
- bodyclose
disable: disable:
- errcheck - errcheck

View file

@ -258,10 +258,11 @@ func TestRemoteAddr(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
_, err = http.DefaultClient.Do(proxyReq) resp, err := http.DefaultClient.Do(proxyReq)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
// RemoteAddr in X-Real-Ip // RemoteAddr in X-Real-Ip
getReq, err := http.NewRequest(http.MethodGet, backend.URL, nil) getReq, err := http.NewRequest(http.MethodGet, backend.URL, nil)
@ -271,15 +272,17 @@ func TestRemoteAddr(t *testing.T) {
expectedRemote = "1.2.3.4" expectedRemote = "1.2.3.4"
getReq.Header["X-Real-ip"] = []string{expectedRemote} getReq.Header["X-Real-ip"] = []string{expectedRemote}
_, err = http.DefaultClient.Do(getReq) resp, err = http.DefaultClient.Do(getReq)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
// Valid X-Real-Ip and invalid X-Forwarded-For // Valid X-Real-Ip and invalid X-Forwarded-For
getReq.Header["X-forwarded-for"] = []string{"1.2.3"} getReq.Header["X-forwarded-for"] = []string{"1.2.3"}
_, err = http.DefaultClient.Do(getReq) resp, err = http.DefaultClient.Do(getReq)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
} }

View file

@ -53,6 +53,7 @@ func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.H
if err != nil { if err != nil {
return errors.New("error while checking: " + r) return errors.New("error while checking: " + r)
} }
defer response.Body.Close()
if response.StatusCode != statusCode { if response.StatusCode != statusCode {
return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode)) return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode))
} }

View file

@ -168,6 +168,7 @@ func TestEndpointAuthorizeToken(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
@ -201,6 +202,7 @@ func TestEndpointAuthorizeToken(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized { if resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusUnauthorized) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusUnauthorized)
@ -297,6 +299,7 @@ func TestEndpointAuthorizeRefreshToken(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
@ -326,6 +329,7 @@ func TestEndpointAuthorizeRefreshToken(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusUnauthorized) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusUnauthorized)
@ -356,6 +360,7 @@ func TestEndpointAuthorizeRefreshToken(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized { if resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusUnauthorized) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusUnauthorized)
@ -434,6 +439,7 @@ func TestEndpointAuthorizeV2RefreshToken(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
@ -509,6 +515,7 @@ func TestEndpointAuthorizeTokenBasic(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
@ -644,6 +651,7 @@ func TestEndpointAuthorizeTokenBasicWithExpiresIn(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
} }
@ -659,6 +667,7 @@ func TestEndpointAuthorizeTokenBasicWithExpiresIn(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
} }
@ -796,6 +805,7 @@ func TestEndpointAuthorizeTokenBasicWithExpiresInAndIssuedAt(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
} }
@ -811,6 +821,7 @@ func TestEndpointAuthorizeTokenBasicWithExpiresInAndIssuedAt(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)
} }
@ -858,6 +869,7 @@ func TestEndpointAuthorizeBasic(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error sending get request: %s", err) t.Fatalf("Error sending get request: %s", err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted { if resp.StatusCode != http.StatusAccepted {
t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted) t.Fatalf("Unexpected status code: %d, expected %d", resp.StatusCode, http.StatusAccepted)

View file

@ -49,6 +49,7 @@ func (hbu *httpBlobUpload) ReadFrom(r io.Reader) (n int64, err error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer resp.Body.Close()
if !SuccessStatus(resp.StatusCode) { if !SuccessStatus(resp.StatusCode) {
return 0, hbu.handleErrorResponse(resp) return 0, hbu.handleErrorResponse(resp)
@ -84,6 +85,7 @@ func (hbu *httpBlobUpload) Write(p []byte) (n int, err error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer resp.Body.Close()
if !SuccessStatus(resp.StatusCode) { if !SuccessStatus(resp.StatusCode) {
return 0, hbu.handleErrorResponse(resp) return 0, hbu.handleErrorResponse(resp)

View file

@ -409,6 +409,7 @@ func (ms *manifests) Exists(ctx context.Context, dgst digest.Digest) (bool, erro
if err != nil { if err != nil {
return false, err return false, err
} }
defer resp.Body.Close()
if SuccessStatus(resp.StatusCode) { if SuccessStatus(resp.StatusCode) {
return true, nil return true, nil

View file

@ -746,6 +746,7 @@ func TestRelativeURL(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error doing layer push relative url: %v", err) t.Fatalf("unexpected error doing layer push relative url: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "relativeurl blob upload", resp, http.StatusCreated) checkResponse(t, "relativeurl blob upload", resp, http.StatusCreated)
u, err = url.Parse(resp.Header.Get("Location")) u, err = url.Parse(resp.Header.Get("Location"))
if err != nil { if err != nil {
@ -783,6 +784,7 @@ func TestRelativeURL(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error doing layer push relative url: %v", err) t.Fatalf("unexpected error doing layer push relative url: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "relativeurl blob upload", resp, http.StatusCreated) checkResponse(t, "relativeurl blob upload", resp, http.StatusCreated)
u, err = url.Parse(resp.Header.Get("Location")) u, err = url.Parse(resp.Header.Get("Location"))
@ -812,6 +814,7 @@ func TestBlobDeleteDisabled(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error deleting when disabled: %v", err) t.Fatalf("unexpected error deleting when disabled: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "status of disabled delete", resp, http.StatusMethodNotAllowed) checkResponse(t, "status of disabled delete", resp, http.StatusMethodNotAllowed)
} }
@ -836,6 +839,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error fetching non-existent layer: %v", err) t.Fatalf("unexpected error fetching non-existent layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching non-existent content", resp, http.StatusNotFound) checkResponse(t, "fetching non-existent content", resp, http.StatusNotFound)
@ -845,6 +849,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error checking head on non-existent layer: %v", err) t.Fatalf("unexpected error checking head on non-existent layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "checking head on non-existent layer", resp, http.StatusNotFound) checkResponse(t, "checking head on non-existent layer", resp, http.StatusNotFound)
@ -857,6 +862,8 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error getting upload status: %v", err) t.Fatalf("unexpected error getting upload status: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "status of deleted upload", resp, http.StatusNoContent) checkResponse(t, "status of deleted upload", resp, http.StatusNoContent)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{"*"}, "Location": []string{"*"},
@ -873,7 +880,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error sending delete request: %v", err) t.Fatalf("unexpected error sending delete request: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "deleting upload", resp, http.StatusNoContent) checkResponse(t, "deleting upload", resp, http.StatusNoContent)
// A status check should result in 404 // A status check should result in 404
@ -881,6 +888,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error getting upload status: %v", err) t.Fatalf("unexpected error getting upload status: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "status of deleted upload", resp, http.StatusNotFound) checkResponse(t, "status of deleted upload", resp, http.StatusNotFound)
// ----------------------------------------- // -----------------------------------------
@ -890,6 +898,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error doing bad layer push: %v", err) t.Fatalf("unexpected error doing bad layer push: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "bad layer push", resp, http.StatusBadRequest) checkResponse(t, "bad layer push", resp, http.StatusBadRequest)
checkBodyHasErrorCodes(t, "bad layer push", resp, v2.ErrorCodeDigestInvalid) checkBodyHasErrorCodes(t, "bad layer push", resp, v2.ErrorCodeDigestInvalid)
@ -983,7 +992,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error checking head on existing layer: %v", err) t.Fatalf("unexpected error checking head on existing layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "checking head on existing layer", resp, http.StatusOK) checkResponse(t, "checking head on existing layer", resp, http.StatusOK)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Content-Length": []string{fmt.Sprint(layerLength)}, "Content-Length": []string{fmt.Sprint(layerLength)},
@ -996,7 +1005,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error fetching layer: %v", err) t.Fatalf("unexpected error fetching layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching layer", resp, http.StatusOK) checkResponse(t, "fetching layer", resp, http.StatusOK)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Content-Length": []string{fmt.Sprint(layerLength)}, "Content-Length": []string{fmt.Sprint(layerLength)},
@ -1018,7 +1027,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error fetching layer: %v", err) t.Fatalf("unexpected error fetching layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching layer bad digest", resp, http.StatusBadRequest) checkResponse(t, "fetching layer bad digest", resp, http.StatusBadRequest)
// Cache headers // Cache headers
@ -1026,7 +1035,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("unexpected error fetching layer: %v", err) t.Fatalf("unexpected error fetching layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching layer", resp, http.StatusOK) checkResponse(t, "fetching layer", resp, http.StatusOK)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Content-Length": []string{fmt.Sprint(layerLength)}, "Content-Length": []string{fmt.Sprint(layerLength)},
@ -1047,7 +1056,7 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
if err != nil { if err != nil {
t.Fatalf("Error constructing request: %s", err) t.Fatalf("Error constructing request: %s", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching layer with etag", resp, http.StatusNotModified) checkResponse(t, "fetching layer with etag", resp, http.StatusNotModified)
// Non-matching etag, gives 200 // Non-matching etag, gives 200
@ -1056,7 +1065,11 @@ func testBlobAPI(t *testing.T, env *testEnv, args blobArgs) *testEnv {
t.Fatalf("Error constructing request: %s", err) t.Fatalf("Error constructing request: %s", err)
} }
req.Header.Set("If-None-Match", "") req.Header.Set("If-None-Match", "")
resp, _ = http.DefaultClient.Do(req) resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Error constructing request: %s", err)
}
defer resp.Body.Close()
checkResponse(t, "fetching layer with invalid etag", resp, http.StatusOK) checkResponse(t, "fetching layer with invalid etag", resp, http.StatusOK)
// Missing tests: // Missing tests:
@ -1082,6 +1095,7 @@ func testBlobDelete(t *testing.T, env *testEnv, args blobArgs) {
if err != nil { if err != nil {
t.Fatalf("unexpected error deleting layer: %v", err) t.Fatalf("unexpected error deleting layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "deleting layer", resp, http.StatusAccepted) checkResponse(t, "deleting layer", resp, http.StatusAccepted)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
@ -1095,6 +1109,7 @@ func testBlobDelete(t *testing.T, env *testEnv, args blobArgs) {
if err != nil { if err != nil {
t.Fatalf("unexpected error checking head on existing layer: %v", err) t.Fatalf("unexpected error checking head on existing layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "checking existence of deleted layer", resp, http.StatusNotFound) checkResponse(t, "checking existence of deleted layer", resp, http.StatusNotFound)
@ -1103,6 +1118,7 @@ func testBlobDelete(t *testing.T, env *testEnv, args blobArgs) {
if err != nil { if err != nil {
t.Fatalf("unexpected error deleting layer: %v", err) t.Fatalf("unexpected error deleting layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "deleting layer", resp, http.StatusNotFound) checkResponse(t, "deleting layer", resp, http.StatusNotFound)
@ -1113,6 +1129,7 @@ func testBlobDelete(t *testing.T, env *testEnv, args blobArgs) {
if err != nil { if err != nil {
t.Fatalf("unexpected error fetching layer: %v", err) t.Fatalf("unexpected error fetching layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "deleting layer bad digest", resp, http.StatusBadRequest) checkResponse(t, "deleting layer bad digest", resp, http.StatusBadRequest)
@ -1136,6 +1153,7 @@ func testBlobDelete(t *testing.T, env *testEnv, args blobArgs) {
if err != nil { if err != nil {
t.Fatalf("unexpected error checking head on existing layer: %v", err) t.Fatalf("unexpected error checking head on existing layer: %v", err)
} }
defer resp.Body.Close()
layerLength, _ := layerFile.Seek(0, io.SeekEnd) layerLength, _ := layerFile.Seek(0, io.SeekEnd)
checkResponse(t, "checking head on reuploaded layer", resp, http.StatusOK) checkResponse(t, "checking head on reuploaded layer", resp, http.StatusOK)
@ -1168,6 +1186,7 @@ func TestDeleteDisabled(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error deleting layer: %v", err) t.Fatalf("unexpected error deleting layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "deleting layer with delete disabled", resp, http.StatusMethodNotAllowed) checkResponse(t, "deleting layer with delete disabled", resp, http.StatusMethodNotAllowed)
} }
@ -1197,6 +1216,7 @@ func TestDeleteReadOnly(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unexpected error deleting layer: %v", err) t.Fatalf("unexpected error deleting layer: %v", err)
} }
defer resp.Body.Close()
checkResponse(t, "deleting layer in read-only mode", resp, http.StatusMethodNotAllowed) checkResponse(t, "deleting layer in read-only mode", resp, http.StatusMethodNotAllowed)
} }
@ -1232,7 +1252,7 @@ func httpDelete(url string) (*http.Response, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// defer resp.Body.Close()
return resp, err return resp, err
} }
@ -1288,6 +1308,7 @@ func TestManifestAPI_DeleteTag(t *testing.T) {
msg := "checking tag no longer exists" msg := "checking tag no longer exists"
resp, err = http.Get(u) resp, err = http.Get(u)
checkErr(t, err, msg) checkErr(t, err, msg)
defer resp.Body.Close()
checkResponse(t, msg, resp, http.StatusNotFound) checkResponse(t, msg, resp, http.StatusNotFound)
digestRef, err := reference.WithDigest(imageName, dgst) digestRef, err := reference.WithDigest(imageName, dgst)
@ -1299,6 +1320,7 @@ func TestManifestAPI_DeleteTag(t *testing.T) {
msg = "checking manifest still exists" msg = "checking manifest still exists"
resp, err = http.Head(u) resp, err = http.Head(u)
checkErr(t, err, msg) checkErr(t, err, msg)
defer resp.Body.Close()
checkResponse(t, msg, resp, http.StatusOK) checkResponse(t, msg, resp, http.StatusOK)
} }
@ -1640,6 +1662,7 @@ func testManifestAPISchema2(t *testing.T, env *testEnv, imageName reference.Name
checkErr(t, err, "building manifest url") checkErr(t, err, "building manifest url")
resp = putManifest(t, "putting manifest no error", manifestURL, schema2.MediaTypeManifest, manifest) resp = putManifest(t, "putting manifest no error", manifestURL, schema2.MediaTypeManifest, manifest)
defer resp.Body.Close()
checkResponse(t, "putting manifest no error", resp, http.StatusCreated) checkResponse(t, "putting manifest no error", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -1649,6 +1672,7 @@ func testManifestAPISchema2(t *testing.T, env *testEnv, imageName reference.Name
// -------------------- // --------------------
// Push by digest -- should get same result // Push by digest -- should get same result
resp = putManifest(t, "putting manifest by digest", manifestDigestURL, schema2.MediaTypeManifest, manifest) resp = putManifest(t, "putting manifest by digest", manifestDigestURL, schema2.MediaTypeManifest, manifest)
defer resp.Body.Close()
checkResponse(t, "putting manifest by digest", resp, http.StatusCreated) checkResponse(t, "putting manifest by digest", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -1733,6 +1757,7 @@ func testManifestAPISchema2(t *testing.T, env *testEnv, imageName reference.Name
if err != nil { if err != nil {
t.Fatalf("Error constructing request: %s", err) t.Fatalf("Error constructing request: %s", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching manifest by name with etag", resp, http.StatusNotModified) checkResponse(t, "fetching manifest by name with etag", resp, http.StatusNotModified)
@ -1746,6 +1771,7 @@ func testManifestAPISchema2(t *testing.T, env *testEnv, imageName reference.Name
if err != nil { if err != nil {
t.Fatalf("Error constructing request: %s", err) t.Fatalf("Error constructing request: %s", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching manifest by dgst with etag", resp, http.StatusNotModified) checkResponse(t, "fetching manifest by dgst with etag", resp, http.StatusNotModified)
@ -1843,6 +1869,7 @@ func testManifestAPIManifestList(t *testing.T, env *testEnv, args manifestArgs)
checkErr(t, err, "building manifest url") checkErr(t, err, "building manifest url")
resp = putManifest(t, "putting manifest list no error", manifestURL, manifestlist.MediaTypeManifestList, deserializedManifestList) resp = putManifest(t, "putting manifest list no error", manifestURL, manifestlist.MediaTypeManifestList, deserializedManifestList)
defer resp.Body.Close()
checkResponse(t, "putting manifest list no error", resp, http.StatusCreated) checkResponse(t, "putting manifest list no error", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -1852,6 +1879,7 @@ func testManifestAPIManifestList(t *testing.T, env *testEnv, args manifestArgs)
// -------------------- // --------------------
// Push by digest -- should get same result // Push by digest -- should get same result
resp = putManifest(t, "putting manifest list by digest", manifestDigestURL, manifestlist.MediaTypeManifestList, deserializedManifestList) resp = putManifest(t, "putting manifest list by digest", manifestDigestURL, manifestlist.MediaTypeManifestList, deserializedManifestList)
defer resp.Body.Close()
checkResponse(t, "putting manifest list by digest", resp, http.StatusCreated) checkResponse(t, "putting manifest list by digest", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -1938,6 +1966,7 @@ func testManifestAPIManifestList(t *testing.T, env *testEnv, args manifestArgs)
if err != nil { if err != nil {
t.Fatalf("Error constructing request: %s", err) t.Fatalf("Error constructing request: %s", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching manifest by name with etag", resp, http.StatusNotModified) checkResponse(t, "fetching manifest by name with etag", resp, http.StatusNotModified)
@ -1951,6 +1980,7 @@ func testManifestAPIManifestList(t *testing.T, env *testEnv, args manifestArgs)
if err != nil { if err != nil {
t.Fatalf("Error constructing request: %s", err) t.Fatalf("Error constructing request: %s", err)
} }
defer resp.Body.Close()
checkResponse(t, "fetching manifest by dgst with etag", resp, http.StatusNotModified) checkResponse(t, "fetching manifest by dgst with etag", resp, http.StatusNotModified)
} }
@ -1966,6 +1996,7 @@ func testManifestDelete(t *testing.T, env *testEnv, args manifestArgs) {
// Delete by digest // Delete by digest
resp, err := httpDelete(manifestDigestURL) resp, err := httpDelete(manifestDigestURL)
checkErr(t, err, "deleting manifest by digest") checkErr(t, err, "deleting manifest by digest")
defer resp.Body.Close()
checkResponse(t, "deleting manifest", resp, http.StatusAccepted) checkResponse(t, "deleting manifest", resp, http.StatusAccepted)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
@ -1984,12 +2015,14 @@ func testManifestDelete(t *testing.T, env *testEnv, args manifestArgs) {
// Delete already deleted manifest by digest // Delete already deleted manifest by digest
resp, err = httpDelete(manifestDigestURL) resp, err = httpDelete(manifestDigestURL)
checkErr(t, err, "re-deleting manifest by digest") checkErr(t, err, "re-deleting manifest by digest")
defer resp.Body.Close()
checkResponse(t, "re-deleting manifest", resp, http.StatusNotFound) checkResponse(t, "re-deleting manifest", resp, http.StatusNotFound)
// -------------------- // --------------------
// Re-upload manifest by digest // Re-upload manifest by digest
resp = putManifest(t, "putting manifest", manifestDigestURL, args.mediaType, manifest) resp = putManifest(t, "putting manifest", manifestDigestURL, args.mediaType, manifest)
defer resp.Body.Close()
checkResponse(t, "putting manifest", resp, http.StatusCreated) checkResponse(t, "putting manifest", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -2016,6 +2049,7 @@ func testManifestDelete(t *testing.T, env *testEnv, args manifestArgs) {
resp, err = httpDelete(unknownManifestDigestURL) resp, err = httpDelete(unknownManifestDigestURL)
checkErr(t, err, "delting unknown manifest by digest") checkErr(t, err, "delting unknown manifest by digest")
defer resp.Body.Close()
checkResponse(t, "fetching deleted manifest", resp, http.StatusNotFound) checkResponse(t, "fetching deleted manifest", resp, http.StatusNotFound)
// -------------------- // --------------------
@ -2024,6 +2058,7 @@ func testManifestDelete(t *testing.T, env *testEnv, args manifestArgs) {
tagRef, _ := reference.WithTag(imageName, tag) tagRef, _ := reference.WithTag(imageName, tag)
manifestTagURL, _ := env.builder.BuildManifestURL(tagRef) manifestTagURL, _ := env.builder.BuildManifestURL(tagRef)
resp = putManifest(t, "putting manifest by tag", manifestTagURL, args.mediaType, manifest) resp = putManifest(t, "putting manifest by tag", manifestTagURL, args.mediaType, manifest)
defer resp.Body.Close()
checkResponse(t, "putting manifest by tag", resp, http.StatusCreated) checkResponse(t, "putting manifest by tag", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -2064,6 +2099,7 @@ func testManifestDelete(t *testing.T, env *testEnv, args manifestArgs) {
// Delete by digest // Delete by digest
resp, err = httpDelete(manifestDigestURL) resp, err = httpDelete(manifestDigestURL)
checkErr(t, err, "deleting manifest by digest") checkErr(t, err, "deleting manifest by digest")
defer resp.Body.Close()
checkResponse(t, "deleting manifest with tag", resp, http.StatusAccepted) checkResponse(t, "deleting manifest with tag", resp, http.StatusAccepted)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
@ -2415,7 +2451,6 @@ func checkResponse(t *testing.T, msg string, resp *http.Response, expectedStatus
if resp.StatusCode != 405 && !reflect.DeepEqual(resp.Header["X-Content-Type-Options"], []string{"nosniff"}) { if resp.StatusCode != 405 && !reflect.DeepEqual(resp.Header["X-Content-Type-Options"], []string{"nosniff"}) {
t.Logf("missing or incorrect header X-Content-Type-Options %s", msg) t.Logf("missing or incorrect header X-Content-Type-Options %s", msg)
maybeDumpResponse(t, resp) maybeDumpResponse(t, resp)
t.FailNow() t.FailNow()
} }
} }
@ -2598,6 +2633,7 @@ func createRepository(env *testEnv, t *testing.T, imageName string, tag string)
checkErr(t, err, "building manifest url") checkErr(t, err, "building manifest url")
resp := putManifest(t, "putting manifest no error", manifestURL, schema2.MediaTypeManifest, manifest) resp := putManifest(t, "putting manifest no error", manifestURL, schema2.MediaTypeManifest, manifest)
defer resp.Body.Close()
checkResponse(t, "putting manifest no error", resp, http.StatusCreated) checkResponse(t, "putting manifest no error", resp, http.StatusCreated)
checkHeaders(t, resp, http.Header{ checkHeaders(t, resp, http.Header{
"Location": []string{manifestDigestURL}, "Location": []string{manifestDigestURL},
@ -2648,10 +2684,15 @@ func TestRegistryAsCacheMutationAPIs(t *testing.T) {
} }
resp := putManifest(t, "putting missing config manifest", manifestURL, schema2.MediaTypeManifest, manifest) resp := putManifest(t, "putting missing config manifest", manifestURL, schema2.MediaTypeManifest, manifest)
defer resp.Body.Close()
checkResponse(t, "putting missing config manifest", resp, errcode.ErrorCodeUnsupported.Descriptor().HTTPStatusCode) checkResponse(t, "putting missing config manifest", resp, errcode.ErrorCodeUnsupported.Descriptor().HTTPStatusCode)
// Manifest Delete // Manifest Delete
resp, _ = httpDelete(manifestURL) resp, err = httpDelete(manifestURL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()
checkResponse(t, "deleting config manifest from cache", resp, errcode.ErrorCodeUnsupported.Descriptor().HTTPStatusCode) checkResponse(t, "deleting config manifest from cache", resp, errcode.ErrorCodeUnsupported.Descriptor().HTTPStatusCode)
// Blob upload initialization // Blob upload initialization
@ -2671,7 +2712,11 @@ func TestRegistryAsCacheMutationAPIs(t *testing.T) {
// Blob Delete // Blob Delete
ref, _ := reference.WithDigest(imageName, digestSha256EmptyTar) ref, _ := reference.WithDigest(imageName, digestSha256EmptyTar)
blobURL, _ := env.builder.BuildBlobURL(ref) blobURL, _ := env.builder.BuildBlobURL(ref)
resp, _ = httpDelete(blobURL) resp, err = httpDelete(blobURL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()
checkResponse(t, "deleting blob from cache", resp, errcode.ErrorCodeUnsupported.Descriptor().HTTPStatusCode) checkResponse(t, "deleting blob from cache", resp, errcode.ErrorCodeUnsupported.Descriptor().HTTPStatusCode)
} }

View file

@ -128,6 +128,7 @@ func TestAppDispatcher(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %v != %v", resp.StatusCode, http.StatusOK) t.Fatalf("unexpected status code: %v != %v", resp.StatusCode, http.StatusOK)

View file

@ -118,6 +118,7 @@ func TestGracefulShutdown(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
if resp.Status != "200 OK" { if resp.Status != "200 OK" {
t.Error("response status is not 200 OK: ", resp.Status) t.Error("response status is not 200 OK: ", resp.Status)
} }
@ -313,6 +314,7 @@ func TestRegistrySupportedCipherSuite(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
if resp.Status != "200 OK" { if resp.Status != "200 OK" {
t.Error("response status is not 200 OK: ", resp.Status) t.Error("response status is not 200 OK: ", resp.Status)
} }

View file

@ -67,6 +67,8 @@ func fetchAWSIPs(url string) (awsIPResponse, error) {
if err != nil { if err != nil {
return response, err return response, err
} }
defer resp.Body.Close()
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
return response, fmt.Errorf("failed to fetch network data. response = %s", body) return response, fmt.Errorf("failed to fetch network data. response = %s", body)

View file

@ -656,7 +656,9 @@ func (suite *DriverSuite) TestURLFor(c *check.C) {
} }
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
response, _ = http.Head(url) response, err = http.Head(url)
c.Assert(err, check.IsNil)
defer response.Body.Close()
c.Assert(response.StatusCode, check.Equals, 200) c.Assert(response.StatusCode, check.Equals, 200)
c.Assert(response.ContentLength, check.Equals, int64(32)) c.Assert(response.ContentLength, check.Equals, int64(32))
} }