From 058b6f2b7aabe0fbc67c57034b5e69270a17c84d Mon Sep 17 00:00:00 2001 From: lfbzhm Date: Tue, 12 Nov 2024 14:15:41 +0000 Subject: [PATCH] libct/cg: add test for remove a non-exist dir in a ro mount point Signed-off-by: lfbzhm --- libcontainer/cgroups/utils_test.go | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/libcontainer/cgroups/utils_test.go b/libcontainer/cgroups/utils_test.go index fc81992f0b0..f5f2ba335a4 100644 --- a/libcontainer/cgroups/utils_test.go +++ b/libcontainer/cgroups/utils_test.go @@ -3,11 +3,14 @@ package cgroups import ( "bytes" "errors" + "os" + "path/filepath" "reflect" "strings" "testing" "github.com/moby/sys/mountinfo" + "golang.org/x/sys/unix" ) const fedoraMountinfo = `15 35 0:3 / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw @@ -661,3 +664,42 @@ func TestConvertBlkIOToIOWeightValue(t *testing.T) { } } } + +// TestRemovePathEROFS is to test remove a non-exist dir in a ro mount point. +// The similar issue example: https://github.com/opencontainers/runc/issues/4518 +func TestRemovePathEROFS(t *testing.T) { + dirFrom, err := os.MkdirTemp(".", "from-") + if err != nil { + t.Skip("can't create a tmp dir") + } + defer func() { + _ = unix.Rmdir(dirFrom) + }() + dirTo, err := os.MkdirTemp(".", "to-") + if err != nil { + t.Skip("can't create a tmp dir") + } + defer func() { + _ = unix.Rmdir(dirTo) + }() + err = unix.Mount(dirFrom, dirTo, "", unix.MS_BIND, "") + if err != nil { + t.Skip("no permission of mount") + } + err = unix.Mount("", dirTo, "", unix.MS_REMOUNT|unix.MS_BIND|unix.MS_RDONLY, "") + if err != nil { + t.Skip("no permission of mount") + } + defer func() { + _ = unix.Unmount(dirTo, 0) + }() + nonExistDir := filepath.Join(dirTo, "nothing") + err = rmdir(nonExistDir, true) + if !errors.Is(err, unix.EROFS) { + t.Fatalf("expected the error of removing a non-exist dir %s in a ro mount point with rmdir to be unix.EROFS, but got %v", nonExistDir, err) + } + err = RemovePath(nonExistDir) + if err != nil { + t.Fatalf("expected the error of removing a non-exist dir %s in a ro mount point with RemovePath to be nil, but got %v", nonExistDir, err) + } +}