From: Douglas Bagnall Date: Wed, 4 Jun 2025 01:53:32 +0000 (+1200) Subject: tests/samba-tool: optionally allow exception to be a failure in .run*() X-Git-Tag: tevent-0.17.0~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b5b08780f6d16f1c771bb54e85bc94f94d57aeed;p=thirdparty%2Fsamba.git tests/samba-tool: optionally allow exception to be a failure in .run*() BUG: https://bugzilla.samba.org/show_bug.cgi?id=13613 Signed-off-by: Douglas Bagnall Reviewed-by: Jennifer Sutton Reviewed-by: Rowland Penny --- diff --git a/python/samba/tests/samba_tool/base.py b/python/samba/tests/samba_tool/base.py index 689ab11dde2..0fe450386c0 100644 --- a/python/samba/tests/samba_tool/base.py +++ b/python/samba/tests/samba_tool/base.py @@ -63,21 +63,34 @@ class SambaToolCmdTest(samba.tests.BlackboxTestCase): credentials=creds, lp=lp) @classmethod - def _run(cls, *argv, verbose=False): + def _run(cls, *argv, verbose=False, catch_error=False): """run a samba-tool command. positional arguments are effectively what gets passed to bin/samba-tool. + Add catch_error=True to make samba-tool exceptions into + failures. + Add verbose=True during development to see the expanded command and results. + """ - cmd, args = cmd_sambatool()._resolve('samba-tool', *argv, - outf=cls.stringIO(), - errf=cls.stringIO()) - result = cmd._run(*args) - out = cmd.outf.getvalue() - err = cmd.errf.getvalue() + try: + cmd, args = cmd_sambatool()._resolve('samba-tool', *argv, + outf=cls.stringIO(), + errf=cls.stringIO()) + result = cmd._run(*args) + out = cmd.outf.getvalue() + err = cmd.errf.getvalue() + except (Exception, SystemExit) as e: + # We need to catch SystemExit as well as Exception, + # because samba-tool will often convert exceptions into + # exits (SystemExit is a subclass of BaseException but not + # Exception). + if catch_error: + raise AssertionError(f"'samba-tool {' '.join(argv)}' raised {e}") + raise if verbose: print(f"bin/samba-tool {' '.join(argv)}\n\nstdout:\n"