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 <bvanassche@google.com>
This commit is contained in:
Bart Van Assche 2023-04-19 13:49:12 -07:00
parent 37673f0bf5
commit 8a20643f7f

View file

@ -16,6 +16,7 @@
#include "task_profiles.h"
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <gtest/gtest.h>
#include <mntent.h>
#include <processgroup/processgroup.h>
@ -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<FILE, int (*)(FILE*)> 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<std::string> options = Split(mnt->mnt_opts, ",");
return std::count(options.begin(), options.end(), "ro") == 0;
}
return false;
}
@ -145,8 +149,9 @@ class SetAttributeFixture : public TestWithParam<TestParam> {
};
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;
}