From 8a20643f7ffe9e175db11455accb8a0f49fbdb21 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Wed, 19 Apr 2023 13:49:12 -0700 Subject: [PATCH] task_profiles_test: Skip this test if cgroups is read-only GKE provides an unusual environment: the cgroupv2 filesystem is mounted read-only. Skip the task_profiles_test on the host if the cgroup2 filesystem is mounted read-only to prevent that a test fails as follows: Failed to write '-1' to /sys/fs/cgroup/cgroup.procs: Read-only file system. Bug: 278899193 Change-Id: I8c5a0c0848a47a395ae87f2fc31ba0ccda7d7f31 Signed-off-by: Bart Van Assche --- libprocessgroup/task_profiles_test.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libprocessgroup/task_profiles_test.cpp b/libprocessgroup/task_profiles_test.cpp index 842189d43..6a5b48bf3 100644 --- a/libprocessgroup/task_profiles_test.cpp +++ b/libprocessgroup/task_profiles_test.cpp @@ -16,6 +16,7 @@ #include "task_profiles.h" #include +#include #include #include #include @@ -29,13 +30,14 @@ using ::android::base::LogFunction; using ::android::base::LogId; using ::android::base::LogSeverity; using ::android::base::SetLogger; +using ::android::base::Split; using ::android::base::VERBOSE; using ::testing::TestWithParam; using ::testing::Values; namespace { -bool IsCgroupV2Mounted() { +bool IsCgroupV2MountedRw() { std::unique_ptr mnts(setmntent("/proc/mounts", "re"), endmntent); if (!mnts) { LOG(ERROR) << "Failed to open /proc/mounts"; @@ -43,9 +45,11 @@ bool IsCgroupV2Mounted() { } struct mntent* mnt; while ((mnt = getmntent(mnts.get()))) { - if (strcmp(mnt->mnt_type, "cgroup2") == 0) { - return true; + if (strcmp(mnt->mnt_type, "cgroup2") != 0) { + continue; } + const std::vector options = Split(mnt->mnt_opts, ","); + return std::count(options.begin(), options.end(), "ro") == 0; } return false; } @@ -145,8 +149,9 @@ class SetAttributeFixture : public TestWithParam { }; TEST_P(SetAttributeFixture, SetAttribute) { - // Treehugger runs host tests inside a container without cgroupv2 support. - if (!IsCgroupV2Mounted()) { + // Treehugger runs host tests inside a container either without cgroupv2 + // support or with the cgroup filesystem mounted read-only. + if (!IsCgroupV2MountedRw()) { GTEST_SKIP(); return; }