Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil (#1504)
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <[email protected]>
Co-authored-by: chenk <[email protected]>
  • Loading branch information
testwill and chen-keinan authored Dec 5, 2023
1 parent 292678a commit c3e3c4c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 35 deletions.
3 changes: 1 addition & 2 deletions check/controls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions check/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package check

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -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())
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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))
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions cmd/kubernetes_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/kubernetes_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down

0 comments on commit c3e3c4c

Please sign in to comment.