@@ -3,11 +3,13 @@ package cgroups
3
3
import (
4
4
"bytes"
5
5
"errors"
6
+ "path/filepath"
6
7
"reflect"
7
8
"strings"
8
9
"testing"
9
10
10
11
"github.com/moby/sys/mountinfo"
12
+ "golang.org/x/sys/unix"
11
13
)
12
14
13
15
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) {
661
663
}
662
664
}
663
665
}
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