From df1ade964a184852860b5a20592cc757e28fa5f5 Mon Sep 17 00:00:00 2001 From: Kamalesh Babulal Date: Thu, 27 Apr 2023 11:11:16 +0000 Subject: [PATCH] ftests: Add cgroup_get_current_controller_path() (v1) test Add a test cases to stress pybindings of get_current_controller_path(), that calls cgroup_get_current_controller_path() on legacy/hybrid mode. ----------------------------------------------------------------- Test Results: Run Date: Apr 27 11:10:22 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ----------------------------------------------------------- setup 0.00 081-pybindings-cgrp_get_curr_ctrl_path-v1.py 2.34 teardown 0.00 ----------------------------------------------------------- Total Run Time 2.34 Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- ...1-pybindings-cgrp_get_curr_ctrl_path-v1.py | 133 ++++++++++++++++++ tests/ftests/Makefile.am | 1 + 2 files changed, 134 insertions(+) create mode 100755 tests/ftests/081-pybindings-cgrp_get_curr_ctrl_path-v1.py diff --git a/tests/ftests/081-pybindings-cgrp_get_curr_ctrl_path-v1.py b/tests/ftests/081-pybindings-cgrp_get_curr_ctrl_path-v1.py new file mode 100755 index 00000000..d4a07b73 --- /dev/null +++ b/tests/ftests/081-pybindings-cgrp_get_curr_ctrl_path-v1.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# cgroup_get_current_controller_path() test using the python bindings (cgroup v1) +# +# 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 = '081getctrlpathv1' +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 legacy cgroup v1 hierarchy' + + return result, cause + + +def setup(config): + CgroupCli.create(config, CONTROLLER, CGNAME) + + config.process.create_process_in_cgroup(config, CONTROLLER, CGNAME, ignore_systemd=True) + + +def test(config): + result = consts.TEST_PASSED + cause = None + + expected_path = "/" + CGNAME + pid = CgroupCli.get_pids_in_cgroup(config, CGNAME, 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. + # + cgrp_path = cgrp.get_current_controller_path(pid, "memory") + if cgrp_path == expected_path: + result = consts.TEST_FAILED + tmp_cause = 'cgroup path unexpectedly formed {}'.format(cgrp_path) + cause = '\n'.join(filter(None, [cause, tmp_cause])) + + # + # 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 fail because it's not supported + # cgroup v1. + # + try: + cgrp_path = cgrp.get_current_controller_path(pid, None) + except RuntimeError as re: + if '50016' not in str(re): + raise re + + # + # 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, CGNAME, CONTROLLER)[0] + Process.kill(config, pid) + + CgroupCli.delete(config, CONTROLLER, CGNAME) + + +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 2f82f8bb..738c1877 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -101,6 +101,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 078-sudo-cgcreate_systemd_scope.py \ 079-sudo-cgcreate_default_systemd_scope.py \ 080-kernel-domain_invalid.py \ + 081-pybindings-cgrp_get_curr_ctrl_path-v1.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 -- 2.47.2