From c721db5df8706983b927c77875f9aa1f06a44455 Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Sat, 4 Mar 2023 03:14:47 +0000 Subject: [PATCH] ftests: Add a test for cgroup_add_all_controllers() 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 Reviewed-by: Kamalesh Babulal (cherry picked from commit eca89d792f57b21dda820693a9b9b3fb8d253400) --- ...ybindings-cgroup_add_all_controllers-v1.py | 90 +++++++++++++++++++ tests/ftests/Makefile.am | 1 + 2 files changed, 91 insertions(+) create mode 100755 tests/ftests/074-pybindings-cgroup_add_all_controllers-v1.py 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 index 00000000..1207bb29 --- /dev/null +++ b/tests/ftests/074-pybindings-cgroup_add_all_controllers-v1.py @@ -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 +# + +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: diff --git a/tests/ftests/Makefile.am b/tests/ftests/Makefile.am index 470247bd..1a1d1d5b 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -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 -- 2.47.2