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 <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2023-06-07 14:03:06 -07:00
parent 699d118d85
commit 8ced101db5
5 changed files with 15 additions and 57 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)

View File

@ -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)