|
| 1 | +"""Test fixtures copied from https://github.com/getsentry/sentry-python/ |
| 2 | +TODO: move integration to sentry_sdk |
| 3 | +""" |
| 4 | +import importlib.metadata |
| 5 | + |
| 6 | +import pytest |
| 7 | +import sentry_sdk |
| 8 | +from sentry_sdk import Transport |
| 9 | + |
| 10 | +from sentry_sdk.utils import capture_internal_exceptions |
| 11 | + |
| 12 | + |
| 13 | +sentry_sdk_version = importlib.metadata.version('sentry_sdk') |
| 14 | +if not sentry_sdk_version.startswith('1.'): |
| 15 | + pytest.skip(f"Testset is only for sentry_sdk 1.x, given {sentry_sdk_version=}", allow_module_level=True) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def probe(ep): |
| 20 | + @ep.method() |
| 21 | + def probe() -> str: |
| 22 | + raise ZeroDivisionError |
| 23 | + |
| 24 | + @ep.method() |
| 25 | + def probe2() -> str: |
| 26 | + raise RuntimeError |
| 27 | + |
| 28 | + return ep |
| 29 | + |
| 30 | + |
| 31 | +def test_transaction_is_jsonrpc_method( |
| 32 | + probe, |
| 33 | + json_request, |
| 34 | + sentry_init, |
| 35 | + capture_exceptions, |
| 36 | + capture_events, |
| 37 | + assert_log_errors, |
| 38 | +): |
| 39 | + sentry_init(send_default_pii=True) |
| 40 | + exceptions = capture_exceptions() |
| 41 | + events = capture_events() |
| 42 | + |
| 43 | + # Test in batch to ensure we correctly handle multiple requests |
| 44 | + json_request([ |
| 45 | + { |
| 46 | + 'id': 1, |
| 47 | + 'jsonrpc': '2.0', |
| 48 | + 'method': 'probe', |
| 49 | + 'params': {}, |
| 50 | + }, |
| 51 | + { |
| 52 | + 'id': 2, |
| 53 | + 'jsonrpc': '2.0', |
| 54 | + 'method': 'probe2', |
| 55 | + 'params': {}, |
| 56 | + }, |
| 57 | + ]) |
| 58 | + |
| 59 | + assert {type(e) for e in exceptions} == {RuntimeError, ZeroDivisionError} |
| 60 | + |
| 61 | + assert_log_errors( |
| 62 | + '', pytest.raises(ZeroDivisionError), |
| 63 | + '', pytest.raises(RuntimeError), |
| 64 | + ) |
| 65 | + |
| 66 | + assert set([ |
| 67 | + e.get('transaction') for e in events |
| 68 | + ]) == {'test_sentry_sdk_1x.probe.<locals>.probe', 'test_sentry_sdk_1x.probe.<locals>.probe2'} |
| 69 | + |
| 70 | + |
| 71 | +class _TestTransport(Transport): |
| 72 | + def __init__(self, capture_event_callback, capture_envelope_callback): |
| 73 | + Transport.__init__(self) |
| 74 | + self.capture_event = capture_event_callback |
| 75 | + self.capture_envelope = capture_envelope_callback |
| 76 | + self._queue = None |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture |
| 80 | +def monkeypatch_test_transport(monkeypatch): |
| 81 | + def check_event(event): |
| 82 | + def check_string_keys(map): |
| 83 | + for key, value in map.items: |
| 84 | + assert isinstance(key, str) |
| 85 | + if isinstance(value, dict): |
| 86 | + check_string_keys(value) |
| 87 | + |
| 88 | + with capture_internal_exceptions(): |
| 89 | + check_string_keys(event) |
| 90 | + |
| 91 | + def check_envelope(envelope): |
| 92 | + with capture_internal_exceptions(): |
| 93 | + # Assert error events are sent without envelope to server, for compat. |
| 94 | + # This does not apply if any item in the envelope is an attachment. |
| 95 | + if not any(x.type == "attachment" for x in envelope.items): |
| 96 | + assert not any(item.data_category == "error" for item in envelope.items) |
| 97 | + assert not any(item.get_event() is not None for item in envelope.items) |
| 98 | + |
| 99 | + def inner(client): |
| 100 | + monkeypatch.setattr( |
| 101 | + client, "transport", _TestTransport(check_event, check_envelope) |
| 102 | + ) |
| 103 | + |
| 104 | + return inner |
| 105 | + |
| 106 | + |
| 107 | +@pytest.fixture |
| 108 | +def sentry_init(monkeypatch_test_transport, request): |
| 109 | + def inner(*a, **kw): |
| 110 | + hub = sentry_sdk.Hub.current |
| 111 | + client = sentry_sdk.Client(*a, **kw) |
| 112 | + hub.bind_client(client) |
| 113 | + if "transport" not in kw: |
| 114 | + monkeypatch_test_transport(sentry_sdk.Hub.current.client) |
| 115 | + |
| 116 | + if request.node.get_closest_marker("forked"): |
| 117 | + # Do not run isolation if the test is already running in |
| 118 | + # ultimate isolation (seems to be required for celery tests that |
| 119 | + # fork) |
| 120 | + yield inner |
| 121 | + else: |
| 122 | + with sentry_sdk.Hub(None): |
| 123 | + yield inner |
| 124 | + |
| 125 | + |
| 126 | +@pytest.fixture |
| 127 | +def capture_events(monkeypatch): |
| 128 | + def inner(): |
| 129 | + events = [] |
| 130 | + test_client = sentry_sdk.Hub.current.client |
| 131 | + old_capture_event = test_client.transport.capture_event |
| 132 | + old_capture_envelope = test_client.transport.capture_envelope |
| 133 | + |
| 134 | + def append_event(event): |
| 135 | + events.append(event) |
| 136 | + return old_capture_event(event) |
| 137 | + |
| 138 | + def append_envelope(envelope): |
| 139 | + for item in envelope: |
| 140 | + if item.headers.get("type") in ("event", "transaction"): |
| 141 | + test_client.transport.capture_event(item.payload.json) |
| 142 | + return old_capture_envelope(envelope) |
| 143 | + |
| 144 | + monkeypatch.setattr(test_client.transport, "capture_event", append_event) |
| 145 | + monkeypatch.setattr(test_client.transport, "capture_envelope", append_envelope) |
| 146 | + return events |
| 147 | + |
| 148 | + return inner |
| 149 | + |
| 150 | + |
| 151 | +@pytest.fixture |
| 152 | +def capture_exceptions(monkeypatch): |
| 153 | + def inner(): |
| 154 | + errors = set() |
| 155 | + old_capture_event = sentry_sdk.Hub.capture_event |
| 156 | + |
| 157 | + def capture_event(self, event, hint=None): |
| 158 | + if hint: |
| 159 | + if "exc_info" in hint: |
| 160 | + error = hint["exc_info"][1] |
| 161 | + errors.add(error) |
| 162 | + return old_capture_event(self, event, hint=hint) |
| 163 | + |
| 164 | + monkeypatch.setattr(sentry_sdk.Hub, "capture_event", capture_event) |
| 165 | + return errors |
| 166 | + |
| 167 | + return inner |
0 commit comments