From 8ced101db53475c0162401b3b6638ffee0eef6f7 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 7 Jun 2023 14:03:06 -0700 Subject: [PATCH] mount-utils: stop using ioutil in tests io/ioutil is deprecated since Go 1.16. Besides, we now have a nice t.TempDir() function which simplifies things a lot. Signed-off-by: Kir Kolyshkin --- .../k8s.io/mount-utils/mount_helper_test.go | 16 +++-------- .../mount-utils/mount_helper_unix_test.go | 28 +++++-------------- .../k8s.io/mount-utils/mount_linux_test.go | 3 +- .../k8s.io/mount-utils/mount_windows_test.go | 16 ++--------- .../mount-utils/safe_format_and_mount_test.go | 9 +----- 5 files changed, 15 insertions(+), 57 deletions(-) diff --git a/staging/src/k8s.io/mount-utils/mount_helper_test.go b/staging/src/k8s.io/mount-utils/mount_helper_test.go index 1e506bb2edf..abbc3199828 100644 --- a/staging/src/k8s.io/mount-utils/mount_helper_test.go +++ b/staging/src/k8s.io/mount-utils/mount_helper_test.go @@ -18,7 +18,6 @@ package mount import ( "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -105,11 +104,7 @@ func TestDoCleanupMountPoint(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - tmpDir, err := ioutil.TempDir("", "unmount-mount-point-test") - if err != nil { - t.Fatalf("failed to create tmpdir: %v", err) - } - defer os.RemoveAll(tmpDir) + tmpDir := t.TempDir() if tt.prepareMnt == nil { t.Fatalf("prepareMnt function required") @@ -150,15 +145,12 @@ func TestDoCleanupMountPoint(t *testing.T) { } func validateDirExists(dir string) error { - _, err := ioutil.ReadDir(dir) - if err != nil { - return err - } - return nil + _, err := os.ReadDir(dir) + return err } func validateDirNotExists(dir string) error { - _, err := ioutil.ReadDir(dir) + _, err := os.ReadDir(dir) if os.IsNotExist(err) { return nil } diff --git a/staging/src/k8s.io/mount-utils/mount_helper_unix_test.go b/staging/src/k8s.io/mount-utils/mount_helper_unix_test.go index 3baea376ee2..71662b58315 100644 --- a/staging/src/k8s.io/mount-utils/mount_helper_unix_test.go +++ b/staging/src/k8s.io/mount-utils/mount_helper_unix_test.go @@ -20,25 +20,19 @@ limitations under the License. package mount import ( - "io/ioutil" "os" "path/filepath" "reflect" "testing" ) -func writeFile(content string) (string, string, error) { - tempDir, err := ioutil.TempDir("", "mounter_shared_test") +func writeFile(t *testing.T, content string) string { + filename := filepath.Join(t.TempDir(), "mountinfo") + err := os.WriteFile(filename, []byte(content), 0o600) if err != nil { - return "", "", err + t.Fatal(err) } - filename := filepath.Join(tempDir, "mountinfo") - err = ioutil.WriteFile(filename, []byte(content), 0o600) - if err != nil { - os.RemoveAll(tempDir) - return "", "", err - } - return tempDir, filename, nil + return filename } func TestParseMountInfo(t *testing.T) { @@ -84,11 +78,7 @@ func TestParseMountInfo(t *testing.T) { 40 28 0:36 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:22 - cgroup cgroup rw,perf_event 761 60 8:0 / /var/lib/kubelet/plugins/kubernetes.io/iscsi/iface-default/127.0.0.1:3260-iqn.2003-01.org.linux-iscsi.f21.x8664:sn.4b0aae584f7c-lun-0 rw,relatime shared:421 - ext4 /dev/sda rw,context="system_u:object_r:container_file_t:s0:c314,c894",data=ordered ` - tempDir, filename, err := writeFile(info) - if err != nil { - t.Fatalf("cannot create temporary file: %v", err) - } - defer os.RemoveAll(tempDir) + filename := writeFile(t, info) tests := []struct { name string @@ -303,11 +293,7 @@ func TestBadParseMountInfo(t *testing.T) { } for _, test := range tests { - tempDir, filename, err := writeFile(test.info) - if err != nil { - t.Fatalf("cannot create temporary file: %v", err) - } - defer os.RemoveAll(tempDir) + filename := writeFile(t, test.info) infos, err := ParseMountInfo(filename) if err != nil { diff --git a/staging/src/k8s.io/mount-utils/mount_linux_test.go b/staging/src/k8s.io/mount-utils/mount_linux_test.go index 5ea22e1ac4c..7dae6cb73bd 100644 --- a/staging/src/k8s.io/mount-utils/mount_linux_test.go +++ b/staging/src/k8s.io/mount-utils/mount_linux_test.go @@ -22,7 +22,6 @@ package mount import ( "errors" "fmt" - "io/ioutil" "os" "os/exec" "reflect" @@ -430,7 +429,7 @@ func TestSearchMountPoints(t *testing.T) { nil, }, } - tmpFile, err := ioutil.TempFile("", "test-get-filetype") + tmpFile, err := os.CreateTemp("", "test-get-filetype") if err != nil { t.Fatal(err) } diff --git a/staging/src/k8s.io/mount-utils/mount_windows_test.go b/staging/src/k8s.io/mount-utils/mount_windows_test.go index 524ee5f7bae..226af98e618 100644 --- a/staging/src/k8s.io/mount-utils/mount_windows_test.go +++ b/staging/src/k8s.io/mount-utils/mount_windows_test.go @@ -21,7 +21,6 @@ package mount import ( "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -200,12 +199,7 @@ func TestIsLikelyNotMountPoint(t *testing.T) { } for _, test := range tests { - base, err := ioutil.TempDir("", test.fileName) - if err != nil { - t.Fatalf(err.Error()) - } - - defer os.RemoveAll(base) + base := t.TempDir() if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil { t.Fatalf("unexpected error in setUp(%s, %s): %v", test.fileName, test.targetLinkName, err) @@ -280,13 +274,7 @@ func TestFormatAndMount(t *testing.T) { Interface: &fakeMounter, Exec: fakeExec, } - base, err := ioutil.TempDir("", test.device) - if err != nil { - t.Fatalf(err.Error()) - } - defer os.RemoveAll(base) - - target := filepath.Join(base, test.target) + target := filepath.Join(t.TempDir(), test.target) err = mounter.FormatAndMount(test.device, target, test.fstype, test.mountOptions) if test.expectError { assert.NotNil(t, err, "Expect error during FormatAndMount(%s, %s, %s, %v)", test.device, test.target, test.fstype, test.mountOptions) diff --git a/staging/src/k8s.io/mount-utils/safe_format_and_mount_test.go b/staging/src/k8s.io/mount-utils/safe_format_and_mount_test.go index 99a87ae43e2..07279554a4f 100644 --- a/staging/src/k8s.io/mount-utils/safe_format_and_mount_test.go +++ b/staging/src/k8s.io/mount-utils/safe_format_and_mount_test.go @@ -18,8 +18,6 @@ package mount import ( "fmt" - "io/ioutil" - "os" "runtime" "strings" "testing" @@ -58,11 +56,6 @@ func TestSafeFormatAndMount(t *testing.T) { if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { t.Skipf("not supported on GOOS=%s", runtime.GOOS) } - mntDir, err := ioutil.TempDir(os.TempDir(), "mount") - if err != nil { - t.Fatalf("failed to create tmp dir: %v", err) - } - defer os.RemoveAll(mntDir) tests := []struct { description string fstype string @@ -241,7 +234,7 @@ func TestSafeFormatAndMount(t *testing.T) { } device := "/dev/foo" - dest := mntDir + dest := t.TempDir() var err error if len(test.formatOptions) > 0 { err = mounter.FormatAndMountSensitiveWithFormatOptions(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions, test.formatOptions)