From f8b3af78fc59c56edf318bdb763129ab45b07b54 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 2 Nov 2022 22:55:22 +0100 Subject: [PATCH] replace deprecated io/ioutil Signed-off-by: Sebastiaan van Stijn --- configuration/configuration.go | 3 +- registry/auth/htpasswd/access_test.go | 10 +- registry/auth/token/accesscontroller.go | 4 +- registry/auth/token/token_test.go | 3 +- registry/client/blob_writer.go | 3 +- registry/client/errors.go | 3 +- registry/client/repository.go | 9 +- registry/client/repository_test.go | 5 +- registry/handlers/api_test.go | 9 +- registry/handlers/health_test.go | 3 +- registry/proxy/proxyblobstore_test.go | 14 +-- registry/registry.go | 3 +- registry/registry_test.go | 6 +- registry/storage/blob_test.go | 7 +- registry/storage/driver/azure/azure.go | 5 +- registry/storage/driver/filesystem/driver.go | 3 +- .../storage/driver/filesystem/driver_test.go | 7 +- registry/storage/driver/gcs/gcs.go | 10 +- registry/storage/driver/gcs/gcs_test.go | 27 +---- registry/storage/driver/inmemory/driver.go | 5 +- .../middleware/cloudfront/middleware.go | 4 +- .../middleware/cloudfront/middleware_test.go | 3 +- .../driver/middleware/cloudfront/s3filter.go | 4 +- registry/storage/driver/oss/oss.go | 3 +- registry/storage/driver/oss/oss_test.go | 9 +- registry/storage/driver/s3-aws/s3.go | 7 +- registry/storage/driver/s3-aws/s3_test.go | 106 ++++++------------ registry/storage/driver/swift/swift.go | 3 +- registry/storage/driver/swift/swift_test.go | 9 +- .../storage/driver/testsuites/testsuites.go | 19 ++-- registry/storage/filereader.go | 3 +- registry/storage/io.go | 3 +- testutil/handler.go | 3 +- 33 files changed, 113 insertions(+), 202 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index ad7b5009..c0d799f3 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "reflect" "strings" @@ -654,7 +653,7 @@ type Proxy struct { // Configuration.Abc may be replaced by the value of REGISTRY_ABC, // Configuration.Abc.Xyz may be replaced by the value of REGISTRY_ABC_XYZ, and so forth func Parse(rd io.Reader) (*Configuration, error) { - in, err := ioutil.ReadAll(rd) + in, err := io.ReadAll(rd) if err != nil { return nil, err } diff --git a/registry/auth/htpasswd/access_test.go b/registry/auth/htpasswd/access_test.go index 9253ccd2..25947757 100644 --- a/registry/auth/htpasswd/access_test.go +++ b/registry/auth/htpasswd/access_test.go @@ -2,7 +2,7 @@ package htpasswd import ( "bytes" - "io/ioutil" + "io" "net/http" "net/http/httptest" "os" @@ -21,7 +21,7 @@ func TestBasicAccessController(t *testing.T) { MiShil:$2y$05$0oHgwMehvoe8iAWS8I.7l.KoECXrwVaC16RPfaSCU5eVTFrATuMI2 DeokMan:공주님` - tempFile, err := ioutil.TempFile("", "htpasswd-test") + tempFile, err := os.CreateTemp("", "htpasswd-test") if err != nil { t.Fatal("could not create temporary htpasswd file") } @@ -122,7 +122,7 @@ func TestBasicAccessController(t *testing.T) { } func TestCreateHtpasswdFile(t *testing.T) { - tempFile, err := ioutil.TempFile("", "htpasswd-test") + tempFile, err := os.CreateTemp("", "htpasswd-test") if err != nil { t.Fatalf("could not create temporary htpasswd file %v", err) } @@ -135,7 +135,7 @@ func TestCreateHtpasswdFile(t *testing.T) { if _, err := newAccessController(options); err != nil { t.Fatalf("error creating access controller %v", err) } - content, err := ioutil.ReadAll(tempFile) + content, err := io.ReadAll(tempFile) if err != nil { t.Fatalf("failed to read file %v", err) } @@ -150,7 +150,7 @@ func TestCreateHtpasswdFile(t *testing.T) { if _, err := newAccessController(options); err != nil { t.Fatalf("error creating access controller %v", err) } - content, err = ioutil.ReadFile(tempFile.Name()) + content, err = os.ReadFile(tempFile.Name()) if err != nil { t.Fatalf("failed to read file %v", err) } diff --git a/registry/auth/token/accesscontroller.go b/registry/auth/token/accesscontroller.go index ec001ad1..e56a4ccd 100644 --- a/registry/auth/token/accesscontroller.go +++ b/registry/auth/token/accesscontroller.go @@ -7,7 +7,7 @@ import ( "encoding/pem" "errors" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -187,7 +187,7 @@ func newAccessController(options map[string]interface{}) (auth.AccessController, } defer fp.Close() - rawCertBundle, err := ioutil.ReadAll(fp) + rawCertBundle, err := io.ReadAll(fp) if err != nil { return nil, fmt.Errorf("unable to read token auth root certificate bundle file %q: %s", config.rootCertBundle, err) } diff --git a/registry/auth/token/token_test.go b/registry/auth/token/token_test.go index 3d80fa2e..7fa62be5 100644 --- a/registry/auth/token/token_test.go +++ b/registry/auth/token/token_test.go @@ -8,7 +8,6 @@ import ( "encoding/json" "encoding/pem" "fmt" - "io/ioutil" "net/http" "os" "strings" @@ -285,7 +284,7 @@ func writeTempRootCerts(rootKeys []libtrust.PrivateKey) (filename string, err er return "", err } - tempFile, err := ioutil.TempFile("", "rootCertBundle") + tempFile, err := os.CreateTemp("", "rootCertBundle") if err != nil { return "", err } diff --git a/registry/client/blob_writer.go b/registry/client/blob_writer.go index cbda181b..7b467d84 100644 --- a/registry/client/blob_writer.go +++ b/registry/client/blob_writer.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "time" @@ -38,7 +37,7 @@ func (hbu *httpBlobUpload) handleErrorResponse(resp *http.Response) error { } func (hbu *httpBlobUpload) ReadFrom(r io.Reader) (n int64, err error) { - req, err := http.NewRequestWithContext(hbu.ctx, http.MethodPatch, hbu.location, ioutil.NopCloser(r)) + req, err := http.NewRequestWithContext(hbu.ctx, http.MethodPatch, hbu.location, io.NopCloser(r)) if err != nil { return 0, err } diff --git a/registry/client/errors.go b/registry/client/errors.go index 7e5d1236..68bfe19d 100644 --- a/registry/client/errors.go +++ b/registry/client/errors.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "github.com/distribution/distribution/v3/registry/api/errcode" @@ -40,7 +39,7 @@ func (e *UnexpectedHTTPResponseError) Error() string { func parseHTTPErrorResponse(statusCode int, r io.Reader) error { var errors errcode.Errors - body, err := ioutil.ReadAll(r) + body, err := io.ReadAll(r) if err != nil { return err } diff --git a/registry/client/repository.go b/registry/client/repository.go index 81adaac5..10c6fe3f 100644 --- a/registry/client/repository.go +++ b/registry/client/repository.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strconv" @@ -227,7 +226,7 @@ func (t *tags) All(ctx context.Context) ([]string, error) { defer resp.Body.Close() if SuccessStatus(resp.StatusCode) { - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { return tags, err } @@ -268,7 +267,7 @@ func descriptorFromResponse(response *http.Response) (distribution.Descriptor, e digestHeader := headers.Get("Docker-Content-Digest") if digestHeader == "" { - data, err := ioutil.ReadAll(response.Body) + data, err := io.ReadAll(response.Body) if err != nil { return distribution.Descriptor{}, err } @@ -527,7 +526,7 @@ func (ms *manifests) Get(ctx context.Context, dgst digest.Digest, options ...dis } } mt := resp.Header.Get("Content-Type") - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -674,7 +673,7 @@ func (bs *blobs) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) { } defer reader.Close() - return ioutil.ReadAll(reader) + return io.ReadAll(reader) } func (bs *blobs) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) { diff --git a/registry/client/repository_test.go b/registry/client/repository_test.go index b4b527cb..4e926de7 100644 --- a/registry/client/repository_test.go +++ b/registry/client/repository_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "net/http" "net/http/httptest" @@ -126,7 +125,7 @@ func TestBlobServeBlob(t *testing.T) { t.Errorf("Error serving blob: %s", err.Error()) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Errorf("Error reading response body: %s", err.Error()) } @@ -175,7 +174,7 @@ func TestBlobServeBlobHEAD(t *testing.T) { t.Errorf("Error serving blob: %s", err.Error()) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Errorf("Error reading response body: %s", err.Error()) } diff --git a/registry/handlers/api_test.go b/registry/handlers/api_test.go index 2822e071..dcbc1de6 100644 --- a/registry/handlers/api_test.go +++ b/registry/handlers/api_test.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "net/http/httputil" @@ -69,7 +68,7 @@ func TestCheckAPI(t *testing.T) { "Content-Length": []string{"2"}, }) - p, err := ioutil.ReadAll(resp.Body) + p, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("unexpected error reading response body: %v", err) } @@ -1857,7 +1856,7 @@ func testManifestAPISchema2(t *testing.T, env *testEnv, imageName reference.Name } defer resp.Body.Close() - manifestBytes, err := ioutil.ReadAll(resp.Body) + manifestBytes, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("error reading response body: %v", err) } @@ -2091,7 +2090,7 @@ func testManifestAPIManifestList(t *testing.T, env *testEnv, args manifestArgs) } defer resp.Body.Close() - manifestBytes, err := ioutil.ReadAll(resp.Body) + manifestBytes, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("error reading response body: %v", err) } @@ -2614,7 +2613,7 @@ func checkResponse(t *testing.T, msg string, resp *http.Response, expectedStatus // expected error codes, returning the error structure, the json slice and a // count of the errors by code. func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, errorCodes ...errcode.ErrorCode) (errcode.Errors, []byte, map[errcode.ErrorCode]int) { - p, err := ioutil.ReadAll(resp.Body) + p, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("unexpected error reading body %s: %v", msg, err) } diff --git a/registry/handlers/health_test.go b/registry/handlers/health_test.go index 9b28ebfb..e0d61555 100644 --- a/registry/handlers/health_test.go +++ b/registry/handlers/health_test.go @@ -1,7 +1,6 @@ package handlers import ( - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -17,7 +16,7 @@ import ( func TestFileHealthCheck(t *testing.T) { interval := time.Second - tmpfile, err := ioutil.TempFile(os.TempDir(), "healthcheck") + tmpfile, err := os.CreateTemp(os.TempDir(), "healthcheck") if err != nil { t.Fatalf("could not create temporary file: %v", err) } diff --git a/registry/proxy/proxyblobstore_test.go b/registry/proxy/proxyblobstore_test.go index 9b0c1c20..386bd41f 100644 --- a/registry/proxy/proxyblobstore_test.go +++ b/registry/proxy/proxyblobstore_test.go @@ -2,7 +2,6 @@ package proxy import ( "context" - "io/ioutil" "math/rand" "net/http" "net/http/httptest" @@ -114,6 +113,8 @@ func (te *testEnv) RemoteStats() *map[string]int { // Populate remote store and record the digests func makeTestEnv(t *testing.T, name string) *testEnv { + t.Helper() + nameRef, err := reference.WithName(name) if err != nil { t.Fatalf("unable to parse reference: %s", err) @@ -121,15 +122,8 @@ func makeTestEnv(t *testing.T, name string) *testEnv { ctx := context.Background() - truthDir, err := ioutil.TempDir("", "truth") - if err != nil { - t.Fatalf("unable to create tempdir: %s", err) - } - - cacheDir, err := ioutil.TempDir("", "cache") - if err != nil { - t.Fatalf("unable to create tempdir: %s", err) - } + truthDir := t.TempDir() + cacheDir := t.TempDir() localDriver, err := filesystem.FromParameters(map[string]interface{}{ "rootdirectory": truthDir, diff --git a/registry/registry.go b/registry/registry.go index 8eebe6a4..e7fe1699 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "net/http" "os" "os/signal" @@ -268,7 +267,7 @@ func (registry *Registry) ListenAndServe() error { pool := x509.NewCertPool() for _, ca := range config.HTTP.TLS.ClientCAs { - caPem, err := ioutil.ReadFile(ca) + caPem, err := os.ReadFile(ca) if err != nil { return err } diff --git a/registry/registry_test.go b/registry/registry_test.go index c8800b93..5a7210ff 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -13,7 +13,7 @@ import ( "crypto/x509/pkix" "encoding/pem" "fmt" - "io/ioutil" + "io" "math/big" "net" "net/http" @@ -121,7 +121,7 @@ func TestGracefulShutdown(t *testing.T) { if resp.Status != "200 OK" { t.Error("response status is not 200 OK: ", resp.Status) } - if body, err := ioutil.ReadAll(resp.Body); err != nil || string(body) != "{}" { + if body, err := io.ReadAll(resp.Body); err != nil || string(body) != "{}" { t.Error("Body is not {}; ", string(body)) } } @@ -316,7 +316,7 @@ func TestRegistrySupportedCipherSuite(t *testing.T) { if resp.Status != "200 OK" { t.Error("response status is not 200 OK: ", resp.Status) } - if body, err := ioutil.ReadAll(resp.Body); err != nil || string(body) != "{}" { + if body, err := io.ReadAll(resp.Body); err != nil || string(body) != "{}" { t.Error("Body is not {}; ", string(body)) } diff --git a/registry/storage/blob_test.go b/registry/storage/blob_test.go index b0f88ac1..6df9e700 100644 --- a/registry/storage/blob_test.go +++ b/registry/storage/blob_test.go @@ -6,7 +6,6 @@ import ( "crypto/sha256" "fmt" "io" - "io/ioutil" "path" "reflect" "testing" @@ -210,7 +209,7 @@ func TestSimpleBlobUpload(t *testing.T) { } // Re-upload the blob - randomBlob, err := ioutil.ReadAll(randomDataReader) + randomBlob, err := io.ReadAll(randomDataReader) if err != nil { t.Fatalf("Error reading all of blob %s", err.Error()) } @@ -328,7 +327,7 @@ func TestSimpleBlobRead(t *testing.T) { t.Fatalf("seek failed: expected 0 offset, got %d", offset) } - p, err := ioutil.ReadAll(rc) + p, err := io.ReadAll(rc) if err != nil { t.Fatalf("error reading all of blob: %v", err) } @@ -343,7 +342,7 @@ func TestSimpleBlobRead(t *testing.T) { t.Fatalf("error resetting layer reader: %v", err) } - randomLayerData, err := ioutil.ReadAll(randomLayerReader) + randomLayerData, err := io.ReadAll(randomLayerReader) if err != nil { t.Fatalf("random layer read failed: %v", err) } diff --git a/registry/storage/driver/azure/azure.go b/registry/storage/driver/azure/azure.go index 8ef753dc..05b95e47 100644 --- a/registry/storage/driver/azure/azure.go +++ b/registry/storage/driver/azure/azure.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "strings" "time" @@ -115,7 +114,7 @@ func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) { } defer blob.Close() - return ioutil.ReadAll(blob) + return io.ReadAll(blob) } // PutContent stores the []byte content at a location designated by "path". @@ -172,7 +171,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read info := blobRef.Properties size := info.ContentLength if offset >= size { - return ioutil.NopCloser(bytes.NewReader(nil)), nil + return io.NopCloser(bytes.NewReader(nil)), nil } resp, err := blobRef.GetRange(&azure.GetBlobRangeOptions{ diff --git a/registry/storage/driver/filesystem/driver.go b/registry/storage/driver/filesystem/driver.go index c0cae7ca..3db3ebff 100644 --- a/registry/storage/driver/filesystem/driver.go +++ b/registry/storage/driver/filesystem/driver.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "path" "time" @@ -123,7 +122,7 @@ func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) { } defer rc.Close() - p, err := ioutil.ReadAll(rc) + p, err := io.ReadAll(rc) if err != nil { return nil, err } diff --git a/registry/storage/driver/filesystem/driver_test.go b/registry/storage/driver/filesystem/driver_test.go index 2203625f..3f56f168 100644 --- a/registry/storage/driver/filesystem/driver_test.go +++ b/registry/storage/driver/filesystem/driver_test.go @@ -1,7 +1,6 @@ package filesystem import ( - "io/ioutil" "os" "reflect" "testing" @@ -15,13 +14,13 @@ import ( func Test(t *testing.T) { TestingT(t) } func init() { - root, err := ioutil.TempDir("", "driver-") + root, err := os.MkdirTemp("", "driver-") if err != nil { panic(err) } defer os.Remove(root) - driver, err := FromParameters(map[string]interface{}{ + drvr, err := FromParameters(map[string]interface{}{ "rootdirectory": root, }) if err != nil { @@ -29,7 +28,7 @@ func init() { } testsuites.RegisterSuite(func() (storagedriver.StorageDriver, error) { - return driver, nil + return drvr, nil }, testsuites.NeverSkip) } diff --git a/registry/storage/driver/gcs/gcs.go b/registry/storage/driver/gcs/gcs.go index abc32be8..161fdf84 100644 --- a/registry/storage/driver/gcs/gcs.go +++ b/registry/storage/driver/gcs/gcs.go @@ -21,10 +21,10 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/rand" "net/http" "net/url" + "os" "reflect" "regexp" "sort" @@ -151,7 +151,7 @@ func FromParameters(parameters map[string]interface{}) (storagedriver.StorageDri var ts oauth2.TokenSource jwtConf := new(jwt.Config) if keyfile, ok := parameters["keyfile"]; ok { - jsonKey, err := ioutil.ReadFile(fmt.Sprint(keyfile)) + jsonKey, err := os.ReadFile(fmt.Sprint(keyfile)) if err != nil { return nil, err } @@ -263,7 +263,7 @@ func (d *driver) GetContent(context context.Context, path string) ([]byte, error } defer rc.Close() - p, err := ioutil.ReadAll(rc) + p, err := io.ReadAll(rc) if err != nil { return nil, err } @@ -299,7 +299,7 @@ func (d *driver) Reader(context context.Context, path string, offset int64) (io. return nil, err } if offset == int64(obj.Size) { - return ioutil.NopCloser(bytes.NewReader([]byte{})), nil + return io.NopCloser(bytes.NewReader([]byte{})), nil } return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset} } @@ -557,7 +557,7 @@ func (w *writer) init(path string) error { if err != nil { return err } - buffer, err := ioutil.ReadAll(res.Body) + buffer, err := io.ReadAll(res.Body) if err != nil { return err } diff --git a/registry/storage/driver/gcs/gcs_test.go b/registry/storage/driver/gcs/gcs_test.go index 1f13c3c9..c4271cbd 100644 --- a/registry/storage/driver/gcs/gcs_test.go +++ b/registry/storage/driver/gcs/gcs_test.go @@ -5,7 +5,6 @@ package gcs import ( "fmt" - "io/ioutil" "os" "testing" @@ -43,7 +42,7 @@ func init() { return } - root, err := ioutil.TempDir("", "driver-") + root, err := os.MkdirTemp("", "driver-") if err != nil { panic(err) } @@ -95,11 +94,7 @@ func TestCommitEmpty(t *testing.T) { t.Skip(skipGCS()) } - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) + validRoot := t.TempDir() driver, err := gcsDriverConstructor(validRoot) if err != nil { @@ -141,11 +136,7 @@ func TestCommit(t *testing.T) { t.Skip(skipGCS()) } - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) + validRoot := t.TempDir() driver, err := gcsDriverConstructor(validRoot) if err != nil { @@ -227,11 +218,7 @@ func TestEmptyRootList(t *testing.T) { t.Skip(skipGCS()) } - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) + validRoot := t.TempDir() rootedDriver, err := gcsDriverConstructor(validRoot) if err != nil { @@ -282,11 +269,7 @@ func TestMoveDirectory(t *testing.T) { t.Skip(skipGCS()) } - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) + validRoot := t.TempDir() driver, err := gcsDriverConstructor(validRoot) if err != nil { diff --git a/registry/storage/driver/inmemory/driver.go b/registry/storage/driver/inmemory/driver.go index 7e8054e9..c803ac9b 100644 --- a/registry/storage/driver/inmemory/driver.go +++ b/registry/storage/driver/inmemory/driver.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "sync" "time" @@ -79,7 +78,7 @@ func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) { } defer rc.Close() - return ioutil.ReadAll(rc) + return io.ReadAll(rc) } // PutContent stores the []byte content at a location designated by "path". @@ -127,7 +126,7 @@ func (d *driver) reader(ctx context.Context, path string, offset int64) (io.Read return nil, fmt.Errorf("%q is a directory", path) } - return ioutil.NopCloser(found.(*file).sectionReader(offset)), nil + return io.NopCloser(found.(*file).sectionReader(offset)), nil } // Writer returns a FileWriter which will store the content written to it diff --git a/registry/storage/driver/middleware/cloudfront/middleware.go b/registry/storage/driver/middleware/cloudfront/middleware.go index e538e05e..5c2c0995 100644 --- a/registry/storage/driver/middleware/cloudfront/middleware.go +++ b/registry/storage/driver/middleware/cloudfront/middleware.go @@ -7,8 +7,8 @@ import ( "crypto/x509" "encoding/pem" "fmt" - "io/ioutil" "net/url" + "os" "strings" "time" @@ -89,7 +89,7 @@ func newCloudFrontStorageMiddleware(storageDriver storagedriver.StorageDriver, o } // get urlSigner from the file specified in pkPath - pkBytes, err := ioutil.ReadFile(pkPath) + pkBytes, err := os.ReadFile(pkPath) if err != nil { return nil, fmt.Errorf("failed to read privatekey file: %s", err) } diff --git a/registry/storage/driver/middleware/cloudfront/middleware_test.go b/registry/storage/driver/middleware/cloudfront/middleware_test.go index e9cefa56..4e75c643 100644 --- a/registry/storage/driver/middleware/cloudfront/middleware_test.go +++ b/registry/storage/driver/middleware/cloudfront/middleware_test.go @@ -1,7 +1,6 @@ package middleware import ( - "io/ioutil" "os" "testing" @@ -41,7 +40,7 @@ pZeMRablbPQdp8/1NyIwimq1VlG0ohQ4P6qhW7E09ZMC -----END RSA PRIVATE KEY----- ` - file, err := ioutil.TempFile("", "pkey") + file, err := os.CreateTemp("", "pkey") if err != nil { t.Fatal("File cannot be created") } diff --git a/registry/storage/driver/middleware/cloudfront/s3filter.go b/registry/storage/driver/middleware/cloudfront/s3filter.go index c7eb56a3..6b02d29d 100644 --- a/registry/storage/driver/middleware/cloudfront/s3filter.go +++ b/registry/storage/driver/middleware/cloudfront/s3filter.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net" "net/http" "strings" @@ -68,7 +68,7 @@ func fetchAWSIPs(url string) (awsIPResponse, error) { return response, err } if resp.StatusCode != 200 { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) return response, fmt.Errorf("failed to fetch network data. response = %s", body) } decoder := json.NewDecoder(resp.Body) diff --git a/registry/storage/driver/oss/oss.go b/registry/storage/driver/oss/oss.go index aa5b56d2..0c5f76f0 100644 --- a/registry/storage/driver/oss/oss.go +++ b/registry/storage/driver/oss/oss.go @@ -17,7 +17,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "reflect" "strconv" @@ -273,7 +272,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read // OSS sever will always return http.StatusPartialContent if range is acceptable. if resp.StatusCode != http.StatusPartialContent { resp.Body.Close() - return ioutil.NopCloser(bytes.NewReader(nil)), nil + return io.NopCloser(bytes.NewReader(nil)), nil } return resp.Body, nil diff --git a/registry/storage/driver/oss/oss_test.go b/registry/storage/driver/oss/oss_test.go index f1e4a607..a23e166a 100644 --- a/registry/storage/driver/oss/oss_test.go +++ b/registry/storage/driver/oss/oss_test.go @@ -4,7 +4,6 @@ package oss import ( - "io/ioutil" "os" "strconv" "testing" @@ -36,7 +35,7 @@ func init() { encryptionKeyID = os.Getenv("OSS_ENCRYPTIONKEYID") ) - root, err := ioutil.TempDir("", "driver-") + root, err := os.MkdirTemp("", "driver-") if err != nil { panic(err) } @@ -102,11 +101,7 @@ func TestEmptyRootList(t *testing.T) { t.Skip(skipCheck()) } - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) + validRoot := t.TempDir() rootedDriver, err := ossDriverConstructor(validRoot) if err != nil { diff --git a/registry/storage/driver/s3-aws/s3.go b/registry/storage/driver/s3-aws/s3.go index c3d86bb8..8b6e213e 100644 --- a/registry/storage/driver/s3-aws/s3.go +++ b/registry/storage/driver/s3-aws/s3.go @@ -18,7 +18,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math" "net/http" "path/filepath" @@ -607,7 +606,7 @@ func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) { if err != nil { return nil, err } - return ioutil.ReadAll(reader) + return io.ReadAll(reader) } // PutContent stores the []byte content at a location designated by "path". @@ -635,7 +634,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read }) if err != nil { if s3Err, ok := err.(awserr.Error); ok && s3Err.Code() == "InvalidRange" { - return ioutil.NopCloser(bytes.NewReader(nil)), nil + return io.NopCloser(bytes.NewReader(nil)), nil } return nil, parseError(path, err) @@ -1356,7 +1355,7 @@ func (w *writer) Write(p []byte) (int, error) { } defer resp.Body.Close() w.parts = nil - w.readyPart, err = ioutil.ReadAll(resp.Body) + w.readyPart, err = io.ReadAll(resp.Body) if err != nil { return 0, err } diff --git a/registry/storage/driver/s3-aws/s3_test.go b/registry/storage/driver/s3-aws/s3_test.go index d9dbb722..0c79a009 100644 --- a/registry/storage/driver/s3-aws/s3_test.go +++ b/registry/storage/driver/s3-aws/s3_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "math/rand" "os" "path" @@ -52,7 +51,7 @@ func init() { accelerate = os.Getenv("S3_ACCELERATE") ) - root, err := ioutil.TempDir("", "driver-") + root, err := os.MkdirTemp("", "driver-") if err != nil { panic(err) } @@ -166,12 +165,7 @@ func TestEmptyRootList(t *testing.T) { t.Skip(skipS3()) } - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) - + validRoot := t.TempDir() rootedDriver, err := s3DriverConstructor(validRoot, s3.StorageClassStandard) if err != nil { t.Fatalf("unexpected error creating rooted driver: %v", err) @@ -204,9 +198,9 @@ func TestEmptyRootList(t *testing.T) { } keys, _ = slashRootDriver.List(ctx, "/") - for _, path := range keys { - if !storagedriver.PathRegexp.MatchString(path) { - t.Fatalf("unexpected string in path: %q != %q", path, storagedriver.PathRegexp) + for _, p := range keys { + if !storagedriver.PathRegexp.MatchString(p) { + t.Fatalf("unexpected string in path: %q != %q", p, storagedriver.PathRegexp) } } } @@ -249,12 +243,7 @@ func TestStorageClass(t *testing.T) { t.Skip(skipS3()) } - rootDir, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(rootDir) - + rootDir := t.TempDir() contents := []byte("contents") ctx := context.Background() for _, storageClass := range s3StorageClasses { @@ -307,13 +296,9 @@ func TestDelete(t *testing.T) { t.Skip(skipS3()) } - rootDir, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(rootDir) + rootDir := t.TempDir() - driver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) + drvr, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) if err != nil { t.Fatalf("unexpected error creating driver with standard storage: %v", err) } @@ -421,35 +406,35 @@ func TestDelete(t *testing.T) { "/file1": true, } // create a test case for each file - for _, path := range objs { - if skipCase[path] { + for _, p := range objs { + if skipCase[p] { continue } tcs = append(tcs, testCase{ - name: fmt.Sprintf("delete path:'%s'", path), - delete: path, - expected: []string{path}, + name: fmt.Sprintf("delete path:'%s'", p), + delete: p, + expected: []string{p}, }) } init := func() []string { // init file structure matching objs var created []string - for _, path := range objs { - err := driver.PutContent(context.Background(), path, []byte("content "+path)) + for _, p := range objs { + err := drvr.PutContent(context.Background(), p, []byte("content "+p)) if err != nil { - fmt.Printf("unable to init file %s: %s\n", path, err) + fmt.Printf("unable to init file %s: %s\n", p, err) continue } - created = append(created, path) + created = append(created, p) } return created } cleanup := func(objs []string) { var lastErr error - for _, path := range objs { - err := driver.Delete(context.Background(), path) + for _, p := range objs { + err := drvr.Delete(context.Background(), p) if err != nil { switch err.(type) { case storagedriver.PathNotFoundError: @@ -468,7 +453,7 @@ func TestDelete(t *testing.T) { t.Run(tc.name, func(t *testing.T) { objs := init() - err := driver.Delete(context.Background(), tc.delete) + err := drvr.Delete(context.Background(), tc.delete) if tc.err != nil { if err == nil { @@ -496,7 +481,7 @@ func TestDelete(t *testing.T) { return false } for _, path := range objs { - stat, err := driver.Stat(context.Background(), path) + stat, err := drvr.Stat(context.Background(), path) if err != nil { switch err.(type) { case storagedriver.PathNotFoundError: @@ -530,13 +515,9 @@ func TestWalk(t *testing.T) { t.Skip(skipS3()) } - rootDir, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(rootDir) + rootDir := t.TempDir() - driver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) + drvr, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) if err != nil { t.Fatalf("unexpected error creating driver with standard storage: %v", err) } @@ -552,22 +533,22 @@ func TestWalk(t *testing.T) { // create file structure matching fileset above var created []string - for _, path := range fileset { - err := driver.PutContent(context.Background(), path, []byte("content "+path)) + for _, p := range fileset { + err := drvr.PutContent(context.Background(), p, []byte("content "+p)) if err != nil { - fmt.Printf("unable to create file %s: %s\n", path, err) + fmt.Printf("unable to create file %s: %s\n", p, err) continue } - created = append(created, path) + created = append(created, p) } // cleanup defer func() { var lastErr error - for _, path := range created { - err := driver.Delete(context.Background(), path) + for _, p := range created { + err := drvr.Delete(context.Background(), p) if err != nil { - _ = fmt.Errorf("cleanup failed for path %s: %s", path, err) + _ = fmt.Errorf("cleanup failed for path %s: %s", p, err) lastErr = err } } @@ -669,7 +650,7 @@ func TestWalk(t *testing.T) { tc.from = "/" } t.Run(tc.name, func(t *testing.T) { - err := driver.Walk(context.Background(), tc.from, func(fileInfo storagedriver.FileInfo) error { + err := drvr.Walk(context.Background(), tc.from, func(fileInfo storagedriver.FileInfo) error { walked = append(walked, fileInfo.Path()) return tc.fn(fileInfo) }) @@ -689,12 +670,7 @@ func TestOverThousandBlobs(t *testing.T) { t.Skip(skipS3()) } - rootDir, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(rootDir) - + rootDir := t.TempDir() standardDriver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) if err != nil { t.Fatalf("unexpected error creating driver with standard storage: %v", err) @@ -722,12 +698,7 @@ func TestMoveWithMultipartCopy(t *testing.T) { t.Skip(skipS3()) } - rootDir, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(rootDir) - + rootDir := t.TempDir() d, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) if err != nil { t.Fatalf("unexpected error creating driver: %v", err) @@ -776,12 +747,7 @@ func TestListObjectsV2(t *testing.T) { t.Skip(skipS3()) } - rootDir, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(rootDir) - + rootDir := t.TempDir() d, err := s3DriverConstructor(rootDir, s3.StorageClassStandard) if err != nil { t.Fatalf("unexpected error creating driver: %v", err) @@ -794,8 +760,8 @@ func TestListObjectsV2(t *testing.T) { for i := 0; i < n; i++ { filePaths = append(filePaths, fmt.Sprintf("%s/%d", prefix, i)) } - for _, path := range filePaths { - if err := d.PutContent(ctx, path, []byte(path)); err != nil { + for _, p := range filePaths { + if err := d.PutContent(ctx, p, []byte(p)); err != nil { t.Fatalf("unexpected error putting content: %v", err) } } diff --git a/registry/storage/driver/swift/swift.go b/registry/storage/driver/swift/swift.go index 753c9992..b5c4dcd4 100644 --- a/registry/storage/driver/swift/swift.go +++ b/registry/storage/driver/swift/swift.go @@ -25,7 +25,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strconv" @@ -336,7 +335,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read return nil, storagedriver.PathNotFoundError{Path: path} } if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == http.StatusRequestedRangeNotSatisfiable { - return ioutil.NopCloser(bytes.NewReader(nil)), nil + return io.NopCloser(bytes.NewReader(nil)), nil } return file, err } diff --git a/registry/storage/driver/swift/swift_test.go b/registry/storage/driver/swift/swift_test.go index af1d78ba..6b4342dd 100644 --- a/registry/storage/driver/swift/swift_test.go +++ b/registry/storage/driver/swift/swift_test.go @@ -1,7 +1,6 @@ package swift import ( - "io/ioutil" "os" "reflect" "strconv" @@ -58,7 +57,7 @@ func init() { container = "test" } - prefix, err := ioutil.TempDir("", "driver-") + prefix, err := os.MkdirTemp("", "driver-") if err != nil { panic(err) } @@ -100,11 +99,7 @@ func init() { } func TestEmptyRootList(t *testing.T) { - validRoot, err := ioutil.TempDir("", "driver-") - if err != nil { - t.Fatalf("unexpected error creating temporary directory: %v", err) - } - defer os.Remove(validRoot) + validRoot := t.TempDir() rootedDriver, err := swiftDriverConstructor(validRoot) if err != nil { diff --git a/registry/storage/driver/testsuites/testsuites.go b/registry/storage/driver/testsuites/testsuites.go index 4c4c15f6..58f6b6d6 100644 --- a/registry/storage/driver/testsuites/testsuites.go +++ b/registry/storage/driver/testsuites/testsuites.go @@ -5,7 +5,6 @@ import ( "context" "crypto/sha256" "io" - "io/ioutil" "math/rand" "net/http" "os" @@ -325,7 +324,7 @@ func (suite *DriverSuite) TestReaderWithOffset(c *check.C) { c.Assert(err, check.IsNil) defer reader.Close() - readContents, err := ioutil.ReadAll(reader) + readContents, err := io.ReadAll(reader) c.Assert(err, check.IsNil) c.Assert(readContents, check.DeepEquals, append(append(contentsChunk1, contentsChunk2...), contentsChunk3...)) @@ -334,7 +333,7 @@ func (suite *DriverSuite) TestReaderWithOffset(c *check.C) { c.Assert(err, check.IsNil) defer reader.Close() - readContents, err = ioutil.ReadAll(reader) + readContents, err = io.ReadAll(reader) c.Assert(err, check.IsNil) c.Assert(readContents, check.DeepEquals, append(contentsChunk2, contentsChunk3...)) @@ -343,7 +342,7 @@ func (suite *DriverSuite) TestReaderWithOffset(c *check.C) { c.Assert(err, check.IsNil) defer reader.Close() - readContents, err = ioutil.ReadAll(reader) + readContents, err = io.ReadAll(reader) c.Assert(err, check.IsNil) c.Assert(readContents, check.DeepEquals, contentsChunk3) @@ -643,7 +642,7 @@ func (suite *DriverSuite) TestURLFor(c *check.C) { c.Assert(err, check.IsNil) defer response.Body.Close() - read, err := ioutil.ReadAll(response.Body) + read, err := io.ReadAll(response.Body) c.Assert(err, check.IsNil) c.Assert(read, check.DeepEquals, contents) @@ -874,7 +873,7 @@ func (suite *DriverSuite) TestConcurrentStreamReads(c *check.C) { reader, err := suite.StorageDriver.Reader(suite.ctx, filename, offset) c.Assert(err, check.IsNil) - readContents, err := ioutil.ReadAll(reader) + readContents, err := io.ReadAll(reader) c.Assert(err, check.IsNil) c.Assert(readContents, check.DeepEquals, contents[offset:]) } @@ -941,7 +940,7 @@ func (suite *DriverSuite) TestConcurrentFileStreams(c *check.C) { // reader, err := suite.StorageDriver.Reader(suite.ctx, filename, offset) // c.Assert(err, check.IsNil) // -// readContents, err := ioutil.ReadAll(reader) +// readContents, err := io.ReadAll(reader) // c.Assert(err, check.IsNil) // // c.Assert(readContents, check.DeepEquals, contents) @@ -1104,7 +1103,7 @@ func (suite *DriverSuite) benchmarkDeleteFiles(c *check.C, numFiles int64) { } func (suite *DriverSuite) testFileStreams(c *check.C, size int64) { - tf, err := ioutil.TempFile("", "tf") + tf, err := os.CreateTemp("", "tf") c.Assert(err, check.IsNil) defer os.Remove(tf.Name()) defer tf.Close() @@ -1135,7 +1134,7 @@ func (suite *DriverSuite) testFileStreams(c *check.C, size int64) { c.Assert(err, check.IsNil) defer reader.Close() - readContents, err := ioutil.ReadAll(reader) + readContents, err := io.ReadAll(reader) c.Assert(err, check.IsNil) c.Assert(readContents, check.DeepEquals, contents) @@ -1171,7 +1170,7 @@ func (suite *DriverSuite) writeReadCompareStreams(c *check.C, filename string, c c.Assert(err, check.IsNil) defer reader.Close() - readContents, err := ioutil.ReadAll(reader) + readContents, err := io.ReadAll(reader) c.Assert(err, check.IsNil) c.Assert(readContents, check.DeepEquals, contents) diff --git a/registry/storage/filereader.go b/registry/storage/filereader.go index 031204bd..b5004eb4 100644 --- a/registry/storage/filereader.go +++ b/registry/storage/filereader.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "io" - "io/ioutil" storagedriver "github.com/distribution/distribution/v3/registry/storage/driver" ) @@ -126,7 +125,7 @@ func (fr *fileReader) reader() (io.Reader, error) { // reader that returns io.EOF. However, we do not set fr.rc, // allowing future attempts at getting a reader to possibly // succeed if the file turns up later. - return ioutil.NopCloser(bytes.NewReader([]byte{})), nil + return io.NopCloser(bytes.NewReader([]byte{})), nil default: return nil, err } diff --git a/registry/storage/io.go b/registry/storage/io.go index 561704ae..a96296e5 100644 --- a/registry/storage/io.go +++ b/registry/storage/io.go @@ -4,7 +4,6 @@ import ( "context" "errors" "io" - "io/ioutil" "github.com/distribution/distribution/v3/registry/storage/driver" ) @@ -25,7 +24,7 @@ func getContent(ctx context.Context, driver driver.StorageDriver, p string) ([]b func readAllLimited(r io.Reader, limit int64) ([]byte, error) { r = limitReader(r, limit) - return ioutil.ReadAll(r) + return io.ReadAll(r) } // limitReader returns a new reader limited to n bytes. Unlike io.LimitReader, diff --git a/testutil/handler.go b/testutil/handler.go index 00cd8a6a..9e691106 100644 --- a/testutil/handler.go +++ b/testutil/handler.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "net/url" "sort" @@ -111,7 +110,7 @@ func NewHandler(requestResponseMap RequestResponseMap) http.Handler { func (app *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() - requestBody, _ := ioutil.ReadAll(r.Body) + requestBody, _ := io.ReadAll(r.Body) request := Request{ Method: r.Method, Route: r.URL.Path,