]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a test for cgroup_change_cgroup_path()
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 17 May 2023 20:52:01 +0000 (14:52 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 23 May 2023 21:43:11 +0000 (15:43 -0600)
Add a functional test that exercises cgroup_change_cgroup_path()
via the python bindings.

-----------------------------------------------------------------
Test Results:
Run Date:                          May 17 21:20:19
Passed:                                  1 test(s)
Skipped:                                 0 test(s)
Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
Test                           Time (sec)
-----------------------------------------
setup                                0.00
087-sudo-move_pid.py                 2.06
teardown                             0.00
-----------------------------------------
Total Run Time

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

diff --git a/tests/ftests/087-sudo-move_pid.py b/tests/ftests/087-sudo-move_pid.py
new file mode 100755 (executable)
index 0000000..f5eee7d
--- /dev/null
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: LGPL-2.1-only
+#
+# Test to move a pid to a cgroup
+#
+# Copyright (c) 2023 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+from libcgroup import Cgroup, Mode, Version
+from process import Process
+import consts
+import ftests
+import sys
+import os
+
+CGNAME = '087movepid'
+
+
+def prereqs(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    if Cgroup.cgroup_mode() != Mode.CGROUP_MODE_UNIFIED:
+        result = consts.TEST_SKIPPED
+        cause = 'This test requires the unified cgroup hierarchy'
+
+    return result, cause
+
+
+def setup(config):
+    result = consts.TEST_PASSED
+    cause = None
+
+    cg = Cgroup(CGNAME, Version.CGROUP_V2)
+    cg.create()
+
+    pid = config.process.create_process(config)
+
+    path = Cgroup.get_current_controller_path(pid)
+    if path == '/' + CGNAME:
+        result = consts.TEST_FAILED
+        cause = 'The PID was already in the destination cgroup {}'.format(CGNAME)
+
+    return result, cause, pid
+
+
+def test(config, pid):
+    result = consts.TEST_PASSED
+    cause = None
+
+    Cgroup.move_process(pid, CGNAME)
+
+    path = Cgroup.get_current_controller_path(pid)
+    if path != '/' + CGNAME:
+        result = consts.TEST_FAILED
+        cause = 'Expected the pid to be in {} cgroup, but was instead in {}'.format(CGNAME, path)
+
+    return result, cause
+
+
+def teardown(config, pid):
+    Process.kill(config, pid)
+
+    cg = Cgroup(CGNAME, Version.CGROUP_V2)
+    cg.delete()
+
+
+def main(config):
+    [result, cause] = prereqs(config)
+    if result != consts.TEST_PASSED:
+        return [result, cause]
+
+    [result, cause, pid] = setup(config)
+    if result != consts.TEST_PASSED:
+        teardown(config, pid)
+        return [result, cause]
+
+    [result, cause] = test(config, pid)
+    teardown(config, pid)
+
+    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 5ab6e38656df503bdb8ce3b874841f8daa5c1a28..a769f6f6a3e4d9ca35ba3e9be7f25cf40024af5f 100644 (file)
@@ -106,6 +106,7 @@ EXTRA_DIST_PYTHON_TESTS = \
                          083-pybindings-helpers_cgroup_mode.py \
                          084-sudo-cgcreate_systemd_scope_pid.py \
                          086-sudo-systemd_cmdline_example.py \
+                         087-sudo-move_pid.py \
                          998-cgdelete-non-existing-shared-mnt-cgroup-v1.py
 # Intentionally omit the stress test from the extra dist
 # 999-stress-cgroup_init.py