Skip to content

Commit

Permalink
Replaced deprecated io/ioutil functions (#768)
Browse files Browse the repository at this point in the history
Signed-off-by: slab713 <109306207+slab713@users.noreply.github.com>
  • Loading branch information
slab713 authored Sep 2, 2022
1 parent 6ae793e commit 8ffb053
Show file tree
Hide file tree
Showing 36 changed files with 249 additions and 280 deletions.
7 changes: 3 additions & 4 deletions cmd/zb/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
mrand "math/rand"
"net/http"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/zb/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
crand "crypto/rand"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"math/big"
"net"
Expand Down Expand Up @@ -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)
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/x509"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"runtime"
"strings"
goSync "sync"
Expand Down Expand Up @@ -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"))
}

Expand Down Expand Up @@ -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)
}
Expand Down
45 changes: 22 additions & 23 deletions pkg/api/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
goerrors "errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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").
Expand All @@ -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
Expand Down Expand 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
Expand Down Expand 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
Expand Down Expand 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
Expand Down Expand 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
Expand Down Expand 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
Expand Down Expand 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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"crypto/x509"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/client_elevated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 8ffb053

Please sign in to comment.