Skip to content

Commit 10d2584

Browse files
committed
runtimetest: validateRootfsPropagation: fixes
This test expects CAP_SYS_ADMIN to be set (in order to perform mounts). Currently, if this capability is not set, it returns bare unix errno (EPERM) from unix.Mount, which is very confusing, since the test just prints "Operation not permitted" and exits. Do the following changes: - move the first mount to before the switch, and skip the test when it returns EPERM; - wrap all unix.Mount errors to provide more context. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 8b26e24 commit 10d2584

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

cmd/runtimetest/main.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,22 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
548548
}
549549
defer os.RemoveAll(targetDir)
550550

551+
mountErr := unix.Mount("/", targetDir, "", unix.MS_BIND|unix.MS_REC, "")
552+
if mountErr == unix.EPERM { //nolint:errcheck // unix errors are bare
553+
// This test needs CAP_SYS_ADMIN to perform mounts.
554+
// EPERM most probably means it was not granted.
555+
c.harness.Skip(1, "unable to perform mount (test requires CAP_SYS_ADMIN)")
556+
return nil
557+
}
558+
if err == nil {
559+
defer unix.Unmount(targetDir, unix.MNT_DETACH) //nolint:errcheck
560+
}
561+
551562
switch spec.Linux.RootfsPropagation {
552563
case "shared", "slave", "private":
564+
if mountErr != nil {
565+
return fmt.Errorf("bind-mount / %s: %w", targetDir, err)
566+
}
553567
mountDir, err := ioutil.TempDir("/", "mount")
554568
if err != nil {
555569
return err
@@ -568,12 +582,8 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
568582
}
569583
defer os.Remove(tmpfile.Name())
570584

571-
if err := unix.Mount("/", targetDir, "", unix.MS_BIND|unix.MS_REC, ""); err != nil {
572-
return err
573-
}
574-
defer unix.Unmount(targetDir, unix.MNT_DETACH) //nolint:errcheck
575585
if err := unix.Mount(testDir, mountDir, "", unix.MS_BIND|unix.MS_REC, ""); err != nil {
576-
return err
586+
return fmt.Errorf("bind-mount %s %s: %w", testDir, mountDir, err)
577587
}
578588
defer unix.Unmount(mountDir, unix.MNT_DETACH) //nolint:errcheck
579589
targetFile := filepath.Join(targetDir, filepath.Join(mountDir, filepath.Base(tmpfile.Name())))
@@ -595,14 +605,12 @@ func (c *complianceTester) validateRootfsPropagation(spec *rspec.Spec) error {
595605
)
596606
}
597607
case "unbindable":
598-
err = unix.Mount("/", targetDir, "", unix.MS_BIND|unix.MS_REC, "")
599-
if err == syscall.EINVAL {
608+
if mountErr == syscall.EINVAL {
600609
c.harness.Pass("root propagation is unbindable")
601610
return nil
602-
} else if err != nil {
603-
return err
611+
} else if mountErr != nil {
612+
return fmt.Errorf("bind-mount / %s: %w", targetDir, err)
604613
}
605-
defer unix.Unmount(targetDir, unix.MNT_DETACH) //nolint:errcheck
606614
c.harness.Fail("root propagation is unbindable")
607615
return nil
608616
default:

0 commit comments

Comments
 (0)