Skip to content

Commit

Permalink
Merge pull request kubernetes#118403 from skitt/ioutil-sig-storage
Browse files Browse the repository at this point in the history
storage: stop using deprecated io/ioutil
  • Loading branch information
k8s-ci-robot committed Jun 21, 2023
2 parents 2802bbc + ab75e48 commit a19153d
Show file tree
Hide file tree
Showing 19 changed files with 38 additions and 56 deletions.
3 changes: 1 addition & 2 deletions pkg/volume/csi/csi_attacher_test.go
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -1644,7 +1643,7 @@ func TestAttacherUnmountDevice(t *testing.T) {
// Make JSON for this object
if tc.jsonFile != "" {
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)
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/csi/csi_mounter_test.go
Expand Up @@ -19,7 +19,6 @@ package csi
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -1065,7 +1064,7 @@ func TestUnmounterTeardown(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 {
t.Fatalf("failed to create tmp dir: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/csi/csi_util_test.go
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestSaveVolumeData(t *testing.T) {
}

// validate content
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if !tc.shouldFail && err != nil {
t.Errorf("failed to read data file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/csi/fake/fake_client.go
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -205,7 +204,7 @@ func (f *NodeClient) NodePublishVolume(ctx context.Context, req *csipb.NodePubli
// "Creation of target_path is the responsibility of the SP."
// Our plugin depends on it.
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)
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/metrics_du_test.go
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package volume_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestMetricsDuGetCapacity(t *testing.T) {
}

// 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()
if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err)
Expand Down
9 changes: 4 additions & 5 deletions pkg/volume/projected/projected_test.go
Expand Up @@ -18,7 +18,6 @@ package projected

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -874,7 +873,7 @@ func TestCollectDataWithServiceAccountToken(t *testing.T) {
}

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 {
t.Fatalf("can't make a temp rootdir: %v", err)
}
Expand Down Expand Up @@ -1139,7 +1138,7 @@ func TestPluginOptional(t *testing.T) {
}
datadirPath := filepath.Join(volumePath, datadir)

infos, err := ioutil.ReadDir(volumePath)
infos, err := os.ReadDir(volumePath)
if err != nil {
t.Fatalf("couldn't find volume path, %s", volumePath)
}
Expand All @@ -1151,7 +1150,7 @@ func TestPluginOptional(t *testing.T) {
}
}

infos, err = ioutil.ReadDir(datadirPath)
infos, err = os.ReadDir(datadirPath)
if err != nil {
t.Fatalf("couldn't find volume data path, %s", datadirPath)
}
Expand Down Expand Up @@ -1292,7 +1291,7 @@ func doTestSecretDataInVolume(volumePath string, secret v1.Secret, t *testing.T)
if _, err := os.Stat(secretDataHostPath); err != nil {
t.Fatalf("SetUp() failed, couldn't find secret data on disk: %v", secretDataHostPath)
} else {
actualSecretBytes, err := ioutil.ReadFile(secretDataHostPath)
actualSecretBytes, err := os.ReadFile(secretDataHostPath)
if err != nil {
t.Fatalf("Couldn't read secret data from: %v", secretDataHostPath)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/volume/util/atomic_writer.go
Expand Up @@ -19,7 +19,6 @@ package util
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -327,7 +326,7 @@ func shouldWriteFile(path string, content []byte) (bool, error) {
return true, nil
}

contentOnFs, err := ioutil.ReadFile(path)
contentOnFs, err := os.ReadFile(path)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -379,7 +378,7 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir

// newTimestampDir creates a new timestamp directory
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 {
klog.Errorf("%s: unable to create new temp directory: %v", w.logContext, err)
return "", err
Expand Down Expand Up @@ -411,11 +410,11 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
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)
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 &
// ~umask". But we want to make sure the specified mode is used
// in the file no matter what the umask is.
Expand Down
7 changes: 3 additions & 4 deletions pkg/volume/util/atomic_writer_test.go
Expand Up @@ -22,7 +22,6 @@ package util
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -766,7 +765,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
return nil
}

content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return err
}
Expand All @@ -781,7 +780,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
return nil
}

d, err := ioutil.ReadDir(targetDir)
d, err := os.ReadDir(targetDir)
if err != nil {
t.Errorf("Unable to read dir %v: %v", targetDir, err)
return
Expand All @@ -790,7 +789,7 @@ func checkVolumeContents(targetDir, tcName string, payload map[string]FileProjec
if strings.HasPrefix(info.Name(), "..") {
continue
}
if info.Mode()&os.ModeSymlink != 0 {
if info.Type()&os.ModeSymlink != 0 {
p := filepath.Join(targetDir, info.Name())
actual, err := os.Readlink(p)
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions pkg/volume/util/fs/fs_windows_test.go
Expand Up @@ -18,7 +18,6 @@ package fs

import (
"fmt"
"io/ioutil"
"os"

"testing"
Expand All @@ -28,21 +27,21 @@ import (

func TestDiskUsage(t *testing.T) {

dir1, err := ioutil.TempDir("", "dir_1")
dir1, err := os.MkdirTemp("", "dir_1")
if err != nil {
t.Fatalf("TestDiskUsage failed: %s", err.Error())
}
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 {
t.Fatalf("TestDiskUsage failed: %s", err.Error())
}
dir2, err := ioutil.TempDir(dir1, "dir_2")
dir2, err := os.MkdirTemp(dir1, "dir_2")
if err != nil {
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 {
t.Fatalf("TestDiskUsage failed: %s", err.Error())
}
Expand Down Expand Up @@ -92,7 +91,7 @@ func TestDiskUsage(t *testing.T) {
}

func TestInfo(t *testing.T) {
dir1, err := ioutil.TempDir("", "dir_1")
dir1, err := os.MkdirTemp("", "dir_1")
if err != nil {
t.Fatalf("TestInfo failed: %s", err.Error())
}
Expand All @@ -107,7 +106,7 @@ func TestInfo(t *testing.T) {
validateInfo(t, availablebytes, capacity, usage, inodesTotal, inodeUsage, inodesFree)

// 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 {
t.Fatalf("TestInfo failed: %s", err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/util/fsquota/common/quota_common_linux_impl.go
Expand Up @@ -22,7 +22,6 @@ package common
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -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
// of the problem that could be caused if this were to happen.
func runXFSQuotaCommand(mountpoint string, command string) (string, error) {
tmpMounts, err := ioutil.TempFile("", "mounts")
tmpMounts, err := os.CreateTemp("", "mounts")
if err != nil {
return "", fmt.Errorf("cannot create temporary mount file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/util/fsquota/project.go
Expand Up @@ -22,7 +22,6 @@ package fsquota
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -267,7 +266,7 @@ func writeProjectFile(base *os.File, projects []projectType) (string, error) {
return "", err
}
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 {
return "", err
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/volume/util/fsquota/quota_linux_test.go
Expand Up @@ -21,7 +21,6 @@ package fsquota

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -97,7 +96,7 @@ type mountpointTest struct {
}

func testBackingDev1(testcase backingDevTest) error {
tmpfile, err := ioutil.TempFile("", "backingdev")
tmpfile, err := os.CreateTemp("", "backingdev")
if err != nil {
return err
}
Expand Down Expand Up @@ -502,7 +501,7 @@ var quotaTestCases = []quotaTestCase{
}

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 {
t.Error(err.Error())
} else {
Expand All @@ -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)
}
}
bytes, err = ioutil.ReadFile(projidFile)
bytes, err = os.ReadFile(projidFile)
if err != nil {
t.Error(err.Error())
} else {
Expand Down Expand Up @@ -598,7 +597,7 @@ func runCaseDisabled(t *testing.T, testcase quotaTestCase, seq int) bool {

func testAddRemoveQuotas(t *testing.T, enabled bool) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LocalStorageCapacityIsolationFSQuotaMonitoring, enabled)()
tmpProjectsFile, err := ioutil.TempFile("", "projects")
tmpProjectsFile, err := os.CreateTemp("", "projects")
if err == nil {
_, err = tmpProjectsFile.WriteString(projectsHeader)
}
Expand All @@ -607,7 +606,7 @@ func testAddRemoveQuotas(t *testing.T, enabled bool) {
}
projectsFile = tmpProjectsFile.Name()
tmpProjectsFile.Close()
tmpProjidFile, err := ioutil.TempFile("", "projid")
tmpProjidFile, err := os.CreateTemp("", "projid")
if err == nil {
_, err = tmpProjidFile.WriteString(projidHeader)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/volume/util/hostutil/hostutil_linux_test.go
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package hostutil

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -193,12 +192,12 @@ func TestGetSELinuxSupport(t *testing.T) {
}

func writeFile(content string) (string, string, error) {
tempDir, err := ioutil.TempDir("", "mounter_shared_test")
tempDir, err := os.MkdirTemp("", "mounter_shared_test")
if err != nil {
return "", "", err
}
filename := filepath.Join(tempDir, "mountinfo")
err = ioutil.WriteFile(filename, []byte(content), 0600)
err = os.WriteFile(filename, []byte(content), 0600)
if err != nil {
os.RemoveAll(tempDir)
return "", "", err
Expand Down
2 changes: 1 addition & 1 deletion pkg/volume/util/io_util.go
Expand Up @@ -38,7 +38,7 @@ func NewIOHandler() IoUtil {
}

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) {
return ioutil.ReadDir(dirname)
Expand Down
3 changes: 1 addition & 2 deletions pkg/volume/util/nested_volumes_test.go
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package util

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

0 comments on commit a19153d

Please sign in to comment.