]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add test for cgroup_get_procs()
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 17 Mar 2023 15:25:49 +0000 (09:25 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 13 Apr 2023 13:51:41 +0000 (07:51 -0600)
Add a functional test for cgroup_get_procs()

-----------------------------------------------------------------
Test Results:
Run Date:                          Mar 17 16:11:34
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                                   Time (sec)
-------------------------------------------------
setup                                        0.00
077-pybindings-cgroup_get_procs.py          40.31
teardown                                     0.00
-------------------------------------------------
Total Run Time                              40.31

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

tests/ftests/077-pybindings-cgroup_get_procs.py [new file with mode: 0755]
tests/ftests/Makefile.am

diff --git a/tests/ftests/077-pybindings-cgroup_get_procs.py b/tests/ftests/077-pybindings-cgroup_get_procs.py
new file mode 100755 (executable)
index 0000000..64f3935
--- /dev/null
@@ -0,0 +1,111 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# cgroup_get_procs() test 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
+from libcgroup import Cgroup, Version
+from process import Process
+import consts
+import ftests
+import sys
+import os
+
+
+CGNAME = '077getprocs/cgwithpids'
+EMPTY_CGNAME = '077getprocs/cgwithoutpids'
+CONTROLLERS = ['cpu', 'pids']
+PID_CNT = 20
+
+initial_pid_list = list()
+
+
+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
+
+
+def setup(config):
+    global initial_pid_list
+    CgroupCli.create(config, CONTROLLERS, CGNAME)
+    CgroupCli.create(config, CONTROLLERS, EMPTY_CGNAME)
+
+    for i in range(0, PID_CNT):
+        pid = config.process.create_process(config)
+        initial_pid_list.append(pid)
+
+    CgroupCli.classify(config, CONTROLLERS, CGNAME, initial_pid_list, ignore_systemd=True)
+    initial_pid_list = initial_pid_list.sort()
+
+
+def test(config):
+    global initial_pid_list
+    result = consts.TEST_PASSED
+    cause = None
+
+    #
+    # Test 1 - verify pids are properly populated and retrieved from a cgroup
+    #
+    cg = Cgroup(CGNAME, Version.CGROUP_V2)
+    for controller in CONTROLLERS:
+        cg.add_controller(controller)
+    pid_list = cg.get_procs().sort()
+
+    if pid_list != initial_pid_list:
+        result = consts.TEST_FAILED
+        tmp_cause = 'The pid lists do not match\n{}\n{}'.format(initial_pid_list, pid_list)
+        cause = '\n'.join(filter(None, [cause, tmp_cause]))
+
+    #
+    # Test 2 - verify there are no pids in the empty cgroup
+    #
+    emptycg = Cgroup(EMPTY_CGNAME, Version.CGROUP_V2)
+    for controller in CONTROLLERS:
+        emptycg.add_controller(controller)
+    empty_pid_list = emptycg.get_procs()
+
+    if len(empty_pid_list) != 0:
+        result = consts.TEST_FAILED
+        tmp_cause = 'The pid list unexpectedly was populated\n{}'.format(empty_pid_list)
+        cause = '\n'.join(filter(None, [cause, tmp_cause]))
+
+    return result, cause
+
+
+def teardown(config):
+    global initial_pid_list
+
+    Process.kill(config, initial_pid_list)
+    CgroupCli.delete(config, CONTROLLERS, EMPTY_CGNAME)
+    CgroupCli.delete(config, CONTROLLERS, os.path.dirname(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 5ed61db88eea525fd6b0db961702bf90ea908d77..78e14ec2857c06a7f4a0e42aaab744c5a3316a3c 100644 (file)
@@ -97,6 +97,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          074-pybindings-cgroup_add_all_controllers-v1.py \
                          075-pybindings-cgroup_compare_cgroup.py \
                          076-cgconfig-auto_convert.py \
+                         077-pybindings-cgroup_get_procs.py \
                          998-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py