From 17a1d1b1c2eee4d9cf6f65eb0844f8030d7d7526 Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Wed, 26 Oct 2022 10:24:12 -0600 Subject: [PATCH] ftests: Add a test to create a systemd scope Add a test to create a systemd scope where the user provides a pid. ----------------------------------------------------------------- Test Results: Run Date: Sep 29 21:27:17 Passed: 2 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ------------------------------------------------ setup 0.00 049-sudo-systemd_create_scope.py 1.04 050-sudo-systemd_create_scope2.py 3.03 teardown 0.00 ------------------------------------------------ Total Run Time 4.07 Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- .../ftests/050-sudo-systemd_create_scope2.py | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100755 tests/ftests/050-sudo-systemd_create_scope2.py diff --git a/tests/ftests/050-sudo-systemd_create_scope2.py b/tests/ftests/050-sudo-systemd_create_scope2.py new file mode 100755 index 00000000..66b7ad34 --- /dev/null +++ b/tests/ftests/050-sudo-systemd_create_scope2.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# Create a systemd scope with an existing PID +# +# Copyright (c) 2022 Oracle and/or its affiliates. +# Author: Tom Hromatka +# + +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 = '050delegated.scope' +pid = None + +# 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): + global pid + + result = consts.TEST_PASSED + cause = None + + pid = int(config.process.create_process(config)) + Cgroup.create_scope(SCOPE, slice_name=SLICE, pid=pid) + + if not Systemd.is_delegated(config, SCOPE): + result = consts.TEST_FAILED + cause = 'Cgroup is not delegated' + + return result, cause + + +def teardown(config, result): + global pid + + 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: -- 2.47.2