mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
FormatAndMount unit test only checks for MountErrorValue now and closed gaps for some error values
This commit is contained in:
parent
99f567aa7a
commit
5d34e5006a
8
mount.go
8
mount.go
@ -84,6 +84,8 @@ const (
|
|||||||
HasFilesystemErrors MountErrorType = "HasFilesystemErrors"
|
HasFilesystemErrors MountErrorType = "HasFilesystemErrors"
|
||||||
UnformattedReadOnly MountErrorType = "UnformattedReadOnly"
|
UnformattedReadOnly MountErrorType = "UnformattedReadOnly"
|
||||||
FormatFailed MountErrorType = "FormatFailed"
|
FormatFailed MountErrorType = "FormatFailed"
|
||||||
|
GetDiskFormatFailed MountErrorType = "GetDiskFormatFailed"
|
||||||
|
UnknownMountError MountErrorType = "UnknownMountError"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MountError struct {
|
type MountError struct {
|
||||||
@ -91,16 +93,16 @@ type MountError struct {
|
|||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mountError *MountError) String() string {
|
func (mountError MountError) String() string {
|
||||||
return mountError.Message
|
return mountError.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mountError *MountError) Error() string {
|
func (mountError MountError) Error() string {
|
||||||
return mountError.Message
|
return mountError.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMountError(mountErrorValue MountErrorType, format string, args ...interface{}) error {
|
func NewMountError(mountErrorValue MountErrorType, format string, args ...interface{}) error {
|
||||||
mountError := &MountError{
|
mountError := MountError{
|
||||||
Type: mountErrorValue,
|
Type: mountErrorValue,
|
||||||
Message: fmt.Sprintf(format, args...),
|
Message: fmt.Sprintf(format, args...),
|
||||||
}
|
}
|
||||||
|
@ -315,12 +315,12 @@ func (mounter *SafeFormatAndMount) formatAndMount(source string, target string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
options = append(options, "defaults")
|
options = append(options, "defaults")
|
||||||
var mountErrorValue MountErrorType
|
mountErrorValue := UnknownMountError
|
||||||
|
|
||||||
// Check if the disk is already formatted
|
// Check if the disk is already formatted
|
||||||
existingFormat, err := mounter.GetDiskFormat(source)
|
existingFormat, err := mounter.GetDiskFormat(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return NewMountError(GetDiskFormatFailed, "failed to get disk format of disk %s: %v", source, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use 'ext4' as the default
|
// Use 'ext4' as the default
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/utils/exec"
|
"k8s.io/utils/exec"
|
||||||
@ -60,12 +59,12 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(mntDir)
|
defer os.RemoveAll(mntDir)
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
description string
|
description string
|
||||||
fstype string
|
fstype string
|
||||||
mountOptions []string
|
mountOptions []string
|
||||||
execScripts []ExecArgs
|
execScripts []ExecArgs
|
||||||
mountErrs []error
|
mountErrs []error
|
||||||
expectedError error
|
expErrorType MountErrorType
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
description: "Test a read only mount of an already formatted device",
|
description: "Test a read only mount of an already formatted device",
|
||||||
@ -90,7 +89,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
execScripts: []ExecArgs{
|
execScripts: []ExecArgs{
|
||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("cannot mount unformatted disk /dev/foo as we are manipulating it in read-only mode"),
|
expErrorType: UnformattedReadOnly,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test a normal mount of unformatted device",
|
description: "Test a normal mount of unformatted device",
|
||||||
@ -107,7 +106,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil},
|
||||||
{"fsck", []string{"-a", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 4}},
|
{"fsck", []string{"-a", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 4}},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("'fsck' found errors on device /dev/foo but could not correct them"),
|
expErrorType: HasFilesystemErrors,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test 'fsck' fails with exit status 1 (errors found and corrected)",
|
description: "Test 'fsck' fails with exit status 1 (errors found and corrected)",
|
||||||
@ -133,7 +132,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nPTTYPE=dos\n", nil},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nPTTYPE=dos\n", nil},
|
||||||
{"fsck", []string{"-a", "/dev/foo"}, "", nil},
|
{"fsck", []string{"-a", "/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("unknown filesystem type '(null)'"),
|
expErrorType: FilesystemMismatch,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'blkid' is called and confirms unformatted disk, format fails",
|
description: "Test that 'blkid' is called and confirms unformatted disk, format fails",
|
||||||
@ -143,7 +142,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
||||||
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", fmt.Errorf("formatting failed")},
|
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", fmt.Errorf("formatting failed")},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("formatting failed"),
|
expErrorType: FormatFailed,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, second mount fails",
|
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, second mount fails",
|
||||||
@ -153,7 +152,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
||||||
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil},
|
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("unknown filesystem type '(null)'"),
|
expErrorType: UnknownMountError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes",
|
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes",
|
||||||
@ -162,7 +161,6 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
||||||
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil},
|
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes with ext3",
|
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes with ext3",
|
||||||
@ -171,7 +169,6 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
||||||
{"mkfs.ext3", []string{"-F", "-m0", "/dev/foo"}, "", nil},
|
{"mkfs.ext3", []string{"-F", "-m0", "/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "test that none ext4 fs does not get called with ext4 options.",
|
description: "test that none ext4 fs does not get called with ext4 options.",
|
||||||
@ -180,7 +177,6 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}},
|
||||||
{"mkfs.xfs", []string{"/dev/foo"}, "", nil},
|
{"mkfs.xfs", []string{"/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'blkid' is called and reports ext4 partition",
|
description: "Test that 'blkid' is called and reports ext4 partition",
|
||||||
@ -198,7 +194,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 4}},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 4}},
|
||||||
{"mkfs.xfs", []string{"/dev/foo"}, "", nil},
|
{"mkfs.xfs", []string{"/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("exit 4"),
|
expErrorType: GetDiskFormatFailed,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'xfs_repair' is called only once, no need to repair the filesystem",
|
description: "Test that 'xfs_repair' is called only once, no need to repair the filesystem",
|
||||||
@ -207,7 +203,6 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=xfs\n", nil},
|
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=xfs\n", nil},
|
||||||
{"xfs_repair", []string{"-n", "/dev/foo"}, "", nil},
|
{"xfs_repair", []string{"-n", "/dev/foo"}, "", nil},
|
||||||
},
|
},
|
||||||
expectedError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'xfs_repair' is called twice and repair the filesystem",
|
description: "Test that 'xfs_repair' is called twice and repair the filesystem",
|
||||||
@ -217,7 +212,6 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"xfs_repair", []string{"-n", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}},
|
{"xfs_repair", []string{"-n", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}},
|
||||||
{"xfs_repair", []string{"/dev/foo"}, "\ndone\n", nil},
|
{"xfs_repair", []string{"/dev/foo"}, "\ndone\n", nil},
|
||||||
},
|
},
|
||||||
expectedError: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'xfs_repair' is called twice and repair the filesystem, but mount failed",
|
description: "Test that 'xfs_repair' is called twice and repair the filesystem, but mount failed",
|
||||||
@ -228,7 +222,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"xfs_repair", []string{"-n", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}},
|
{"xfs_repair", []string{"-n", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}},
|
||||||
{"xfs_repair", []string{"/dev/foo"}, "\ndone\n", nil},
|
{"xfs_repair", []string{"/dev/foo"}, "\ndone\n", nil},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("unknown filesystem type '(null)'"),
|
expErrorType: UnknownMountError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Test that 'xfs_repair' is called twice but could not repair the filesystem",
|
description: "Test that 'xfs_repair' is called twice but could not repair the filesystem",
|
||||||
@ -238,7 +232,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
{"xfs_repair", []string{"-n", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}},
|
{"xfs_repair", []string{"-n", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}},
|
||||||
{"xfs_repair", []string{"/dev/foo"}, "\nAn error occurred\n", &testingexec.FakeExitError{Status: 1}},
|
{"xfs_repair", []string{"/dev/foo"}, "\nAn error occurred\n", &testingexec.FakeExitError{Status: 1}},
|
||||||
},
|
},
|
||||||
expectedError: fmt.Errorf("'xfs_repair' found errors on device %s but could not correct them: %v", "/dev/foo", "\nAn error occurred\n"),
|
expErrorType: HasFilesystemErrors,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,7 +254,7 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
device := "/dev/foo"
|
device := "/dev/foo"
|
||||||
dest := mntDir
|
dest := mntDir
|
||||||
err := mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions)
|
err := mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions)
|
||||||
if test.expectedError == nil {
|
if len(test.expErrorType) == 0 {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("test \"%s\" unexpected non-error: %v", test.description, err)
|
t.Errorf("test \"%s\" unexpected non-error: %v", test.description, err)
|
||||||
}
|
}
|
||||||
@ -277,8 +271,12 @@ func TestSafeFormatAndMount(t *testing.T) {
|
|||||||
t.Errorf("test \"%s\" the correct device was not mounted", test.description)
|
t.Errorf("test \"%s\" the correct device was not mounted", test.description)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err == nil || !strings.HasPrefix(err.Error(), test.expectedError.Error()) {
|
mntErr, ok := err.(MountError)
|
||||||
t.Errorf("test \"%s\" unexpected error: \n [%v]. \nExpecting [%v]", test.description, err, test.expectedError)
|
if !ok {
|
||||||
|
t.Errorf("mount error not of mount error type: %v", err)
|
||||||
|
}
|
||||||
|
if mntErr.Type != test.expErrorType {
|
||||||
|
t.Errorf("test \"%s\" unexpected error: \n [%v]. \nExpecting err type[%v]", test.description, err, test.expErrorType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user