From e167c484720227bf00a037e9409bc2f2e24dd35e Mon Sep 17 00:00:00 2001 From: Kamalesh Babulal Date: Thu, 16 Feb 2023 13:36:48 +0000 Subject: [PATCH] tests: Add a test for the python bindings to cgroup_set_default_systemd_cgroup Add a test for the python bindings to cgroup_set_default_systemd_cgroup() ----------------------------------------------------------------- Test Results: Run Date: Feb 16 13:36:28 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) ----------------------------------------------------- setup 0.00 071-sudo-set_default_systemd_cgroup.py 0.02 teardown 0.00 ----------------------------------------------------- Total Run Time 0.02 Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka TJH: Remove unnecessary int() typecast and add the test to Makefile.am (cherry picked from commit 0b4016adcb47ee4dd5f478389281e84386527d89) --- .../071-sudo-set_default_systemd_cgroup.py | 180 ++++++++++++++++++ tests/ftests/Makefile.am | 1 + 2 files changed, 181 insertions(+) create mode 100755 tests/ftests/071-sudo-set_default_systemd_cgroup.py diff --git a/tests/ftests/071-sudo-set_default_systemd_cgroup.py b/tests/ftests/071-sudo-set_default_systemd_cgroup.py new file mode 100755 index 00000000..4d67d9ba --- /dev/null +++ b/tests/ftests/071-sudo-set_default_systemd_cgroup.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# set_default_systemd_cgroup functionality test using the python bindings +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Kamalesh Babulal +# + +from cgroup import Cgroup as CgroupCli, Mode +from libcgroup import Version, Cgroup +import consts +import ftests +import sys +import os + + +CONTROLLER = 'cpu' +SYSTEMD_CGNAME = '071_cg_in_scope' +OTHER_CGNAME = '071_cg_not_in_scope' + +SLICE = 'libcgtests.slice' +SCOPE = 'test071.scope' + +CONFIG_FILE_NAME = os.path.join(os.getcwd(), '071cgconfig.conf') +SYSTEMD_DEFAULT_CGROUP_DIR = '/var/run/libcgroup' +SYSTEMD_DEFAULT_CGROUP_FILE = '/var/run/libcgroup/systemd' + +# List of libcgroup.Cgroup objects +CGRPS_LIST = list() +MODE = Mode.CGROUP_MODE_UNK + + +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 create_cgrp(config, CGNAME, controller=CONTROLLER, ignore_systemd=False): + global CGRPS_LIST, MODE + + result = consts.TEST_PASSED + cause = None + + cgrp = Cgroup(CGNAME, Version.CGROUP_V1) + if controller is not None: + cgrp.add_controller(controller) + cgrp.create() + + if not CgroupCli.exists(config, controller, CGNAME, ignore_systemd=ignore_systemd): + result = consts.TEST_FAILED + if MODE == Mode.CGROUP_MODE_UNIFIED: + cause = 'Failed to create {}'.format(os.path.join('/sys/fs/cgroup', CGNAME)) + else: + cause = ( + 'Failed to create {}' + ''.format(os.path.join('/sys/fs/cgroup', (controller or ''), CGNAME)) + ) + return result, cause + + CGRPS_LIST.append(cgrp) + + return result, cause + + +def setup(config): + global MODE + + result = consts.TEST_PASSED + cause = None + + # probe the current cgroup set up mode + MODE = Cgroup.cgroup_mode() + + if not os.path.isdir(SYSTEMD_DEFAULT_CGROUP_DIR): + os.mkdir(SYSTEMD_DEFAULT_CGROUP_DIR) + + # Emulate the systemd slice/scope creation + f = open(SYSTEMD_DEFAULT_CGROUP_FILE, 'w') + f.write(os.path.join(SLICE, SCOPE)) + f.close() + + if not os.path.exists(SYSTEMD_DEFAULT_CGROUP_FILE): + result = consts.TEST_FAILED + cause = 'Failed to create %s' % SYSTEMD_DEFAULT_CGROUP_FILE + return result, cause + + # create /sys/fs/cgroup/cpu/libcgtests.slice (v1) or /sys/fs/cgroup/libcgtests.slice (v2) + result, cause = create_cgrp(config, SLICE, ignore_systemd=True) + if result == consts.TEST_FAILED: + return result, cause + + # create /sys/fs/cgroup/cpu/libcgtests.slice/test071.scope (v1) or + # /sys/fs/cgroup/libcgtests.slice/tests071.scope (v2) + result, cause = create_cgrp(config, os.path.join(SLICE, SCOPE), ignore_systemd=True) + if result == consts.TEST_FAILED: + return result, cause + + # In hybrid mode /sys/fs/cgroup/unified/libcgtests.slice/test071.scope + # is created by the systemd and we relay on it for checking if the + # slice and scope were set as default systemd cgroup (new cgroup root) + if MODE == Mode.CGROUP_MODE_HYBRID: + result, cause = create_cgrp(config, SLICE, None, ignore_systemd=True) + if result == consts.TEST_FAILED: + return result, cause + + result, cause = create_cgrp(config, os.path.join(SLICE, SCOPE), None, ignore_systemd=True) + + return result, cause + + +def test(config): + result = consts.TEST_PASSED + cause = None + + # Create cgroup before setting the default systemd cgroup + result, cause = create_cgrp(config, OTHER_CGNAME, ignore_systemd=True) + if result == consts.TEST_FAILED: + return result, cause + + Cgroup.cgroup_set_default_systemd_cgroup() + + # Create cgroup after setting the default systemd cgroup + result, cause = create_cgrp(config, SYSTEMD_CGNAME) + if result == consts.TEST_FAILED: + return result, cause + + return result, cause + + +def teardown(config): + global CGRPS_LIST + + # the last object in the list is created with the default + # systemd cgroup set for others we need unset it + cgroup = CGRPS_LIST.pop() + cgroup.delete() + + # unset the default systemd cgroup by deleting the + # /var/run/libcgroup/systemd and calling + # cgroup_set_default_systemd_cgroup() + if os.path.exists(SYSTEMD_DEFAULT_CGROUP_FILE): + os.unlink(SYSTEMD_DEFAULT_CGROUP_FILE) + + Cgroup.cgroup_set_default_systemd_cgroup() + + for cgroup in reversed(CGRPS_LIST): + cgroup.delete() + + +def main(config): + [result, cause] = prereqs(config) + if result != consts.TEST_PASSED: + return [result, cause] + + [result, cause] = setup(config) + if result != consts.TEST_PASSED: + return [result, cause] + + try: + [result, cause] = test(config) + finally: + 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 d10c7c99..1f83cd31 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -91,6 +91,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 068-sudo-systemd_cgexec-v2.py \ 069-sudo-systemd_cgxget-cpu-settings-v1.py \ 070-sudo-systemd_cgxget-cpu-settings-v2.py \ + 071-sudo-set_default_systemd_cgroup.py \ 098-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