From: Kamalesh Babulal Date: Thu, 27 Apr 2023 11:22:04 +0000 (+0000) Subject: ftests: Add cgroup_get_current_controller_path() (v2) test X-Git-Tag: v3.1.0~83 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3515a9eb9bc3eb306aca7c2e6f87ed92c904a7be;p=thirdparty%2Flibcgroup.git ftests: Add cgroup_get_current_controller_path() (v2) test Add a test cases to stress pybindings of get_current_controller_path(), that calls cgroup_get_current_controller_path() on unified mode. ----------------------------------------------------------------- Test Results: Run Date: Apr 27 11:21:33 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ----------------------------------------------------------- setup 0.00 082-pybindings-cgrp_get_curr_ctrl_path-v2.py 2.18 teardown 0.00 ----------------------------------------------------------- Total Run Time 2.18 Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/tests/ftests/082-pybindings-cgrp_get_curr_ctrl_path-v2.py b/tests/ftests/082-pybindings-cgrp_get_curr_ctrl_path-v2.py new file mode 100755 index 00000000..1426619f --- /dev/null +++ b/tests/ftests/082-pybindings-cgrp_get_curr_ctrl_path-v2.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# cgroup_get_current_controller_path() test using the python bindings (cgroup v2) +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Kamalesh Babulal +# + +from cgroup import Cgroup as CgroupCli, Mode +from libcgroup import Cgroup, Version +from process import Process +import consts +import ftests +import sys +import os + + +CGNAME = '082getctrlpathv2' +CHILDCG = '082getctrlpathv2/childcg' +CONTROLLER = 'cpu' + + +def prereqs(config): + result = consts.TEST_PASSED + cause = None + + if config.args.container: + result = consts.TEST_SKIPPED + cause = 'This test cannot be run within a container' + return result, cause + + if Cgroup.cgroup_mode() != Mode.CGROUP_MODE_UNIFIED: + result = consts.TEST_SKIPPED + cause = 'This test requires the unified cgroup v2 hierarchy' + + return result, cause + + +def setup(config): + CgroupCli.create(config, CONTROLLER, CGNAME) + CgroupCli.create(config, CONTROLLER, CHILDCG) + + config.process.create_process_in_cgroup(config, CONTROLLER, CHILDCG, ignore_systemd=True) + + +def test(config): + result = consts.TEST_PASSED + cause = None + + expected_path = "/" + CHILDCG + pid = CgroupCli.get_pids_in_cgroup(config, CHILDCG, CONTROLLER)[0] + cgrp = Cgroup(CGNAME, Version.CGROUP_V2) + + # + # Test 1 - get the relative path of cgroup, for the pid's cpu controller. + # It's expected to pass because we had created cgroup on cpu + # hierarchy and moved the task to that group. + # + cgrp_path = cgrp.get_current_controller_path(pid, CONTROLLER) + if cgrp_path != expected_path: + result = consts.TEST_FAILED + cause = 'Expected cgroup path {} got {}'.format(expected_path, cgrp_path) + + # + # Test 2 - get the relative path of cgroup, for the pid's memory controller. + # It's expected to fail because we not had created cgroup. + # + try: + cgrp_path = cgrp.get_current_controller_path(pid, "memory") + except RuntimeError as re: + if '50001' not in str(re): + raise re + + # + # Test 3 - get the relative path of cgroup, for the pid's invalid controller. + # It's expected to fail because such controller doesn't exists. + # + try: + cgrp_path = cgrp.get_current_controller_path(pid, "invalid") + except RuntimeError as re: + if '50011' not in str(re): + raise re + + # + # Test 4 - get the relative path of cgroup, for the pid's pass NULL as + # controller. It's expected to return the path with children. + # + cgrp_path = cgrp.get_current_controller_path(pid) + if cgrp_path != expected_path: + result = consts.TEST_FAILED + tmp_cause = 'Expected cgroup path {} got {}'.format(expected_path, cgrp_path) + cause = '\n'.join(filter(None, [cause, tmp_cause])) + + # + # Test 5 - get the relative path of cgroup, for the pid's pass int as + # controller. It's expected to fail because string is expected + # for the controller name. + # + try: + cgrp_path = cgrp.get_current_controller_path(pid, 1234) + except TypeError as re: + if 'expected controller type string, but passed' not in str(re): + raise re + + return result, cause + + +def teardown(config): + pid = CgroupCli.get_pids_in_cgroup(config, CHILDCG, CONTROLLER)[0] + Process.kill(config, pid) + + CgroupCli.delete(config, CONTROLLER, CGNAME, recursive=True) + + +def main(config): + [result, cause] = prereqs(config) + if result != consts.TEST_PASSED: + return [result, cause] + + setup(config) + [result, cause] = test(config) + 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 738c1877..452d3142 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -102,6 +102,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 079-sudo-cgcreate_default_systemd_scope.py \ 080-kernel-domain_invalid.py \ 081-pybindings-cgrp_get_curr_ctrl_path-v1.py \ + 082-pybindings-cgrp_get_curr_ctrl_path-v2.py \ 083-pybindings-helpers_cgroup_mode.py \ 998-cgdelete-non-existing-shared-mnt-cgroup-v1.py # Intentionally omit the stress test from the extra dist