]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test for set uid/gid
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 27 Jan 2023 21:09:23 +0000 (14:09 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 27 Jan 2023 21:09:23 +0000 (14:09 -0700)
Add a test to exercise the python bindings to set the uid/gid on
a cgroup.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Nov 17 21:10:08
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                           Time (sec)
        -----------------------------------------
        setup                                0.00
        055-sudo-set_uid_gid_v1.py           0.05
        teardown                             0.00
        -----------------------------------------
        Total Run Time                       0.05

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
(cherry picked from commit 9d2dbb7f22a5bbb9d3c32f405b0d954e1fb77081)

tests/ftests/055-sudo-set_uid_gid_v1.py [new file with mode: 0755]
tests/ftests/Makefile.am

diff --git a/tests/ftests/055-sudo-set_uid_gid_v1.py b/tests/ftests/055-sudo-set_uid_gid_v1.py
new file mode 100755 (executable)
index 0000000..159ec50
--- /dev/null
@@ -0,0 +1,115 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Test to set the uid/gid on a cgroup v1 cgroup using the python bindings
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+from cgroup import Cgroup as CgroupCli, CgroupVersion
+from libcgroup import Cgroup, Version
+import consts
+import ftests
+import utils
+import sys
+import os
+
+CGNAME = '055setuidgid'
+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_V1:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires cgroup v1'
+
+    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_V1)
+    cg.set_uid_gid(TASKS_UID, TASKS_GID, CTRL_UID, CTRL_GID)
+    cg.add_controller(CONTROLLER)
+    cg.create(ignore_ownership=False)
+
+    tasks_path = os.path.join(CgroupCli.get_controller_mount_point(CONTROLLER), CGNAME,
+                              'tasks')
+
+    tasks_uid = utils.get_file_owner_uid(config, tasks_path)
+    if tasks_uid != TASKS_UID:
+        result = consts.TEST_FAILED
+        cause = 'Expected tasks owner to be {} but it\'s {}'.format(TASKS_UID, tasks_uid)
+
+    tasks_gid = utils.get_file_owner_gid(config, tasks_path)
+    if tasks_gid != TASKS_GID:
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected tasks group to be {} but it\'s {}'.format(TASKS_GID, tasks_gid)
+        if not cause:
+            cause = tmp_cause
+        else:
+            cause = '{}\n{}'.format(cause, tmp_cause)
+
+    ctrl_path = os.path.join(CgroupCli.get_controller_mount_point(CONTROLLER), CGNAME,
+                             'cgroup.procs')
+
+    ctrl_uid = utils.get_file_owner_uid(config, ctrl_path)
+    if ctrl_uid != CTRL_UID:
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected cgroup.procs owner to be {} but it\'s {}'.format(CTRL_UID, ctrl_uid)
+        if not cause:
+            cause = tmp_cause
+        else:
+            cause = '{}\n{}'.format(cause, tmp_cause)
+
+    ctrl_gid = utils.get_file_owner_gid(config, ctrl_path)
+    if ctrl_gid != CTRL_GID:
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected cgroup.procs group to be {} but it\'s {}'.format(CTRL_GID, ctrl_gid)
+        if not cause:
+            cause = tmp_cause
+        else:
+            cause = '{}\n{}'.format(cause, tmp_cause)
+
+    return result, cause
+
+
+def teardown(config):
+    CgroupCli.delete(config, CONTROLLER, 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:
index f0ddeaebc2e8cbb8350f61883e264db4d1d0152f..50222aec8a7f45dc754dbf4d58f11ad5f4a60fd5 100644 (file)
@@ -75,6 +75,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          052-sudo-cgroup_attach_task.py \
                          053-sudo-cgroup_attach_task_pid.py \
                          054-sudo-set_uid_gid_v2.py \
+                         055-sudo-set_uid_gid_v1.py \
                          098-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py