This commit adds unit tests for cgroup_compare_wildcard_procname().
The following tests are included in this commit:
WildcardProcnameSimpleMatch - Compares an ignore rule with a
wildcarded procname with a matching procname. Expects true.
WildcardProcnameNoMatch - Compares an ignore rule with a wildcarded
procname with a process that has a non-matching name. Expects
false.
ProcnameWildcard_AsteriskNoMatch - Compares a wildcard rule with
a process name that doesn't match. Expects false.
ProcnameWildcard_AsteriskMatch - Compares a wildcard rule with
a process name that does match the rule. Expects true.
ProcnameWildcard_AsteriskNoMatch2 - Compares a wildcard rule
with a process name that is shorter than the rule and thus
does not match. Expects false.
ProcnameWildcard_AsteriskMatchExactly - Compares a wildcard
rule with a process name that matches the rule exactly.
Expects true.
ProcnameWildcard_NoAsteriskMatchExactly - Compares a rule
with no wildcards with a process name that exactly matches
the rule. Expects false as the function exits early due
to no asterisk in the rule.
The results from googletest are reported below:
[----------] 13 tests from CgroupCompareIgnoreRuleTest
<snip>
[ RUN ] CgroupCompareIgnoreRuleTest.WildcardProcnameSimpleMatch
[ OK ] CgroupCompareIgnoreRuleTest.WildcardProcnameSimpleMatch (0 ms)
[ RUN ] CgroupCompareIgnoreRuleTest.WildcardProcnameNoMatch
[ OK ] CgroupCompareIgnoreRuleTest.WildcardProcnameNoMatch (0 ms)
[----------] 13 tests from CgroupCompareIgnoreRuleTest (1 ms total)
[----------] 7 tests from ProcnameWildcardTest
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_NoAsterisk
[ OK ] ProcnameWildcardTest.ProcnameWildcard_NoAsterisk (0 ms)
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch
[ OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch (0 ms)
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatch
[ OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatch (0 ms)
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch2
[ OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskNoMatch2 (0 ms)
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatchExactly
[ OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskMatchExactly (0 ms)
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_NoAsteriskMatchExactly
[ OK ] ProcnameWildcardTest.ProcnameWildcard_NoAsteriskMatchExactly (0 ms)
[ RUN ] ProcnameWildcardTest.ProcnameWildcard_AsteriskFirstChar
[ OK ] ProcnameWildcardTest.ProcnameWildcard_AsteriskFirstChar (0 ms)
[----------] 7 tests from ProcnameWildcardTest (0 ms total)
Signed-off-by: Tom Hromatka <[email protected]>
---
src/api.c | 2 +-
src/libcgroup-internal.h | 3 +
tests/gunit/004-cgroup_compare_ignore_rule.cpp | 44 ++++++++++
.../gunit/005-cgroup_compare_wildcard_procname.cpp | 97 ++++++++++++++++++++++
tests/gunit/Makefile.am | 3 +-
5 files changed, 147 insertions(+), 2 deletions(-)
create mode 100644 tests/gunit/005-cgroup_compare_wildcard_procname.cpp
diff --git a/src/api.c b/src/api.c
index c814d34c8829..396621761ab1 100644
--- a/src/api.c
+++ b/src/api.c
@@ -2811,7 +2811,7 @@ static int cg_prepare_cgroup(struct cgroup *cgroup, pid_t
pid,
* @param procname The name of the new process
* @return True if the procname matches the rule. False otherwise
*/
-static bool cgroup_compare_wildcard_procname(const char * const rule_procname,
+STATIC bool cgroup_compare_wildcard_procname(const char * const rule_procname,
const char * const procname)
{
size_t rule_strlen = strlen(rule_procname);
diff --git a/src/libcgroup-internal.h b/src/libcgroup-internal.h
index c827fea28d35..767cf196a0e7 100644
--- a/src/libcgroup-internal.h
+++ b/src/libcgroup-internal.h
@@ -306,6 +306,9 @@ int cg_get_cgroups_from_proc_cgroups(pid_t pid, char
*cgroup_list[],
bool cgroup_compare_ignore_rule(const struct cgroup_rule * const rule,
pid_t pid, const char * const procname);
+bool cgroup_compare_wildcard_procname(const char * const rule_procname,
+ const char * const procname);
+
#endif /* UNIT_TEST */
__END_DECLS
diff --git a/tests/gunit/004-cgroup_compare_ignore_rule.cpp
b/tests/gunit/004-cgroup_compare_ignore_rule.cpp
index f50f523d18d8..2f585ab9b70f 100644
--- a/tests/gunit/004-cgroup_compare_ignore_rule.cpp
+++ b/tests/gunit/004-cgroup_compare_ignore_rule.cpp
@@ -338,3 +338,47 @@ TEST_F(CgroupCompareIgnoreRuleTest, RootDestinationNoMatch)
ret = cgroup_compare_ignore_rule(&rule, pid, procname);
ASSERT_EQ(ret, false);
}
+
+TEST_F(CgroupCompareIgnoreRuleTest, WildcardProcnameSimpleMatch)
+{
+ char proc_file_contents[] =
+ "7:cpuacct:/MatchCgroup";
+ char rule_controller[] = "cpuacct";
+ char rule_procname[] = "ssh*";
+ char procname[] = "sshd";
+ struct cgroup_rule rule;
+ pid_t pid = 1234;
+ bool ret;
+
+ CreateCgroupProcFile(proc_file_contents);
+
+ rule.procname = rule_procname;
+ rule.is_ignore = true;
+ rule.controllers[0] = rule_controller;
+ sprintf(rule.destination, "MatchCgroup");
+
+ ret = cgroup_compare_ignore_rule(&rule, pid, procname);
+ ASSERT_EQ(ret, true);
+}
+
+TEST_F(CgroupCompareIgnoreRuleTest, WildcardProcnameNoMatch)
+{
+ char proc_file_contents[] =
+ "7:cpuacct:/AnotherCgroup";
+ char rule_controller[] = "cpuacct";
+ char rule_procname[] = "httpd*";
+ char procname[] = "httpx";
+ struct cgroup_rule rule;
+ pid_t pid = 1234;
+ bool ret;
+
+ CreateCgroupProcFile(proc_file_contents);
+
+ rule.procname = rule_procname;
+ rule.is_ignore = true;
+ rule.controllers[0] = rule_controller;
+ sprintf(rule.destination, "AnotherCgroup");
+
+ ret = cgroup_compare_ignore_rule(&rule, pid, procname);
+ ASSERT_EQ(ret, false);
+}
diff --git a/tests/gunit/005-cgroup_compare_wildcard_procname.cpp
b/tests/gunit/005-cgroup_compare_wildcard_procname.cpp
new file mode 100644
index 000000000000..62adf0760734
--- /dev/null
+++ b/tests/gunit/005-cgroup_compare_wildcard_procname.cpp
@@ -0,0 +1,97 @@
+/**
+ * libcgroup googletest for cgroup_compare_wildcard_procname()
+ *
+ * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
+ * Author: Tom Hromatka <[email protected]>
+ */
+
+/*
+ * 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 ProcnameWildcardTest : public ::testing::Test {
+};
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_NoAsterisk)
+{
+ char rule_procname[] = "systemd";
+ char procname[] = "bash";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, false);
+}
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_AsteriskNoMatch)
+{
+ char rule_procname[] = "BobIsYour*";
+ char procname[] = "Linda";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, false);
+}
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_AsteriskMatch)
+{
+ char rule_procname[] = "HelloWorl*";
+ char procname[] = "HelloWorld";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, true);
+}
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_AsteriskNoMatch2)
+{
+ char rule_procname[] = "HelloW*";
+ char procname[] = "Hello";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, false);
+}
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_AsteriskMatchExactly)
+{
+ char rule_procname[] = "strace*";
+ char procname[] = "strace";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, true);
+}
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_NoAsteriskMatchExactly)
+{
+ char rule_procname[] = "systemd-cgls";
+ char procname[] = "systemd-cgls";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, false);
+}
+
+TEST_F(ProcnameWildcardTest, ProcnameWildcard_AsteriskFirstChar)
+{
+ char rule_procname[] = "*";
+ char procname[] = "tomcat";
+ bool ret;
+
+ ret = cgroup_compare_wildcard_procname(rule_procname, procname);
+ ASSERT_EQ(ret, true);
+}
diff --git a/tests/gunit/Makefile.am b/tests/gunit/Makefile.am
index ff2630644044..43526f078f9c 100644
--- a/tests/gunit/Makefile.am
+++ b/tests/gunit/Makefile.am
@@ -42,4 +42,5 @@ gtest_SOURCES = gtest.cpp \
001-path.cpp \
002-cgroup_parse_rules_options.cpp \
003-cg_get_cgroups_from_proc_cgroups.cpp \
- 004-cgroup_compare_ignore_rule.cpp
+ 004-cgroup_compare_ignore_rule.cpp \
+ 005-cgroup_compare_wildcard_procname.cpp
--
1.8.3.1
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel