]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test for cgroup_add_all_controllers()
authorTom Hromatka <tom.hromatka@oracle.com>
Sat, 4 Mar 2023 03:14:47 +0000 (03:14 +0000)
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 04 04:00:53
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                                Time (sec)
--------------------------------------------------------------
setup                                                     0.00
074-pybindings-cgroup_add_all_controllers-v1.py           0.13
teardown                                                  0.00
--------------------------------------------------------------
Total Run Time                                            0.13

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

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

diff --git a/tests/ftests/074-pybindings-cgroup_add_all_controllers-v1.py b/tests/ftests/074-pybindings-cgroup_add_all_controllers-v1.py
new file mode 100755 (executable)
index 0000000..1207bb2
--- /dev/null
@@ -0,0 +1,90 @@
+#!/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
+
+CGNAME = '074cggetcg/child'
+CONTROLLERS = ['cpu', 'memory', 'pids']
+
+
+def prereqs(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    if CgroupCli.get_cgroup_mode(config) != Mode.CGROUP_MODE_LEGACY:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires the legacy cgroup hierarchy'
+
+    return result, cause
+
+
+def setup(config):
+    CgroupCli.create(config, CONTROLLERS, CGNAME)
+
+
+def test(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    cgall = Cgroup(CGNAME, Version.CGROUP_V1)
+    cgall.add_all_controllers()
+    cgall.get()
+
+    controllers = list()
+    with open('/proc/cgroups') as pc:
+        for i, line in enumerate(pc.readlines()):
+            if i == 0:
+                continue
+            if int(line.split()[1]) > 0:
+                # If the hierarchy is greater than zero, then use the controller
+                controllers.append(line.split()[0])
+
+    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]))
+
+    for controller in CONTROLLERS:
+        if len(str(cgall.controllers[controller])) <= 1:
+            result = consts.TEST_FAILED
+            tmp_cause = 'Controller {} was not populated'.format(controller)
+            cause = '\n'.join(filter(None, [cause, tmp_cause]))
+
+    return result, cause
+
+
+def teardown(config):
+    CgroupCli.delete(config, CONTROLLERS, CGNAME)
+
+
+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 470247bd4a1de802ffbb6316a4f06f1c14c426b5..1a1d1d5b92f59991c2690cf54b5a3507d582e7c5 100644 (file)
@@ -94,6 +94,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          071-sudo-set_default_systemd_cgroup.py \
                          072-pybindings-cgroup_get_cgroup.py \
                          073-pybindings-cgroup_add_all_controllers-v2.py \
+                         074-pybindings-cgroup_add_all_controllers-v1.py \
                          998-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py