Skip to content
Snippets Groups Projects
Commit 02ba890e authored by Thomas Woerner's avatar Thomas Woerner
Browse files

tests/utils.py: Shorten run_playbook for smaller traceback with assert

Most of the content has been moved to the new function _run_playbook to
reduce the traceback output in the case of a test failure.
parent 8515c9a4
Branches
Tags
No related merge requests found
......@@ -148,30 +148,6 @@ def write_logs(result, test_name):
log_file.write(result.stderr.decode("utf-8"))
def _run_playbook(playbook):
"""
Create a inventory using a temporary file and run ansible using it.
The logs of the run will be placed in `tests/logs/`.
"""
with tempfile.NamedTemporaryFile() as inventory_file:
inventory_file.write(get_inventory_content())
inventory_file.flush()
cmd_options = ["-i", inventory_file.name]
verbose = os.environ.get("IPA_VERBOSITY", None)
if verbose is not None:
cmd_options.append(verbose)
cmd = ["ansible-playbook"] + cmd_options + [playbook]
# pylint: disable=subprocess-run-check
process = subprocess.run(
cmd, cwd=SCRIPT_DIR, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
test_name = get_test_name_from_playbook_path(playbook)
write_logs(process, test_name)
return process
def _truncate(lines, charcount, minlines=0):
output = ""
line_count = 1
......@@ -188,12 +164,11 @@ def _truncate(lines, charcount, minlines=0):
return output
def run_playbook(playbook, allow_failures=False):
def _run_playbook(playbook):
"""
Run an Ansible playbook and assert the return code.
Create a inventory using a temporary file and run ansible using it.
Call ansible (using _run_playbook function) and assert the result of
the execution.
The logs of the run will be placed in `tests/logs/`.
In case of failure the tail of the error message will be displayed
as an assertion message.
......@@ -201,16 +176,29 @@ def run_playbook(playbook, allow_failures=False):
The full log of the execution will be available in the directory
`tests/logs/`.
"""
result = _run_playbook(playbook)
if allow_failures:
return result
with tempfile.NamedTemporaryFile() as inventory_file:
inventory_file.write(get_inventory_content())
inventory_file.flush()
cmd_options = ["-i", inventory_file.name]
verbose = os.environ.get("IPA_VERBOSITY", None)
if verbose is not None:
cmd_options.append(verbose)
cmd = ["ansible-playbook"] + cmd_options + [playbook]
process = subprocess.run(
cmd, cwd=SCRIPT_DIR, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False
)
test_name = get_test_name_from_playbook_path(playbook)
write_logs(process, test_name)
msg = ""
if process.returncode != 0:
status_code_msg = "ansible-playbook return code: {0}".format(
result.returncode
process.returncode
)
_stdout = result.stdout.decode("utf8")
_stderr = result.stderr.decode("utf8")
_stdout = process.stdout.decode("utf8")
_stderr = process.stderr.decode("utf8")
# Truncate stdout and stderr in the way that it hopefully
# shows all important information. At least 15 lines of stdout
# (Ansible tasks) and remaining from stderr to fill up to
......@@ -222,22 +210,31 @@ def run_playbook(playbook, allow_failures=False):
minlines=15)
stderr = _truncate(_stderr.splitlines(), maxlen - len(stdout))
assert_msg = "\n".join(
msg = "\n".join(
[
"",
"-" * 30 + " Captured stdout " + "-" * 30,
stdout,
"-" * 30 + " Captured stderr " + "-" * 30,
stderr,
"-" * 30 + " Playbook Return Code " + "-" * 30,
status_code_msg,
stderr
]
)
msg += "-" * 30 + " Playbook Return Code " + "-" * 30 + "\n"
msg += status_code_msg
return process, msg
# Need to get the last bytes of msg otherwise Azure
# will cut it out.
assert result.returncode == 0, assert_msg[-2500:]
def run_playbook(playbook, allow_failures=False):
"""
Run an Ansible playbook and assert the return code.
Call ansible (using _run_playbook function) and assert the result of
the execution.
"""
result, assert_msg = _run_playbook(playbook)
if not allow_failures:
assert result.returncode == 0, assert_msg
return result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment