From: Tom Hromatka Date: Fri, 27 Jan 2023 20:48:31 +0000 (-0700) Subject: ftests: Add a test to exercise invalid systemd parameters X-Git-Tag: v3.1.0~227 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8012757a76d1d9f179ca33fa156397c0ea53e72b;p=thirdparty%2Flibcgroup.git ftests: Add a test to exercise invalid systemd parameters Add a test that exercises invalid parameters to cgroup_create_scope2(). ----------------------------------------------------------------- Test Results: Run Date: Jan 25 09:32:58 Passed: 1 test(s) Skipped: 0 test(s) Failed: 0 test(s) ----------------------------------------------------------------- Timing Results: Test Time (sec) -------------------------------------------------------- setup 0.00 059-sudo-invalid_systemd_create_scope2.py 0.00 teardown 0.00 -------------------------------------------------------- Total Run Time 0.00 Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/tests/ftests/059-sudo-invalid_systemd_create_scope2.py b/tests/ftests/059-sudo-invalid_systemd_create_scope2.py new file mode 100755 index 00000000..228ff351 --- /dev/null +++ b/tests/ftests/059-sudo-invalid_systemd_create_scope2.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-only +# +# Test invalid parameters for systemd_create_scope2() +# +# Copyright (c) 2023 Oracle and/or its affiliates. +# Author: Tom Hromatka +# + +from cgroup import CgroupVersion as CgroupCliVersion +from libcgroup import Cgroup, Version +import ftests +import consts +import utils +import sys +import os + +# 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 CgroupCliVersion.get_version(CONTROLLER) != CgroupCliVersion.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): + pass + + +def test(config): + result = consts.TEST_PASSED + cause = None + + cg1 = Cgroup("InvalidNameBecauseNoSlash", Version.CGROUP_V2) + cg1.add_controller(CONTROLLER) + try: + cg1.create_scope2() + except RuntimeError as re: + if '50011' not in str(re): + result = consts.TEST_FAILED + cause = 'Expected ECGINVAL (50011) but received {}'.format(re) + else: + result = consts.TEST_FAILED + cause = 'An invalid cgroup name unexpectedly passed: {}'.format(cg1.name) + + + cg2 = Cgroup("Invalid/TooMany/Slashes", Version.CGROUP_V2) + cg2.add_controller(CONTROLLER) + try: + cg2.create_scope2() + except RuntimeError as re: + if '50011' not in str(re): + result = consts.TEST_FAILED + tmp_cause = 'Expected ECGINVAL (50011) but received {}'.format(re) + if not cause: + cause = tmp_cause + else: + cause = '{}\n{}'.format(cause, tmp_cause) + else: + result = consts.TEST_FAILED + cause = 'An invalid cgroup name unexpectedly passed: {}'.format(cg1.name) + if not cause: + cause = tmp_cause + else: + cause = '{}\n{}'.format(cause, tmp_cause) + + return result, cause + + +def teardown(config, result): + pass + + +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: diff --git a/tests/ftests/Makefile.am b/tests/ftests/Makefile.am index 7fd05f15..282e0ff5 100644 --- a/tests/ftests/Makefile.am +++ b/tests/ftests/Makefile.am @@ -79,6 +79,7 @@ EXTRA_DIST_PYTHON_TESTS = \ 056-sudo-set_permissions_v2.py \ 057-sudo-set_permissions_v1.py \ 058-sudo-systemd_create_scope2.py \ + 059-sudo-invalid_systemd_create_scope2.py \ 098-cgdelete-non-existing-shared-mnt-cgroup-v1.py # Intentionally omit the stress test from the extra dist # 999-stress-cgroup_init.py