]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test to exercise invalid systemd parameters
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 27 Jan 2023 20:48:31 +0000 (13:48 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 27 Jan 2023 21:04:22 +0000 (14:04 -0700)
Add a test that exercises invalid parameters to
cgroup_create_scope2().

-----------------------------------------------------------------
Test Results:
        Run Date:                          Jan 25 09:32:58
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                          Time (sec)
        --------------------------------------------------------
        setup                                               0.00
        059-sudo-invalid_systemd_create_scope2.py           0.00
        teardown                                            0.00
        --------------------------------------------------------
        Total Run Time                                      0.00

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
tests/ftests/059-sudo-invalid_systemd_create_scope2.py [new file with mode: 0755]
tests/ftests/Makefile.am

diff --git a/tests/ftests/059-sudo-invalid_systemd_create_scope2.py b/tests/ftests/059-sudo-invalid_systemd_create_scope2.py
new file mode 100755 (executable)
index 0000000..228ff35
--- /dev/null
@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Test invalid parameters for systemd_create_scope2()
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+from cgroup import CgroupVersion as CgroupCliVersion
+from libcgroup import Cgroup, Version
+import ftests
+import consts
+import utils
+import sys
+import os
+
+# Which controller isn't all that important, but it is important that we
+# have a cgroup v2 controller
+CONTROLLER = 'cpu'
+
+
+def prereqs(config):
+    if config.args.container:
+        result = consts.TEST_SKIPPED
+        cause = 'This test cannot be run within a container'
+        return result, cause
+
+    if CgroupCliVersion.get_version(CONTROLLER) != CgroupCliVersion.CGROUP_V2:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires cgroup v2'
+        return result, cause
+
+    result = consts.TEST_PASSED
+    cause = None
+
+    return result, cause
+
+
+def setup(config):
+    pass
+
+
+def test(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    cg1 = Cgroup("InvalidNameBecauseNoSlash", Version.CGROUP_V2)
+    cg1.add_controller(CONTROLLER)
+    try:
+        cg1.create_scope2()
+    except RuntimeError as re:
+        if '50011' not in str(re):
+            result = consts.TEST_FAILED
+            cause = 'Expected ECGINVAL (50011) but received {}'.format(re)
+    else:
+        result = consts.TEST_FAILED
+        cause = 'An invalid cgroup name unexpectedly passed: {}'.format(cg1.name)
+
+
+    cg2 = Cgroup("Invalid/TooMany/Slashes", Version.CGROUP_V2)
+    cg2.add_controller(CONTROLLER)
+    try:
+        cg2.create_scope2()
+    except RuntimeError as re:
+        if '50011' not in str(re):
+            result = consts.TEST_FAILED
+            tmp_cause = 'Expected ECGINVAL (50011) but received {}'.format(re)
+            if not cause:
+                cause = tmp_cause
+            else:
+                cause = '{}\n{}'.format(cause, tmp_cause)
+    else:
+        result = consts.TEST_FAILED
+        cause = 'An invalid cgroup name unexpectedly passed: {}'.format(cg1.name)
+        if not cause:
+            cause = tmp_cause
+        else:
+            cause = '{}\n{}'.format(cause, tmp_cause)
+
+    return result, cause
+
+
+def teardown(config, result):
+    pass
+
+
+def main(config):
+    [result, cause] = prereqs(config)
+    if result != consts.TEST_PASSED:
+        return [result, cause]
+
+    try:
+        result = consts.TEST_FAILED
+        setup(config)
+        [result, cause] = test(config)
+    finally:
+        teardown(config, result)
+
+    return [result, cause]
+
+
+if __name__ == '__main__':
+    config = ftests.parse_args()
+    # this test was invoked directly.  run only it
+    config.args.num = int(os.path.basename(__file__).split('-')[0])
+    sys.exit(ftests.main(config))
+
+# vim: set et ts=4 sw=4:
index 7fd05f15f0f0d5edea2e6eb8b548565a36079914..282e0ff59cb49e0f484fa186e8cb438022ffe51a 100644 (file)
@@ -79,6 +79,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          056-sudo-set_permissions_v2.py \
                          057-sudo-set_permissions_v1.py \
                          058-sudo-systemd_create_scope2.py \
+                         059-sudo-invalid_systemd_create_scope2.py \
                          098-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py