mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 22:05:59 +00:00
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:
parent
699d118d85
commit
8ced101db5
@ -18,7 +18,6 @@ package mount
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -105,11 +104,7 @@ func TestDoCleanupMountPoint(t *testing.T) {
|
|||||||
|
|
||||||
for name, tt := range tests {
|
for name, tt := range tests {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
tmpDir, err := ioutil.TempDir("", "unmount-mount-point-test")
|
tmpDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create tmpdir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
if tt.prepareMnt == nil {
|
if tt.prepareMnt == nil {
|
||||||
t.Fatalf("prepareMnt function required")
|
t.Fatalf("prepareMnt function required")
|
||||||
@ -150,15 +145,12 @@ func TestDoCleanupMountPoint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateDirExists(dir string) error {
|
func validateDirExists(dir string) error {
|
||||||
_, err := ioutil.ReadDir(dir)
|
_, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateDirNotExists(dir string) error {
|
func validateDirNotExists(dir string) error {
|
||||||
_, err := ioutil.ReadDir(dir)
|
_, err := os.ReadDir(dir)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -20,25 +20,19 @@ limitations under the License.
|
|||||||
package mount
|
package mount
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeFile(content string) (string, string, error) {
|
func writeFile(t *testing.T, content string) string {
|
||||||
tempDir, err := ioutil.TempDir("", "mounter_shared_test")
|
filename := filepath.Join(t.TempDir(), "mountinfo")
|
||||||
|
err := os.WriteFile(filename, []byte(content), 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
filename := filepath.Join(tempDir, "mountinfo")
|
return filename
|
||||||
err = ioutil.WriteFile(filename, []byte(content), 0o600)
|
|
||||||
if err != nil {
|
|
||||||
os.RemoveAll(tempDir)
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
return tempDir, filename, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMountInfo(t *testing.T) {
|
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
|
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
|
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)
|
filename := writeFile(t, info)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot create temporary file: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@ -303,11 +293,7 @@ func TestBadParseMountInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tempDir, filename, err := writeFile(test.info)
|
filename := writeFile(t, test.info)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cannot create temporary file: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
infos, err := ParseMountInfo(filename)
|
infos, err := ParseMountInfo(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -22,7 +22,6 @@ package mount
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -430,7 +429,7 @@ func TestSearchMountPoints(t *testing.T) {
|
|||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
tmpFile, err := ioutil.TempFile("", "test-get-filetype")
|
tmpFile, err := os.CreateTemp("", "test-get-filetype")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ package mount
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -200,12 +199,7 @@ func TestIsLikelyNotMountPoint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
base, err := ioutil.TempDir("", test.fileName)
|
base := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
defer os.RemoveAll(base)
|
|
||||||
|
|
||||||
if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil {
|
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)
|
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,
|
Interface: &fakeMounter,
|
||||||
Exec: fakeExec,
|
Exec: fakeExec,
|
||||||
}
|
}
|
||||||
base, err := ioutil.TempDir("", test.device)
|
target := filepath.Join(t.TempDir(), test.target)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(base)
|
|
||||||
|
|
||||||
target := filepath.Join(base, test.target)
|
|
||||||
err = mounter.FormatAndMount(test.device, target, test.fstype, test.mountOptions)
|
err = mounter.FormatAndMount(test.device, target, test.fstype, test.mountOptions)
|
||||||
if test.expectError {
|
if test.expectError {
|
||||||
assert.NotNil(t, err, "Expect error during FormatAndMount(%s, %s, %s, %v)", test.device, test.target, test.fstype, test.mountOptions)
|
assert.NotNil(t, err, "Expect error during FormatAndMount(%s, %s, %s, %v)", test.device, test.target, test.fstype, test.mountOptions)
|
||||||
|
@ -18,8 +18,6 @@ package mount
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -58,11 +56,6 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
|
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
|
||||||
t.Skipf("not supported on GOOS=%s", runtime.GOOS)
|
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 {
|
tests := []struct {
|
||||||
description string
|
description string
|
||||||
fstype string
|
fstype string
|
||||||
@ -241,7 +234,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
device := "/dev/foo"
|
device := "/dev/foo"
|
||||||
dest := mntDir
|
dest := t.TempDir()
|
||||||
var err error
|
var err error
|
||||||
if len(test.formatOptions) > 0 {
|
if len(test.formatOptions) > 0 {
|
||||||
err = mounter.FormatAndMountSensitiveWithFormatOptions(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions, test.formatOptions)
|
err = mounter.FormatAndMountSensitiveWithFormatOptions(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions, test.formatOptions)
|
||||||
|
Loading…
Reference in New Issue
Block a user