This commit adds unit tests for cgroup_build_tasks_procs_path()

[----------] 6 tests from BuildTasksProcPathTest
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_ControllerNotFound
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_ControllerNotFound 
(0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_UnknownCgVersion
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_UnknownCgVersion (0 
ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1 (0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2 (1 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1WithNs
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV1WithNs (0 ms)
[ RUN      ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2WithNs
[       OK ] BuildTasksProcPathTest.BuildTasksProcPathTest_CgV2WithNs (0 ms)
[----------] 6 tests from BuildTasksProcPathTest (1 ms total)

Signed-off-by: Tom Hromatka <tom.hroma...@oracle.com>
---
 gunit/013-cgroup_build_tasks_procs_path.cpp | 160 ++++++++++++++++++++
 gunit/Makefile.am                           |   3 +-
 2 files changed, 162 insertions(+), 1 deletion(-)
 create mode 100644 gunit/013-cgroup_build_tasks_procs_path.cpp

diff --git a/gunit/013-cgroup_build_tasks_procs_path.cpp 
b/gunit/013-cgroup_build_tasks_procs_path.cpp
new file mode 100644
index 000000000000..7b778d058ca2
--- /dev/null
+++ b/gunit/013-cgroup_build_tasks_procs_path.cpp
@@ -0,0 +1,160 @@
+/**
+ * libcgroup googletest for cgroup_build_tasks_procs_path()
+ *
+ * Copyright (c) 2020 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hroma...@oracle.com>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#include "gtest/gtest.h"
+
+#include "libcgroup-internal.h"
+
+class BuildTasksProcPathTest : public ::testing::Test {
+       protected:
+
+       /**
+        * Setup this test case
+        *
+        * This test case calls cg_build_path() to generate various
+        * cgroup paths.  The SetUp() routine creates a simple mount
+        * table that can be used to verify cg_build_path() behavior.
+        *
+        * cg_mount_table for this test is as follows:
+        *      name            mount_point                index  version
+        *      ----------------------------------------------------------
+        *      controller0     /sys/fs/cgroup/controller0     0      UNK
+        *      controller1     /sys/fs/cgroup/controller1     1        2
+        *      controller2     /sys/fs/cgroup/controller2     2        1
+        *      controller3     /sys/fs/cgroup/controller3     3        2
+        *      controller4     /sys/fs/cgroup/controller4     4        1
+        *      controller5     /sys/fs/cgroup/controller5     5        2
+        *
+        * Note that controllers 1 and 4 are also given namespaces
+        */
+       void SetUp() override {
+               char NAMESPACE1[] = "ns1";
+               char NAMESPACE4[] = "ns4";
+               const int ENTRY_CNT = 6;
+               int i;
+
+               memset(&cg_mount_table, 0, sizeof(cg_mount_table));
+               memset(cg_namespace_table, 0,
+                       CG_CONTROLLER_MAX * sizeof(cg_namespace_table[0]));
+
+               // Populate the mount table
+               for (i = 0; i < ENTRY_CNT; i++) {
+                       snprintf(cg_mount_table[i].name, FILENAME_MAX,
+                                "controller%d", i);
+                       cg_mount_table[i].index = i;
+
+                       snprintf(cg_mount_table[i].mount.path, FILENAME_MAX,
+                                "/sys/fs/cgroup/%s", cg_mount_table[i].name);
+                       cg_mount_table[i].mount.next = NULL;
+
+                       if (i == 0)
+                               cg_mount_table[i].version = CGROUP_UNK;
+                       else
+                               cg_mount_table[i].version =
+                                       (cg_version_t)((i % 2) + 1);
+               }
+
+               // Give a couple of the entries a namespace as well
+               cg_namespace_table[1] = NAMESPACE1;
+               cg_namespace_table[4] = NAMESPACE4;
+       }
+};
+
+TEST_F(BuildTasksProcPathTest, BuildTasksProcPathTest_ControllerNotFound)
+{
+       char ctrlname[] = "InvalidCtrlr";
+       char path[FILENAME_MAX];
+       char cgname[] = "foo";
+       int ret;
+
+       ret = cgroup_build_tasks_procs_path(path, sizeof(path), cgname,
+                                           ctrlname);
+       ASSERT_EQ(ret, ECGOTHER);
+       ASSERT_STREQ(path, "\0");
+}
+
+TEST_F(BuildTasksProcPathTest, BuildTasksProcPathTest_UnknownCgVersion)
+{
+       char ctrlname[] = "controller0";
+       char path[FILENAME_MAX];
+       char cgname[] = "bar";
+       int ret;
+
+       ret = cgroup_build_tasks_procs_path(path, sizeof(path), cgname,
+                                           ctrlname);
+       ASSERT_EQ(ret, ECGOTHER);
+       ASSERT_STREQ(path, "\0");
+}
+
+TEST_F(BuildTasksProcPathTest, BuildTasksProcPathTest_CgV1)
+{
+       char ctrlname[] = "controller2";
+       char path[FILENAME_MAX];
+       char cgname[] = "Container7";
+       int ret;
+
+       ret = cgroup_build_tasks_procs_path(path, sizeof(path), cgname,
+                                           ctrlname);
+       ASSERT_EQ(ret, 0);
+       ASSERT_STREQ(path, "/sys/fs/cgroup/controller2/Container7/tasks");
+}
+
+TEST_F(BuildTasksProcPathTest, BuildTasksProcPathTest_CgV2)
+{
+       char ctrlname[] = "controller3";
+       struct cgroup_controller ctrlr = {0};
+       char path[FILENAME_MAX];
+       char cgname[] = "tomcat";
+       int ret;
+
+       ret = cgroup_build_tasks_procs_path(path, sizeof(path), cgname,
+                                           ctrlname);
+       ASSERT_EQ(ret, 0);
+       ASSERT_STREQ(path, "/sys/fs/cgroup/controller3/tomcat/cgroup.procs");
+}
+
+TEST_F(BuildTasksProcPathTest, BuildTasksProcPathTest_CgV1WithNs)
+{
+       char ctrlname[] = "controller4";
+       struct cgroup_controller ctrlr = {0};
+       char path[FILENAME_MAX];
+       char cgname[] = "database12";
+       int ret;
+
+       ret = cgroup_build_tasks_procs_path(path, sizeof(path), cgname,
+                                           ctrlname);
+       ASSERT_EQ(ret, 0);
+       ASSERT_STREQ(path, "/sys/fs/cgroup/controller4/ns4/database12/tasks");
+}
+
+TEST_F(BuildTasksProcPathTest, BuildTasksProcPathTest_CgV2WithNs)
+{
+       char ctrlname[] = "controller1";
+       struct cgroup_controller ctrlr = {0};
+       char path[FILENAME_MAX];
+       char cgname[] = "server";
+       int ret;
+
+       ret = cgroup_build_tasks_procs_path(path, sizeof(path), cgname,
+                                           ctrlname);
+       ASSERT_EQ(ret, 0);
+       ASSERT_STREQ(path, 
"/sys/fs/cgroup/controller1/ns1/server/cgroup.procs");
+}
diff --git a/gunit/Makefile.am b/gunit/Makefile.am
index 438bd25b7734..7ae8b3bb463d 100644
--- a/gunit/Makefile.am
+++ b/gunit/Makefile.am
@@ -49,6 +49,7 @@ gtest_SOURCES = gtest.cpp \
                009-cgroup_set_values_recursive.cpp \
                010-cgroup_chown_chmod_tasks.cpp \
                011-cgroupv2_subtree_control.cpp \
-               012-cgroup_create_cgroup.cpp
+               012-cgroup_create_cgroup.cpp \
+               013-cgroup_build_tasks_procs_path.cpp
 gtest_LDFLAGS = -L$(top_builddir)/googletest/googletest -l:libgtest.so \
                -rpath $(abs_top_builddir)/googletest/googletest
-- 
2.25.4



_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to