Skip to content

Commit 2c9b929

Browse files
author
Dongsu Park
committed
runtimetest: correctly check for a readable directory
So far tests linux_masked_paths.t & linux_readonly_paths.t have failed with error messages like `cannot test read access for "/dirname"`. That's because `testReadAccess()` only checked if the given path is a regular file, returning an error for every other case. `testReadAccess()` should actually take care of another case of a given path being a directory, to be able to correctly check for its readability. Also run `testFileReadAccess()` or `testFileWriteAccess()` for all file types, not only a normal file, because the runtime spec does not mandate the type of masked files. It could be actually a character device like `/dev/null`, especially in case of runc. Found by @alban. Signed-off-by: Dongsu Park <[email protected]>
1 parent 9185c46 commit 2c9b929

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

cmd/runtimetest/main.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,25 @@ func testReadAccess(path string) (readable bool, err error) {
409409
if err != nil {
410410
return false, err
411411
}
412-
if fi.Mode()&os.ModeType == 0 {
413-
return testFileReadAccess(path)
412+
413+
// In case of a directory, we should check its readability in a special way.
414+
// In other files, we should not check its Mode explicitly, because the runtime
415+
// spec does not mandate the type of masked files. It could be a regular file
416+
// or a character file (/dev/null), which is the case for runtimes like runc.
417+
if fi.IsDir() {
418+
return testDirectoryReadAccess(path)
414419
}
415-
return false, fmt.Errorf("cannot test read access for %q (mode %d)", path, fi.Mode())
420+
return testFileReadAccess(path)
421+
}
422+
423+
func testDirectoryReadAccess(path string) (readable bool, err error) {
424+
if files, err := ioutil.ReadDir(path); err != nil || len(files) == 0 {
425+
// err from reading from a directory should not be considered as test failure,
426+
// it just means that the test program successfully assessed that
427+
// the directory is not readable.
428+
return false, nil
429+
}
430+
return true, nil
416431
}
417432

418433
func testFileReadAccess(path string) (readable bool, err error) {
@@ -439,12 +454,15 @@ func testWriteAccess(path string) (writable bool, err error) {
439454
if err != nil {
440455
return false, err
441456
}
457+
458+
// In case of a directory, we should check its readability in a special way.
459+
// In other files, we should not check its Mode explicitly, because the runtime
460+
// spec does not mandate the type of masked files. It could be a regular file
461+
// or a character file (/dev/null), which is the case for runtimes like runc.
442462
if fi.IsDir() {
443463
return testDirectoryWriteAccess(path)
444-
} else if fi.Mode()&os.ModeType == 0 {
445-
return testFileWriteAccess(path)
446464
}
447-
return false, fmt.Errorf("cannot test write access for %q (mode %d)", path, fi.Mode())
465+
return testFileWriteAccess(path)
448466
}
449467

450468
func testDirectoryWriteAccess(path string) (writable bool, err error) {

validation/linux_masked_paths.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ func main() {
2121
if err != nil {
2222
return err
2323
}
24+
// create a temp file to make testDir non-empty
25+
tmpfile, err := ioutil.TempFile(testDir, "tmp")
26+
if err != nil {
27+
return err
28+
}
29+
defer os.Remove(tmpfile.Name())
2430

2531
testFile := filepath.Join(path, "masked-file")
2632

validation/linux_readonly_paths.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ func main() {
2121
if err != nil {
2222
return err
2323
}
24+
// create a temp file to make testDir non-empty
25+
tmpfile, err := ioutil.TempFile(testDir, "tmp")
26+
if err != nil {
27+
return err
28+
}
29+
defer os.Remove(tmpfile.Name())
2430

2531
testFile := filepath.Join(path, "readonly-file")
2632

0 commit comments

Comments
 (0)