]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add cgroup_get_current_controller_path() (v2) test
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Thu, 27 Apr 2023 11:22:04 +0000 (11:22 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 27 Apr 2023 14:24:06 +0000 (08:24 -0600)
Add a test cases to stress pybindings of get_current_controller_path(),
that calls cgroup_get_current_controller_path() on unified mode.

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 27 11:21:33
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                             Time (sec)
        -----------------------------------------------------------
        setup                                                  0.00
        082-pybindings-cgrp_get_curr_ctrl_path-v2.py           2.18
        teardown                                               0.00
        -----------------------------------------------------------
        Total Run Time                                         2.18

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
tests/ftests/082-pybindings-cgrp_get_curr_ctrl_path-v2.py [new file with mode: 0755]
tests/ftests/Makefile.am

diff --git a/tests/ftests/082-pybindings-cgrp_get_curr_ctrl_path-v2.py b/tests/ftests/082-pybindings-cgrp_get_curr_ctrl_path-v2.py
new file mode 100755 (executable)
index 0000000..1426619
--- /dev/null
@@ -0,0 +1,134 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# cgroup_get_current_controller_path() test using the python bindings (cgroup v2)
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Kamalesh Babulal <kamalesh.babulal@oracle.com>
+#
+
+from cgroup import Cgroup as CgroupCli, Mode
+from libcgroup import Cgroup, Version
+from process import Process
+import consts
+import ftests
+import sys
+import os
+
+
+CGNAME = '082getctrlpathv2'
+CHILDCG = '082getctrlpathv2/childcg'
+CONTROLLER = 'cpu'
+
+
+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
+
+    if Cgroup.cgroup_mode() != Mode.CGROUP_MODE_UNIFIED:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires the unified cgroup v2 hierarchy'
+
+    return result, cause
+
+
+def setup(config):
+    CgroupCli.create(config, CONTROLLER, CGNAME)
+    CgroupCli.create(config, CONTROLLER, CHILDCG)
+
+    config.process.create_process_in_cgroup(config, CONTROLLER, CHILDCG, ignore_systemd=True)
+
+
+def test(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    expected_path = "/" + CHILDCG
+    pid = CgroupCli.get_pids_in_cgroup(config, CHILDCG, CONTROLLER)[0]
+    cgrp = Cgroup(CGNAME, Version.CGROUP_V2)
+
+    #
+    # Test 1 - get the relative path of cgroup, for the pid's cpu controller.
+    #          It's expected to pass because we had created cgroup on cpu
+    #          hierarchy and moved the task to that group.
+    #
+    cgrp_path = cgrp.get_current_controller_path(pid, CONTROLLER)
+    if cgrp_path != expected_path:
+        result = consts.TEST_FAILED
+        cause = 'Expected cgroup path {} got {}'.format(expected_path, cgrp_path)
+
+    #
+    # Test 2 - get the relative path of cgroup, for the pid's memory controller.
+    #          It's expected to fail because we not had created cgroup.
+    #
+    try:
+        cgrp_path = cgrp.get_current_controller_path(pid, "memory")
+    except RuntimeError as re:
+        if '50001' not in str(re):
+            raise re
+
+    #
+    # Test 3 - get the relative path of cgroup, for the pid's invalid controller.
+    #          It's expected to fail because such controller doesn't exists.
+    #
+    try:
+        cgrp_path = cgrp.get_current_controller_path(pid, "invalid")
+    except RuntimeError as re:
+        if '50011' not in str(re):
+            raise re
+
+    #
+    # Test 4 - get the relative path of cgroup, for the pid's pass NULL as
+    #          controller. It's expected to return the path with children.
+    #
+    cgrp_path = cgrp.get_current_controller_path(pid)
+    if cgrp_path != expected_path:
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected cgroup path {} got {}'.format(expected_path, cgrp_path)
+        cause = '\n'.join(filter(None, [cause, tmp_cause]))
+
+    #
+    # Test 5 - get the relative path of cgroup, for the pid's pass int as
+    #          controller. It's expected to fail because string is expected
+    #          for the controller name.
+    #
+    try:
+        cgrp_path = cgrp.get_current_controller_path(pid, 1234)
+    except TypeError as re:
+        if 'expected controller type string, but passed' not in str(re):
+            raise re
+
+    return result, cause
+
+
+def teardown(config):
+    pid = CgroupCli.get_pids_in_cgroup(config, CHILDCG, CONTROLLER)[0]
+    Process.kill(config, pid)
+
+    CgroupCli.delete(config, CONTROLLER, 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:
index 738c1877034480663c878f458efb21cad78c157e..452d3142cb93119122d19fc5cbc415f820c6e8c5 100644 (file)
@@ -102,6 +102,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          079-sudo-cgcreate_default_systemd_scope.py \
                          080-kernel-domain_invalid.py \
                          081-pybindings-cgrp_get_curr_ctrl_path-v1.py \
+                         082-pybindings-cgrp_get_curr_ctrl_path-v2.py \
                          083-pybindings-helpers_cgroup_mode.py \
                          998-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist