|
| 1 | +"""Tests for the utility class SentryMonitoredBroker.""" |
| 2 | + |
| 3 | +import time |
| 4 | +from uuid import uuid4 |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from insights.core.dr import MissingRequirements |
| 9 | +from insights.core.spec_factory import ContentException |
| 10 | + |
| 11 | + |
| 12 | +from ccx_messaging.monitored_broker import SentryMonitoredBroker |
| 13 | + |
| 14 | + |
| 15 | +def test_usage(): |
| 16 | + """Check that normal usage for the broker is kept across inheritance.""" |
| 17 | + broker = SentryMonitoredBroker() |
| 18 | + cluster_id = uuid4() |
| 19 | + timestamp = time.time() |
| 20 | + |
| 21 | + broker["cluster_id"] = cluster_id |
| 22 | + broker["timestamp"] = timestamp |
| 23 | + |
| 24 | + assert broker["cluster_id"] == cluster_id |
| 25 | + assert broker["timestamp"] == timestamp |
| 26 | + |
| 27 | + |
| 28 | +@patch("ccx_messaging.monitored_broker.capture_exception") |
| 29 | +def test_add_exception(sentry_capture_exception_mock): |
| 30 | + """Check add_exception behavior.""" |
| 31 | + broker = SentryMonitoredBroker() |
| 32 | + |
| 33 | + broker.add_exception(None, KeyboardInterrupt, None) |
| 34 | + assert sentry_capture_exception_mock.called |
| 35 | + |
| 36 | + |
| 37 | +NO_CAPTURE_EXCEPTIONS = [MissingRequirements, ContentException] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("exception", NO_CAPTURE_EXCEPTIONS) |
| 41 | +def test_add_exception_no_capture(exception): |
| 42 | + """Check that selected exceptions are not captured by Sentry.""" |
| 43 | + broker = SentryMonitoredBroker() |
| 44 | + |
| 45 | + with patch("ccx_messaging.monitored_broker.capture_exception") as capture_exception_mock: |
| 46 | + # Adding an argument to constructor because it is required by some of the exceptions |
| 47 | + broker.add_exception(None, exception("some message"), None) |
| 48 | + assert not capture_exception_mock.called |
0 commit comments