From c3e3c4c31c26e6dddfe937e46f3f0dc8c98aa774 Mon Sep 17 00:00:00 2001 From: guangwu Date: Tue, 5 Dec 2023 16:52:24 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil (#1504) Signed-off-by: guoguangwu Co-authored-by: chenk --- check/controls_test.go | 3 +-- check/test_test.go | 3 +-- cmd/common.go | 3 +-- cmd/common_test.go | 22 +++++++++++----------- cmd/kubernetes_version.go | 8 ++++---- cmd/kubernetes_version_test.go | 7 +++---- cmd/run_test.go | 5 ++--- cmd/util_test.go | 13 ++++++------- 8 files changed, 29 insertions(+), 35 deletions(-) diff --git a/check/controls_test.go b/check/controls_test.go index 5393eef..c6d7f1c 100644 --- a/check/controls_test.go +++ b/check/controls_test.go @@ -19,7 +19,6 @@ import ( "encoding/json" "encoding/xml" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -53,7 +52,7 @@ func TestYamlFiles(t *testing.T) { } if !info.IsDir() { t.Logf("reading file: %s", path) - in, err := ioutil.ReadFile(path) + in, err := os.ReadFile(path) if err != nil { t.Fatalf("error opening file %s: %v", path, err) } diff --git a/check/test_test.go b/check/test_test.go index 29eb269..9da55a4 100644 --- a/check/test_test.go +++ b/check/test_test.go @@ -16,7 +16,6 @@ package check import ( "fmt" - "io/ioutil" "os" "strings" "testing" @@ -29,7 +28,7 @@ var ( func init() { var err error - in, err = ioutil.ReadFile("data") + in, err = os.ReadFile("data") if err != nil { panic("Failed reading test data: " + err.Error()) } diff --git a/cmd/common.go b/cmd/common.go index dc447e8..6f83cee 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -18,7 +18,6 @@ import ( "bufio" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -71,7 +70,7 @@ func runChecks(nodetype check.NodeType, testYamlFile, detectedVersion string) { os.Exit(1) } - in, err := ioutil.ReadFile(testYamlFile) + in, err := os.ReadFile(testYamlFile) if err != nil { exitWithError(fmt.Errorf("error opening %s test file: %v", testYamlFile, err)) } diff --git a/cmd/common_test.go b/cmd/common_test.go index aa006b1..3627c98 100644 --- a/cmd/common_test.go +++ b/cmd/common_test.go @@ -18,7 +18,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "os" "path" "path/filepath" @@ -681,7 +681,7 @@ func TestPrintSummary(t *testing.T) { os.Stdout = w printSummary(resultTotals, "totals") w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = rescueStdout assert.Contains(t, string(out), "49 checks PASS\n12 checks FAIL\n14 checks WARN\n0 checks INFO\n\n") @@ -700,7 +700,7 @@ func TestPrettyPrintNoSummary(t *testing.T) { noSummary = true prettyPrint(controlsCollection[0], resultTotals) w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = rescueStdout assert.NotContains(t, string(out), "49 checks PASS") @@ -719,7 +719,7 @@ func TestPrettyPrintSummary(t *testing.T) { noSummary = false prettyPrint(controlsCollection[0], resultTotals) w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = rescueStdout assert.Contains(t, string(out), "49 checks PASS") @@ -737,7 +737,7 @@ func TestWriteStdoutOutputNoTotal(t *testing.T) { noTotals = true writeStdoutOutput(controlsCollection) w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = rescueStdout assert.NotContains(t, string(out), "49 checks PASS") @@ -757,7 +757,7 @@ func TestWriteStdoutOutputTotal(t *testing.T) { noTotals = false writeStdoutOutput(controlsCollection) w.Close() - out, _ := ioutil.ReadAll(r) + out, _ := io.ReadAll(r) os.Stdout = rescueStdout @@ -767,7 +767,7 @@ func TestWriteStdoutOutputTotal(t *testing.T) { func parseControlsJsonFile(filepath string) ([]*check.Controls, error) { var result []*check.Controls - d, err := ioutil.ReadFile(filepath) + d, err := os.ReadFile(filepath) if err != nil { return nil, err } @@ -782,7 +782,7 @@ func parseControlsJsonFile(filepath string) ([]*check.Controls, error) { func parseResultJsonFile(filepath string) (JsonOutputFormat, error) { var result JsonOutputFormat - d, err := ioutil.ReadFile(filepath) + d, err := os.ReadFile(filepath) if err != nil { return result, err } @@ -797,7 +797,7 @@ func parseResultJsonFile(filepath string) (JsonOutputFormat, error) { func parseResultNoTotalsJsonFile(filepath string) ([]*check.Controls, error) { var result []*check.Controls - d, err := ioutil.ReadFile(filepath) + d, err := os.ReadFile(filepath) if err != nil { return nil, err } @@ -822,7 +822,7 @@ type restoreFn func() func fakeExecutableInPath(execFile, execCode string) (restoreFn, error) { pathenv := os.Getenv("PATH") - tmp, err := ioutil.TempDir("", "TestfakeExecutableInPath") + tmp, err := os.MkdirTemp("", "TestfakeExecutableInPath") if err != nil { return nil, err } @@ -833,7 +833,7 @@ func fakeExecutableInPath(execFile, execCode string) (restoreFn, error) { } if len(execCode) > 0 { - ioutil.WriteFile(filepath.Join(tmp, execFile), []byte(execCode), 0700) + os.WriteFile(filepath.Join(tmp, execFile), []byte(execCode), 0700) } else { f, err := os.OpenFile(execFile, os.O_CREATE|os.O_EXCL, 0700) if err != nil { diff --git a/cmd/kubernetes_version.go b/cmd/kubernetes_version.go index da7769c..ae11909 100644 --- a/cmd/kubernetes_version.go +++ b/cmd/kubernetes_version.go @@ -5,7 +5,7 @@ import ( "encoding/json" "encoding/pem" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -45,7 +45,7 @@ func getKubeVersionFromRESTAPI() (*KubeVersion, error) { return nil, err } - tb, err := ioutil.ReadFile(tokenfile) + tb, err := os.ReadFile(tokenfile) if err != nil { glog.V(2).Infof("Failed reading token file Error: %s", err) return nil, err @@ -143,11 +143,11 @@ func getWebData(srvURL, token string, cacert *tls.Certificate) ([]byte, error) { return nil, err } - return ioutil.ReadAll(resp.Body) + return io.ReadAll(resp.Body) } func loadCertificate(certFile string) (*tls.Certificate, error) { - cacert, err := ioutil.ReadFile(certFile) + cacert, err := os.ReadFile(certFile) if err != nil { return nil, err } diff --git a/cmd/kubernetes_version_test.go b/cmd/kubernetes_version_test.go index 9b8bc8d..d76fbe2 100644 --- a/cmd/kubernetes_version_test.go +++ b/cmd/kubernetes_version_test.go @@ -3,7 +3,6 @@ package cmd import ( "crypto/tls" "fmt" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -12,13 +11,13 @@ import ( ) func TestLoadCertificate(t *testing.T) { - tmp, err := ioutil.TempDir("", "TestFakeLoadCertificate") + tmp, err := os.MkdirTemp("", "TestFakeLoadCertificate") if err != nil { t.Fatalf("unable to create temp directory: %v", err) } defer os.RemoveAll(tmp) - goodCertFile, _ := ioutil.TempFile(tmp, "good-cert-*") + goodCertFile, _ := os.CreateTemp(tmp, "good-cert-*") _, _ = goodCertFile.Write([]byte(`-----BEGIN CERTIFICATE----- MIICyDCCAbCgAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl cm5ldGVzMB4XDTE5MTEwODAxNDAwMFoXDTI5MTEwNTAxNDAwMFowFTETMBEGA1UE @@ -36,7 +35,7 @@ jLv3UYZRHMpuNS8BJU74BuVzVPHd55RAl+bV8yemdZJ7pPzMvGbZ7zRXWODTDlge CQb9lY+jYErisH8Sq7uABFPvi7RaTh8SS7V7OxqHZvmttNTdZs4TIkk45JK7Y+Xq FAjB57z2NcIgJuVpQnGRYtr/JcH2Qdsq8bLtXaojUIWOOqoTDRLYozdMOOQ= -----END CERTIFICATE-----`)) - badCertFile, _ := ioutil.TempFile(tmp, "bad-cert-*") + badCertFile, _ := os.CreateTemp(tmp, "bad-cert-*") cases := []struct { file string diff --git a/cmd/run_test.go b/cmd/run_test.go index 2be443f..7495e5c 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -1,7 +1,6 @@ package cmd import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -45,7 +44,7 @@ func TestGetTestYamlFiles(t *testing.T) { // Set up temp config directory var err error - cfgDir, err = ioutil.TempDir("", "kube-bench-test") + cfgDir, err = os.MkdirTemp("", "kube-bench-test") if err != nil { t.Fatalf("Failed to create temp directory") } @@ -59,7 +58,7 @@ func TestGetTestYamlFiles(t *testing.T) { // We never expect config.yaml to be returned for _, filename := range []string{"one.yaml", "two.yaml", "three.yaml", "config.yaml"} { - err = ioutil.WriteFile(filepath.Join(d, filename), []byte("hello world"), 0666) + err = os.WriteFile(filepath.Join(d, filename), []byte("hello world"), 0666) if err != nil { t.Fatalf("error writing temp file %s: %v", filename, err) } diff --git a/cmd/util_test.go b/cmd/util_test.go index b5d83fd..2c24a7a 100644 --- a/cmd/util_test.go +++ b/cmd/util_test.go @@ -16,7 +16,6 @@ package cmd import ( "errors" - "io/ioutil" "os" "path/filepath" "reflect" @@ -399,7 +398,7 @@ func TestGetServiceFiles(t *testing.T) { func TestGetDatadirFiles(t *testing.T) { var err error - datadir, err := ioutil.TempDir("", "kube-bench-test-etcd-data-dir") + datadir, err := os.MkdirTemp("", "kube-bench-test-etcd-data-dir") if err != nil { t.Fatalf("Failed to create temp directory") } @@ -474,7 +473,7 @@ func TestMakeSubsitutions(t *testing.T) { func TestGetConfigFilePath(t *testing.T) { var err error - cfgDir, err = ioutil.TempDir("", "kube-bench-test") + cfgDir, err = os.MkdirTemp("", "kube-bench-test") if err != nil { t.Fatalf("Failed to create temp directory") } @@ -484,7 +483,7 @@ func TestGetConfigFilePath(t *testing.T) { if err != nil { t.Fatalf("Failed to create temp dir") } - err = ioutil.WriteFile(filepath.Join(d, "master.yaml"), []byte("hello world"), 0666) + err = os.WriteFile(filepath.Join(d, "master.yaml"), []byte("hello world"), 0666) if err != nil { t.Logf("Failed to create temp file") } @@ -545,7 +544,7 @@ func TestDecrementVersion(t *testing.T) { } func TestGetYamlFilesFromDir(t *testing.T) { - cfgDir, err := ioutil.TempDir("", "kube-bench-test") + cfgDir, err := os.MkdirTemp("", "kube-bench-test") if err != nil { t.Fatalf("Failed to create temp directory") } @@ -557,11 +556,11 @@ func TestGetYamlFilesFromDir(t *testing.T) { t.Fatalf("Failed to create temp dir") } - err = ioutil.WriteFile(filepath.Join(d, "something.yaml"), []byte("hello world"), 0666) + err = os.WriteFile(filepath.Join(d, "something.yaml"), []byte("hello world"), 0666) if err != nil { t.Fatalf("error writing file %v", err) } - err = ioutil.WriteFile(filepath.Join(d, "config.yaml"), []byte("hello world"), 0666) + err = os.WriteFile(filepath.Join(d, "config.yaml"), []byte("hello world"), 0666) if err != nil { t.Fatalf("error writing file %v", err) }