From: Tom Hromatka Date: Wed, 17 May 2023 20:52:01 +0000 (-0600) Subject: ftests: Add a test for cgroup_change_cgroup_path() X-Git-Tag: v3.1.0~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e7a70099f2e4195e413fdb129073cdd7b0e4655;p=thirdparty%2Flibcgroup.git ftests: Add a test for cgroup_change_cgroup_path() Add a functional test that exercises cgroup_change_cgroup_path() via the python bindings. ----------------------------------------------------------------- Test Results: Run Date: May 17 21:20:19 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ----------------------------------------- setup 0.00 087-sudo-move_pid.py 2.06 teardown 0.00 ----------------------------------------- Total Run Time Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/tests/ftests/087-sudo-move_pid.py b/tests/ftests/087-sudo-move_pid.py new file mode 100755 index 00000000..f5eee7d8 --- /dev/null +++ b/tests/ftests/087-sudo-move_pid.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# Test to move a pid to a cgroup +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Tom Hromatka +# + +from libcgroup import Cgroup, Mode, Version +from process import Process +import consts +import ftests +import sys +import os + +CGNAME = '087movepid' + + +def prereqs(config): + result = consts.TEST_PASSED + cause = None + + if Cgroup.cgroup_mode() != Mode.CGROUP_MODE_UNIFIED: + result = consts.TEST_SKIPPED + cause = 'This test requires the unified cgroup hierarchy' + + return result, cause + + +def setup(config): + result = consts.TEST_PASSED + cause = None + + cg = Cgroup(CGNAME, Version.CGROUP_V2) + cg.create() + + pid = config.process.create_process(config) + + path = Cgroup.get_current_controller_path(pid) + if path == '/' + CGNAME: + result = consts.TEST_FAILED + cause = 'The PID was already in the destination cgroup {}'.format(CGNAME) + + return result, cause, pid + + +def test(config, pid): + result = consts.TEST_PASSED + cause = None + + Cgroup.move_process(pid, CGNAME) + + path = Cgroup.get_current_controller_path(pid) + if path != '/' + CGNAME: + result = consts.TEST_FAILED + cause = 'Expected the pid to be in {} cgroup, but was instead in {}'.format(CGNAME, path) + + return result, cause + + +def teardown(config, pid): + Process.kill(config, pid) + + cg = Cgroup(CGNAME, Version.CGROUP_V2) + cg.delete() + + +def main(config): + [result, cause] = prereqs(config) + if result != consts.TEST_PASSED: + return [result, cause] + + [result, cause, pid] = setup(config) + if result != consts.TEST_PASSED: + teardown(config, pid) + return [result, cause] + + [result, cause] = test(config, pid) + teardown(config, pid) + + 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 5ab6e386..a769f6f6 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -106,6 +106,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 083-pybindings-helpers_cgroup_mode.py \ 084-sudo-cgcreate_systemd_scope_pid.py \ 086-sudo-systemd_cmdline_example.py \ + 087-sudo-move_pid.py \ 998-cgdelete-non-existing-shared-mnt-cgroup-v1.py # Intentionally omit the stress test from the extra dist # 999-stress-cgroup_init.py