From: Tom Hromatka Date: Fri, 17 Mar 2023 15:25:49 +0000 (-0600) Subject: ftests: Add test for cgroup_get_procs() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d69a27d7d864ea381b85a219576a4d1c7a03e3e;p=thirdparty%2Flibcgroup.git ftests: Add test for cgroup_get_procs() Add a functional test for cgroup_get_procs() ----------------------------------------------------------------- Test Results: Run Date: Mar 17 16:11:34 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ------------------------------------------------- setup 0.00 077-pybindings-cgroup_get_procs.py 40.31 teardown 0.00 ------------------------------------------------- Total Run Time 40.31 Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal (cherry picked from commit e7efc43140e5be079619a06efb50d859f5027b16) --- diff --git a/tests/ftests/077-pybindings-cgroup_get_procs.py b/tests/ftests/077-pybindings-cgroup_get_procs.py new file mode 100755 index 00000000..64f3935e --- /dev/null +++ b/tests/ftests/077-pybindings-cgroup_get_procs.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# cgroup_get_procs() test using the python bindings +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Tom Hromatka +# + +from cgroup import Cgroup as CgroupCli +from libcgroup import Cgroup, Version +from process import Process +import consts +import ftests +import sys +import os + + +CGNAME = '077getprocs/cgwithpids' +EMPTY_CGNAME = '077getprocs/cgwithoutpids' +CONTROLLERS = ['cpu', 'pids'] +PID_CNT = 20 + +initial_pid_list = list() + + +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 + + +def setup(config): + global initial_pid_list + CgroupCli.create(config, CONTROLLERS, CGNAME) + CgroupCli.create(config, CONTROLLERS, EMPTY_CGNAME) + + for i in range(0, PID_CNT): + pid = config.process.create_process(config) + initial_pid_list.append(pid) + + CgroupCli.classify(config, CONTROLLERS, CGNAME, initial_pid_list, ignore_systemd=True) + initial_pid_list = initial_pid_list.sort() + + +def test(config): + global initial_pid_list + result = consts.TEST_PASSED + cause = None + + # + # Test 1 - verify pids are properly populated and retrieved from a cgroup + # + cg = Cgroup(CGNAME, Version.CGROUP_V2) + for controller in CONTROLLERS: + cg.add_controller(controller) + pid_list = cg.get_procs().sort() + + if pid_list != initial_pid_list: + result = consts.TEST_FAILED + tmp_cause = 'The pid lists do not match\n{}\n{}'.format(initial_pid_list, pid_list) + cause = '\n'.join(filter(None, [cause, tmp_cause])) + + # + # Test 2 - verify there are no pids in the empty cgroup + # + emptycg = Cgroup(EMPTY_CGNAME, Version.CGROUP_V2) + for controller in CONTROLLERS: + emptycg.add_controller(controller) + empty_pid_list = emptycg.get_procs() + + if len(empty_pid_list) != 0: + result = consts.TEST_FAILED + tmp_cause = 'The pid list unexpectedly was populated\n{}'.format(empty_pid_list) + cause = '\n'.join(filter(None, [cause, tmp_cause])) + + return result, cause + + +def teardown(config): + global initial_pid_list + + Process.kill(config, initial_pid_list) + CgroupCli.delete(config, CONTROLLERS, EMPTY_CGNAME) + CgroupCli.delete(config, CONTROLLERS, os.path.dirname(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 5ed61db8..78e14ec2 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -97,6 +97,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 074-pybindings-cgroup_add_all_controllers-v1.py \ 075-pybindings-cgroup_compare_cgroup.py \ 076-cgconfig-auto_convert.py \ + 077-pybindings-cgroup_get_procs.py \ 998-cgdelete-non-existing-shared-mnt-cgroup-v1.py # Intentionally omit the stress test from the extra dist # 999-stress-cgroup_init.py