]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test for cgroup_add_all_controllers()
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 3 Mar 2023 18:16:00 +0000 (11:16 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 28 Mar 2023 17:08:35 +0000 (11:08 -0600)
Add a test for cgroup_add_all_controllers()

-----------------------------------------------------------------
Test Results:
        Run Date:                          Mar 03 19:24:58
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                                             Time (sec)
        -----------------------------------------------------------
        setup                                                  0.00
        073-pybindings-cgroup_add_all_controllers-v2.py        0.08
        teardown                                               0.00
        -----------------------------------------------------------
        Total Run Time                                         0.08

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

tests/ftests/073-pybindings-cgroup_add_all_controllers-v2.py [new file with mode: 0755]
tests/ftests/Makefile.am

diff --git a/tests/ftests/073-pybindings-cgroup_add_all_controllers-v2.py b/tests/ftests/073-pybindings-cgroup_add_all_controllers-v2.py
new file mode 100755 (executable)
index 0000000..f4ef20f
--- /dev/null
@@ -0,0 +1,100 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# cgroup_add_all_controllers() test using the python bindings
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+from libcgroup import Cgroup, Version, Mode
+from cgroup import Cgroup as CgroupCli
+import consts
+import ftests
+import sys
+import os
+
+
+PARENTCG = '073cgggetcg'
+CGNAME = '{}/child'.format(PARENTCG)
+CONTROLLERS = ['cpu', 'memory', 'pids']
+SUBTREE_CONTROL = '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 CgroupCli.get_cgroup_mode(config) != Mode.CGROUP_MODE_UNIFIED:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires the unified cgroup hierarchy'
+
+    return result, cause
+
+
+def setup(config):
+    CgroupCli.create(config, CONTROLLERS, PARENTCG)
+
+    # Ensure that the subtree control file differs from the cgroup.controllers file
+    CgroupCli.subtree_control(config, PARENTCG, CONTROLLERS, enable=False, ignore_systemd=True)
+    CgroupCli.subtree_control(config, PARENTCG, SUBTREE_CONTROL, enable=True, ignore_systemd=True)
+    CgroupCli.get_and_validate(config, PARENTCG, 'cgroup.subtree_control', SUBTREE_CONTROL, True)
+
+    CgroupCli.create(config, CONTROLLERS, CGNAME)
+
+
+def test(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    cgget = Cgroup(CGNAME, Version.CGROUP_V2)
+    cgget.get()
+
+    cgall = Cgroup(CGNAME, Version.CGROUP_V2)
+    cgall.add_all_controllers()
+    cgall.get()
+
+    if len(CONTROLLERS) != len(cgall.controllers):
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected {} controllers in cgall but received {}'.format(
+                    len(CONTROLLERS), len(cgall.controllers))
+        cause = '\n'.join(filter(None, [cause, tmp_cause]))
+
+    if len(str(cgall)) != len(str(cgget)):
+        result = consts.TEST_FAILED
+        tmp_cause = 'Expected {} lines in cgall but received {}'.format(
+                    len(str(cgget)), len(str(cgall)))
+        cause = '\n'.join(filter(None, [cause, tmp_cause]))
+
+    return result, cause
+
+
+def teardown(config):
+    CgroupCli.delete(config, CONTROLLERS, CGNAME)
+    CgroupCli.delete(config, CONTROLLERS, PARENTCG)
+
+
+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 59e0377c999a439cff1d1ad678849012fc364c1b..470247bd4a1de802ffbb6316a4f06f1c14c426b5 100644 (file)
@@ -93,6 +93,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          070-sudo-systemd_cgxget-cpu-settings-v2.py \
                          071-sudo-set_default_systemd_cgroup.py \
                          072-pybindings-cgroup_get_cgroup.py \
+                         073-pybindings-cgroup_add_all_controllers-v2.py \
                          998-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py