Skip to content

Commit

Permalink
mount-utils: stop using ioutil in tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kolyshkin committed Jun 14, 2023
1 parent 699d118 commit 8ced101
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 57 deletions.
16 changes: 4 additions & 12 deletions staging/src/k8s.io/mount-utils/mount_helper_test.go
Expand Up @@ -18,7 +18,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 7 additions & 21 deletions staging/src/k8s.io/mount-utils/mount_helper_unix_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions staging/src/k8s.io/mount-utils/mount_linux_test.go
Expand Up @@ -22,7 +22,6 @@ package mount
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect"
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 2 additions & 14 deletions staging/src/k8s.io/mount-utils/mount_windows_test.go
Expand Up @@ -21,7 +21,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions staging/src/k8s.io/mount-utils/safe_format_and_mount_test.go
Expand Up @@ -18,8 +18,6 @@ package mount

import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8ced101

Please sign in to comment.