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

-----------------------------------------------------------------
Test Results:
        Run Date:                          Nov 17 21:55:52
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                               Time (sec)
        ---------------------------------------------
        setup                                    0.00
        056-sudo-set_permissions_v2.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>
tests/ftests/056-sudo-set_permissions_v2.py [new file with mode: 0755]
tests/ftests/Makefile.am

diff --git a/tests/ftests/056-sudo-set_permissions_v2.py b/tests/ftests/056-sudo-set_permissions_v2.py
new file mode 100755 (executable)
index 0000000..8c21f5e
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Test to set the permissions on a 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 stat
+import sys
+import os
+
+CGNAME = '056setperms'
+CONTROLLER = 'memory'
+# 0711
+DIR_MODE = stat.S_IRWXU | stat.S_IXGRP | stat.S_IXOTH
+# 0640
+CTRL_MODE = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP
+# 0771
+TASK_MODE = stat.S_IRWXU | stat.S_IRWXG | stat.S_IXOTH
+
+
+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_permissions(DIR_MODE, CTRL_MODE, TASK_MODE)
+    cg.add_controller(CONTROLLER)
+    cg.create(ignore_ownership=False)
+
+    dir_path = os.path.join(CgroupCli.get_controller_mount_point(CONTROLLER), CGNAME)
+
+    dir_mode = utils.get_file_permissions(config, dir_path)
+    if int(dir_mode, 8) != DIR_MODE:
+        result = consts.TEST_FAILED
+        cause = 'Expected directory mode to be {} but it\'s {}'.format(
+                    format(DIR_MODE, '03o'), dir_mode)
+
+    ctrl_path = os.path.join(CgroupCli.get_controller_mount_point(CONTROLLER), CGNAME,
+                             'cgroup.procs')
+
+    ctrl_mode = utils.get_file_permissions(config, ctrl_path)
+    if int(ctrl_mode, 8) != CTRL_MODE:
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected cgroup.procs mode to be {} but it\'s {}'.format(
+                    format(CTRL_MODE, '03o'), ctrl_mode)
+        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:
index 50222aec8a7f45dc754dbf4d58f11ad5f4a60fd5..b793bb7cc5f991664622976c10ae5609ca40effd 100644 (file)
@@ -76,6 +76,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          053-sudo-cgroup_attach_task_pid.py \
                          054-sudo-set_uid_gid_v2.py \
                          055-sudo-set_uid_gid_v1.py \
+                         056-sudo-set_permissions_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