From: Tom Hromatka Date: Thu, 7 May 2020 20:15:24 +0000 (+0000) Subject: gunit: Add unit tests for cgroup_process_v1_mnt() X-Git-Tag: v2.0.3~11^2^2~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ba5c1c25757e6960d14347261786a084077d913;p=thirdparty%2Flibcgroup.git gunit: Add unit tests for cgroup_process_v1_mnt() This commit adds unit tests for cgroup_process_v1_mnt(). Note that the compiler flag -Wno-write-strings was also added because C++ throws a warning due to how the controllers[] array is utilized. [----------] 4 tests from CgroupProcessV1MntTest [ RUN ] CgroupProcessV1MntTest.AddV1Mount [ OK ] CgroupProcessV1MntTest.AddV1Mount (0 ms) [ RUN ] CgroupProcessV1MntTest.AddV1Mount_Duplicate [ OK ] CgroupProcessV1MntTest.AddV1Mount_Duplicate (0 ms) [ RUN ] CgroupProcessV1MntTest.AddV1NamedMount [ OK ] CgroupProcessV1MntTest.AddV1NamedMount (0 ms) [ RUN ] CgroupProcessV1MntTest.AddV1NamedMount_Duplicate [ OK ] CgroupProcessV1MntTest.AddV1NamedMount_Duplicate (0 ms) [----------] 4 tests from CgroupProcessV1MntTest (0 ms total) Signed-off-by: Tom Hromatka --- diff --git a/gunit/007-cgroup_process_v1_mount.cpp b/gunit/007-cgroup_process_v1_mount.cpp new file mode 100644 index 00000000..7a82f400 --- /dev/null +++ b/gunit/007-cgroup_process_v1_mount.cpp @@ -0,0 +1,111 @@ +/** + * libcgroup googletest for cgroup_process_v1_mnt() + * + * 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 + +#include "gtest/gtest.h" +#include "libcgroup-internal.h" + +static int mnt_tbl_idx = 0; + +class CgroupProcessV1MntTest : public ::testing::Test { +}; + +TEST_F(CgroupProcessV1MntTest, AddV1Mount) +{ + char *controllers[] = {"cpu", "memory", NULL}; + struct mntent ent = (struct mntent) { + .mnt_fsname = "cgroup", + .mnt_dir = "/sys/fs/cgroup/memory", + .mnt_type = "cgroup", + .mnt_opts = "rw,nosuid,nodev,noexec,relatime,seclabel,memory", + }; + int ret; + + ret = cgroup_process_v1_mnt(controllers, &ent, &mnt_tbl_idx); + + ASSERT_EQ(ret, 0); + ASSERT_EQ(mnt_tbl_idx, 1); + ASSERT_STREQ(cg_mount_table[0].name, "memory"); + ASSERT_STREQ(cg_mount_table[0].mount.path, ent.mnt_dir); +} + +/* The AddV1Mount() test above added the memory controller to the + * cg_mount_table[]. Now let's add another mount point of the + * memory controller to test the duplicate mount handling + */ +TEST_F(CgroupProcessV1MntTest, AddV1Mount_Duplicate) +{ + char *controllers[] = {"cpu", "cpuset", "memory", NULL}; + struct mntent ent = (struct mntent) { + .mnt_fsname = "cgroup", + .mnt_dir = "/cgroup/memory", + .mnt_type = "cgroup", + .mnt_opts = "rw,nosuid,nodev,noexec,relatime,seclabel,memory", + }; + int ret; + + ASSERT_EQ(NULL, cg_mount_table[0].mount.next); + + ret = cgroup_process_v1_mnt(controllers, &ent, &mnt_tbl_idx); + + ASSERT_EQ(ret, 0); + ASSERT_EQ(mnt_tbl_idx, 1); + ASSERT_STREQ(cg_mount_table[0].mount.next->path, ent.mnt_dir); +} + +TEST_F(CgroupProcessV1MntTest, AddV1NamedMount) +{ + char *controllers[] = {"cpu", "memory", "systemd", NULL}; + struct mntent ent = (struct mntent) { + .mnt_fsname = "cgroup", + .mnt_dir = "/sys/fs/cgroup/systemd", + .mnt_type = "cgroup", + .mnt_opts = "rw,nosuid,nodev,noexec,relatime,seclabel,name=systemd", + }; + int ret; + + ret = cgroup_process_v1_mnt(controllers, &ent, &mnt_tbl_idx); + + ASSERT_EQ(ret, 0); + ASSERT_EQ(mnt_tbl_idx, 2); + ASSERT_STREQ(cg_mount_table[1].name, "name=systemd"); + ASSERT_STREQ(cg_mount_table[1].mount.path, ent.mnt_dir); +} + +TEST_F(CgroupProcessV1MntTest, AddV1NamedMount_Duplicate) +{ + char *controllers[] = {"cpu", "memory", "systemd", NULL}; + struct mntent ent = (struct mntent) { + .mnt_fsname = "cgroup", + .mnt_dir = "/cgroup2/systemd", + .mnt_type = "cgroup", + .mnt_opts = "rw,nosuid,nodev,noexec,relatime,seclabel,name=systemd", + }; + int ret; + + ret = cgroup_process_v1_mnt(controllers, &ent, &mnt_tbl_idx); + + ASSERT_EQ(ret, 0); + ASSERT_EQ(mnt_tbl_idx, 2); + ASSERT_STREQ(cg_mount_table[1].name, "name=systemd"); + ASSERT_STREQ(cg_mount_table[1].mount.next->path, ent.mnt_dir); +} diff --git a/gunit/Makefile.am b/gunit/Makefile.am index c06e0429..a3e8ef08 100644 --- a/gunit/Makefile.am +++ b/gunit/Makefile.am @@ -24,6 +24,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include \ -I$(top_builddir)/googletest/googletest/include \ -I$(top_builddir)/googletest/googletest \ -std=c++11 \ + -Wno-write-strings \ -DSTATIC= \ -DUNIT_TEST LDADD = $(top_builddir)/src/.libs/libcgroupfortesting.la @@ -42,6 +43,7 @@ gtest_SOURCES = gtest.cpp \ 003-cg_get_cgroups_from_proc_cgroups.cpp \ 004-cgroup_compare_ignore_rule.cpp \ 005-cgroup_compare_wildcard_procname.cpp \ - 006-cgroup_get_cgroup.cpp + 006-cgroup_get_cgroup.cpp \ + 007-cgroup_process_v1_mount.cpp gtest_LDFLAGS = -L$(top_builddir)/googletest/googletest -l:libgtest.so \ -rpath $(abs_top_builddir)/googletest/googletest