@@ -3,11 +3,13 @@ package cgroups
33import (
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
1315const 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