From: Tom Hromatka Date: Fri, 27 Jan 2023 20:34:45 +0000 (-0700) Subject: ftests: Add a test for set uid/gid X-Git-Tag: v3.1.0~234 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7b6d6d0690aaa1f9d54736ca69c5fe84fe9a30b;p=thirdparty%2Flibcgroup.git ftests: Add a test for set uid/gid Add a test to exercise the python bindings to set the uid/gid on a cgroup. ----------------------------------------------------------------- Test Results: Run Date: Nov 16 23:12:25 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ----------------------------------------- setup 0.00 054-sudo-set_uid_gid_v2.py 0.02 teardown 0.00 ----------------------------------------- Total Run Time 0.02 Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/tests/ftests/054-sudo-set_uid_gid_v2.py b/tests/ftests/054-sudo-set_uid_gid_v2.py new file mode 100755 index 00000000..4116f34b --- /dev/null +++ b/tests/ftests/054-sudo-set_uid_gid_v2.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# Test to set the uid/gid on a cgroup v2 cgroup using the python bindings +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Tom Hromatka +# + +from cgroup import Cgroup as CgroupCli, CgroupVersion +from libcgroup import Cgroup, Version +import consts +import ftests +import utils +import sys +import os + +CGNAME = '054setuidgid' +CONTROLLER = 'cpu' +TASKS_UID = 1234 +TASKS_GID = 5678 +CTRL_UID = 3456 +CTRL_GID = 7890 + + +def prereqs(config): + result = consts.TEST_PASSED + cause = None + + if CgroupVersion.get_version(CONTROLLER) != CgroupVersion.CGROUP_V2: + result = consts.TEST_SKIPPED + cause = 'This test requires cgroup v2' + + return result, cause + + +def setup(config): + return consts.TEST_PASSED, None + + +def test(config): + result = consts.TEST_PASSED + cause = None + + cg = Cgroup(CGNAME, Version.CGROUP_V2) + cg.set_uid_gid(TASKS_UID, TASKS_GID, CTRL_UID, CTRL_GID) + cg.add_controller(CONTROLLER) + cg.create(ignore_ownership=False) + + ctrl_path = os.path.join(CgroupCli.get_controller_mount_point(CONTROLLER), CGNAME, + 'cgroup.procs') + + uid = utils.get_file_owner_uid(config, ctrl_path) + if uid != CTRL_UID: + result = consts.TEST_FAILED + cause = 'Expected cgroup.procs owner to be {} but it\'s {}'.format(CTRL_UID, uid) + + gid = utils.get_file_owner_gid(config, ctrl_path) + if gid != CTRL_GID: + result = consts.TEST_FAILED + tmp_cause = 'Expected cgroup.procs group to be {} but it\'s {}'.format(CTRL_GID, gid) + if not cause: + cause = tmp_cause + else: + cause = '{}\n{}'.format(cause, tmp_cause) + + return result, cause + + +def teardown(config): + CgroupCli.delete(config, None, CGNAME) + + return consts.TEST_PASSED, None + + +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 59022b1a..f0ddeaeb 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -72,6 +72,9 @@ EXTRA_DIST_PYTHON_TESTS = \ 049-sudo-systemd_create_scope.py \ 050-sudo-systemd_create_scope_w_pid.py \ 051-sudo-cgroup_get_cgroup.py \ + 052-sudo-cgroup_attach_task.py \ + 053-sudo-cgroup_attach_task_pid.py \ + 054-sudo-set_uid_gid_v2.py \ 098-cgdelete-non-existing-shared-mnt-cgroup-v1.py # Intentionally omit the stress test from the extra dist # 999-stress-cgroup_init.py