| 1 |
/* |
| 2 |
Copyright 2014 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package mount |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"runtime" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"k8s.io/utils/exec" |
| 26 |
testingexec "k8s.io/utils/exec/testing" |
| 27 |
) |
| 28 |
|
| 29 |
type ErrorMounter struct { |
| 30 |
*FakeMounter |
| 31 |
errIndex int |
| 32 |
err []error |
| 33 |
} |
| 34 |
|
| 35 |
func (mounter *ErrorMounter) Mount(source string, target string, fstype string, options []string) error { |
| 36 |
return mounter.MountSensitive(source, target, fstype, options, nil /* sensitiveOptions */) |
| 37 |
} |
| 38 |
|
| 39 |
func (mounter *ErrorMounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { |
| 40 |
i := mounter.errIndex |
| 41 |
mounter.errIndex++ |
| 42 |
if mounter.err != nil && mounter.err[i] != nil { |
| 43 |
return mounter.err[i] |
| 44 |
} |
| 45 |
return mounter.FakeMounter.Mount(source, target, fstype, options) |
| 46 |
} |
| 47 |
|
| 48 |
type ExecArgs struct { |
| 49 |
command string |
| 50 |
args []string |
| 51 |
output string |
| 52 |
err error |
| 53 |
} |
| 54 |
|
| 55 |
func TestSafeFormatAndMount(t *testing.T) { |
| 56 |
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { |
| 57 |
t.Skipf("not supported on GOOS=%s", runtime.GOOS) |
| 58 |
} |
| 59 |
tests := []struct { |
| 60 |
description string |
| 61 |
fstype string |
| 62 |
mountOptions []string |
| 63 |
sensitiveMountOptions []string |
| 64 |
formatOptions []string |
| 65 |
execScripts []ExecArgs |
| 66 |
mountErrs []error |
| 67 |
expErrorType MountErrorType |
| 68 |
}{ |
| 69 |
{ |
| 70 |
description: "Test a read only mount of an already formatted device", |
| 71 |
fstype: "ext4", |
| 72 |
mountOptions: []string{"ro"}, |
| 73 |
execScripts: []ExecArgs{ |
| 74 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
description: "Test a normal mount of an already formatted device", |
| 79 |
fstype: "ext4", |
| 80 |
execScripts: []ExecArgs{ |
| 81 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil}, |
| 82 |
{"fsck", []string{"-a", "/dev/foo"}, "", nil}, |
| 83 |
}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
description: "Test a read only mount of unformatted device", |
| 87 |
fstype: "ext4", |
| 88 |
mountOptions: []string{"ro"}, |
| 89 |
execScripts: []ExecArgs{ |
| 90 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 91 |
}, |
| 92 |
expErrorType: UnformattedReadOnly, |
| 93 |
}, |
| 94 |
{ |
| 95 |
description: "Test a normal mount of unformatted device", |
| 96 |
fstype: "ext4", |
| 97 |
execScripts: []ExecArgs{ |
| 98 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 99 |
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil}, |
| 100 |
}, |
| 101 |
}, |
| 102 |
{ |
| 103 |
description: "Test 'fsck' fails with exit status 4", |
| 104 |
fstype: "ext4", |
| 105 |
execScripts: []ExecArgs{ |
| 106 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil}, |
| 107 |
{"fsck", []string{"-a", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 4}}, |
| 108 |
}, |
| 109 |
expErrorType: HasFilesystemErrors, |
| 110 |
}, |
| 111 |
{ |
| 112 |
description: "Test 'fsck' fails with exit status 1 (errors found and corrected)", |
| 113 |
fstype: "ext4", |
| 114 |
execScripts: []ExecArgs{ |
| 115 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil}, |
| 116 |
{"fsck", []string{"-a", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 1}}, |
| 117 |
}, |
| 118 |
}, |
| 119 |
{ |
| 120 |
description: "Test 'fsck' fails with exit status other than 1 and 4 (likely unformatted device)", |
| 121 |
fstype: "ext4", |
| 122 |
execScripts: []ExecArgs{ |
| 123 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil}, |
| 124 |
{"fsck", []string{"-a", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 8}}, |
| 125 |
}, |
| 126 |
}, |
| 127 |
{ |
| 128 |
description: "Test that 'blkid' is called and fails", |
| 129 |
fstype: "ext4", |
| 130 |
mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")}, |
| 131 |
execScripts: []ExecArgs{ |
| 132 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nPTTYPE=dos\n", nil}, |
| 133 |
{"fsck", []string{"-a", "/dev/foo"}, "", nil}, |
| 134 |
}, |
| 135 |
expErrorType: FilesystemMismatch, |
| 136 |
}, |
| 137 |
{ |
| 138 |
description: "Test that 'blkid' is called and confirms unformatted disk, format fails", |
| 139 |
fstype: "ext4", |
| 140 |
mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")}, |
| 141 |
execScripts: []ExecArgs{ |
| 142 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 143 |
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", fmt.Errorf("formatting failed")}, |
| 144 |
}, |
| 145 |
expErrorType: FormatFailed, |
| 146 |
}, |
| 147 |
{ |
| 148 |
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, second mount fails", |
| 149 |
fstype: "ext4", |
| 150 |
mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")}, |
| 151 |
execScripts: []ExecArgs{ |
| 152 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 153 |
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil}, |
| 154 |
}, |
| 155 |
expErrorType: UnknownMountError, |
| 156 |
}, |
| 157 |
{ |
| 158 |
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes", |
| 159 |
fstype: "ext4", |
| 160 |
execScripts: []ExecArgs{ |
| 161 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 162 |
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", nil}, |
| 163 |
}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes with ext3", |
| 167 |
fstype: "ext3", |
| 168 |
execScripts: []ExecArgs{ |
| 169 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 170 |
{"mkfs.ext3", []string{"-F", "-m0", "/dev/foo"}, "", nil}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
description: "test that none ext4 fs does not get called with ext4 options.", |
| 175 |
fstype: "xfs", |
| 176 |
execScripts: []ExecArgs{ |
| 177 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 178 |
{"mkfs.xfs", []string{"-f", "/dev/foo"}, "", nil}, |
| 179 |
}, |
| 180 |
}, |
| 181 |
{ |
| 182 |
description: "Test that 'blkid' is called and reports ext4 partition", |
| 183 |
fstype: "ext4", |
| 184 |
execScripts: []ExecArgs{ |
| 185 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "DEVNAME=/dev/foo\nTYPE=ext4\n", nil}, |
| 186 |
{"fsck", []string{"-a", "/dev/foo"}, "", nil}, |
| 187 |
}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
description: "Test that 'blkid' is called but has some usage or other errors (an exit code of 4 is returned)", |
| 191 |
fstype: "xfs", |
| 192 |
mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'"), nil}, |
| 193 |
execScripts: []ExecArgs{ |
| 194 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 4}}, |
| 195 |
{"mkfs.xfs", []string{"-f", "/dev/foo"}, "", nil}, |
| 196 |
}, |
| 197 |
expErrorType: GetDiskFormatFailed, |
| 198 |
}, |
| 199 |
{ |
| 200 |
description: "Test that 'blkid' is called and confirms unformatted disk, format fails with sensitive options", |
| 201 |
fstype: "ext4", |
| 202 |
sensitiveMountOptions: []string{"mySecret"}, |
| 203 |
mountErrs: []error{fmt.Errorf("unknown filesystem type '(null)'")}, |
| 204 |
execScripts: []ExecArgs{ |
| 205 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 206 |
{"mkfs.ext4", []string{"-F", "-m0", "/dev/foo"}, "", fmt.Errorf("formatting failed")}, |
| 207 |
}, |
| 208 |
expErrorType: FormatFailed, |
| 209 |
}, |
| 210 |
{ |
| 211 |
description: "Test that 'blkid' is called and confirms unformatted disk, format passes, mount passes (with format options)", |
| 212 |
fstype: "ext4", |
| 213 |
formatOptions: []string{"-b", "1024"}, |
| 214 |
execScripts: []ExecArgs{ |
| 215 |
{"blkid", []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", "/dev/foo"}, "", &testingexec.FakeExitError{Status: 2}}, |
| 216 |
{"mkfs.ext4", []string{"-b", "1024", "-F", "-m0", "/dev/foo"}, "", nil}, |
| 217 |
}, |
| 218 |
}, |
| 219 |
} |
| 220 |
|
| 221 |
for _, test := range tests { |
| 222 |
fakeMounter := ErrorMounter{NewFakeMounter(nil), 0, test.mountErrs} |
| 223 |
fakeExec := &testingexec.FakeExec{ExactOrder: true} |
| 224 |
for _, script := range test.execScripts { |
| 225 |
fakeCmd := &testingexec.FakeCmd{} |
| 226 |
cmdAction := makeFakeCmd(fakeCmd, script.command, script.args...) |
| 227 |
outputAction := makeFakeOutput(script.output, script.err) |
| 228 |
fakeCmd.CombinedOutputScript = append(fakeCmd.CombinedOutputScript, outputAction) |
| 229 |
fakeExec.CommandScript = append(fakeExec.CommandScript, cmdAction) |
| 230 |
} |
| 231 |
mounter := SafeFormatAndMount{ |
| 232 |
Interface: &fakeMounter, |
| 233 |
Exec: fakeExec, |
| 234 |
} |
| 235 |
|
| 236 |
device := "/dev/foo" |
| 237 |
dest := t.TempDir() |
| 238 |
var err error |
| 239 |
if len(test.formatOptions) > 0 { |
| 240 |
err = mounter.FormatAndMountSensitiveWithFormatOptions(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions, test.formatOptions) |
| 241 |
} else if len(test.sensitiveMountOptions) == 0 { |
| 242 |
err = mounter.FormatAndMount(device, dest, test.fstype, test.mountOptions) |
| 243 |
} else { |
| 244 |
err = mounter.FormatAndMountSensitive(device, dest, test.fstype, test.mountOptions, test.sensitiveMountOptions) |
| 245 |
} |
| 246 |
if len(test.expErrorType) == 0 { |
| 247 |
if err != nil { |
| 248 |
t.Errorf("test \"%s\" unexpected non-error: %v", test.description, err) |
| 249 |
} |
| 250 |
|
| 251 |
// Check that something was mounted on the directory |
| 252 |
isNotMountPoint, err := fakeMounter.IsLikelyNotMountPoint(dest) |
| 253 |
if err != nil || isNotMountPoint { |
| 254 |
t.Errorf("test \"%s\" the directory was not mounted", test.description) |
| 255 |
} |
| 256 |
|
| 257 |
// check that the correct device was mounted |
| 258 |
mountedDevice, _, err := GetDeviceNameFromMount(fakeMounter.FakeMounter, dest) |
| 259 |
if err != nil || mountedDevice != device { |
| 260 |
t.Errorf("test \"%s\" the correct device was not mounted", test.description) |
| 261 |
} |
| 262 |
} else { |
| 263 |
mntErr, ok := err.(MountError) |
| 264 |
if !ok { |
| 265 |
t.Errorf("mount error not of mount error type: %v", err) |
| 266 |
} |
| 267 |
if mntErr.Type != test.expErrorType { |
| 268 |
t.Errorf("test \"%s\" unexpected error: \n [%v]. \nExpecting err type[%v]", test.description, err, test.expErrorType) |
| 269 |
} |
| 270 |
if len(test.sensitiveMountOptions) == 0 { |
| 271 |
if strings.Contains(mntErr.Error(), sensitiveOptionsRemoved) { |
| 272 |
t.Errorf("test \"%s\" returned an error unexpectedly containing the string %q: %v", test.description, sensitiveOptionsRemoved, err) |
| 273 |
} |
| 274 |
} else { |
| 275 |
if !strings.Contains(err.Error(), sensitiveOptionsRemoved) { |
| 276 |
t.Errorf("test \"%s\" returned an error without the string %q: %v", test.description, sensitiveOptionsRemoved, err) |
| 277 |
} |
| 278 |
for _, sensitiveOption := range test.sensitiveMountOptions { |
| 279 |
if strings.Contains(err.Error(), sensitiveOption) { |
| 280 |
t.Errorf("test \"%s\" returned an error with a sensitive string (%q): %v", test.description, sensitiveOption, err) |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
func makeFakeCmd(fakeCmd *testingexec.FakeCmd, cmd string, args ...string) testingexec.FakeCommandAction { |
| 289 |
c := cmd |
| 290 |
a := args |
| 291 |
return func(cmd string, args ...string) exec.Cmd { |
| 292 |
command := testingexec.InitFakeCmd(fakeCmd, c, a...) |
| 293 |
return command |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func makeFakeOutput(output string, err error) testingexec.FakeAction { |
| 298 |
o := output |
| 299 |
return func() ([]byte, []byte, error) { |
| 300 |
return []byte(o), nil, err |
| 301 |
} |
| 302 |
} |