]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test to create a systemd scope
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 26 Oct 2022 16:21:03 +0000 (10:21 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 1 Nov 2022 21:21:06 +0000 (15:21 -0600)
-----------------------------------------------------------------
Test Results:
        Run Date:                          Sep 06 14:46:15
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                       Time (sec)
        -----------------------------------------------------
        setup                                            0.00
        049-sudo-systemd_create_scope.py                 0.08
        teardown                                         0.00
        -----------------------------------------------------
        Total Run Time                                   0.03

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

diff --git a/tests/ftests/049-sudo-systemd_create_scope.py b/tests/ftests/049-sudo-systemd_create_scope.py
new file mode 100755 (executable)
index 0000000..a8e9a7c
--- /dev/null
@@ -0,0 +1,106 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Create a systemd scope
+#
+# Copyright (c) 2022 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+from cgroup import Cgroup as CgroupCli
+from cgroup import CgroupVersion
+from run import Run, RunError
+from libcgroup import Cgroup
+from systemd import Systemd
+import ftests
+import consts
+import sys
+import os
+
+SLICE = 'libcgtests.slice'
+SCOPE = '049delegated.scope'
+
+# 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 CgroupVersion.get_version(CONTROLLER) != CgroupVersion.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
+
+    Cgroup.create_scope(scope_name=SCOPE, slice_name=SLICE)
+
+    if not Systemd.is_delegated(config, SCOPE):
+        result = consts.TEST_FAILED
+        cause = 'Cgroup is not delegated'
+
+    return result, cause
+
+
+def teardown(config, result):
+    pid = CgroupCli.get(config, cgname=os.path.join(SLICE, SCOPE), setting='cgroup.procs',
+                        print_headers=False, values_only=True)
+    if pid is not None:
+        Run.run(['kill', '-9', str(pid)], shell_bool=True)
+
+    if result != consts.TEST_PASSED:
+        # Something went wrong.  Let's force the removal of the cgroups just to be safe.
+        # Note that this should remove the cgroup, but it won't remove it from systemd's
+        # internal caches, so the system may not return to its 'pristine' prior-to-this-test
+        # state
+        try:
+            CgroupCli.delete(config, None, os.path.join(SLICE, SCOPE))
+        except RunError:
+            pass
+    else:
+        # There is no need to remove the scope.  systemd should automatically remove it
+        # once there are no processes inside of it
+        pass
+
+    return consts.TEST_PASSED, None
+
+
+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: