From 3d779b492d0a78361e3566ac08781ff4a62ad40e Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Dec 17 2019 08:20:43 +0000 Subject: ipatests: assert_error: allow regexp match Enhance the assert_error subroutine to provide regular expression matching against the command's stderr output, in additional to substring match. Part of: https://pagure.io/freeipa/issue/8142 Reviewed-By: Florence Blanc-Renaud --- diff --git a/ipatests/pytest_ipa/integration/tasks.py b/ipatests/pytest_ipa/integration/tasks.py index b9fcc05..32ddb05 100644 --- a/ipatests/pytest_ipa/integration/tasks.py +++ b/ipatests/pytest_ipa/integration/tasks.py @@ -1604,9 +1604,19 @@ def upload_temp_contents(host, contents, encoding='utf-8'): return tmpname -def assert_error(result, stderr_text, returncode=None): - "Assert that `result` command failed and its stderr contains `stderr_text`" - assert stderr_text in result.stderr_text, result.stderr_text +def assert_error(result, pattern, returncode=None): + """ + Assert that ``result`` command failed and its stderr contains ``pattern``. + ``pattern`` may be a ``str`` or a ``re.Pattern`` (regular expression). + + """ + if isinstance(pattern, re.Pattern): + assert pattern.search(result.stderr_text), \ + f"pattern {pattern} not found in stderr {result.stderr_text!r}" + else: + assert pattern in result.stderr_text, \ + f"substring {pattern!r} not found in stderr {result.stderr_text!r}" + if returncode is not None: assert result.returncode == returncode else: