From 5ac6ee887ba6edb66f95eee26e107cc90a595e6a Mon Sep 17 00:00:00 2001 From: Kamalesh Babulal Date: Fri, 10 Feb 2023 14:53:48 -0700 Subject: [PATCH] ftests: Add a test to validate cgexec's systemd options (cgroup v1) Add a test case to validate cgexec tool's systemd options on cgroup legacy/hybrid setup modes. ----------------------------------------------------------------- Test Results: Run Date: Feb 05 07:48:48 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) -------------------------------------------- setup 0.00 067-sudo-systemd_cgexec-v1.py 5.32 teardown 0.00 -------------------------------------------- Total Run Time 5.32 Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka (cherry picked from commit 4e3dc1b617881f0dcfc75e83a345779eca61e8c8) --- tests/ftests/067-sudo-systemd_cgexec-v1.py | 168 +++++++++++++++++++++ tests/ftests/Makefile.am | 1 + 2 files changed, 169 insertions(+) create mode 100755 tests/ftests/067-sudo-systemd_cgexec-v1.py diff --git a/tests/ftests/067-sudo-systemd_cgexec-v1.py b/tests/ftests/067-sudo-systemd_cgexec-v1.py new file mode 100755 index 00000000..c1d04907 --- /dev/null +++ b/tests/ftests/067-sudo-systemd_cgexec-v1.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# Advanced cgexec functionality test - '-b' '-g' (cgroup v1) +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Kamalesh Babulal +# + +from cgroup import Cgroup, CgroupVersion +from systemd import Systemd +from run import Run, RunError +import consts +import ftests +import time +import sys +import os + + +CONTROLLER = 'cpu' +SYSTEMD_CGNAME = '067_cg_in_scope' +OTHER_CGNAME = '067_cg_not_in_scope' + +SLICE = 'libcgtests.slice' +SCOPE = 'test067.scope' + +CONFIG_FILE_NAME = os.path.join(os.getcwd(), '067cgconfig.conf') + +SYSTEMD_PIDS = '' +OTHER_PIDS = '' + + +def prereqs(config): + result = consts.TEST_PASSED + cause = None + + if CgroupVersion.get_version('cpu') != CgroupVersion.CGROUP_V1: + result = consts.TEST_SKIPPED + cause = 'This test requires the cgroup v1 cpu controller' + return result, cause + + if config.args.container: + result = consts.TEST_SKIPPED + cause = 'This test cannot be run within a container' + + return result, cause + + +def setup(config): + result = consts.TEST_PASSED + cause = None + + Systemd.write_config_with_pid(config, CONFIG_FILE_NAME, SLICE, SCOPE) + + Cgroup.configparser(config, load_file=CONFIG_FILE_NAME) + + # create and check if the cgroup was created under the systemd default path + if not Cgroup.create_and_validate(config, CONTROLLER, SYSTEMD_CGNAME): + result = consts.TEST_FAILED + cause = ( + 'Failed to create systemd delegated cgroup {} under ' + '/sys/fs/cgroup/{}/{}/{}/'.format(SYSTEMD_CGNAME, CONTROLLER, SLICE, SCOPE) + ) + return result, cause + + # create and check if the cgroup was created under the controller sub-tree + if not Cgroup.create_and_validate(config, CONTROLLER, OTHER_CGNAME, ignore_systemd=True): + result = consts.TEST_FAILED + cause = ( + 'Failed to create cgroup {} under ' + '/sys/fs/cgroup/{}/'.format(OTHER_CGNAME, CONTROLLER) + ) + + return result, cause + + +def create_process_get_pid(config, CGNAME, SLICENAME='', ignore_systemd=False): + result = consts.TEST_PASSED + cause = None + + config.process.create_process_in_cgroup( + config, CONTROLLER, CGNAME, cgclassify=False, + ignore_systemd=ignore_systemd + ) + + # We need pause, before the cgroups.procs gets updated, post cgexec + time.sleep(1) + + pids = Cgroup.get_pids_in_cgroup(config, os.path.join(SLICENAME, CGNAME), CONTROLLER) + if pids is None: + result = consts.TEST_FAILED + cause = 'No processes were found in cgroup {}'.format(CGNAME) + + return pids, result, cause + + +def terminate_process(config, pids): + if pids: + for p in pids.splitlines(): + Run.run(['sudo', 'kill', '-9', p]) + + +def test(config): + global SYSTEMD_PIDS, OTHER_PIDS + + result = consts.TEST_PASSED + cause = None + + # Test cgclassify, that creates a process and then uses cgclassify + # to migrate the task the cgroup. + SYSTEMD_PIDS, result, cause = create_process_get_pid( + config, SYSTEMD_CGNAME, + os.path.join(SLICE, SCOPE) + ) + + OTHER_PIDS, result, tmp_cause = create_process_get_pid( + config, OTHER_CGNAME, + ignore_systemd=True + ) + cause = '\n'.join(filter(None, [cause, tmp_cause])) + + return result, cause + + +def teardown(config): + global SYSTEMD_PIDS, OTHER_PIDS + + terminate_process(config, SYSTEMD_PIDS) + terminate_process(config, OTHER_PIDS) + + # We need a pause, so that cgroup.procs gets updated. + time.sleep(1) + + Systemd.remove_scope_slice_conf(config, SLICE, SCOPE, CONTROLLER, CONFIG_FILE_NAME) + + # Incase the error occurs before the creation of OTHER_CGNAME, + # let's ignore the exception + try: + Cgroup.delete(config, CONTROLLER, OTHER_CGNAME, ignore_systemd=True) + except RunError as re: + if 'No such file or directory' not in re.stderr: + raise re + + +def main(config): + [result, cause] = prereqs(config) + if result != consts.TEST_PASSED: + return [result, cause] + + [result, cause] = setup(config) + if result != consts.TEST_PASSED: + return [result, cause] + + try: + [result, cause] = test(config) + finally: + teardown(config) + + 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: diff --git a/tests/ftests/Makefile.am b/tests/ftests/Makefile.am index a18533a9..1910c25a 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -87,6 +87,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 064-sudo-systemd_cgset-v2.py \ 065-sudo-systemd_cgclassify-v1.py \ 066-sudo-systemd_cgclassify-v2.py \ + 067-sudo-systemd_cgexec-v1.py \ 098-cgdelete-non-existing-shared-mnt-cgroup-v1.py # Intentionally omit the stress test from the extra dist # 999-stress-cgroup_init.py -- 2.47.2