Skip to content

Commit

Permalink
CA-390129: Add test for the exception handler xen-bugtool.log_excepti…
Browse files Browse the repository at this point in the history
…ons()

Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
  • Loading branch information
bernhardkaindl committed Mar 22, 2024
1 parent b468eb5 commit 2cbaed3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ repos:
files: '(^xen-bugtool|\.py)$'


# If you are working on a PR and the branch shall not be up-to-date with the base
# branch, export SKIP=check-branch-needs-rebase or use these commands to skip the check:
# SKIP=check-branch-needs-rebase time pre-commit run --hook-stage push --all-files -v
# SKIP=check-branch-needs-rebase git commit -s -m "your message"
# or to skip all checks because you ran them before: git push --no-verify
- repo: local
hooks:
- id: check-branch-needs-rebase
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_dump_xapi_rrds.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,37 @@ def test_dump_xapi_rrds_master(mocker, isolated_bugtool, mock_session1, mock_url

run_dump_xapi_rrds(mocker, isolated_bugtool, mock_session1, mock_urlopen)
assert_mock_session1(isolated_bugtool, mock_urlopen)


def test_log_exceptions(isolated_bugtool, capsys):
"""Test log_exceptions() to log the exception message and return the exception type."""

try:
with isolated_bugtool.log_exceptions():
raise IOError("message")
except BaseException: # pragma: no cover # NOSONAR
assert False, "log_exceptions() is expected to catch all Exceptions"
finally:
with capsys.disabled():
# Assert that the exception message is logged to stdout
assert_log_contents(capsys.readouterr().out.splitlines())

# Assert that the exception message is logged to the log file
with open(isolated_bugtool.XEN_BUGTOOL_LOG, "r") as f:
log = f.read().splitlines()
assert_log_contents(log)

# Clear the log file to indicate that we checked the log file contents
with open(isolated_bugtool.XEN_BUGTOOL_LOG, "w") as f:
f.write("")


def assert_log_contents(log):
"""Assert the contents of the log"""

# Check the log file contents
assert log[0] == "message, Traceback (most recent call last):"
assert ", in log_exceptions" in log[1]
assert log[2] == " yield"
assert ", in test_log_exceptions" in log[3]
assert log[4] == ' raise IOError("message")'

0 comments on commit 2cbaed3

Please sign in to comment.