Skip to content

Commit 832faf0

Browse files
committed
libct/cg: add test for remove a non-existent dir in a ro mount point
Signed-off-by: lfbzhm <[email protected]> (cherry picked from commit 119111a) Signed-off-by: lfbzhm <[email protected]>
1 parent e41cc27 commit 832faf0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

libcontainer/cgroups/utils_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package cgroups
33
import (
44
"bytes"
55
"errors"
6+
"path/filepath"
67
"reflect"
78
"strings"
89
"testing"
910

1011
"github.com/moby/sys/mountinfo"
12+
"golang.org/x/sys/unix"
1113
)
1214

1315
const fedoraMountinfo = `15 35 0:3 / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw
@@ -661,3 +663,29 @@ func TestConvertBlkIOToIOWeightValue(t *testing.T) {
661663
}
662664
}
663665
}
666+
667+
// TestRemovePathReadOnly is to test remove a non-existent dir in a ro mount point.
668+
// The similar issue example: https://github.com/opencontainers/runc/issues/4518
669+
func TestRemovePathReadOnly(t *testing.T) {
670+
dirTo := t.TempDir()
671+
err := unix.Mount(t.TempDir(), dirTo, "", unix.MS_BIND, "")
672+
if err != nil {
673+
t.Skip("no permission of mount")
674+
}
675+
defer func() {
676+
_ = unix.Unmount(dirTo, 0)
677+
}()
678+
err = unix.Mount("", dirTo, "", unix.MS_REMOUNT|unix.MS_BIND|unix.MS_RDONLY, "")
679+
if err != nil {
680+
t.Skip("no permission of mount")
681+
}
682+
nonExistentDir := filepath.Join(dirTo, "non-existent-dir")
683+
err = rmdir(nonExistentDir, true)
684+
if !errors.Is(err, unix.EROFS) {
685+
t.Fatalf("expected the error of removing a non-existent dir %s in a ro mount point with rmdir to be unix.EROFS, but got: %v", nonExistentDir, err)
686+
}
687+
err = RemovePath(nonExistentDir)
688+
if err != nil {
689+
t.Fatalf("expected the error of removing a non-existent dir %s in a ro mount point with RemovePath to be nil, but got: %v", nonExistentDir, err)
690+
}
691+
}

0 commit comments

Comments
 (0)