From e6fa5a7156fc567c9979b4ee1d522633810f1f46 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Thu, 13 Oct 2022 12:10:58 -0500 Subject: [PATCH] Replace usage of io/ioutil package (#9613) `io/ioutil` has been deprecated since go 1.16 and the linter started to complain about it. --- cli/cmd/check_test.go | 6 +++--- cli/cmd/inject_test.go | 5 ++--- cli/cmd/install_test.go | 14 +++++++------- cli/cmd/metrics_diagnostics_util_test.go | 4 ++-- cli/cmd/options.go | 7 +++---- cli/cmd/upgrade_test.go | 7 +++---- cni-plugin/test/install-cni_test.go | 17 ++++++++--------- controller/cmd/identity/main.go | 3 +-- controller/proxy-injector/fake/factory.go | 16 ++++++++-------- controller/proxy-injector/webhook.go | 4 ++-- pkg/config/config.go | 4 ++-- pkg/issuercerts/issuercerts.go | 8 ++++---- pkg/k8s/metrics.go | 4 ++-- pkg/k8s/portforward.go | 6 +++--- pkg/profiles/openapi.go | 3 +-- pkg/protohttp/protohttp_test.go | 6 +++--- pkg/tls/cred.go | 6 +++--- pkg/util/http.go | 3 +-- proxy-identity/main.go | 5 ++--- test/integration/viz/edges/edges_test.go | 3 +-- testutil/test_data_diff.go | 7 +++---- testutil/test_helper.go | 5 ++--- viz/cmd/tap_test.go | 6 +++--- web/srv/api_handlers_test.go | 13 +++++++------ 24 files changed, 76 insertions(+), 86 deletions(-) diff --git a/cli/cmd/check_test.go b/cli/cmd/check_test.go index 2e22fa251..d0e9ab3ca 100644 --- a/cli/cmd/check_test.go +++ b/cli/cmd/check_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "os" "testing" "github.com/linkerd/linkerd2/pkg/healthcheck" @@ -33,7 +33,7 @@ func TestCheckStatus(t *testing.T) { output := bytes.NewBufferString("") healthcheck.RunChecks(output, stderr, hc, tableOutput) - goldenFileBytes, err := ioutil.ReadFile("testdata/check_output.golden") + goldenFileBytes, err := os.ReadFile("testdata/check_output.golden") if err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -67,7 +67,7 @@ func TestCheckStatus(t *testing.T) { output := bytes.NewBufferString("") healthcheck.RunChecks(output, stderr, hc, jsonOutput) - goldenFileBytes, err := ioutil.ReadFile("testdata/check_output_json.golden") + goldenFileBytes, err := os.ReadFile("testdata/check_output_json.golden") if err != nil { t.Fatalf("Unexpected error: %v", err) } diff --git a/cli/cmd/inject_test.go b/cli/cmd/inject_test.go index bc1f74c12..a0bbfccb9 100644 --- a/cli/cmd/inject_test.go +++ b/cli/cmd/inject_test.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -630,10 +629,10 @@ func TestWalk(t *testing.T) { file1 = filepath.Join(tmpFolderRoot, "root.txt") file2 = filepath.Join(tmpFolderData, "data.txt") ) - if err := ioutil.WriteFile(file1, data, 0600); err != nil { + if err := os.WriteFile(file1, data, 0600); err != nil { t.Fatal("Unexpected error: ", err) } - if err := ioutil.WriteFile(file2, data, 0600); err != nil { + if err := os.WriteFile(file2, data, 0600); err != nil { t.Fatal("Unexpected error: ", err) } diff --git a/cli/cmd/install_test.go b/cli/cmd/install_test.go index 70a404977..c88268b17 100644 --- a/cli/cmd/install_test.go +++ b/cli/cmd/install_test.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "os" "path/filepath" "testing" @@ -319,7 +319,7 @@ func testInstallOptionsHA(ha bool) (*charts.Values, error) { return nil, err } - data, err := ioutil.ReadFile(filepath.Join("testdata", "valid-crt.pem")) + data, err := os.ReadFile(filepath.Join("testdata", "valid-crt.pem")) if err != nil { return nil, err } @@ -336,7 +336,7 @@ func testInstallOptionsHA(ha bool) (*charts.Values, error) { } values.Identity.Issuer.TLS.KeyPEM = key - data, err = ioutil.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem")) + data, err = os.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem")) if err != nil { return nil, err } @@ -376,15 +376,15 @@ func testInstallValues() (*charts.Values, error) { values.PolicyController.Image.Version = installControlPlaneVersion values.HeartbeatSchedule = fakeHeartbeatSchedule() - identityCert, err := ioutil.ReadFile(filepath.Join("testdata", "valid-crt.pem")) + identityCert, err := os.ReadFile(filepath.Join("testdata", "valid-crt.pem")) if err != nil { return nil, err } - identityKey, err := ioutil.ReadFile(filepath.Join("testdata", "valid-key.pem")) + identityKey, err := os.ReadFile(filepath.Join("testdata", "valid-key.pem")) if err != nil { return nil, err } - trustAnchorsPEM, err := ioutil.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem")) + trustAnchorsPEM, err := os.ReadFile(filepath.Join("testdata", "valid-trust-anchors.pem")) if err != nil { return nil, err } @@ -516,7 +516,7 @@ func TestValidate(t *testing.T) { } values.Identity.Issuer.TLS.KeyPEM = key - ca, err := ioutil.ReadFile(filepath.Join("testdata", tc.crtFilePrefix+"-trust-anchors.pem")) + ca, err := os.ReadFile(filepath.Join("testdata", tc.crtFilePrefix+"-trust-anchors.pem")) if err != nil { t.Fatal(err) } diff --git a/cli/cmd/metrics_diagnostics_util_test.go b/cli/cmd/metrics_diagnostics_util_test.go index a26dccc7b..a764b42bb 100644 --- a/cli/cmd/metrics_diagnostics_util_test.go +++ b/cli/cmd/metrics_diagnostics_util_test.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "os" "testing" @@ -84,7 +84,7 @@ func Test_obfuscateMetrics(t *testing.T) { t.Errorf("error opening test input file: %v\n", err) } - fileBytes, err := ioutil.ReadAll(file) + fileBytes, err := io.ReadAll(file) if err != nil { t.Errorf("error reading test input file: %v\n", err) } diff --git a/cli/cmd/options.go b/cli/cmd/options.go index 115dc4b55..aeec9908e 100644 --- a/cli/cmd/options.go +++ b/cli/cmd/options.go @@ -7,7 +7,6 @@ import ( "path/filepath" "fmt" - "io/ioutil" "net" "strings" "time" @@ -152,7 +151,7 @@ func makeInstallUpgradeFlags(defaults *l5dcharts.Values) ([]flag.Flag, *pflag.Fl "A path to a PEM-encoded file containing Linkerd Identity trust anchors (generated by default)", func(values *l5dcharts.Values, value string) error { if value != "" { - data, err := ioutil.ReadFile(filepath.Clean(value)) + data, err := os.ReadFile(filepath.Clean(value)) if err != nil { return err } @@ -184,7 +183,7 @@ func makeInstallUpgradeFlags(defaults *l5dcharts.Values) ([]flag.Flag, *pflag.Fl } func loadCrtPEM(path string) (string, error) { - data, err := ioutil.ReadFile(filepath.Clean(path)) + data, err := os.ReadFile(filepath.Clean(path)) if err != nil { return "", err } @@ -197,7 +196,7 @@ func loadCrtPEM(path string) (string, error) { } func loadKeyPEM(path string) (string, error) { - data, err := ioutil.ReadFile(filepath.Clean(path)) + data, err := os.ReadFile(filepath.Clean(path)) if err != nil { return "", err } diff --git a/cli/cmd/upgrade_test.go b/cli/cmd/upgrade_test.go index 1c353374f..e9286cdbb 100644 --- a/cli/cmd/upgrade_test.go +++ b/cli/cmd/upgrade_test.go @@ -6,7 +6,6 @@ import ( "encoding/base64" "fmt" "io" - "io/ioutil" "os" "strings" "testing" @@ -462,15 +461,15 @@ func generateCerts(t *testing.T, name string, b64encode bool) issuerCerts { keyPem := strings.TrimSpace(issuer.Cred.EncodePrivateKeyPEM()) crtPem := strings.TrimSpace(issuer.Cred.EncodeCertificatePEM()) - caFile, err := ioutil.TempFile("", "ca.*.pem") + caFile, err := os.CreateTemp("", "ca.*.pem") if err != nil { t.Fatal(err) } - crtFile, err := ioutil.TempFile("", "crt.*.pem") + crtFile, err := os.CreateTemp("", "crt.*.pem") if err != nil { t.Fatal(err) } - keyFile, err := ioutil.TempFile("", "key.*.pem") + keyFile, err := os.CreateTemp("", "key.*.pem") if err != nil { t.Fatal(err) } diff --git a/cni-plugin/test/install-cni_test.go b/cni-plugin/test/install-cni_test.go index 5ceefbf52..cc6fbc114 100644 --- a/cni-plugin/test/install-cni_test.go +++ b/cni-plugin/test/install-cni_test.go @@ -19,7 +19,6 @@ import ( "bytes" "flag" "fmt" - "io/ioutil" "os" "os/exec" "os/user" @@ -58,7 +57,7 @@ func setEnv(key, value string, t *testing.T) { } func mktemp(dir, prefix string, t *testing.T) string { - tempDir, err := ioutil.TempDir(dir, prefix) + tempDir, err := os.MkdirTemp(dir, prefix) if err != nil { testutil.AnnotatedFatalf(t, "couldn't get current working directory", "couldn't get current working directory: %v", err) @@ -77,7 +76,7 @@ func pwd(t *testing.T) string { } func ls(dir string, t *testing.T) []string { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { testutil.AnnotatedFatalf(t, "failed to list files", "failed to list files: %v", err) @@ -90,12 +89,12 @@ func ls(dir string, t *testing.T) []string { } func cp(src, dest string, t *testing.T) { - data, err := ioutil.ReadFile(src) + data, err := os.ReadFile(src) if err != nil { testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to read file %v", src), "failed to read file %v: %v", src, err) } - if err = ioutil.WriteFile(dest, data, 0600); err != nil { + if err = os.WriteFile(dest, data, 0600); err != nil { testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to write file %v", dest), "failed to write file %v: %v", dest, err) } @@ -192,7 +191,7 @@ func startDocker(testNum int, wd string, testWorkRootDir string, tempCNINetDir s containerID, err := cmd.Output() if err != nil { - errFileContents, _ := ioutil.ReadFile(errFileName) + errFileContents, _ := os.ReadFile(errFileName) t.Logf("%v contents:\n\n%v\n\n", errFileName, string(errFileContents)) testutil.Fatalf(t, "test %v ERROR: failed to start docker container '%v', see %v", testNum, dockerImage, errFileName) @@ -214,13 +213,13 @@ func docker(cmd, containerID string, t *testing.T) { // compareConfResult does a string compare of 2 test files. func compareConfResult(testWorkRootDir string, tempCNINetDir string, result string, expected string, t *testing.T) { tempResult := tempCNINetDir + "/" + result - resultFile, err := ioutil.ReadFile(tempResult) + resultFile, err := os.ReadFile(tempResult) if err != nil { testutil.AnnotatedFatalf(t, "failed to read file", "failed to read file %v: %v", tempResult, err) } - expectedFile, err := ioutil.ReadFile(expected) + expectedFile, err := os.ReadFile(expected) if err != nil { testutil.AnnotatedFatalf(t, fmt.Sprintf("failed to read file %v", expected), "failed to read file %v, err: %v", expected, err) @@ -265,7 +264,7 @@ func doTest(testNum int, wd string, initialNetConfFile string, finalNetConfFile if initialNetConfFile != "NONE" { setEnv(cniConfName, initialNetConfFile, t) } - defaultData, err := ioutil.ReadFile(wd + "../deployment/linkerd-cni.conf.default") + defaultData, err := os.ReadFile(wd + "../deployment/linkerd-cni.conf.default") if err != nil { testutil.AnnotatedFatalf(t, "failed to read file linkerd-cni.conf.default", "failed to read file %v, err: %v", wd+"../deployment/linkerd-cni.conf.default", err) diff --git a/controller/cmd/identity/main.go b/controller/cmd/identity/main.go index a93698d7e..d8d1ed54c 100644 --- a/controller/cmd/identity/main.go +++ b/controller/cmd/identity/main.go @@ -4,7 +4,6 @@ import ( "context" "flag" "fmt" - "io/ioutil" "net" "os" "os/signal" @@ -53,7 +52,7 @@ func Main(args []string) { flags.ConfigureAndParse(cmd, args) - identityTrustAnchorPEM, err := ioutil.ReadFile(k8s.MountPathTrustRootsPEM) + identityTrustAnchorPEM, err := os.ReadFile(k8s.MountPathTrustRootsPEM) if err != nil { log.Fatalf("could not read identity trust anchors PEM: %s", err.Error()) } diff --git a/controller/proxy-injector/fake/factory.go b/controller/proxy-injector/fake/factory.go index d891f7fbf..64db58d79 100644 --- a/controller/proxy-injector/fake/factory.go +++ b/controller/proxy-injector/fake/factory.go @@ -1,7 +1,7 @@ package fake import ( - "io/ioutil" + "os" "path/filepath" admissionv1beta1 "k8s.io/api/admission/v1beta1" @@ -31,7 +31,7 @@ func NewFactory(rootDir string) *Factory { // bytes. If the file doesn't exist in the 'fake/data' folder, an error will be // returned. func (f *Factory) FileContents(filename string) ([]byte, error) { - return ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + return os.ReadFile(filepath.Join(f.rootDir, filename)) } // AdmissionReview returns the content of the specified file as an @@ -40,7 +40,7 @@ func (f *Factory) FileContents(filename string) ([]byte, error) { // ii. the file content isn't a valid YAML structure that can be unmarshalled // into AdmissionReview type func (f *Factory) AdmissionReview(filename string) (*admissionv1beta1.AdmissionReview, error) { - b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + b, err := os.ReadFile(filepath.Join(f.rootDir, filename)) if err != nil { return nil, err } @@ -58,7 +58,7 @@ func (f *Factory) AdmissionReview(filename string) (*admissionv1beta1.AdmissionR // ii. the file content isn't a valid YAML structure that can be unmarshalled // into Deployment type func (f *Factory) Deployment(filename string) (*appsv1.Deployment, error) { - b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + b, err := os.ReadFile(filepath.Join(f.rootDir, filename)) if err != nil { return nil, err } @@ -77,7 +77,7 @@ func (f *Factory) Deployment(filename string) (*appsv1.Deployment, error) { // ii. the file content isn't a valid YAML structure that can be unmarshalled // into Container type func (f *Factory) Container(filename string) (*corev1.Container, error) { - b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + b, err := os.ReadFile(filepath.Join(f.rootDir, filename)) if err != nil { return nil, err } @@ -96,7 +96,7 @@ func (f *Factory) Container(filename string) (*corev1.Container, error) { // ii. the file content isn't a valid YAML structure that can be unmarshalled // into ConfigMap type func (f *Factory) ConfigMap(filename string) (*corev1.ConfigMap, error) { - b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + b, err := os.ReadFile(filepath.Join(f.rootDir, filename)) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (f *Factory) ConfigMap(filename string) (*corev1.ConfigMap, error) { // ii. the file content isn't a valid YAML structure that can be unmarshalled // into Namespace type func (f *Factory) Namespace(filename string) (*corev1.Namespace, error) { - b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + b, err := os.ReadFile(filepath.Join(f.rootDir, filename)) if err != nil { return nil, err } @@ -134,7 +134,7 @@ func (f *Factory) Namespace(filename string) (*corev1.Namespace, error) { // ii. the file content isn't a valid YAML structure that can be unmarshalled // into Volume type func (f *Factory) Volume(filename string) (*corev1.Volume, error) { - b, err := ioutil.ReadFile(filepath.Join(f.rootDir, filename)) + b, err := os.ReadFile(filepath.Join(f.rootDir, filename)) if err != nil { return nil, err } diff --git a/controller/proxy-injector/webhook.go b/controller/proxy-injector/webhook.go index 3a91e1825..a186ad0e0 100644 --- a/controller/proxy-injector/webhook.go +++ b/controller/proxy-injector/webhook.go @@ -3,7 +3,7 @@ package injector import ( "context" "fmt" - "io/ioutil" + "os" "strings" "github.com/linkerd/linkerd2/controller/k8s" @@ -45,7 +45,7 @@ func Inject(linkerdNamespace string) webhook.Handler { return nil, err } - caPEM, err := ioutil.ReadFile(pkgK8s.MountPathTrustRootsPEM) + caPEM, err := os.ReadFile(pkgK8s.MountPathTrustRootsPEM) if err != nil { return nil, err } diff --git a/pkg/config/config.go b/pkg/config/config.go index 968d4d93f..4e5ecefe3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -3,7 +3,7 @@ package config import ( "context" "fmt" - "io/ioutil" + "os" "path/filepath" "github.com/linkerd/linkerd2/pkg/k8s" @@ -19,7 +19,7 @@ import ( // Values returns the Value struct from the linkerd-config ConfigMap func Values(path string) (*l5dcharts.Values, error) { p := filepath.Clean(path) - configYaml, err := ioutil.ReadFile(p) + configYaml, err := os.ReadFile(p) if err != nil { return nil, fmt.Errorf("failed to read config file: %w", err) } diff --git a/pkg/issuercerts/issuercerts.go b/pkg/issuercerts/issuercerts.go index 480ae3c09..3919a4ce3 100644 --- a/pkg/issuercerts/issuercerts.go +++ b/pkg/issuercerts/issuercerts.go @@ -6,7 +6,7 @@ import ( "crypto/rsa" "crypto/x509" "fmt" - "io/ioutil" + "os" "path/filepath" "time" @@ -86,12 +86,12 @@ func FetchExternalIssuerData(ctx context.Context, api kubernetes.Interface, cont // LoadIssuerCrtAndKeyFromFiles loads the issuer certificate and key from files func LoadIssuerCrtAndKeyFromFiles(keyPEMFile, crtPEMFile string) (string, string, error) { - key, err := ioutil.ReadFile(filepath.Clean(keyPEMFile)) + key, err := os.ReadFile(filepath.Clean(keyPEMFile)) if err != nil { return "", "", err } - crt, err := ioutil.ReadFile(filepath.Clean(crtPEMFile)) + crt, err := os.ReadFile(filepath.Clean(crtPEMFile)) if err != nil { return "", "", err } @@ -106,7 +106,7 @@ func LoadIssuerDataFromFiles(keyPEMFile, crtPEMFile, trustPEMFile string) (*Issu return nil, err } - anchors, err := ioutil.ReadFile(filepath.Clean(trustPEMFile)) + anchors, err := os.ReadFile(filepath.Clean(trustPEMFile)) if err != nil { return nil, err } diff --git a/pkg/k8s/metrics.go b/pkg/k8s/metrics.go index 6caa73e7d..15edfcc3c 100644 --- a/pkg/k8s/metrics.go +++ b/pkg/k8s/metrics.go @@ -2,7 +2,7 @@ package k8s import ( "fmt" - "io/ioutil" + "io" "net/http" "os" @@ -45,5 +45,5 @@ func getResponse(url string) ([]byte, error) { return nil, err } defer resp.Body.Close() - return ioutil.ReadAll(resp.Body) + return io.ReadAll(resp.Body) } diff --git a/pkg/k8s/portforward.go b/pkg/k8s/portforward.go index 0ce443245..7f8f1f533 100644 --- a/pkg/k8s/portforward.go +++ b/pkg/k8s/portforward.go @@ -3,7 +3,7 @@ package k8s import ( "context" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/url" @@ -178,8 +178,8 @@ func (pf *PortForward) run() error { return err } - out := ioutil.Discard - errOut := ioutil.Discard + out := io.Discard + errOut := io.Discard if pf.emitLogs { out = os.Stdout errOut = os.Stderr diff --git a/pkg/profiles/openapi.go b/pkg/profiles/openapi.go index 4d46a89ba..877d6a570 100644 --- a/pkg/profiles/openapi.go +++ b/pkg/profiles/openapi.go @@ -3,7 +3,6 @@ package profiles import ( "fmt" "io" - "io/ioutil" "net/http" "path" "sort" @@ -29,7 +28,7 @@ func RenderOpenAPI(fileName, namespace, name, clusterDomain string, w io.Writer) return err } - bytes, err := ioutil.ReadAll(input) + bytes, err := io.ReadAll(input) if err != nil { return fmt.Errorf("Error reading file: %w", err) } diff --git a/pkg/protohttp/protohttp_test.go b/pkg/protohttp/protohttp_test.go index e15ba2fc0..cdd28d265 100644 --- a/pkg/protohttp/protohttp_test.go +++ b/pkg/protohttp/protohttp_test.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "log" "net/http" "strings" @@ -363,7 +363,7 @@ func TestCheckIfResponseHasError(t *testing.T) { message := SerializeAsPayload(protoInBytes) response := &http.Response{ Header: make(http.Header), - Body: ioutil.NopCloser(bytes.NewReader(message)), + Body: io.NopCloser(bytes.NewReader(message)), StatusCode: http.StatusInternalServerError, } response.Header.Set(errorHeader, "error") @@ -394,7 +394,7 @@ func TestCheckIfResponseHasError(t *testing.T) { response := &http.Response{ Header: make(http.Header), - Body: ioutil.NopCloser(bytes.NewReader(j)), + Body: io.NopCloser(bytes.NewReader(j)), StatusCode: http.StatusForbidden, Status: "403 Forbidden", } diff --git a/pkg/tls/cred.go b/pkg/tls/cred.go index 51372a62a..886b9edf6 100644 --- a/pkg/tls/cred.go +++ b/pkg/tls/cred.go @@ -9,7 +9,7 @@ import ( "encoding/pem" "errors" "fmt" - "io/ioutil" + "os" "path/filepath" "time" ) @@ -195,12 +195,12 @@ func ValidateAndCreateCreds(crt, key string) (*Cred, error) { // ReadPEMCreds reads PEM-encoded credentials from the named files. func ReadPEMCreds(keyPath, crtPath string) (*Cred, error) { - keyb, err := ioutil.ReadFile(filepath.Clean(keyPath)) + keyb, err := os.ReadFile(filepath.Clean(keyPath)) if err != nil { return nil, err } - crtb, err := ioutil.ReadFile(filepath.Clean(crtPath)) + crtb, err := os.ReadFile(filepath.Clean(crtPath)) if err != nil { return nil, err } diff --git a/pkg/util/http.go b/pkg/util/http.go index 1a4bc6862..e603a7a4a 100644 --- a/pkg/util/http.go +++ b/pkg/util/http.go @@ -3,7 +3,6 @@ package util import ( "fmt" "io" - "io/ioutil" "strings" httpPb "github.com/linkerd/linkerd2-proxy-api/go/http_types" @@ -55,7 +54,7 @@ func ParseMethod(method string) *httpPb.HttpMethod { // reached, the full bytes are returned. If the limit is reached, an error is // returned. func ReadAllLimit(r io.Reader, limit int) ([]byte, error) { - bytes, err := ioutil.ReadAll(io.LimitReader(r, int64(limit))) + bytes, err := io.ReadAll(io.LimitReader(r, int64(limit))) if err != nil { return nil, err } diff --git a/proxy-identity/main.go b/proxy-identity/main.go index f51e56de3..caae5ebaf 100644 --- a/proxy-identity/main.go +++ b/proxy-identity/main.go @@ -7,7 +7,6 @@ import ( "crypto/x509/pkix" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "syscall" @@ -116,7 +115,7 @@ func generateAndStoreKey(p string) (key *ecdsa.PrivateKey, err error) { } pemb := tls.EncodePrivateKeyP8(key) - err = ioutil.WriteFile(p, pemb, 0600) + err = os.WriteFile(p, pemb, 0600) return } @@ -138,7 +137,7 @@ func generateAndStoreCSR(p, id string, key *ecdsa.PrivateKey) ([]byte, error) { return nil, fmt.Errorf("failed to create CSR: %w", err) } - if err = ioutil.WriteFile(p, csrb, 0600); err != nil { + if err = os.WriteFile(p, csrb, 0600); err != nil { return nil, fmt.Errorf("failed to write CSR: %w", err) } diff --git a/test/integration/viz/edges/edges_test.go b/test/integration/viz/edges/edges_test.go index 39f93d8a0..f096c5f7a 100644 --- a/test/integration/viz/edges/edges_test.go +++ b/test/integration/viz/edges/edges_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "regexp" "strings" @@ -108,7 +107,7 @@ func TestDirectEdges(t *testing.T) { } ip = strings.Trim(ip, "\"") // strip quotes - b, err := ioutil.ReadFile("testdata/slow-cooker.yaml") + b, err := os.ReadFile("testdata/slow-cooker.yaml") if err != nil { testutil.AnnotatedError(t, "error reading file slow-cooker.yaml", err) } diff --git a/testutil/test_data_diff.go b/testutil/test_data_diff.go index f6ab6c6d3..2ac4b2d79 100644 --- a/testutil/test_data_diff.go +++ b/testutil/test_data_diff.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path/filepath" "testing" @@ -83,7 +82,7 @@ func ReadTestdata(fileName string) string { panic(fmt.Sprintf("Failed to open expected output file: %v", err)) } - fixture, err := ioutil.ReadAll(file) + fixture, err := io.ReadAll(file) if err != nil { panic(fmt.Sprintf("Failed to read expected output file: %v", err)) } @@ -110,14 +109,14 @@ func unmarshalYAML(data []byte) ([]interface{}, error) { func writeTestdata(fileName string, data []byte) { p := filepath.Join("testdata", fileName) - if err := ioutil.WriteFile(p, data, 0600); err != nil { + if err := os.WriteFile(p, data, 0600); err != nil { panic(err) } } func writeRejects(origFileName string, data []byte, rejectPath string) { p := filepath.Join(rejectPath, origFileName+".rej") - if err := ioutil.WriteFile(p, data, 0600); err != nil { + if err := os.WriteFile(p, data, 0600); err != nil { panic(err) } } diff --git a/testutil/test_helper.go b/testutil/test_helper.go index 30b886551..8b100f831 100644 --- a/testutil/test_helper.go +++ b/testutil/test_helper.go @@ -9,7 +9,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net/http" "os" "os/exec" @@ -656,7 +655,7 @@ func (h *TestHelper) HTTPGetURL(url string) (string, error) { } defer resp.Body.Close() - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("Error reading response body: %w", err) } @@ -728,7 +727,7 @@ func (h *TestHelper) DownloadCLIBinary(filepath, version string) error { // ReadFile reads a file from disk and returns the contents as a string. func ReadFile(file string) (string, error) { - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if err != nil { return "", err } diff --git a/viz/cmd/tap_test.go b/viz/cmd/tap_test.go index 75fe6e703..b65b288d0 100644 --- a/viz/cmd/tap_test.go +++ b/viz/cmd/tap_test.go @@ -3,9 +3,9 @@ package cmd import ( "bytes" "context" - "io/ioutil" "net/http" "net/http/httptest" + "os" "testing" "github.com/golang/protobuf/ptypes/duration" @@ -149,7 +149,7 @@ func busyTest(t *testing.T, output string) { goldenFilePath = "testdata/tap_busy_output.golden" } - goldenFileBytes, err := ioutil.ReadFile(goldenFilePath) + goldenFileBytes, err := os.ReadFile(goldenFilePath) if err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -207,7 +207,7 @@ func TestRequestTapByResourceFromAPI(t *testing.T) { t.Fatalf("Unexpected error: %v", err) } - goldenFileBytes, err := ioutil.ReadFile("testdata/tap_empty_output.golden") + goldenFileBytes, err := os.ReadFile("testdata/tap_empty_output.golden") if err != nil { t.Fatalf("Unexpected error: %v", err) } diff --git a/web/srv/api_handlers_test.go b/web/srv/api_handlers_test.go index b43edb7d3..9a48be774 100644 --- a/web/srv/api_handlers_test.go +++ b/web/srv/api_handlers_test.go @@ -4,9 +4,10 @@ import ( "bytes" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "net/http/httptest" + "os" "testing" "github.com/go-test/deep" @@ -71,7 +72,7 @@ func TestHandleApiCheck(t *testing.T) { h.handleAPICheck(w, req, httprouter.Params{}) resp := w.Result() defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("not expecting error reading response body but got: %v", err) } @@ -83,7 +84,7 @@ func TestHandleApiCheck(t *testing.T) { if diff := deep.Equal(resp.Header, expectedHeaders); diff != nil { t.Errorf("Unexpected header: %v", diff) } - apiCheckOutputGolden, err := ioutil.ReadFile("testdata/api_check_output.json") + apiCheckOutputGolden, err := os.ReadFile("testdata/api_check_output.json") if err != nil { t.Fatalf("not expecting error reading api check output golden file but got: %v", err) } @@ -129,7 +130,7 @@ func TestHandleApiGateway(t *testing.T) { handler.handleAPIGateways(recorder, req, httprouter.Params{}) resp := recorder.Result() defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("not expecting error reading response body but got: %v", err) } @@ -146,7 +147,7 @@ func TestHandleApiGateway(t *testing.T) { t.Errorf("Unexpected header: %v", diff) } - apiGatewayOutputGolden, err := ioutil.ReadFile("testdata/api_gateway_output.json") + apiGatewayOutputGolden, err := os.ReadFile("testdata/api_gateway_output.json") if err != nil { t.Fatalf("not expecting error reading api check output golden file but got: %v", err) } @@ -171,7 +172,7 @@ func TestHandleApiGateway(t *testing.T) { handler.handleAPIGateways(recorder, req, httprouter.Params{}) resp := recorder.Result() defer resp.Body.Close() - _, err := ioutil.ReadAll(resp.Body) + _, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("not expecting error reading response body but got: %v", err) }