Repository: opencontainers/runc
File: libcontainer/cgroups/utils_test.go (Go)

Code before:
				t.Errorf("memswap: %d, memory %d, expected success, got error %s", c.memswap, c.memory, err)
			}
			if swap != c.expected {
				t.Errorf("memswap: %d, memory %d, expected %d, got %d", c.memswap, c.memory, c.expected, swap)
			}
		})
	}
}

func TestConvertBlkIOToIOWeightValue(t *testing.T) {
	cases := map[uint16]uint64{
		0:    0,
		10:   1,
		1000: 10000,
	}
	for i, expected := range cases {
		got := ConvertBlkIOToIOWeightValue(i)
		if got != expected {
			t.Errorf("expected ConvertBlkIOToIOWeightValue(%d) to be %d, got %d", i, expected, got)
		}
	}
}

// TestRemovePathEROFS is to test remove a non-existent dir in a ro mount point.
// The similar issue example: https://github.com/opencontainers/runc/issues/4518
func TestRemovePathEROFS(t *testing.T) {
	dirTo := t.TempDir()
	err := unix.Mount(t.TempDir(), 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)
	}()
	nonExistentDir := filepath.Join(dirTo, "non-existent-dir")
	err = rmdir(nonExistentDir, true)
	if !errors.Is(err, unix.EROFS) {
		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)
	}
	err = RemovePath(nonExistentDir)
	if err != nil {
		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)
	}
}


Review comment (suggestion):
```suggestion
func TestRemovePathReadOnly(t *testing.T) {
```

(I thought this test involved with the EROFS file system)

Code after:
				t.Errorf("memswap: %d, memory %d, expected success, got error %s", c.memswap, c.memory, err)
			}
			if swap != c.expected {
				t.Errorf("memswap: %d, memory %d, expected %d, got %d", c.memswap, c.memory, c.expected, swap)
			}
		})
	}
}

func TestConvertBlkIOToIOWeightValue(t *testing.T) {
	cases := map[uint16]uint64{
		0:    0,
		10:   1,
		1000: 10000,
	}
	for i, expected := range cases {
		got := ConvertBlkIOToIOWeightValue(i)
		if got != expected {
			t.Errorf("expected ConvertBlkIOToIOWeightValue(%d) to be %d, got %d", i, expected, got)
		}
	}
}

// TestRemovePathReadOnly is to test remove a non-existent dir in a ro mount point.
// The similar issue example: https://github.com/opencontainers/runc/issues/4518
func TestRemovePathReadOnly(t *testing.T) {
	dirTo := t.TempDir()
	err := unix.Mount(t.TempDir(), dirTo, "", unix.MS_BIND, "")
	if err != nil {
		t.Skip("no permission of mount")
	}
	defer func() {
		_ = unix.Unmount(dirTo, 0)
	}()
	err = unix.Mount("", dirTo, "", unix.MS_REMOUNT|unix.MS_BIND|unix.MS_RDONLY, "")
	if err != nil {
		t.Skip("no permission of mount")
	}
	nonExistentDir := filepath.Join(dirTo, "non-existent-dir")
	err = rmdir(nonExistentDir, true)
	if !errors.Is(err, unix.EROFS) {
		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)
	}
	err = RemovePath(nonExistentDir)
	if err != nil {
		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)
	}
}