From 8ffb053cec01bd7541ef40679f5ad24d44070fce Mon Sep 17 00:00:00 2001 From: slab713 <109306207+slab713@users.noreply.github.com> Date: Fri, 2 Sep 2022 14:56:02 +0200 Subject: [PATCH] Replaced deprecated io/ioutil functions (#768) Signed-off-by: slab713 <109306207+slab713@users.noreply.github.com> --- cmd/zb/helper.go | 7 +- cmd/zb/perf.go | 3 +- pkg/api/authn.go | 3 +- pkg/api/controller.go | 8 +- pkg/api/controller_test.go | 45 ++++++------ pkg/api/routes.go | 3 +- pkg/cli/client.go | 6 +- pkg/cli/client_elevated_test.go | 3 +- pkg/cli/client_test.go | 7 +- pkg/cli/config_cmd.go | 5 +- pkg/cli/config_cmd_test.go | 9 +-- pkg/cli/config_reloader_test.go | 13 ++-- pkg/cli/extensions_test.go | 21 +++--- pkg/cli/image_cmd_test.go | 3 +- pkg/cli/root_test.go | 81 ++++++++++----------- pkg/cli/stress_test.go | 7 +- pkg/extensions/extensions_test.go | 5 +- pkg/extensions/lint/lint_test.go | 25 +++---- pkg/extensions/scrub/scrub_test.go | 13 ++-- pkg/extensions/search/common/common_test.go | 8 +- pkg/extensions/search/cve/cve_test.go | 13 ++-- pkg/extensions/search/digest/digest_test.go | 7 +- pkg/extensions/sync/sync_disabled_test.go | 3 +- pkg/extensions/sync/sync_internal_test.go | 5 +- pkg/extensions/sync/sync_test.go | 39 +++++----- pkg/extensions/sync/utils.go | 5 +- pkg/log/log_test.go | 32 ++++---- pkg/storage/local.go | 34 ++++----- pkg/storage/local_elevated_test.go | 3 +- pkg/storage/local_test.go | 33 ++++----- pkg/storage/s3/s3_test.go | 51 +++++++------ pkg/storage/scrub.go | 3 +- pkg/storage/scrub_test.go | 7 +- pkg/test/common.go | 11 ++- pkg/test/common_test.go | 5 +- pkg/test/mocks/storage_driver_mock.go | 3 +- 36 files changed, 249 insertions(+), 280 deletions(-) diff --git a/cmd/zb/helper.go b/cmd/zb/helper.go index e9684d14b..d4364ab1e 100644 --- a/cmd/zb/helper.go +++ b/cmd/zb/helper.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" mrand "math/rand" "net/http" @@ -118,7 +117,7 @@ func pullAndCollect(url string, repos []string, manifestItem manifestStruct, manifestBody := resp.Body() // file copy simulation - _, err = io.Copy(ioutil.Discard, bytes.NewReader(manifestBody)) + _, err = io.Copy(io.Discard, bytes.NewReader(manifestBody)) latency = time.Since(start) @@ -176,7 +175,7 @@ func pullAndCollect(url string, repos []string, manifestItem manifestStruct, configBody := resp.Body() // file copy simulation - _, err = io.Copy(ioutil.Discard, bytes.NewReader(configBody)) + _, err = io.Copy(io.Discard, bytes.NewReader(configBody)) latency = time.Since(start) @@ -230,7 +229,7 @@ func pullAndCollect(url string, repos []string, manifestItem manifestStruct, blobBody := resp.Body() // file copy simulation - _, err = io.Copy(ioutil.Discard, bytes.NewReader(blobBody)) + _, err = io.Copy(io.Discard, bytes.NewReader(blobBody)) if err != nil { log.Fatal(err) } diff --git a/cmd/zb/perf.go b/cmd/zb/perf.go index 790c1046f..8f4fd7443 100644 --- a/cmd/zb/perf.go +++ b/cmd/zb/perf.go @@ -4,7 +4,6 @@ import ( crand "crypto/rand" "crypto/tls" "fmt" - "io/ioutil" "log" "math/big" "net" @@ -724,7 +723,7 @@ func Perf( log.Fatal(err) // file closed on exit } - if err := ioutil.WriteFile(fmt.Sprintf("%s.json", outFmt), jsonOut, defaultFilePerms); err != nil { + if err := os.WriteFile(fmt.Sprintf("%s.json", outFmt), jsonOut, defaultFilePerms); err != nil { log.Fatal(err) } } diff --git a/pkg/api/authn.go b/pkg/api/authn.go index 12e80bcbe..aa9a0a262 100644 --- a/pkg/api/authn.go +++ b/pkg/api/authn.go @@ -5,7 +5,6 @@ import ( "crypto/x509" "encoding/base64" "fmt" - "io/ioutil" "net/http" "os" "strconv" @@ -132,7 +131,7 @@ func basicAuthHandler(ctlr *Controller) mux.MiddlewareFunc { } if ctlr.Config.HTTP.Auth.LDAP.CACert != "" { - caCert, err := ioutil.ReadFile(ctlr.Config.HTTP.Auth.LDAP.CACert) + caCert, err := os.ReadFile(ctlr.Config.HTTP.Auth.LDAP.CACert) if err != nil { panic(err) } diff --git a/pkg/api/controller.go b/pkg/api/controller.go index bf2eff324..257770689 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -5,9 +5,9 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "net" "net/http" + "os" "runtime" "strings" goSync "sync" @@ -92,11 +92,11 @@ func DumpRuntimeParams(log log.Logger) { evt = evt.Uint64("max. open files", rLimit.Cur) } - if content, err := ioutil.ReadFile("/proc/sys/net/core/somaxconn"); err == nil { + if content, err := os.ReadFile("/proc/sys/net/core/somaxconn"); err == nil { evt = evt.Str("listen backlog", strings.TrimSuffix(string(content), "\n")) } - if content, err := ioutil.ReadFile("/proc/sys/user/max_inotify_watches"); err == nil { + if content, err := os.ReadFile("/proc/sys/user/max_inotify_watches"); err == nil { evt = evt.Str("max. inotify watches", strings.TrimSuffix(string(content), "\n")) } @@ -196,7 +196,7 @@ func (c *Controller) Run(reloadCtx context.Context) error { clientAuth = tls.RequireAndVerifyClientCert } - caCert, err := ioutil.ReadFile(c.Config.HTTP.TLS.CACert) + caCert, err := os.ReadFile(c.Config.HTTP.TLS.CACert) if err != nil { panic(err) } diff --git a/pkg/api/controller_test.go b/pkg/api/controller_test.go index 9e634a298..f9034ad76 100644 --- a/pkg/api/controller_test.go +++ b/pkg/api/controller_test.go @@ -13,7 +13,6 @@ import ( goerrors "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -907,7 +906,7 @@ func TestMultipleInstance(t *testing.T) { func TestTLSWithBasicAuth(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -967,7 +966,7 @@ func TestTLSWithBasicAuth(t *testing.T) { func TestTLSWithBasicAuthAllowReadAccess(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -1036,7 +1035,7 @@ func TestTLSWithBasicAuthAllowReadAccess(t *testing.T) { func TestMutualTLSAuthWithUserPermissions(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -1119,7 +1118,7 @@ func TestMutualTLSAuthWithUserPermissions(t *testing.T) { func TestMutualTLSAuthWithoutCN(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile("../../test/data/noidentity/ca.crt") + caCert, err := os.ReadFile("../../test/data/noidentity/ca.crt") So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -1176,7 +1175,7 @@ func TestMutualTLSAuthWithoutCN(t *testing.T) { func TestTLSMutualAuth(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -1243,7 +1242,7 @@ func TestTLSMutualAuth(t *testing.T) { func TestTLSMutualAuthAllowReadAccess(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -1325,7 +1324,7 @@ func TestTLSMutualAuthAllowReadAccess(t *testing.T) { func TestTLSMutualAndBasicAuth(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -1402,7 +1401,7 @@ func TestTLSMutualAndBasicAuth(t *testing.T) { func TestTLSMutualAndBasicAuthAllowReadAccess(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -3219,7 +3218,7 @@ func TestCrossRepoMount(t *testing.T) { blob := manifestDigest.Encoded() - buf, err := ioutil.ReadFile(path.Join(ctlr.Config.Storage.RootDirectory, "zot-cve-test/blobs/sha256/"+blob)) + buf, err := os.ReadFile(path.Join(ctlr.Config.Storage.RootDirectory, "zot-cve-test/blobs/sha256/"+blob)) if err != nil { panic(err) } @@ -3519,7 +3518,7 @@ func TestParallelRequests(t *testing.T) { blobPath := path.Join("../../test/data", testcase.srcImageName, "blobs/sha256", blob) - buf, err := ioutil.ReadFile(blobPath) + buf, err := os.ReadFile(blobPath) if err != nil { panic(err) } @@ -3975,7 +3974,7 @@ func TestImageSignatures(t *testing.T) { err = json.Unmarshal(resp.Body(), &refs) So(err, ShouldBeNil) So(len(refs.References), ShouldEqual, 1) - err = ioutil.WriteFile(path.Join(dir, repoName, "blobs", + err = os.WriteFile(path.Join(dir, repoName, "blobs", strings.ReplaceAll(refs.References[0].Digest.String(), ":", "/")), []byte("corrupt"), 0o600) So(err, ShouldBeNil) resp, err = resty.R().SetQueryParam("artifactType", notreg.ArtifactTypeNotation).Get( @@ -5371,7 +5370,7 @@ func TestManifestImageIndex(t *testing.T) { }) Convey("Corrupt index", func() { - err = ioutil.WriteFile(path.Join(dir, "index", "blobs", index1dgst.Algorithm().String(), index1dgst.Encoded()), + err = os.WriteFile(path.Join(dir, "index", "blobs", index1dgst.Algorithm().String(), index1dgst.Encoded()), []byte("deadbeef"), storage.DefaultFilePerms) So(err, ShouldBeNil) resp, err = resty.R().Delete(baseURL + fmt.Sprintf("/v2/index/manifests/%s", index1dgst)) @@ -5751,7 +5750,7 @@ func TestInjectTooManyOpenFiles(t *testing.T) { _, err = os.Stat(indexFile) So(err, ShouldBeNil) indexContent := []byte(`not a JSON content`) - err = ioutil.WriteFile(indexFile, indexContent, 0o600) + err = os.WriteFile(indexFile, indexContent, 0o600) So(err, ShouldBeNil) resp, err = resty.R().SetHeader("Content-Type", "application/vnd.oci.image.manifest.v1+json"). @@ -5771,7 +5770,7 @@ func TestPeriodicGC(t *testing.T) { conf := config.New() conf.HTTP.Port = port - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Output = logFile.Name() defer os.Remove(logFile.Name()) // clean up @@ -5814,7 +5813,7 @@ func TestPeriodicGC(t *testing.T) { conf := config.New() conf.HTTP.Port = port - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Output = logFile.Name() defer os.Remove(logFile.Name()) // clean up @@ -5854,7 +5853,7 @@ func TestPeriodicTasks(t *testing.T) { conf := config.New() conf.HTTP.Port = port - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Output = logFile.Name() defer os.Remove(logFile.Name()) // clean up @@ -5890,7 +5889,7 @@ func TestPeriodicTasks(t *testing.T) { conf := config.New() conf.HTTP.Port = port - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Output = logFile.Name() defer os.Remove(logFile.Name()) // clean up @@ -6096,7 +6095,7 @@ func TestDistSpecExtensions(t *testing.T) { Search: searchConfig, } - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Output = logFile.Name() defer os.Remove(logFile.Name()) // clean up @@ -6132,7 +6131,7 @@ func TestDistSpecExtensions(t *testing.T) { conf.HTTP.Port = port - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Output = logFile.Name() defer os.Remove(logFile.Name()) // clean up @@ -6164,7 +6163,7 @@ func getAllBlobs(imagePath string) []string { return []string{} } - buf, err := ioutil.ReadFile(path.Join(imagePath, "index.json")) + buf, err := os.ReadFile(path.Join(imagePath, "index.json")) if err != nil { panic(err) } @@ -6181,7 +6180,7 @@ func getAllBlobs(imagePath string) []string { blobList = append(blobList, digest.Encoded()) p := path.Join(imagePath, "blobs", digest.Algorithm().String(), digest.Encoded()) - buf, err = ioutil.ReadFile(p) + buf, err = os.ReadFile(p) if err != nil { panic(err) @@ -6209,7 +6208,7 @@ func getAllManifests(imagePath string) []string { return []string{} } - buf, err := ioutil.ReadFile(path.Join(imagePath, "index.json")) + buf, err := os.ReadFile(path.Join(imagePath, "index.json")) if err != nil { panic(err) } diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 55fe660e5..8fcf66d43 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -15,7 +15,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/url" "path" @@ -444,7 +443,7 @@ func (rh *RouteHandler) UpdateManifest(response http.ResponseWriter, request *ht return } - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) // hard to reach test case, injected error (simulates an interrupted image manifest upload) // err could be io.ErrUnexpectedEOF if err := test.Error(err); err != nil { diff --git a/pkg/cli/client.go b/pkg/cli/client.go index e03ef6222..a56896858 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client.go @@ -10,7 +10,7 @@ import ( "crypto/x509" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -131,7 +131,7 @@ func doHTTPRequest(req *http.Request, verifyTLS bool, resultsPtr interface{}) (h return nil, zotErrors.ErrUnauthorizedAccess } - bodyBytes, _ := ioutil.ReadAll(resp.Body) + bodyBytes, _ := io.ReadAll(resp.Body) return nil, errors.New(string(bodyBytes)) //nolint: goerr113 } @@ -179,7 +179,7 @@ func getTLSConfig(certsPath string, caCertPool *x509.CertPool) (*tls.Config, err return nil, err } - caCert, err := ioutil.ReadFile(caCertFile) + caCert, err := os.ReadFile(caCertFile) if err != nil { return nil, err } diff --git a/pkg/cli/client_elevated_test.go b/pkg/cli/client_elevated_test.go index 2a4583265..a3125444b 100644 --- a/pkg/cli/client_elevated_test.go +++ b/pkg/cli/client_elevated_test.go @@ -9,7 +9,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -67,7 +66,7 @@ func TestElevatedPrivilegesTLSNewControllerPrivilegedCert(t *testing.T) { os.Chdir(wd) - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) diff --git a/pkg/cli/client_test.go b/pkg/cli/client_test.go index b7e7c9eba..556f2e6ef 100644 --- a/pkg/cli/client_test.go +++ b/pkg/cli/client_test.go @@ -9,7 +9,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -46,7 +45,7 @@ const ( func TestTLSWithAuth(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -152,7 +151,7 @@ func TestTLSWithAuth(t *testing.T) { func TestTLSWithoutAuth(t *testing.T) { Convey("Home certs - Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) @@ -222,7 +221,7 @@ func TestTLSWithoutAuth(t *testing.T) { func TestTLSBadCerts(t *testing.T) { Convey("Make a new controller", t, func() { - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) So(err, ShouldBeNil) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) diff --git a/pkg/cli/config_cmd.go b/pkg/cli/config_cmd.go index e31e4d16d..65498a4e2 100644 --- a/pkg/cli/config_cmd.go +++ b/pkg/cli/config_cmd.go @@ -6,7 +6,6 @@ package cli import ( "errors" "fmt" - "io/ioutil" "os" "path" "strconv" @@ -135,7 +134,7 @@ func getConfigMapFromFile(filePath string) ([]interface{}, error) { file.Close() - data, err := ioutil.ReadFile(filePath) + data, err := os.ReadFile(filePath) if err != nil { return nil, err } @@ -169,7 +168,7 @@ func saveConfigMapToFile(filePath string, configMap []interface{}) error { return err } - if err := ioutil.WriteFile(filePath, marshalled, defaultFilePerms); err != nil { + if err := os.WriteFile(filePath, marshalled, defaultFilePerms); err != nil { return err } diff --git a/pkg/cli/config_cmd_test.go b/pkg/cli/config_cmd_test.go index 2e51fcb3c..7a7b16af7 100644 --- a/pkg/cli/config_cmd_test.go +++ b/pkg/cli/config_cmd_test.go @@ -5,7 +5,6 @@ package cli //nolint:testpackage import ( "bytes" - "io/ioutil" "log" "os" "strings" @@ -70,7 +69,7 @@ func TestConfigCmdMain(t *testing.T) { cmd.SetArgs(args) _ = cmd.Execute() - actual, err := ioutil.ReadFile(file) + actual, err := os.ReadFile(file) if err != nil { panic(err) } @@ -288,7 +287,7 @@ func TestConfigCmdMain(t *testing.T) { err := cmd.Execute() So(err, ShouldBeNil) - actual, err := ioutil.ReadFile(configPath) + actual, err := os.ReadFile(configPath) if err != nil { panic(err) } @@ -324,7 +323,7 @@ func TestConfigCmdMain(t *testing.T) { err := cmd.Execute() So(err, ShouldBeNil) - actual, err := ioutil.ReadFile(configPath) + actual, err := os.ReadFile(configPath) if err != nil { panic(err) } @@ -347,7 +346,7 @@ func TestConfigCmdMain(t *testing.T) { err := cmd.Execute() So(err, ShouldBeNil) - actual, err := ioutil.ReadFile(configPath) + actual, err := os.ReadFile(configPath) if err != nil { panic(err) } diff --git a/pkg/cli/config_reloader_test.go b/pkg/cli/config_reloader_test.go index 6e8ac9877..30d3ad44a 100644 --- a/pkg/cli/config_reloader_test.go +++ b/pkg/cli/config_reloader_test.go @@ -3,7 +3,6 @@ package cli_test import ( "fmt" "io" - "io/ioutil" "os" "testing" "time" @@ -23,7 +22,7 @@ func TestConfigReloader(t *testing.T) { port := test.GetFreePort() baseURL := test.GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) username := "alice" @@ -78,7 +77,7 @@ func TestConfigReloader(t *testing.T) { } }`, port, htpasswdPath, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up @@ -161,7 +160,7 @@ func TestConfigReloader(t *testing.T) { port := test.GetFreePort() baseURL := test.GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -202,7 +201,7 @@ func TestConfigReloader(t *testing.T) { } }`, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up @@ -291,7 +290,7 @@ func TestConfigReloader(t *testing.T) { port := test.GetFreePort() baseURL := test.GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -332,7 +331,7 @@ func TestConfigReloader(t *testing.T) { } }`, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up diff --git a/pkg/cli/extensions_test.go b/pkg/cli/extensions_test.go index 4003884a8..cdad72cdf 100644 --- a/pkg/cli/extensions_test.go +++ b/pkg/cli/extensions_test.go @@ -5,7 +5,6 @@ package cli_test import ( "fmt" - "io/ioutil" "net/http" "os" "testing" @@ -24,7 +23,7 @@ func TestServeExtensions(t *testing.T) { Convey("config file with no extensions", t, func(c C) { port := GetFreePort() baseURL := GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -42,7 +41,7 @@ func TestServeExtensions(t *testing.T) { } }`, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up _, err = cfgfile.Write([]byte(content)) @@ -65,7 +64,7 @@ func TestServeExtensions(t *testing.T) { Convey("config file with empty extensions", t, func(c C) { port := GetFreePort() baseURL := GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -85,7 +84,7 @@ func TestServeExtensions(t *testing.T) { } }`, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up _, err = cfgfile.Write([]byte(content)) @@ -109,13 +108,13 @@ func TestServeExtensions(t *testing.T) { func testWithMetricsEnabled(cfgContentFormat string) { port := GetFreePort() baseURL := GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up content := fmt.Sprintf(cfgContentFormat, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up @@ -221,7 +220,7 @@ func TestServeMetricsExtension(t *testing.T) { Convey("with explicit disable", t, func(c C) { port := GetFreePort() baseURL := GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -244,7 +243,7 @@ func TestServeMetricsExtension(t *testing.T) { } }`, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up _, err = cfgfile.Write([]byte(content)) @@ -676,14 +675,14 @@ func runCLIWithConfig(tempDir string, config string) (string, error) { port := GetFreePort() baseURL := GetBaseURL(port) - logFile, err := ioutil.TempFile(tempDir, "zot-log*.txt") + logFile, err := os.CreateTemp(tempDir, "zot-log*.txt") if err != nil { return "", err } defer os.Remove(logFile.Name()) // clean up - cfgfile, err := ioutil.TempFile(tempDir, "zot-test*.json") + cfgfile, err := os.CreateTemp(tempDir, "zot-test*.json") if err != nil { return "", err } diff --git a/pkg/cli/image_cmd_test.go b/pkg/cli/image_cmd_test.go index 92cd8a5be..ba67c2151 100644 --- a/pkg/cli/image_cmd_test.go +++ b/pkg/cli/image_cmd_test.go @@ -8,7 +8,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "log" "os" "path" @@ -1414,7 +1413,7 @@ func makeConfigFile(content string) string { configPath := path.Join(home + "/.zot") - if err := ioutil.WriteFile(configPath, []byte(content), 0o600); err != nil { + if err := os.WriteFile(configPath, []byte(content), 0o600); err != nil { panic(err) } diff --git a/pkg/cli/root_test.go b/pkg/cli/root_test.go index 5d20b7792..64ad47e94 100644 --- a/pkg/cli/root_test.go +++ b/pkg/cli/root_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "path" "testing" @@ -78,7 +77,7 @@ func TestServe(t *testing.T) { }) Convey("bad config", func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"log":{}}`) @@ -98,7 +97,7 @@ func TestVerify(t *testing.T) { defer func() { os.Args = oldArgs }() Convey("Test verify bad config", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"log":{}}`) @@ -111,7 +110,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify storage driver different than s3", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot", "storageDriver": {"name": "gcs"}}, @@ -126,7 +125,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify subpath storage driver different than s3", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot", "storageDriver": {"name": "s3"}, @@ -142,7 +141,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify subpath storage config", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot", @@ -218,7 +217,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify w/ authorization and w/o authentication", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -234,7 +233,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify w/ authorization and w/ authentication", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -251,7 +250,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify anonymous authorization", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -268,7 +267,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify default authorization fail", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -287,7 +286,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify default authorization fail", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -307,7 +306,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify w/ sync and w/o filesystem storage", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot", "storageDriver": {"name": "s3"}}, @@ -324,7 +323,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify w/ sync and w/ filesystem storage", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -341,7 +340,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify with bad sync prefixes", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -359,7 +358,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify with bad authorization repo patterns", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -375,7 +374,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify sync config default tls value", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -394,7 +393,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify sync without retry options", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"storage":{"rootDirectory":"/tmp/zot"}, @@ -411,7 +410,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify config with unknown keys", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"}, @@ -426,7 +425,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify config with missing basedn key", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"}, @@ -442,7 +441,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify config with missing address key", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"}, @@ -458,7 +457,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify config with missing userattribute key", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"}, @@ -474,7 +473,7 @@ func TestVerify(t *testing.T) { }) Convey("Test verify good config", t, func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"}, @@ -498,7 +497,7 @@ func TestLoadConfig(t *testing.T) { }) Convey("Test subpath config combination", t, func(c C) { config := config.New() - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) content := []byte(`{"storage":{"rootDirectory":"/tmp/zot", @@ -557,7 +556,7 @@ func TestGC(t *testing.T) { }) Convey("Test GC config corner cases", t, func(c C) { - contents, err := ioutil.ReadFile("../../examples/config-gc.json") + contents, err := os.ReadFile("../../examples/config-gc.json") So(err, ShouldBeNil) Convey("GC delay without GC", func() { @@ -565,14 +564,14 @@ func TestGC(t *testing.T) { err = json.Unmarshal(contents, config) config.Storage.GC = false - file, err := ioutil.TempFile("", "gc-config-*.json") + file, err := os.CreateTemp("", "gc-config-*.json") So(err, ShouldBeNil) defer os.Remove(file.Name()) contents, err = json.MarshalIndent(config, "", " ") So(err, ShouldBeNil) - err = ioutil.WriteFile(file.Name(), contents, 0o600) + err = os.WriteFile(file.Name(), contents, 0o600) So(err, ShouldBeNil) err = cli.LoadConfiguration(config, file.Name()) So(err, ShouldBeNil) @@ -585,14 +584,14 @@ func TestGC(t *testing.T) { config.Storage.GCDelay = 0 config.Storage.GCInterval = 24 * time.Hour - file, err := ioutil.TempFile("", "gc-config-*.json") + file, err := os.CreateTemp("", "gc-config-*.json") So(err, ShouldBeNil) defer os.Remove(file.Name()) contents, err = json.MarshalIndent(config, "", " ") So(err, ShouldBeNil) - err = ioutil.WriteFile(file.Name(), contents, 0o600) + err = os.WriteFile(file.Name(), contents, 0o600) So(err, ShouldBeNil) err = cli.LoadConfiguration(config, file.Name()) So(err, ShouldBeNil) @@ -603,14 +602,14 @@ func TestGC(t *testing.T) { err = json.Unmarshal(contents, config) config.Storage.GCDelay = -1 * time.Second - file, err := ioutil.TempFile("", "gc-config-*.json") + file, err := os.CreateTemp("", "gc-config-*.json") So(err, ShouldBeNil) defer os.Remove(file.Name()) contents, err = json.MarshalIndent(config, "", " ") So(err, ShouldBeNil) - err = ioutil.WriteFile(file.Name(), contents, 0o600) + err = os.WriteFile(file.Name(), contents, 0o600) So(err, ShouldBeNil) err = cli.LoadConfiguration(config, file.Name()) So(err, ShouldNotBeNil) @@ -619,7 +618,7 @@ func TestGC(t *testing.T) { Convey("GC delay when GC = false", func() { config := config.New() - file, err := ioutil.TempFile("", "gc-false-config-*.json") + file, err := os.CreateTemp("", "gc-false-config-*.json") So(err, ShouldBeNil) defer os.Remove(file.Name()) @@ -627,7 +626,7 @@ func TestGC(t *testing.T) { "gc": false}, "http": {"address": "127.0.0.1", "port": "8080"}, "log": {"level": "debug"}}`) - err = ioutil.WriteFile(file.Name(), content, 0o600) + err = os.WriteFile(file.Name(), content, 0o600) So(err, ShouldBeNil) err = cli.LoadConfiguration(config, file.Name()) So(err, ShouldBeNil) @@ -639,14 +638,14 @@ func TestGC(t *testing.T) { err = json.Unmarshal(contents, config) config.Storage.GCInterval = -1 * time.Second - file, err := ioutil.TempFile("", "gc-config-*.json") + file, err := os.CreateTemp("", "gc-config-*.json") So(err, ShouldBeNil) defer os.Remove(file.Name()) contents, err = json.MarshalIndent(config, "", " ") So(err, ShouldBeNil) - err = ioutil.WriteFile(file.Name(), contents, 0o600) + err = os.WriteFile(file.Name(), contents, 0o600) So(err, ShouldBeNil) err = cli.LoadConfiguration(config, file.Name()) So(err, ShouldNotBeNil) @@ -683,7 +682,7 @@ func TestScrub(t *testing.T) { }) Convey("bad config", func(c C) { - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(`{"log":{}}`) @@ -720,7 +719,7 @@ func TestScrub(t *testing.T) { time.Sleep(100 * time.Millisecond) } - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(fmt.Sprintf(`{ @@ -752,7 +751,7 @@ func TestScrub(t *testing.T) { Convey("no image store provided", func(c C) { port := GetFreePort() - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(fmt.Sprintf(`{ @@ -782,7 +781,7 @@ func TestScrub(t *testing.T) { repoName := "badIndex" - repo, err := ioutil.TempDir(dir, repoName) + repo, err := os.MkdirTemp(dir, repoName) if err != nil { panic(err) } @@ -793,19 +792,19 @@ func TestScrub(t *testing.T) { if _, err = os.Stat(fmt.Sprintf("%s/oci-layout", repo)); err != nil { content := []byte(`{"imageLayoutVersion": "1.0.0"}`) - if err = ioutil.WriteFile(fmt.Sprintf("%s/oci-layout", repo), content, 0o600); err != nil { + if err = os.WriteFile(fmt.Sprintf("%s/oci-layout", repo), content, 0o600); err != nil { panic(err) } } if _, err = os.Stat(fmt.Sprintf("%s/index.json", repo)); err != nil { content := []byte(`not a JSON content`) - if err = ioutil.WriteFile(fmt.Sprintf("%s/index.json", repo), content, 0o600); err != nil { + if err = os.WriteFile(fmt.Sprintf("%s/index.json", repo), content, 0o600); err != nil { panic(err) } } - tmpfile, err := ioutil.TempFile("", "zot-test*.json") + tmpfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up content := []byte(fmt.Sprintf(`{ diff --git a/pkg/cli/stress_test.go b/pkg/cli/stress_test.go index 7d2a24bdb..051a193ae 100644 --- a/pkg/cli/stress_test.go +++ b/pkg/cli/stress_test.go @@ -6,7 +6,6 @@ package cli_test import ( "context" "fmt" - "io/ioutil" "os" "os/exec" "sync" @@ -46,7 +45,7 @@ func TestSressTooManyOpenFiles(t *testing.T) { conf.Storage.Dedupe = false conf.Storage.GC = true - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer func() { @@ -93,7 +92,7 @@ func TestSressTooManyOpenFiles(t *testing.T) { } }`, dir, conf.Storage.Dedupe, conf.Storage.GC, port, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up _, err = cfgfile.Write([]byte(content)) @@ -135,7 +134,7 @@ func TestSressTooManyOpenFiles(t *testing.T) { stopServer(ctlr) time.Sleep(2 * time.Second) - scrubFile, err := ioutil.TempFile("", "zot-scrub*.txt") + scrubFile, err := os.CreateTemp("", "zot-scrub*.txt") So(err, ShouldBeNil) defer func() { diff --git a/pkg/extensions/extensions_test.go b/pkg/extensions/extensions_test.go index 49e54f96e..e78169b73 100644 --- a/pkg/extensions/extensions_test.go +++ b/pkg/extensions/extensions_test.go @@ -5,7 +5,6 @@ package extensions_test import ( "context" - "io/ioutil" "os" "testing" @@ -35,7 +34,7 @@ func TestEnableExtension(t *testing.T) { conf.Extensions.Sync = syncConfig conf.HTTP.Port = port - logFile, err := ioutil.TempFile(globalDir, "zot-log*.txt") + logFile, err := os.CreateTemp(globalDir, "zot-log*.txt") So(err, ShouldBeNil) conf.Log.Level = "info" conf.Log.Output = logFile.Name() @@ -73,7 +72,7 @@ func TestMetricsExtension(t *testing.T) { conf.HTTP.Port = port baseURL := test.GetBaseURL(port) - logFile, err := ioutil.TempFile(globalDir, "zot-log*.txt") + logFile, err := os.CreateTemp(globalDir, "zot-log*.txt") So(err, ShouldBeNil) defaultValue := true diff --git a/pkg/extensions/lint/lint_test.go b/pkg/extensions/lint/lint_test.go index 9cbc304b9..238b1c9bf 100644 --- a/pkg/extensions/lint/lint_test.go +++ b/pkg/extensions/lint/lint_test.go @@ -6,7 +6,6 @@ package lint_test import ( "context" "encoding/json" - "io/ioutil" "net/http" "os" "path" @@ -380,7 +379,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { } var index ispec.Index - buf, err := ioutil.ReadFile(path.Join(dir, "zot-test", "index.json")) + buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json")) So(err, ShouldBeNil) err = json.Unmarshal(buf, &index) So(err, ShouldBeNil) @@ -388,7 +387,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { manifestDigest := index.Manifests[0].Digest var manifest ispec.Manifest - buf, err = ioutil.ReadFile(path.Join(dir, "zot-test", "blobs", + buf, err = os.ReadFile(path.Join(dir, "zot-test", "blobs", manifestDigest.Algorithm().String(), manifestDigest.Encoded())) So(err, ShouldBeNil) err = json.Unmarshal(buf, &manifest) @@ -408,7 +407,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { digest := godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = ioutil.WriteFile(path.Join(dir, "zot-test", "blobs", + err = os.WriteFile(path.Join(dir, "zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()), content, 0o600) So(err, ShouldBeNil) @@ -444,7 +443,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { } var index ispec.Index - buf, err := ioutil.ReadFile(path.Join(dir, "zot-test", "index.json")) + buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json")) So(err, ShouldBeNil) err = json.Unmarshal(buf, &index) So(err, ShouldBeNil) @@ -452,7 +451,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { manifestDigest := index.Manifests[0].Digest var manifest ispec.Manifest - buf, err = ioutil.ReadFile(path.Join(dir, "zot-test", "blobs", + buf, err = os.ReadFile(path.Join(dir, "zot-test", "blobs", manifestDigest.Algorithm().String(), manifestDigest.Encoded())) So(err, ShouldBeNil) err = json.Unmarshal(buf, &manifest) @@ -471,7 +470,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { digest := godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = ioutil.WriteFile(path.Join(dir, "zot-test", "blobs", + err = os.WriteFile(path.Join(dir, "zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()), content, 0o600) So(err, ShouldBeNil) @@ -507,7 +506,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { } var index ispec.Index - buf, err := ioutil.ReadFile(path.Join(dir, "zot-test", "index.json")) + buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json")) So(err, ShouldBeNil) err = json.Unmarshal(buf, &index) So(err, ShouldBeNil) @@ -515,7 +514,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { manifestDigest := index.Manifests[0].Digest var manifest ispec.Manifest - buf, err = ioutil.ReadFile(path.Join(dir, "zot-test", "blobs", + buf, err = os.ReadFile(path.Join(dir, "zot-test", "blobs", manifestDigest.Algorithm().String(), manifestDigest.Encoded())) So(err, ShouldBeNil) err = json.Unmarshal(buf, &manifest) @@ -536,7 +535,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { digest := godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = ioutil.WriteFile(path.Join(dir, "zot-test", "blobs", + err = os.WriteFile(path.Join(dir, "zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()), content, 0o600) So(err, ShouldBeNil) @@ -572,7 +571,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { } var index ispec.Index - buf, err := ioutil.ReadFile(path.Join(dir, "zot-test", "index.json")) + buf, err := os.ReadFile(path.Join(dir, "zot-test", "index.json")) So(err, ShouldBeNil) err = json.Unmarshal(buf, &index) So(err, ShouldBeNil) @@ -580,7 +579,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { manifestDigest := index.Manifests[0].Digest var manifest ispec.Manifest - buf, err = ioutil.ReadFile(path.Join(dir, "zot-test", "blobs", + buf, err = os.ReadFile(path.Join(dir, "zot-test", "blobs", manifestDigest.Algorithm().String(), manifestDigest.Encoded())) So(err, ShouldBeNil) err = json.Unmarshal(buf, &manifest) @@ -600,7 +599,7 @@ func TestVerifyMandatoryAnnotationsFunction(t *testing.T) { digest := godigest.FromBytes(content) So(digest, ShouldNotBeNil) - err = ioutil.WriteFile(path.Join(dir, "zot-test", "blobs", + err = os.WriteFile(path.Join(dir, "zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()), content, 0o600) So(err, ShouldBeNil) diff --git a/pkg/extensions/scrub/scrub_test.go b/pkg/extensions/scrub/scrub_test.go index 23b38ffb7..17f472519 100644 --- a/pkg/extensions/scrub/scrub_test.go +++ b/pkg/extensions/scrub/scrub_test.go @@ -6,7 +6,6 @@ package scrub_test import ( "context" "fmt" - "io/ioutil" "os" "path" "testing" @@ -34,7 +33,7 @@ func TestScrubExtension(t *testing.T) { port := test.GetFreePort() url := test.GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -92,7 +91,7 @@ func TestScrubExtension(t *testing.T) { port := test.GetFreePort() url := test.GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -157,7 +156,7 @@ func TestScrubExtension(t *testing.T) { port := test.GetFreePort() url := test.GetBaseURL(port) - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -219,7 +218,7 @@ func TestScrubExtension(t *testing.T) { func TestRunScrubRepo(t *testing.T) { Convey("Blobs integrity not affected", t, func(c C) { - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -247,7 +246,7 @@ func TestRunScrubRepo(t *testing.T) { }) Convey("Blobs integrity affected", t, func(c C) { - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -283,7 +282,7 @@ func TestRunScrubRepo(t *testing.T) { }) Convey("CheckRepo error - not enough permissions to access root directory", t, func(c C) { - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up diff --git a/pkg/extensions/search/common/common_test.go b/pkg/extensions/search/common/common_test.go index a96ef4501..6792bbe57 100644 --- a/pkg/extensions/search/common/common_test.go +++ b/pkg/extensions/search/common/common_test.go @@ -8,7 +8,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/url" "os" "os/exec" @@ -157,7 +157,7 @@ func signUsingCosign(port string) error { defer func() { _ = os.Chdir(cwd) }() - tdir, err := ioutil.TempDir("", "cosign") + tdir, err := os.MkdirTemp("", "cosign") if err != nil { return err } @@ -194,7 +194,7 @@ func signUsingNotary(port string) error { defer func() { _ = os.Chdir(cwd) }() - tdir, err := ioutil.TempDir("", "notation") + tdir, err := os.MkdirTemp("", "notation") if err != nil { return err } @@ -653,7 +653,7 @@ func TestExpandedRepoInfo(t *testing.T) { indexPath := path.Join(tempDir, repo1, "index.json") indexFile, err := os.Open(indexPath) So(err, ShouldBeNil) - buf, err := ioutil.ReadAll(indexFile) + buf, err := io.ReadAll(indexFile) So(err, ShouldBeNil) var index ispec.Index diff --git a/pkg/extensions/search/cve/cve_test.go b/pkg/extensions/search/cve/cve_test.go index f16a96c12..cefdbdddd 100644 --- a/pkg/extensions/search/cve/cve_test.go +++ b/pkg/extensions/search/cve/cve_test.go @@ -8,7 +8,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "path" @@ -75,7 +74,7 @@ type CVE struct { } func testSetup() error { - dir, err := ioutil.TempDir("", "util_test") + dir, err := os.MkdirTemp("", "util_test") if err != nil { return err } @@ -128,7 +127,7 @@ func generateTestData() error { // nolint: gocyclo return err } - if err = ioutil.WriteFile(path.Join(dbDir, "zot-nonreadable-test", "index.json"), buf, 0o111); err != nil { + if err = os.WriteFile(path.Join(dbDir, "zot-nonreadable-test", "index.json"), buf, 0o111); err != nil { return err } @@ -194,7 +193,7 @@ func generateTestData() error { // nolint: gocyclo return err } - if err = ioutil.WriteFile(path.Join(dbDir, "zot-squashfs-test", "oci-layout"), buf, 0o644); err != nil { //nolint: gosec + if err = os.WriteFile(path.Join(dbDir, "zot-squashfs-test", "oci-layout"), buf, 0o644); err != nil { //nolint: gosec return err } @@ -312,7 +311,7 @@ func generateTestData() error { // nolint: gocyclo } func makeTestFile(fileName, content string) error { - if err := ioutil.WriteFile(fileName, []byte(content), 0o600); err != nil { + if err := os.WriteFile(fileName, []byte(content), 0o600); err != nil { panic(err) } @@ -680,12 +679,12 @@ func TestHTTPOptionsResponse(t *testing.T) { ctlr := api.NewController(conf) - firstDir, err := ioutil.TempDir("", "oci-repo-test") + firstDir, err := os.MkdirTemp("", "oci-repo-test") if err != nil { panic(err) } - secondDir, err := ioutil.TempDir("", "oci-repo-test") + secondDir, err := os.MkdirTemp("", "oci-repo-test") if err != nil { panic(err) } diff --git a/pkg/extensions/search/digest/digest_test.go b/pkg/extensions/search/digest/digest_test.go index 1050f65f0..809943dae 100644 --- a/pkg/extensions/search/digest/digest_test.go +++ b/pkg/extensions/search/digest/digest_test.go @@ -7,7 +7,6 @@ package digestinfo_test import ( "context" "encoding/json" - "io/ioutil" "os" "testing" "time" @@ -63,12 +62,12 @@ func init() { } func testSetup() error { - dir, err := ioutil.TempDir("", "digest_test") + dir, err := os.MkdirTemp("", "digest_test") if err != nil { return err } - subDir, err := ioutil.TempDir("", "sub_digest_test") + subDir, err := os.MkdirTemp("", "sub_digest_test") if err != nil { return err } @@ -305,7 +304,7 @@ func TestDigestSearchHTTPSubPaths(t *testing.T) { ctlr := api.NewController(conf) - globalDir, err := ioutil.TempDir("", "digest_test") + globalDir, err := os.MkdirTemp("", "digest_test") if err != nil { panic(err) } diff --git a/pkg/extensions/sync/sync_disabled_test.go b/pkg/extensions/sync/sync_disabled_test.go index 2d59e3039..2d2f51566 100644 --- a/pkg/extensions/sync/sync_disabled_test.go +++ b/pkg/extensions/sync/sync_disabled_test.go @@ -5,7 +5,6 @@ package sync_test import ( "context" - "io/ioutil" "os" "testing" @@ -27,7 +26,7 @@ func TestSyncExtension(t *testing.T) { globalDir := t.TempDir() defaultValue := true - logFile, err := ioutil.TempFile(globalDir, "zot-log*.txt") + logFile, err := os.CreateTemp(globalDir, "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) diff --git a/pkg/extensions/sync/sync_internal_test.go b/pkg/extensions/sync/sync_internal_test.go index 19a6874f1..5c9349b73 100644 --- a/pkg/extensions/sync/sync_internal_test.go +++ b/pkg/extensions/sync/sync_internal_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "net/url" "os" "path" @@ -97,13 +96,13 @@ func TestSyncInternal(t *testing.T) { _, err = getFileCredentials("/path/to/inexistent/file") So(err, ShouldNotBeNil) - tempFile, err := ioutil.TempFile("", "sync-credentials-") + tempFile, err := os.CreateTemp("", "sync-credentials-") if err != nil { panic(err) } content := []byte(`{`) - if err := ioutil.WriteFile(tempFile.Name(), content, 0o600); err != nil { + if err := os.WriteFile(tempFile.Name(), content, 0o600); err != nil { panic(err) } diff --git a/pkg/extensions/sync/sync_test.go b/pkg/extensions/sync/sync_test.go index be9dcf28b..d7ff94a9b 100644 --- a/pkg/extensions/sync/sync_test.go +++ b/pkg/extensions/sync/sync_test.go @@ -11,7 +11,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -117,7 +116,7 @@ func startUpstreamServer( CACert: CACert, } - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) if err != nil { panic(err) } @@ -199,7 +198,7 @@ func startDownstreamServer( CACert: CACert, } - caCert, err := ioutil.ReadFile(CACert) + caCert, err := os.ReadFile(CACert) if err != nil { panic(err) } @@ -778,7 +777,7 @@ func TestConfigReloader(t *testing.T) { destConfig.HTTP.Port = destPort - destDir, err := ioutil.TempDir("", "oci-dest-repo-test") + destDir, err := os.MkdirTemp("", "oci-dest-repo-test") if err != nil { panic(err) } @@ -791,7 +790,7 @@ func TestConfigReloader(t *testing.T) { destConfig.Extensions.Search = nil destConfig.Extensions.Sync = syncConfig - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) defer os.Remove(logFile.Name()) // clean up @@ -808,7 +807,7 @@ func TestConfigReloader(t *testing.T) { "http": {"address": "127.0.0.1", "port": "%s"}, "log": {"level": "debug", "output": "%s"}}`, destDir, destPort, logFile.Name()) - cfgfile, err := ioutil.TempFile("", "zot-test*.json") + cfgfile, err := os.CreateTemp("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(cfgfile.Name()) // clean up @@ -913,7 +912,7 @@ func TestMandatoryAnnotations(t *testing.T) { destConfig.Extensions = &extconf.ExtensionConfig{} destConfig.Extensions.Sync = syncConfig - logFile, err := ioutil.TempFile("", "zot-log*.txt") + logFile, err := os.CreateTemp("", "zot-log*.txt") So(err, ShouldBeNil) destConfig.Log.Output = logFile.Name() @@ -1033,7 +1032,7 @@ func TestTLS(t *testing.T) { var srcIndex ispec.Index var destIndex ispec.Index - srcBuf, err := ioutil.ReadFile(path.Join(srcDir, testImage, "index.json")) + srcBuf, err := os.ReadFile(path.Join(srcDir, testImage, "index.json")) if err != nil { panic(err) } @@ -1097,7 +1096,7 @@ func TestTLS(t *testing.T) { // wait till ready for { - destBuf, _ := ioutil.ReadFile(path.Join(destDir, testImage, "index.json")) + destBuf, _ := os.ReadFile(path.Join(destDir, testImage, "index.json")) _ = json.Unmarshal(destBuf, &destIndex) time.Sleep(500 * time.Millisecond) if len(destIndex.Manifests) > 0 { @@ -1737,13 +1736,13 @@ func TestInvalidCerts(t *testing.T) { } func makeCredentialsFile(fileContent string) string { - tmpfile, err := ioutil.TempFile("", "sync-credentials-") + tmpfile, err := os.CreateTemp("", "sync-credentials-") if err != nil { panic(err) } content := []byte(fileContent) - if err := ioutil.WriteFile(tmpfile.Name(), content, 0o600); err != nil { + if err := os.WriteFile(tmpfile.Name(), content, 0o600); err != nil { panic(err) } @@ -2341,7 +2340,7 @@ func TestPeriodicallySignaturesErr(t *testing.T) { // trigger permission denied on upstream manifest var srcIndex ispec.Index - srcBuf, err := ioutil.ReadFile(path.Join(srcDir, repoName, "index.json")) + srcBuf, err := os.ReadFile(path.Join(srcDir, repoName, "index.json")) if err != nil { panic(err) } @@ -2424,7 +2423,7 @@ func TestPeriodicallySignaturesErr(t *testing.T) { var artifactManifest artifactspec.Manifest for _, ref := range referrers.References { refPath := path.Join(srcDir, repoName, "blobs", string(ref.Digest.Algorithm()), ref.Digest.Hex()) - body, err := ioutil.ReadFile(refPath) + body, err := os.ReadFile(refPath) So(err, ShouldBeNil) err = json.Unmarshal(body, &artifactManifest) @@ -2592,7 +2591,7 @@ func TestSignatures(t *testing.T) { var artifactManifest artifactspec.Manifest for _, ref := range referrers.References { refPath := path.Join(srcDir, repoName, "blobs", string(ref.Digest.Algorithm()), ref.Digest.Hex()) - body, err := ioutil.ReadFile(refPath) + body, err := os.ReadFile(refPath) So(err, ShouldBeNil) err = json.Unmarshal(body, &artifactManifest) @@ -3105,7 +3104,7 @@ func TestOnDemandPullsOnce(t *testing.T) { case <-done: return default: - dirs, err := ioutil.ReadDir(syncBlobUploadDir) + dirs, err := os.ReadDir(syncBlobUploadDir) if err != nil { continue } @@ -3499,7 +3498,7 @@ func TestSyncOnlyDiff(t *testing.T) { time.Sleep(3 * time.Second) - body, err := ioutil.ReadFile(path.Join(destDir, "sync.log")) + body, err := os.ReadFile(path.Join(destDir, "sync.log")) if err != nil { log.Fatalf("unable to read file: %v", err) } @@ -3680,7 +3679,7 @@ func TestSyncSignaturesDiff(t *testing.T) { So(err, ShouldBeNil) defer func() { _ = os.Chdir(cwd) }() - tdir, err := ioutil.TempDir("", "sigs") + tdir, err := os.MkdirTemp("", "sigs") So(err, ShouldBeNil) _ = os.Chdir(tdir) @@ -3773,7 +3772,7 @@ func TestSyncSignaturesDiff(t *testing.T) { // now add new signatures to upstream and let sync detect that upstream signatures changed and pull them So(os.RemoveAll(tdir), ShouldBeNil) - tdir, err = ioutil.TempDir("", "sigs") + tdir, err = os.MkdirTemp("", "sigs") So(err, ShouldBeNil) defer os.RemoveAll(tdir) _ = os.Chdir(tdir) @@ -3807,7 +3806,7 @@ func TestSyncSignaturesDiff(t *testing.T) { var srcIndex ispec.Index var destIndex ispec.Index - srcBuf, err := ioutil.ReadFile(path.Join(srcDir, repoName, "index.json")) + srcBuf, err := os.ReadFile(path.Join(srcDir, repoName, "index.json")) if err != nil { panic(err) } @@ -3816,7 +3815,7 @@ func TestSyncSignaturesDiff(t *testing.T) { panic(err) } - destBuf, err := ioutil.ReadFile(path.Join(destDir, repoName, "index.json")) + destBuf, err := os.ReadFile(path.Join(destDir, repoName, "index.json")) if err != nil { panic(err) } diff --git a/pkg/extensions/sync/utils.go b/pkg/extensions/sync/utils.go index b37b4c31a..b82d81437 100644 --- a/pkg/extensions/sync/utils.go +++ b/pkg/extensions/sync/utils.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "net/url" "os" "path" @@ -187,7 +186,7 @@ func getRepoDestination(remoteRepo string, content Content) string { // Get sync.FileCredentials from file. func getFileCredentials(filepath string) (CredentialsFile, error) { - credsFile, err := ioutil.ReadFile(filepath) + credsFile, err := os.ReadFile(filepath) if err != nil { return nil, err } @@ -225,7 +224,7 @@ func getHTTPClient(regCfg *RegistryConfig, upstreamURL string, credentials Crede clientKey := path.Join(regCfg.CertDir, "client.key") caCertPath := path.Join(regCfg.CertDir, "ca.crt") - caCert, err := ioutil.ReadFile(caCertPath) + caCert, err := os.ReadFile(caCertPath) if err != nil { log.Error().Str("errorType", TypeOf(err)). Err(err).Msg("couldn't read CA certificate") diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index 89c7dfcbb..4e8d26bba 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -8,7 +8,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "path" @@ -108,7 +108,7 @@ func TestAuditLogMessages(t *testing.T) { So(resp, ShouldNotBeNil) So(resp.StatusCode(), ShouldEqual, http.StatusOK) - byteValue, _ := ioutil.ReadAll(auditFile) + byteValue, _ := io.ReadAll(auditFile) So(len(byteValue), ShouldEqual, 0) }) @@ -120,13 +120,13 @@ func TestAuditLogMessages(t *testing.T) { So(resp.StatusCode(), ShouldEqual, http.StatusAccepted) // wait until the file is populated - byteValue, _ := ioutil.ReadAll(auditFile) + byteValue, _ := io.ReadAll(auditFile) for { if len(byteValue) != 0 { break } time.Sleep(100 * time.Millisecond) - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) } var auditLog AuditLog @@ -153,13 +153,13 @@ func TestAuditLogMessages(t *testing.T) { So(location, ShouldNotBeEmpty) // wait until the file is populated - byteValue, _ := ioutil.ReadAll(auditFile) + byteValue, _ := io.ReadAll(auditFile) for { if len(byteValue) != 0 { break } time.Sleep(100 * time.Millisecond) - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) } var auditLog AuditLog @@ -188,13 +188,13 @@ func TestAuditLogMessages(t *testing.T) { So(resp.Header().Get(constants.DistContentDigestKey), ShouldNotBeEmpty) // wait until the file is populated - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) for { if len(byteValue) != 0 { break } time.Sleep(100 * time.Millisecond) - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) } err = json.Unmarshal(byteValue, &auditLog) @@ -216,13 +216,13 @@ func TestAuditLogMessages(t *testing.T) { So(resp.Header().Get("Content-Length"), ShouldEqual, "0") // wait until the file is populated - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) for { if len(byteValue) != 0 { break } time.Sleep(100 * time.Millisecond) - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) } err = json.Unmarshal(byteValue, &auditLog) @@ -249,13 +249,13 @@ func TestAuditLogMessages(t *testing.T) { So(location, ShouldNotBeEmpty) // wait until the file is populated - byteValue, _ := ioutil.ReadAll(auditFile) + byteValue, _ := io.ReadAll(auditFile) for { if len(byteValue) != 0 { break } time.Sleep(100 * time.Millisecond) - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) } var auditLog AuditLog @@ -284,13 +284,13 @@ func TestAuditLogMessages(t *testing.T) { So(resp.StatusCode(), ShouldEqual, http.StatusAccepted) // wait until the file is populated - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) for { if len(byteValue) != 0 { break } time.Sleep(100 * time.Millisecond) - byteValue, _ = ioutil.ReadAll(auditFile) + byteValue, _ = io.ReadAll(auditFile) } err = json.Unmarshal(byteValue, &auditLog) @@ -317,7 +317,7 @@ func TestLogErrors(t *testing.T) { Convey("Get error when opening log file", t, func() { dir := t.TempDir() logPath := path.Join(dir, "logFile") - err := ioutil.WriteFile(logPath, []byte{}, 0o000) + err := os.WriteFile(logPath, []byte{}, 0o000) So(err, ShouldBeNil) So(func() { _ = log.NewLogger(zerolog.DebugLevel.String(), logPath) @@ -333,7 +333,7 @@ func TestNewAuditLogger(t *testing.T) { Convey("Get error when opening audit file", t, func() { dir := t.TempDir() logPath := path.Join(dir, "logFile") - err := ioutil.WriteFile(logPath, []byte{}, 0o000) + err := os.WriteFile(logPath, []byte{}, 0o000) So(err, ShouldBeNil) So(func() { _ = log.NewAuditLogger(zerolog.DebugLevel.String(), logPath) diff --git a/pkg/storage/local.go b/pkg/storage/local.go index 70b948560..a6892d131 100644 --- a/pkg/storage/local.go +++ b/pkg/storage/local.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "io/fs" - "io/ioutil" "os" "path" "path/filepath" @@ -269,7 +268,7 @@ func (is *ImageStoreLocal) ValidateRepo(name string) (bool, error) { return false, zerr.ErrRepoNotFound } - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { is.log.Error().Err(err).Str("dir", dir).Msg("unable to read directory") @@ -300,7 +299,7 @@ func (is *ImageStoreLocal) ValidateRepo(name string) (bool, error) { } } - buf, err := ioutil.ReadFile(path.Join(dir, ispec.ImageLayoutFile)) + buf, err := os.ReadFile(path.Join(dir, ispec.ImageLayoutFile)) if err != nil { return false, err } @@ -326,7 +325,7 @@ func (is *ImageStoreLocal) GetRepositories() ([]string, error) { is.RLock(&lockLatency) defer is.RUnlock(&lockLatency) - _, err := ioutil.ReadDir(dir) + _, err := os.ReadDir(dir) if err != nil { is.log.Error().Err(err).Msg("failure walking storage root-dir") @@ -373,7 +372,7 @@ func (is *ImageStoreLocal) GetImageTags(repo string) ([]string, error) { is.RLock(&lockLatency) defer is.RUnlock(&lockLatency) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -411,7 +410,7 @@ func (is *ImageStoreLocal) GetImageManifest(repo, reference string) ([]byte, str is.RLock(&lockLatency) defer is.RUnlock(&lockLatency) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -460,7 +459,7 @@ func (is *ImageStoreLocal) GetImageManifest(repo, reference string) ([]byte, str p := path.Join(dir, "blobs", digest.Algorithm().String(), digest.Encoded()) - buf, err = ioutil.ReadFile(p) + buf, err = os.ReadFile(p) if err != nil { is.log.Error().Err(err).Str("blob", p).Msg("failed to read manifest") @@ -538,7 +537,8 @@ func (is *ImageStoreLocal) validateOCIManifest(repo, reference string, manifest return "", nil } -/** +/* +* before an image index manifest is pushed to a repo, its constituent manifests are pushed first, so when updating/removing this image index manifest, we also need to determine if there are other image index manifests which refer to the @@ -551,7 +551,7 @@ func pruneImageManifestsFromIndex(dir string, digest godigest.Digest, // nolint: ) ([]ispec.Descriptor, error) { indexPath := path.Join(dir, "blobs", digest.Algorithm().String(), digest.Encoded()) - buf, err := ioutil.ReadFile(indexPath) + buf, err := os.ReadFile(indexPath) if err != nil { log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -574,7 +574,7 @@ func pruneImageManifestsFromIndex(dir string, digest godigest.Digest, // nolint: for _, otherIndex := range otherImgIndexes { indexPath := path.Join(dir, "blobs", otherIndex.Digest.Algorithm().String(), otherIndex.Digest.Encoded()) - buf, err := ioutil.ReadFile(indexPath) + buf, err := os.ReadFile(indexPath) if err != nil { log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -683,7 +683,7 @@ func (is *ImageStoreLocal) PutImageManifest(repo, reference, mediaType string, / dir := path.Join(is.rootDir, repo) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -874,7 +874,7 @@ func (is *ImageStoreLocal) DeleteImageManifest(repo, reference string) error { is.Lock(&lockLatency) defer is.Unlock(&lockLatency) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -1572,7 +1572,7 @@ func (is *ImageStoreLocal) GetIndexContent(repo string) ([]byte, error) { is.RLock(&lockLatency) defer is.RUnlock(&lockLatency) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { if os.IsNotExist(err) { is.log.Error().Err(err).Str("dir", dir).Msg("index.json doesn't exist") @@ -1646,7 +1646,7 @@ func (is *ImageStoreLocal) GetReferrers(repo, digest, artifactType string) ([]ar is.RLock(&lockLatency) defer is.RUnlock(&lockLatency) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json") @@ -1675,7 +1675,7 @@ func (is *ImageStoreLocal) GetReferrers(repo, digest, artifactType string) ([]ar p := path.Join(dir, "blobs", manifest.Digest.Algorithm().String(), manifest.Digest.Encoded()) - buf, err = ioutil.ReadFile(p) + buf, err = os.ReadFile(p) if err != nil { is.log.Error().Err(err).Str("blob", p).Msg("failed to read manifest") @@ -1718,7 +1718,7 @@ func (is *ImageStoreLocal) GetReferrers(repo, digest, artifactType string) ([]ar func (is *ImageStoreLocal) writeFile(filename string, data []byte) error { if !is.commit { - return ioutil.WriteFile(filename, data, DefaultFilePerms) + return os.WriteFile(filename, data, DefaultFilePerms) } fhandle, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, DefaultFilePerms) @@ -1753,7 +1753,7 @@ func ValidateHardLink(rootDir string) error { return err } - err := ioutil.WriteFile(path.Join(rootDir, "hardlinkcheck.txt"), + err := os.WriteFile(path.Join(rootDir, "hardlinkcheck.txt"), []byte("check whether hardlinks work on filesystem"), DefaultFilePerms) if err != nil { return err diff --git a/pkg/storage/local_elevated_test.go b/pkg/storage/local_elevated_test.go index 66244bfb6..f0ed204c5 100644 --- a/pkg/storage/local_elevated_test.go +++ b/pkg/storage/local_elevated_test.go @@ -6,7 +6,6 @@ package storage_test import ( "bytes" _ "crypto/sha256" - "io/ioutil" "os" "os/exec" "path" @@ -57,7 +56,7 @@ func TestElevatedPrivilegesInvalidDedupe(t *testing.T) { panic(err) } - err = ioutil.WriteFile(path.Join(dir, "dedupe2", "blobs/sha256", blobDigest1), content, 0o755) // nolint: gosec + err = os.WriteFile(path.Join(dir, "dedupe2", "blobs/sha256", blobDigest1), content, 0o755) // nolint: gosec if err != nil { panic(err) } diff --git a/pkg/storage/local_test.go b/pkg/storage/local_test.go index 98ecce908..252a22864 100644 --- a/pkg/storage/local_test.go +++ b/pkg/storage/local_test.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/fs" - "io/ioutil" "math/big" "os" "path" @@ -181,7 +180,7 @@ func TestGetReferrers(t *testing.T) { digest := godigest.FromBytes(body) buf := bytes.NewBuffer(body) buflen := buf.Len() - err = ioutil.WriteFile(path.Join(imgStore.RootDir(), //nolint: gosec + err = os.WriteFile(path.Join(imgStore.RootDir(), //nolint: gosec "zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()), buf.Bytes(), 0o644) So(err, ShouldBeNil) @@ -871,7 +870,7 @@ func FuzzGetReferrers(f *testing.F) { digest := godigest.FromBytes([]byte(data)) buf := bytes.NewBuffer([]byte(data)) buflen := buf.Len() - err = ioutil.WriteFile(path.Join(imgStore.RootDir(), //nolint: gosec + err = os.WriteFile(path.Join(imgStore.RootDir(), //nolint: gosec "zot-test", "blobs", digest.Algorithm().String(), digest.Encoded()), buf.Bytes(), 0o644) if err != nil { @@ -1119,7 +1118,7 @@ func TestNegativeCases(t *testing.T) { } // Init repo should fail if repo is a file. - err = ioutil.WriteFile(path.Join(dir, "file-test"), []byte("this is test file"), 0o755) // nolint:gosec + err = os.WriteFile(path.Join(dir, "file-test"), []byte("this is test file"), 0o755) // nolint:gosec So(err, ShouldBeNil) err = imgStore.InitRepo("file-test") So(err, ShouldNotBeNil) @@ -1162,17 +1161,17 @@ func TestNegativeCases(t *testing.T) { panic(err) } - err = ioutil.WriteFile(path.Join(dir, "invalid-test", "blobs"), []byte{}, 0o755) // nolint: gosec + err = os.WriteFile(path.Join(dir, "invalid-test", "blobs"), []byte{}, 0o755) // nolint: gosec if err != nil { panic(err) } - err = ioutil.WriteFile(path.Join(dir, "invalid-test", "index.json"), []byte{}, 0o755) // nolint: gosec + err = os.WriteFile(path.Join(dir, "invalid-test", "index.json"), []byte{}, 0o755) // nolint: gosec if err != nil { panic(err) } - err = ioutil.WriteFile(path.Join(dir, "invalid-test", ispec.ImageLayoutFile), []byte{}, 0o755) // nolint: gosec + err = os.WriteFile(path.Join(dir, "invalid-test", ispec.ImageLayoutFile), []byte{}, 0o755) // nolint: gosec if err != nil { panic(err) } @@ -1193,7 +1192,7 @@ func TestNegativeCases(t *testing.T) { So(err, ShouldNotBeNil) So(isValid, ShouldEqual, false) - err = ioutil.WriteFile(path.Join(dir, "invalid-test", ispec.ImageLayoutFile), []byte("{}"), 0o755) // nolint: gosec + err = os.WriteFile(path.Join(dir, "invalid-test", ispec.ImageLayoutFile), []byte("{}"), 0o755) // nolint: gosec if err != nil { panic(err) } @@ -1203,7 +1202,7 @@ func TestNegativeCases(t *testing.T) { So(err, ShouldEqual, zerr.ErrRepoBadVersion) So(isValid, ShouldEqual, false) - files, err := ioutil.ReadDir(path.Join(dir, "test")) + files, err := os.ReadDir(path.Join(dir, "test")) if err != nil { panic(err) } @@ -1265,7 +1264,7 @@ func TestNegativeCases(t *testing.T) { So(err, ShouldNotBeNil) So(os.RemoveAll(path.Join(dir, "test")), ShouldBeNil) So(imgStore.InitRepo("test"), ShouldBeNil) - So(ioutil.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600), ShouldBeNil) + So(os.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600), ShouldBeNil) _, err = imgStore.GetImageTags("test") So(err, ShouldNotBeNil) }) @@ -1308,7 +1307,7 @@ func TestNegativeCases(t *testing.T) { So(imgStore.InitRepo("test"), ShouldBeNil) - err = ioutil.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600) + err = os.WriteFile(path.Join(dir, "test", "index.json"), []byte{}, 0o600) if err != nil { panic(err) } @@ -1385,7 +1384,7 @@ func TestNegativeCases(t *testing.T) { dir := t.TempDir() filePath := path.Join(dir, "file.txt") - err := ioutil.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec + err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec if err != nil { panic(err) } @@ -1440,7 +1439,7 @@ func TestHardLink(t *testing.T) { dir := t.TempDir() filePath := path.Join(dir, "file.txt") - err := ioutil.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec + err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec if err != nil { panic(err) } @@ -1454,7 +1453,7 @@ func TestHardLink(t *testing.T) { err := storage.ValidateHardLink(dir) So(err, ShouldBeNil) - err = ioutil.WriteFile(path.Join(dir, "hardtest.txt"), []byte("testing hard link code"), 0o644) //nolint: gosec + err = os.WriteFile(path.Join(dir, "hardtest.txt"), []byte("testing hard link code"), 0o644) //nolint: gosec if err != nil { panic(err) } @@ -1895,7 +1894,7 @@ func TestGarbageCollectForImageStore(t *testing.T) { dir := t.TempDir() Convey("Garbage collect error for repo with config removed", func() { - logFile, _ := ioutil.TempFile("", "zot-log*.txt") + logFile, _ := os.CreateTemp("", "zot-log*.txt") defer os.Remove(logFile.Name()) // clean up @@ -1927,7 +1926,7 @@ func TestGarbageCollectForImageStore(t *testing.T) { }) Convey("Garbage collect error - not enough permissions to access index.json", func() { - logFile, _ := ioutil.TempFile("", "zot-log*.txt") + logFile, _ := os.CreateTemp("", "zot-log*.txt") defer os.Remove(logFile.Name()) // clean up @@ -2020,7 +2019,7 @@ func TestGetRepositoriesError(t *testing.T) { err := os.Mkdir(path.Join(dir, "test-dir"), 0o755) So(err, ShouldBeNil) - err = ioutil.WriteFile(path.Join(dir, "test-dir/test-file"), []byte("this is test file"), 0o000) + err = os.WriteFile(path.Join(dir, "test-dir/test-file"), []byte("this is test file"), 0o000) So(err, ShouldBeNil) _, err = imgStore.GetRepositories() diff --git a/pkg/storage/s3/s3_test.go b/pkg/storage/s3/s3_test.go index ac01afe20..041d59b61 100644 --- a/pkg/storage/s3/s3_test.go +++ b/pkg/storage/s3/s3_test.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path" "strings" @@ -217,7 +216,7 @@ func (s *StorageDriverMock) Reader(ctx context.Context, path string, offset int6 return s.ReaderFn(ctx, path, offset) } - return ioutil.NopCloser(strings.NewReader("")), nil + return io.NopCloser(strings.NewReader("")), nil } func (s *StorageDriverMock) Writer(ctx context.Context, path string, isAppend bool) (driver.FileWriter, error) { @@ -477,7 +476,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { return &FileWriterMock{}, errS3 }, ReaderFn: func(ctx context.Context, path string, offset int64) (io.ReadCloser, error) { - return ioutil.NopCloser(strings.NewReader("")), errS3 + return io.NopCloser(strings.NewReader("")), errS3 }, WalkFn: func(ctx context.Context, path string, f driver.WalkFn) error { return errS3 @@ -655,7 +654,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { return &FileWriterMock{}, errS3 }, }) - _, err := imgStore.PutBlobChunkStreamed(testImage, "uuid", ioutil.NopCloser(strings.NewReader(""))) + _, err := imgStore.PutBlobChunkStreamed(testImage, "uuid", io.NopCloser(strings.NewReader(""))) So(err, ShouldNotBeNil) }) @@ -667,7 +666,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }}, nil }, }) - _, err := imgStore.PutBlobChunkStreamed(testImage, "uuid", ioutil.NopCloser(strings.NewReader(""))) + _, err := imgStore.PutBlobChunkStreamed(testImage, "uuid", io.NopCloser(strings.NewReader(""))) So(err, ShouldNotBeNil) }) @@ -677,7 +676,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { return &FileWriterMock{}, errS3 }, }) - _, err := imgStore.PutBlobChunk(testImage, "uuid", 0, 100, ioutil.NopCloser(strings.NewReader(""))) + _, err := imgStore.PutBlobChunk(testImage, "uuid", 0, 100, io.NopCloser(strings.NewReader(""))) So(err, ShouldNotBeNil) }) @@ -694,7 +693,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, nil }, }) - _, err := imgStore.PutBlobChunk(testImage, "uuid", 0, 100, ioutil.NopCloser(strings.NewReader(""))) + _, err := imgStore.PutBlobChunk(testImage, "uuid", 0, 100, io.NopCloser(strings.NewReader(""))) So(err, ShouldNotBeNil) }) @@ -708,7 +707,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, nil }, }) - _, err := imgStore.PutBlobChunk(testImage, "uuid", 12, 100, ioutil.NopCloser(strings.NewReader(""))) + _, err := imgStore.PutBlobChunk(testImage, "uuid", 12, 100, io.NopCloser(strings.NewReader(""))) So(err, ShouldNotBeNil) }) @@ -723,7 +722,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, }) d := godigest.FromBytes([]byte("test")) - err := imgStore.FinishBlobUpload(testImage, "uuid", ioutil.NopCloser(strings.NewReader("")), d.String()) + err := imgStore.FinishBlobUpload(testImage, "uuid", io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) @@ -738,7 +737,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, }) d := godigest.FromBytes([]byte("test")) - err := imgStore.FinishBlobUpload(testImage, "uuid", ioutil.NopCloser(strings.NewReader("")), d.String()) + err := imgStore.FinishBlobUpload(testImage, "uuid", io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) @@ -749,7 +748,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, }) d := godigest.FromBytes([]byte("test")) - err := imgStore.FinishBlobUpload(testImage, "uuid", ioutil.NopCloser(strings.NewReader("")), d.String()) + err := imgStore.FinishBlobUpload(testImage, "uuid", io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) @@ -760,7 +759,7 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, }) d := godigest.FromBytes([]byte("")) - err := imgStore.FinishBlobUpload(testImage, "uuid", ioutil.NopCloser(strings.NewReader("")), d.String()) + err := imgStore.FinishBlobUpload(testImage, "uuid", io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) @@ -771,14 +770,14 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, }) d := godigest.FromBytes([]byte("")) - _, _, err := imgStore.FullBlobUpload(testImage, ioutil.NopCloser(strings.NewReader("")), d.String()) + _, _, err := imgStore.FullBlobUpload(testImage, io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) Convey("Test FullBlobUpload2", t, func(c C) { imgStore = createMockStorage(testDir, tdir, false, &StorageDriverMock{}) d := godigest.FromBytes([]byte(" ")) - _, _, err := imgStore.FullBlobUpload(testImage, ioutil.NopCloser(strings.NewReader("")), d.String()) + _, _, err := imgStore.FullBlobUpload(testImage, io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) @@ -789,14 +788,14 @@ func TestNegativeCasesObjectsStorage(t *testing.T) { }, }) d := godigest.FromBytes([]byte("")) - _, _, err := imgStore.FullBlobUpload(testImage, ioutil.NopCloser(strings.NewReader("")), d.String()) + _, _, err := imgStore.FullBlobUpload(testImage, io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) Convey("Test GetBlob", t, func(c C) { imgStore = createMockStorage(testDir, tdir, false, &StorageDriverMock{ ReaderFn: func(ctx context.Context, path string, offset int64) (io.ReadCloser, error) { - return ioutil.NopCloser(strings.NewReader("")), errS3 + return io.NopCloser(strings.NewReader("")), errS3 }, }) d := godigest.FromBytes([]byte("")) @@ -1010,12 +1009,12 @@ func TestS3Dedupe(t *testing.T) { Convey("Check backward compatibility - switch dedupe to false", func() { /* copy cache to the new storage with dedupe false (doing this because we already have a cache object holding the lock on cache db file) */ - input, err := ioutil.ReadFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName)) + input, err := os.ReadFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName)) So(err, ShouldBeNil) tdir = t.TempDir() - err = ioutil.WriteFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName), input, 0o600) + err = os.WriteFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName), input, 0o600) So(err, ShouldBeNil) storeDriver, imgStore, _ := createObjectsStore(testDir, tdir, false) @@ -1747,12 +1746,12 @@ func TestS3DedupeErr(t *testing.T) { So(err, ShouldBeNil) // copy cache db to the new imagestore - input, err := ioutil.ReadFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName)) + input, err := os.ReadFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName)) So(err, ShouldBeNil) tdir = t.TempDir() - err = ioutil.WriteFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName), input, 0o600) + err = os.WriteFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName), input, 0o600) So(err, ShouldBeNil) imgStore = createMockStorage(testDir, tdir, true, &StorageDriverMock{ @@ -1784,12 +1783,12 @@ func TestS3DedupeErr(t *testing.T) { So(err, ShouldBeNil) // copy cache db to the new imagestore - input, err := ioutil.ReadFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName)) + input, err := os.ReadFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName)) So(err, ShouldBeNil) tdir = t.TempDir() - err = ioutil.WriteFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName), input, 0o600) + err = os.WriteFile(path.Join(tdir, s3.CacheDBName+storage.DBExtensionName), input, 0o600) So(err, ShouldBeNil) imgStore = createMockStorage(testDir, tdir, true, &StorageDriverMock{ @@ -1802,10 +1801,10 @@ func TestS3DedupeErr(t *testing.T) { }, ReaderFn: func(ctx context.Context, path string, offset int64) (io.ReadCloser, error) { if strings.Contains(path, "repo1/dst1") { - return ioutil.NopCloser(strings.NewReader("")), errS3 + return io.NopCloser(strings.NewReader("")), errS3 } - return ioutil.NopCloser(strings.NewReader("")), nil + return io.NopCloser(strings.NewReader("")), nil }, }) @@ -1856,7 +1855,7 @@ func TestS3DedupeErr(t *testing.T) { }, }) d := godigest.FromBytes([]byte("")) - _, _, err := imgStore.FullBlobUpload(testImage, ioutil.NopCloser(strings.NewReader("")), d.String()) + _, _, err := imgStore.FullBlobUpload(testImage, io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) @@ -1868,7 +1867,7 @@ func TestS3DedupeErr(t *testing.T) { }, }) d := godigest.FromBytes([]byte("")) - err := imgStore.FinishBlobUpload(testImage, "uuid", ioutil.NopCloser(strings.NewReader("")), d.String()) + err := imgStore.FinishBlobUpload(testImage, "uuid", io.NopCloser(strings.NewReader("")), d.String()) So(err, ShouldNotBeNil) }) } diff --git a/pkg/storage/scrub.go b/pkg/storage/scrub.go index 23498e8fd..131486166 100644 --- a/pkg/storage/scrub.go +++ b/pkg/storage/scrub.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "path" "strings" @@ -105,7 +104,7 @@ func CheckRepo(imageName string, imgStore ImageStore) ([]ScrubImageResult, error imgStore.RLock(&lockLatency) defer imgStore.RUnlock(&lockLatency) - buf, err := ioutil.ReadFile(path.Join(dir, "index.json")) + buf, err := os.ReadFile(path.Join(dir, "index.json")) if err != nil { return results, err } diff --git a/pkg/storage/scrub_test.go b/pkg/storage/scrub_test.go index 9b9365dc0..c0e36646f 100644 --- a/pkg/storage/scrub_test.go +++ b/pkg/storage/scrub_test.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "os" "path" "regexp" @@ -164,7 +163,7 @@ func TestCheckAllBlobsIntegrity(t *testing.T) { So(actual, ShouldContainSubstring, "test 1.0 affected parse application/vnd.oci.image.manifest.v1+json") // put manifest content back to file - err = ioutil.WriteFile(manifestFile, content, 0o600) + err = os.WriteFile(manifestFile, content, 0o600) So(err, ShouldBeNil) }) @@ -192,7 +191,7 @@ func TestCheckAllBlobsIntegrity(t *testing.T) { So(actual, ShouldContainSubstring, "test 1.0 affected stat: parse application/vnd.oci.image.config.v1+json") // put config content back to file - err = ioutil.WriteFile(configFile, content, 0o600) + err = os.WriteFile(configFile, content, 0o600) So(err, ShouldBeNil) }) @@ -220,7 +219,7 @@ func TestCheckAllBlobsIntegrity(t *testing.T) { So(actual, ShouldContainSubstring, "test 1.0 affected blob: bad blob digest") // put layer content back to file - err = ioutil.WriteFile(layerFile, content, 0o600) + err = os.WriteFile(layerFile, content, 0o600) So(err, ShouldBeNil) }) diff --git a/pkg/test/common.go b/pkg/test/common.go index ab675f99f..6c9bd92db 100644 --- a/pkg/test/common.go +++ b/pkg/test/common.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "math/big" "net/http" @@ -67,14 +66,14 @@ func MakeHtpasswdFile() string { } func MakeHtpasswdFileFromString(fileContent string) string { - htpasswdFile, err := ioutil.TempFile("", "htpasswd-") + htpasswdFile, err := os.CreateTemp("", "htpasswd-") if err != nil { panic(err) } // bcrypt(username="test", passwd="test") content := []byte(fileContent) - if err := ioutil.WriteFile(htpasswdFile.Name(), content, 0o600); err != nil { //nolint:gomnd + if err := os.WriteFile(htpasswdFile.Name(), content, 0o600); err != nil { //nolint:gomnd panic(err) } @@ -109,9 +108,9 @@ func CopyFiles(sourceDir, destDir string) error { return fmt.Errorf("CopyFiles os.MkdirAll failed: %w", err) } - files, err := ioutil.ReadDir(sourceDir) + files, err := os.ReadDir(sourceDir) if err != nil { - return fmt.Errorf("CopyFiles ioutil.ReadDir failed: %w", err) + return fmt.Errorf("CopyFiles os.ReadDir failed: %w", err) } for _, file := range files { @@ -258,7 +257,7 @@ func GetOciLayoutDigests(imagePath string) (godigest.Digest, godigest.Digest, go panic(err) } - manifestBuf, err := ioutil.ReadAll(manifestBlob) + manifestBuf, err := io.ReadAll(manifestBlob) if err != nil { panic(err) } diff --git a/pkg/test/common_test.go b/pkg/test/common_test.go index 39ace4bd7..c011ee76f 100644 --- a/pkg/test/common_test.go +++ b/pkg/test/common_test.go @@ -6,7 +6,6 @@ package test_test import ( "context" "encoding/json" - "io/ioutil" "os" "path" "testing" @@ -58,7 +57,7 @@ func TestCopyFiles(t *testing.T) { dir := t.TempDir() filePath := path.Join(dir, "file.txt") - err := ioutil.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec + err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec if err != nil { panic(err) } @@ -103,7 +102,7 @@ func TestGetOciLayoutDigests(t *testing.T) { panic(err) } - buf, err := ioutil.ReadFile(path.Join(dir, "test-manifest", "index.json")) + buf, err := os.ReadFile(path.Join(dir, "test-manifest", "index.json")) if err != nil { panic(err) } diff --git a/pkg/test/mocks/storage_driver_mock.go b/pkg/test/mocks/storage_driver_mock.go index 945dd6c66..17f8c0ccf 100644 --- a/pkg/test/mocks/storage_driver_mock.go +++ b/pkg/test/mocks/storage_driver_mock.go @@ -3,7 +3,6 @@ package mocks import ( "context" "io" - "io/ioutil" "strings" "time" @@ -58,7 +57,7 @@ func (s *StorageDriverMock) Reader(ctx context.Context, path string, offset int6 return s.ReaderFn(ctx, path, offset) } - return ioutil.NopCloser(strings.NewReader("")), nil + return io.NopCloser(strings.NewReader("")), nil } func (s *StorageDriverMock) Writer(ctx context.Context, path string, isAppend bool) (driver.FileWriter, error) {