]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
gunit/test-012: Create subtree_control for leaf cgroup
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 20 Dec 2022 16:09:13 +0000 (21:39 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 6 Jan 2023 15:07:27 +0000 (08:07 -0700)
The test case relies on the parent cgroup's subtree_control file, while
creating a cgroup to verify the controllers enabled in created cgroup v2
and the current code cleverly emulates it.  With the change to enable
the controller at the leaf cgroup this emulation gets difficult, so
introduce a dummy cgroup v2 mount that will help in the construction of
the path when the cgroup v2 is created with the empty controller because
the brilliance lies in emulating it by manually creating the
subtree_control file to give the impression of enabling the controller
in the cgroup v2 cgroup.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
tests/gunit/012-cgroup_create_cgroup.cpp

index dbe7c5342814489d464ff491dd4503caebb42833..5491b121a4f7f742b66e952cfc7f06c4fa6fefcc 100644 (file)
@@ -40,6 +40,22 @@ static const int VERSIONS_CNT =
 class CgroupCreateCgroupTest : public ::testing::Test {
        protected:
 
+       void MountCgroupV2()
+       {
+               char *mnt_dir = strdup(PARENT_DIR);
+               struct mntent ent = (struct mntent) {
+                       .mnt_fsname = "cgroup2",
+                       .mnt_dir = mnt_dir,
+                       .mnt_type = "cgroup2",
+                       .mnt_opts = "rw,relatime,seclabel",
+               };
+               int mnt_tbl_idx = 0;
+               int ret;
+
+               ret = cgroup_process_v2_mnt(&ent, &mnt_tbl_idx);
+               ASSERT_EQ(mnt_tbl_idx, 0);
+       }
+
        void SetUp() override
        {
                char tmp_path[FILENAME_MAX];
@@ -90,6 +106,7 @@ class CgroupCreateCgroupTest : public ::testing::Test {
                                ASSERT_TRUE(false);
                        }
                }
+               MountCgroupV2();
        }
 
        /*
@@ -115,6 +132,32 @@ class CgroupCreateCgroupTest : public ::testing::Test {
        }
 };
 
+static void create_subtree_contents(const char * const cg_name,
+                                   const char * const ctrl)
+{
+       char tmp_path[FILENAME_MAX];
+       FILE *f;
+       int ret;
+
+       memset(tmp_path, 0, sizeof(tmp_path));
+       ret = snprintf(tmp_path, FILENAME_MAX - 1,
+                      "%s/%s/cgroup.subtree_control",
+                      PARENT_DIR, cg_name);
+       ASSERT_GT(ret, 0);
+
+       f = fopen(tmp_path, "w");
+       ASSERT_NE(f, nullptr);
+       fclose(f);
+
+       memset(tmp_path, 0, sizeof(tmp_path));
+       ret = snprintf(tmp_path, FILENAME_MAX - 1, "%s/%s",
+                      PARENT_DIR, cg_name);
+       ASSERT_GT(ret, 0);
+
+       ret = cgroupv2_subtree_control(tmp_path, ctrl, true);
+       ASSERT_EQ(ret, 0);
+}
+
 static void verify_cgroup_created(const char * const cg_name,
                                  const char * const ctrl)
 {
@@ -135,14 +178,15 @@ static void verify_cgroup_created(const char * const cg_name,
        closedir(dir);
 }
 
-static void verify_subtree_contents(const char * const expected)
+static void verify_subtree_contents(const char * const cg_name,
+                                   const char * const expected)
 {
        char tmp_path[FILENAME_MAX], buf[4092];
        FILE *f;
 
        memset(tmp_path, 0, sizeof(tmp_path));
-       snprintf(tmp_path, FILENAME_MAX - 1, "%s/cgroup.subtree_control",
-                PARENT_DIR);
+       snprintf(tmp_path, FILENAME_MAX - 1, "%s/%s/cgroup.subtree_control",
+                PARENT_DIR, cg_name);
        f = fopen(tmp_path, "r");
        ASSERT_NE(f, nullptr);
 
@@ -182,14 +226,13 @@ TEST_F(CgroupCreateCgroupTest, CgroupCreateCgroupV2)
        cg = cgroup_new_cgroup(cg_name);
        ASSERT_NE(cg, nullptr);
 
-       ctrl = cgroup_add_controller(cg, ctrl_name);
-       ASSERT_NE(ctrl, nullptr);
-
        ret = cgroup_create_cgroup(cg, 0);
        ASSERT_EQ(ret, 0);
 
+       create_subtree_contents(cg_name, ctrl_name);
+
        verify_cgroup_created(cg_name, NULL);
-       verify_subtree_contents("+freezer");
+       verify_subtree_contents(cg_name, "+freezer");
 }
 
 TEST_F(CgroupCreateCgroupTest, CgroupCreateCgroupV1AndV2)
@@ -204,16 +247,22 @@ TEST_F(CgroupCreateCgroupTest, CgroupCreateCgroupV1AndV2)
        cg = cgroup_new_cgroup(cg_name);
        ASSERT_NE(cg, nullptr);
 
-       ctrl = cgroup_add_controller(cg, ctrl1_name);
-       ASSERT_NE(ctrl, nullptr);
-       ctrl = NULL;
        ctrl = cgroup_add_controller(cg, ctrl2_name);
        ASSERT_NE(ctrl, nullptr);
 
        ret = cgroup_create_cgroup(cg, 1);
        ASSERT_EQ(ret, 0);
 
+       cg = NULL;
+       cg = cgroup_new_cgroup(cg_name);
+       ASSERT_NE(cg, nullptr);
+
+       ret = cgroup_create_cgroup(cg, 1);
+       ASSERT_EQ(ret, 0);
+
+       create_subtree_contents(cg_name, ctrl1_name);
+
        verify_cgroup_created(cg_name, NULL);
        verify_cgroup_created(cg_name, ctrl2_name);
-       verify_subtree_contents("+memory");
+       verify_subtree_contents(cg_name, "+memory");
 }