Skip to content

Commit

Permalink
fix log capture for stdout/stderr tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Jun 13, 2022
1 parent 5583e71 commit 511a532
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ addopts =
weaver/
log_cli = false
log_level = DEBUG
log_format = [%%(asctime)s] %%(levelname)-8.8s [%%(threadName)s][%%(name)s] %%(message)s
log_date_format = %%Y-%%m-%%d %%H:%%M:%%S
# FIXME: format specifiers incorrectly escaped (https://github.com/pytest-dev/pytest/issues/10019)
# fails our tests if provided since name and message are required to validate functionalities
#log_format = [%%(asctime)s] %%(levelname)-8.8s [%%(threadName)s][%%(name)s] %%(message)s
#log_date_format = %%Y-%%m-%%d %%H:%%M:%%S
python_files = test_*.py
markers =
cli: mark test as related to CLI operations
Expand Down
20 changes: 13 additions & 7 deletions tests/processes/test_wps_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class MockWpsPackage(WpsPackage):
"""
Mock of WPS package definition that ignores real status location updates and returns the mock for test validation.
"""
mock_status_location = None
def __init__(self, *_, **__):
super(MockWpsPackage, self).__init__(*_, **__)
self.mock_status_location = None

@property
def status_location(self):
Expand Down Expand Up @@ -186,7 +188,7 @@ def test_stdout_stderr_logging_for_commandline_tool_success():
assert f"{log_cwltool} completed success" in log_data


def test_stdout_stderr_logging_for_commandline_tool_failure():
def test_stdout_stderr_logging_for_commandline_tool_failure(caplog):
"""
Execute a process and assert that stderr is correctly logged to log file upon failing process execution.
"""
Expand All @@ -211,18 +213,20 @@ def test_stdout_stderr_logging_for_commandline_tool_failure():
except PackageExecutionError as exception:
assert "Failed package execution" in exception.args[0]
assert "Missing required input parameter 'message'" in exception.args[0]
log_err = stderr.getvalue()
# depending on debug/command-line & pytest test order, the captured logs by be 'hijacked' or not
# use whichever one is active (stderr empty if captured by pytest)
log_err = stderr.getvalue() or caplog.text
assert "Could not retrieve any internal application log." not in log_err, (
"Since tool did not reach execution, not captured logged is expected."
)
assert "Traceback (most recent call last):" in log_err
assert "[weaver.processes.wps_package|mock-process]" in log_err
assert "weaver.processes.wps_package|mock-process" in log_err
assert "Missing required input parameter 'message'" in log_err
else:
pytest.fail("\"wps_package._handler()\" was expected to throw \"PackageExecutionError\" exception")


def test_stdout_stderr_logging_for_commandline_tool_exception():
def test_stdout_stderr_logging_for_commandline_tool_exception(caplog):
"""
Execute a process and assert that traceback is correctly logged to log file upon failing process execution.
"""
Expand All @@ -246,11 +250,13 @@ def test_stdout_stderr_logging_for_commandline_tool_exception():
wps_package_instance._handler(wps_request, wps_response)
except PackageExecutionError as exception:
assert "Completed permanentFail" in exception.args[0]
log_err = stderr.getvalue()
# depending on debug/command-line & pytest test order, the captured logs by be 'hijacked' or not
# use whichever one is active (stderr empty if captured by pytest)
log_err = stderr.getvalue() or caplog.text
assert "Could not retrieve any internal application log." in log_err, (
"Since command did not run, nothing captured is expected"
)
assert "Traceback (most recent call last):" in log_err
assert "[weaver.processes.wps_package|mock-process]" in log_err
assert "weaver.processes.wps_package|mock-process" in log_err
else:
pytest.fail("\"wps_package._handler()\" was expected to throw \"PackageExecutionError\" exception")
2 changes: 2 additions & 0 deletions weaver/typedefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@
"inputs": CWL_Inputs,
"outputs": CWL_Outputs,
"steps": Dict[CWL_WorkflowStepID, CWL_WorkflowStep],
"stderr": str,
"stdout": str,
"$namespaces": Dict[str, str],
"$schemas": Dict[str, str]
}, total=False)
Expand Down

0 comments on commit 511a532

Please sign in to comment.