Merge pull request #118403 from skitt/ioutil-sig-storage

storage: stop using deprecated io/ioutil
This commit is contained in:
Kubernetes Prow Robot 2023-06-21 16:49:41 -07:00 committed by GitHub
commit a19153ded9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 38 additions and 56 deletions

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
@ -1644,7 +1643,7 @@ func TestAttacherUnmountDevice(t *testing.T) {
// Make JSON for this object // Make JSON for this object
if tc.jsonFile != "" { if tc.jsonFile != "" {
dataPath := filepath.Join(dir, volDataFileName) dataPath := filepath.Join(dir, volDataFileName)
if err := ioutil.WriteFile(dataPath, []byte(tc.jsonFile), 0644); err != nil { if err := os.WriteFile(dataPath, []byte(tc.jsonFile), 0644); err != nil {
t.Fatalf("error creating %s: %s", dataPath, err) t.Fatalf("error creating %s: %s", dataPath, err)
} }
} }

View File

@ -19,7 +19,6 @@ package csi
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -1065,7 +1064,7 @@ func TestUnmounterTeardown(t *testing.T) {
} }
func TestIsCorruptedDir(t *testing.T) { func TestIsCorruptedDir(t *testing.T) {
existingMountPath, err := ioutil.TempDir(os.TempDir(), "blobfuse-csi-mount-test") existingMountPath, err := os.MkdirTemp(os.TempDir(), "blobfuse-csi-mount-test")
if err != nil { if err != nil {
t.Fatalf("failed to create tmp dir: %v", err) t.Fatalf("failed to create tmp dir: %v", err)
} }

View File

@ -21,7 +21,6 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -143,7 +142,7 @@ func TestSaveVolumeData(t *testing.T) {
} }
// validate content // validate content
data, err := ioutil.ReadFile(file) data, err := os.ReadFile(file)
if !tc.shouldFail && err != nil { if !tc.shouldFail && err != nil {
t.Errorf("failed to read data file: %v", err) t.Errorf("failed to read data file: %v", err)
} }

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
@ -205,7 +204,7 @@ func (f *NodeClient) NodePublishVolume(ctx context.Context, req *csipb.NodePubli
// "Creation of target_path is the responsibility of the SP." // "Creation of target_path is the responsibility of the SP."
// Our plugin depends on it. // Our plugin depends on it.
if req.VolumeCapability.GetBlock() != nil { if req.VolumeCapability.GetBlock() != nil {
if err := ioutil.WriteFile(req.TargetPath, []byte{}, 0644); err != nil { if err := os.WriteFile(req.TargetPath, []byte{}, 0644); err != nil {
return nil, fmt.Errorf("cannot create target path %s for block file: %s", req.TargetPath, err) return nil, fmt.Errorf("cannot create target path %s for block file: %s", req.TargetPath, err)
} }
} else { } else {

View File

@ -17,7 +17,6 @@ limitations under the License.
package volume_test package volume_test
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -60,7 +59,7 @@ func TestMetricsDuGetCapacity(t *testing.T) {
} }
// Write a file and expect Used to increase // Write a file and expect Used to increase
ioutil.WriteFile(filepath.Join(tmpDir, "f1"), []byte("Hello World"), os.ModeTemporary) os.WriteFile(filepath.Join(tmpDir, "f1"), []byte("Hello World"), os.ModeTemporary)
actual, err = metrics.GetMetrics() actual, err = metrics.GetMetrics()
if err != nil { if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err) t.Errorf("Unexpected error when calling GetMetrics %v", err)

View File

@ -18,7 +18,6 @@ package projected
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -874,7 +873,7 @@ func TestCollectDataWithServiceAccountToken(t *testing.T) {
} }
func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) { func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) {
tempDir, err := ioutil.TempDir("", "projected_volume_test.") tempDir, err := os.MkdirTemp("", "projected_volume_test.")
if err != nil { if err != nil {
t.Fatalf("can't make a temp rootdir: %v", err) t.Fatalf("can't make a temp rootdir: %v", err)
} }
@ -1139,7 +1138,7 @@ func TestPluginOptional(t *testing.T) {
} }
datadirPath := filepath.Join(volumePath, datadir) datadirPath := filepath.Join(volumePath, datadir)
infos, err := ioutil.ReadDir(volumePath) infos, err := os.ReadDir(volumePath)
if err != nil { if err != nil {
t.Fatalf("couldn't find volume path, %s", volumePath) t.Fatalf("couldn't find volume path, %s", volumePath)
} }
@ -1151,7 +1150,7 @@ func TestPluginOptional(t *testing.T) {
} }
} }
infos, err = ioutil.ReadDir(datadirPath) infos, err = os.ReadDir(datadirPath)
if err != nil { if err != nil {
t.Fatalf("couldn't find volume data path, %s", datadirPath) t.Fatalf("couldn't find volume data path, %s", datadirPath)
} }
@ -1292,7 +1291,7 @@ func doTestSecretDataInVolume(volumePath string, secret v1.Secret, t *testing.T)
if _, err := os.Stat(secretDataHostPath); err != nil { if _, err := os.Stat(secretDataHostPath); err != nil {
t.Fatalf("SetUp() failed, couldn't find secret data on disk: %v", secretDataHostPath) t.Fatalf("SetUp() failed, couldn't find secret data on disk: %v", secretDataHostPath)
} else { } else {
actualSecretBytes, err := ioutil.ReadFile(secretDataHostPath) actualSecretBytes, err := os.ReadFile(secretDataHostPath)
if err != nil { if err != nil {
t.Fatalf("Couldn't read secret data from: %v", secretDataHostPath) t.Fatalf("Couldn't read secret data from: %v", secretDataHostPath)
} }

View File

@ -19,7 +19,6 @@ package util
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -327,7 +326,7 @@ func shouldWriteFile(path string, content []byte) (bool, error) {
return true, nil return true, nil
} }
contentOnFs, err := ioutil.ReadFile(path) contentOnFs, err := os.ReadFile(path)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -379,7 +378,7 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
// newTimestampDir creates a new timestamp directory // newTimestampDir creates a new timestamp directory
func (w *AtomicWriter) newTimestampDir() (string, error) { func (w *AtomicWriter) newTimestampDir() (string, error) {
tsDir, err := ioutil.TempDir(w.targetDir, time.Now().UTC().Format("..2006_01_02_15_04_05.")) tsDir, err := os.MkdirTemp(w.targetDir, time.Now().UTC().Format("..2006_01_02_15_04_05."))
if err != nil { if err != nil {
klog.Errorf("%s: unable to create new temp directory: %v", w.logContext, err) klog.Errorf("%s: unable to create new temp directory: %v", w.logContext, err)
return "", err return "", err
@ -411,11 +410,11 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
return err return err
} }
if err := ioutil.WriteFile(fullPath, content, mode); err != nil { if err := os.WriteFile(fullPath, content, mode); err != nil {
klog.Errorf("%s: unable to write file %s with mode %v: %v", w.logContext, fullPath, mode, err) klog.Errorf("%s: unable to write file %s with mode %v: %v", w.logContext, fullPath, mode, err)
return err return err
} }
// Chmod is needed because ioutil.WriteFile() ends up calling // Chmod is needed because os.WriteFile() ends up calling
// open(2) to create the file, so the final mode used is "mode & // open(2) to create the file, so the final mode used is "mode &
// ~umask". But we want to make sure the specified mode is used // ~umask". But we want to make sure the specified mode is used
// in the file no matter what the umask is. // in the file no matter what the umask is.

View File

@ -22,7 +22,6 @@ package util
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -766,7 +765,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
return nil return nil
} }
content, err := ioutil.ReadFile(path) content, err := os.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
@ -781,7 +780,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
return nil return nil
} }
d, err := ioutil.ReadDir(targetDir) d, err := os.ReadDir(targetDir)
if err != nil { if err != nil {
t.Errorf("Unable to read dir %v: %v", targetDir, err) t.Errorf("Unable to read dir %v: %v", targetDir, err)
return return
@ -790,7 +789,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
if strings.HasPrefix(info.Name(), "..") { if strings.HasPrefix(info.Name(), "..") {
continue continue
} }
if info.Mode()&os.ModeSymlink != 0 { if info.Type()&os.ModeSymlink != 0 {
p := filepath.Join(targetDir, info.Name()) p := filepath.Join(targetDir, info.Name())
actual, err := os.Readlink(p) actual, err := os.Readlink(p)
if err != nil { if err != nil {

View File

@ -18,7 +18,6 @@ package fs
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -28,21 +27,21 @@ import (
func TestDiskUsage(t *testing.T) { func TestDiskUsage(t *testing.T) {
dir1, err := ioutil.TempDir("", "dir_1") dir1, err := os.MkdirTemp("", "dir_1")
if err != nil { if err != nil {
t.Fatalf("TestDiskUsage failed: %s", err.Error()) t.Fatalf("TestDiskUsage failed: %s", err.Error())
} }
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
tmpfile1, err := ioutil.TempFile(dir1, "test") tmpfile1, err := os.CreateTemp(dir1, "test")
if _, err = tmpfile1.WriteString("just for testing"); err != nil { if _, err = tmpfile1.WriteString("just for testing"); err != nil {
t.Fatalf("TestDiskUsage failed: %s", err.Error()) t.Fatalf("TestDiskUsage failed: %s", err.Error())
} }
dir2, err := ioutil.TempDir(dir1, "dir_2") dir2, err := os.MkdirTemp(dir1, "dir_2")
if err != nil { if err != nil {
t.Fatalf("TestDiskUsage failed: %s", err.Error()) t.Fatalf("TestDiskUsage failed: %s", err.Error())
} }
tmpfile2, err := ioutil.TempFile(dir2, "test") tmpfile2, err := os.CreateTemp(dir2, "test")
if _, err = tmpfile2.WriteString("just for testing"); err != nil { if _, err = tmpfile2.WriteString("just for testing"); err != nil {
t.Fatalf("TestDiskUsage failed: %s", err.Error()) t.Fatalf("TestDiskUsage failed: %s", err.Error())
} }
@ -92,7 +91,7 @@ func TestDiskUsage(t *testing.T) {
} }
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
dir1, err := ioutil.TempDir("", "dir_1") dir1, err := os.MkdirTemp("", "dir_1")
if err != nil { if err != nil {
t.Fatalf("TestInfo failed: %s", err.Error()) t.Fatalf("TestInfo failed: %s", err.Error())
} }
@ -107,7 +106,7 @@ func TestInfo(t *testing.T) {
validateInfo(t, availablebytes, capacity, usage, inodesTotal, inodeUsage, inodesFree) validateInfo(t, availablebytes, capacity, usage, inodesTotal, inodeUsage, inodesFree)
// should pass for file // should pass for file
tmpfile1, err := ioutil.TempFile(dir1, "test") tmpfile1, err := os.CreateTemp(dir1, "test")
if _, err = tmpfile1.WriteString("just for testing"); err != nil { if _, err = tmpfile1.WriteString("just for testing"); err != nil {
t.Fatalf("TestInfo failed: %s", err.Error()) t.Fatalf("TestInfo failed: %s", err.Error())
} }

View File

@ -22,7 +22,6 @@ package common
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
@ -144,7 +143,7 @@ func doRunXFSQuotaCommand(mountpoint string, mountsFile, command string) (string
// See https://bugzilla.redhat.com/show_bug.cgi?id=237120 for an example // See https://bugzilla.redhat.com/show_bug.cgi?id=237120 for an example
// of the problem that could be caused if this were to happen. // of the problem that could be caused if this were to happen.
func runXFSQuotaCommand(mountpoint string, command string) (string, error) { func runXFSQuotaCommand(mountpoint string, command string) (string, error) {
tmpMounts, err := ioutil.TempFile("", "mounts") tmpMounts, err := os.CreateTemp("", "mounts")
if err != nil { if err != nil {
return "", fmt.Errorf("cannot create temporary mount file: %v", err) return "", fmt.Errorf("cannot create temporary mount file: %v", err)
} }

View File

@ -22,7 +22,6 @@ package fsquota
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -267,7 +266,7 @@ func writeProjectFile(base *os.File, projects []projectType) (string, error) {
return "", err return "", err
} }
mode := stat.Mode() & os.ModePerm mode := stat.Mode() & os.ModePerm
f, err := ioutil.TempFile(filepath.Dir(oname), filepath.Base(oname)) f, err := os.CreateTemp(filepath.Dir(oname), filepath.Base(oname))
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -21,7 +21,6 @@ package fsquota
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -97,7 +96,7 @@ type mountpointTest struct {
} }
func testBackingDev1(testcase backingDevTest) error { func testBackingDev1(testcase backingDevTest) error {
tmpfile, err := ioutil.TempFile("", "backingdev") tmpfile, err := os.CreateTemp("", "backingdev")
if err != nil { if err != nil {
return err return err
} }
@ -502,7 +501,7 @@ var quotaTestCases = []quotaTestCase{
} }
func compareProjectsFiles(t *testing.T, testcase quotaTestCase, projectsFile string, projidFile string, enabled bool) { func compareProjectsFiles(t *testing.T, testcase quotaTestCase, projectsFile string, projidFile string, enabled bool) {
bytes, err := ioutil.ReadFile(projectsFile) bytes, err := os.ReadFile(projectsFile)
if err != nil { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
} else { } else {
@ -515,7 +514,7 @@ func compareProjectsFiles(t *testing.T, testcase quotaTestCase, projectsFile str
t.Errorf("Case %v /etc/projects miscompare: expected\n`%s`\ngot\n`%s`\n", testcase.path, p, s) t.Errorf("Case %v /etc/projects miscompare: expected\n`%s`\ngot\n`%s`\n", testcase.path, p, s)
} }
} }
bytes, err = ioutil.ReadFile(projidFile) bytes, err = os.ReadFile(projidFile)
if err != nil { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
} else { } else {
@ -598,7 +597,7 @@ func runCaseDisabled(t *testing.T, testcase quotaTestCase, seq int) bool {
func testAddRemoveQuotas(t *testing.T, enabled bool) { func testAddRemoveQuotas(t *testing.T, enabled bool) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LocalStorageCapacityIsolationFSQuotaMonitoring, enabled)() defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LocalStorageCapacityIsolationFSQuotaMonitoring, enabled)()
tmpProjectsFile, err := ioutil.TempFile("", "projects") tmpProjectsFile, err := os.CreateTemp("", "projects")
if err == nil { if err == nil {
_, err = tmpProjectsFile.WriteString(projectsHeader) _, err = tmpProjectsFile.WriteString(projectsHeader)
} }
@ -607,7 +606,7 @@ func testAddRemoveQuotas(t *testing.T, enabled bool) {
} }
projectsFile = tmpProjectsFile.Name() projectsFile = tmpProjectsFile.Name()
tmpProjectsFile.Close() tmpProjectsFile.Close()
tmpProjidFile, err := ioutil.TempFile("", "projid") tmpProjidFile, err := os.CreateTemp("", "projid")
if err == nil { if err == nil {
_, err = tmpProjidFile.WriteString(projidHeader) _, err = tmpProjidFile.WriteString(projidHeader)
} }

View File

@ -20,7 +20,6 @@ limitations under the License.
package hostutil package hostutil
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -193,12 +192,12 @@ func TestGetSELinuxSupport(t *testing.T) {
} }
func writeFile(content string) (string, string, error) { func writeFile(content string) (string, string, error) {
tempDir, err := ioutil.TempDir("", "mounter_shared_test") tempDir, err := os.MkdirTemp("", "mounter_shared_test")
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
filename := filepath.Join(tempDir, "mountinfo") filename := filepath.Join(tempDir, "mountinfo")
err = ioutil.WriteFile(filename, []byte(content), 0600) err = os.WriteFile(filename, []byte(content), 0600)
if err != nil { if err != nil {
os.RemoveAll(tempDir) os.RemoveAll(tempDir)
return "", "", err return "", "", err

View File

@ -38,7 +38,7 @@ func NewIOHandler() IoUtil {
} }
func (handler *osIOHandler) ReadFile(filename string) ([]byte, error) { func (handler *osIOHandler) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename) return os.ReadFile(filename)
} }
func (handler *osIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) { func (handler *osIOHandler) ReadDir(dirname string) ([]os.FileInfo, error) {
return ioutil.ReadDir(dirname) return ioutil.ReadDir(dirname)

View File

@ -17,7 +17,6 @@ limitations under the License.
package util package util
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -202,7 +201,7 @@ func TestGetNestedMountpoints(t *testing.T) {
}, },
} }
for _, test := range tc { for _, test := range tc {
dir, err := ioutil.TempDir("", "TestMakeNestedMountpoints.") dir, err := os.MkdirTemp("", "TestMakeNestedMountpoints.")
if err != nil { if err != nil {
t.Errorf("Unexpected error trying to create temp directory: %v", err) t.Errorf("Unexpected error trying to create temp directory: %v", err)
return return

View File

@ -19,7 +19,6 @@ package util
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -173,7 +172,7 @@ func LoadPodFromFile(filePath string) (*v1.Pod, error) {
if filePath == "" { if filePath == "" {
return nil, fmt.Errorf("file path not specified") return nil, fmt.Errorf("file path not specified")
} }
podDef, err := ioutil.ReadFile(filePath) podDef, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read file path %s: %+v", filePath, err) return nil, fmt.Errorf("failed to read file path %s: %+v", filePath, err)
} }

View File

@ -17,7 +17,6 @@ limitations under the License.
package util package util
import ( import (
"io/ioutil"
"os" "os"
"reflect" "reflect"
"runtime" "runtime"
@ -93,7 +92,7 @@ spec:
} }
for _, test := range tests { for _, test := range tests {
tempFile, err := ioutil.TempFile("", "podfile") tempFile, err := os.CreateTemp("", "podfile")
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
if err != nil { if err != nil {
t.Fatalf("cannot create temporary file: %v", err) t.Fatalf("cannot create temporary file: %v", err)

View File

@ -18,7 +18,6 @@ package volumepathhandler
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -279,12 +278,12 @@ func (v VolumePathHandler) IsDeviceBindMountExist(mapPath string) (bool, error)
// GetDeviceBindMountRefs searches bind mounts under global map path // GetDeviceBindMountRefs searches bind mounts under global map path
func (v VolumePathHandler) GetDeviceBindMountRefs(devPath string, mapPath string) ([]string, error) { func (v VolumePathHandler) GetDeviceBindMountRefs(devPath string, mapPath string) ([]string, error) {
var refs []string var refs []string
files, err := ioutil.ReadDir(mapPath) files, err := os.ReadDir(mapPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, file := range files { for _, file := range files {
if file.Mode()&os.ModeDevice != os.ModeDevice { if file.Type()&os.ModeDevice != os.ModeDevice {
continue continue
} }
filename := file.Name() filename := file.Name()

View File

@ -22,7 +22,6 @@ package volumepathhandler
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -133,7 +132,7 @@ func getLoopDeviceFromSysfs(path string) (string, error) {
backingFile := fmt.Sprintf("%s/loop/backing_file", device) backingFile := fmt.Sprintf("%s/loop/backing_file", device)
// The contents of this file is the absolute path of "path". // The contents of this file is the absolute path of "path".
data, err := ioutil.ReadFile(backingFile) data, err := os.ReadFile(backingFile)
if err != nil { if err != nil {
continue continue
} }