From: Tom Hromatka Date: Wed, 23 Sep 2020 16:47:32 +0000 (-0600) Subject: gunit: Add unit tests for cgroup_build_tasks_procs_path() X-Git-Tag: v2.0.3~11^2^2~33^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d4359f76f5e8fbf4ec40392dda8cfa443b20dee;p=thirdparty%2Flibcgroup.git gunit: Add unit tests for cgroup_build_tasks_procs_path() 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 --- 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 00000000..7b778d05 --- /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 + */ + +/* + * 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 . + */ + +#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 438bd25b..7ae8b3bb 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