]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test for cgroup_get_cgroup()
authorTom Hromatka <tom.hromatka@oracle.com>
Tue, 20 Dec 2022 22:41:22 +0000 (22:41 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 6 Jan 2023 15:28:48 +0000 (08:28 -0700)
Add a functional test to exercise cgroup_get_cgroup()

-----------------------------------------------------------------
Test Results:
        Run Date:                          Dec 20 22:41:28
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                              Time (sec)
        --------------------------------------------
        setup                                   0.00
        051-sudo-cgroup_get_cgroup.py           0.03
        teardown                                0.00
        --------------------------------------------
        Total Run Time                          0.03

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

diff --git a/tests/ftests/051-sudo-cgroup_get_cgroup.py b/tests/ftests/051-sudo-cgroup_get_cgroup.py
new file mode 100755 (executable)
index 0000000..9814eec
--- /dev/null
@@ -0,0 +1,94 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Exercise cgroup_create_cgroup() and cgroup_get_cgroup()
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+from libcgroup import Cgroup, Version
+from cgroup import CgroupVersion
+import ftests
+import consts
+import sys
+import os
+
+CGNAME = '051cgnewcg'
+
+# Which controller isn't all that important, but it is important that we
+# have a cgroup v2 controller
+CONTROLLER = 'cpu'
+
+
+def prereqs(config):
+    if config.args.container:
+        result = consts.TEST_SKIPPED
+        cause = 'This test cannot be run within a container'
+        return result, cause
+
+    if CgroupVersion.get_version(CONTROLLER) != CgroupVersion.CGROUP_V2:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires cgroup v2'
+        return result, cause
+
+    result = consts.TEST_PASSED
+    cause = None
+
+    return result, cause
+
+
+def setup(config):
+    cg = Cgroup(CGNAME, Version.CGROUP_V2)
+    cg.add_controller(CONTROLLER)
+    cg.create()
+
+
+def test(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    cg = Cgroup(CGNAME, Version.CGROUP_V2)
+    cg.get()
+
+    if len(cg.controllers) != 1:
+        # only one controller, cpu, should be enabled
+        result = consts.TEST_FAILED
+        cause = 'Expected one controller to be enabled, but {} were enabled'.format(
+                len(cg.controllers))
+
+    return result, cause
+
+
+def teardown(config, result):
+    cg = Cgroup(CGNAME, Version.CGROUP_V2)
+    try:
+        cg.delete()
+    except RuntimeError:
+        pass
+
+    return consts.TEST_PASSED, None
+
+
+def main(config):
+    [result, cause] = prereqs(config)
+    if result != consts.TEST_PASSED:
+        return [result, cause]
+
+    try:
+        result = consts.TEST_FAILED
+        setup(config)
+        [result, cause] = test(config)
+    finally:
+        teardown(config, result)
+
+    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 dc23dbba93c08020ebed5e016fe541a9c556cff7..00eb261057c948fb7d3e39bafb818826f9a4c03b 100644 (file)
@@ -70,7 +70,8 @@ EXTRA_DIST_PYTHON_TESTS = \
                          047-cgcreate-delete_cgrp_shared_mnt.py \
                          048-pybindings-get_cgroup_mode.py \
                          049-sudo-systemd_create_scope.py \
-                         050-sudo-systemd_create_scope2.py
+                         050-sudo-systemd_create_scope2.py \
+                         051-sudo-cgroup_get_cgroup.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py