|
| 1 | +"""Unit tests for functions defined in src/sentry.py.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pytest_mock import MockerFixture |
| 5 | + |
| 6 | +from constants import ( |
| 7 | + SENTRY_CA_CERTS_ENV_VAR, |
| 8 | + SENTRY_DEFAULT_ENVIRONMENT, |
| 9 | + SENTRY_DEFAULT_TRACES_SAMPLE_RATE, |
| 10 | + SENTRY_DSN_ENV_VAR, |
| 11 | + SENTRY_ENVIRONMENT_ENV_VAR, |
| 12 | + SENTRY_EXCLUDED_ROUTES, |
| 13 | +) |
| 14 | +from sentry import initialize_sentry, sentry_traces_sampler |
| 15 | + |
| 16 | + |
| 17 | +class TestInitializeSentry: |
| 18 | + """Tests for the initialize_sentry function.""" |
| 19 | + |
| 20 | + def test_dsn_not_set( |
| 21 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 22 | + ) -> None: |
| 23 | + """Test that Sentry is not initialized when DSN env var is unset.""" |
| 24 | + monkeypatch.delenv(SENTRY_DSN_ENV_VAR, raising=False) |
| 25 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 26 | + |
| 27 | + initialize_sentry() |
| 28 | + |
| 29 | + mock_init.assert_not_called() |
| 30 | + |
| 31 | + def test_dsn_empty_string( |
| 32 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 33 | + ) -> None: |
| 34 | + """Test that Sentry is not initialized when DSN is an empty string.""" |
| 35 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, "") |
| 36 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 37 | + |
| 38 | + initialize_sentry() |
| 39 | + |
| 40 | + mock_init.assert_not_called() |
| 41 | + |
| 42 | + def test_dsn_set_no_ca_certs( |
| 43 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 44 | + ) -> None: |
| 45 | + """Test Sentry init without CA certs env var uses ca_certs=None.""" |
| 46 | + dsn = "https://key@sentry.io/123" |
| 47 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, dsn) |
| 48 | + monkeypatch.delenv(SENTRY_ENVIRONMENT_ENV_VAR, raising=False) |
| 49 | + monkeypatch.delenv(SENTRY_CA_CERTS_ENV_VAR, raising=False) |
| 50 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 51 | + |
| 52 | + initialize_sentry() |
| 53 | + |
| 54 | + mock_init.assert_called_once() |
| 55 | + call_kwargs = mock_init.call_args.kwargs |
| 56 | + assert call_kwargs["dsn"] == dsn |
| 57 | + assert call_kwargs["ca_certs"] is None |
| 58 | + assert call_kwargs["send_default_pii"] is False |
| 59 | + assert call_kwargs["environment"] == SENTRY_DEFAULT_ENVIRONMENT |
| 60 | + assert call_kwargs["release"].startswith("lightspeed-stack@") |
| 61 | + |
| 62 | + def test_ca_certs_file_exists( |
| 63 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 64 | + ) -> None: |
| 65 | + """Test that ca_certs is set when SENTRY_CA_CERTS points to an existing file.""" |
| 66 | + ca_path = "/etc/pki/tls/certs/ca-bundle.crt" |
| 67 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, "https://key@sentry.io/123") |
| 68 | + monkeypatch.setenv(SENTRY_CA_CERTS_ENV_VAR, ca_path) |
| 69 | + monkeypatch.delenv(SENTRY_ENVIRONMENT_ENV_VAR, raising=False) |
| 70 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 71 | + mocker.patch("sentry.os.path.exists", return_value=True) |
| 72 | + |
| 73 | + initialize_sentry() |
| 74 | + |
| 75 | + mock_init.assert_called_once() |
| 76 | + assert mock_init.call_args.kwargs["ca_certs"] == ca_path |
| 77 | + |
| 78 | + def test_ca_certs_file_missing( |
| 79 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 80 | + ) -> None: |
| 81 | + """Test that ca_certs is None and a warning is logged when the cert file is missing.""" |
| 82 | + ca_path = "/nonexistent/ca-bundle.crt" |
| 83 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, "https://key@sentry.io/123") |
| 84 | + monkeypatch.setenv(SENTRY_CA_CERTS_ENV_VAR, ca_path) |
| 85 | + monkeypatch.delenv(SENTRY_ENVIRONMENT_ENV_VAR, raising=False) |
| 86 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 87 | + mocker.patch("sentry.os.path.exists", return_value=False) |
| 88 | + mock_logger = mocker.patch("sentry.logger") |
| 89 | + |
| 90 | + initialize_sentry() |
| 91 | + |
| 92 | + mock_init.assert_called_once() |
| 93 | + assert mock_init.call_args.kwargs["ca_certs"] is None |
| 94 | + mock_logger.warning.assert_called_once_with( |
| 95 | + "CA cert file specified by %s not found at %s; " |
| 96 | + "proceeding without custom CA certs", |
| 97 | + SENTRY_CA_CERTS_ENV_VAR, |
| 98 | + ca_path, |
| 99 | + ) |
| 100 | + |
| 101 | + def test_custom_environment( |
| 102 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 103 | + ) -> None: |
| 104 | + """Test that a custom SENTRY_ENVIRONMENT value is passed to init.""" |
| 105 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, "https://key@sentry.io/123") |
| 106 | + monkeypatch.setenv(SENTRY_ENVIRONMENT_ENV_VAR, "staging") |
| 107 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 108 | + |
| 109 | + initialize_sentry() |
| 110 | + |
| 111 | + mock_init.assert_called_once() |
| 112 | + assert mock_init.call_args.kwargs["environment"] == "staging" |
| 113 | + |
| 114 | + def test_default_environment( |
| 115 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 116 | + ) -> None: |
| 117 | + """Test that default environment is used when env var is unset.""" |
| 118 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, "https://key@sentry.io/123") |
| 119 | + monkeypatch.delenv(SENTRY_ENVIRONMENT_ENV_VAR, raising=False) |
| 120 | + mock_init = mocker.patch("sentry.sentry_sdk.init") |
| 121 | + |
| 122 | + initialize_sentry() |
| 123 | + |
| 124 | + mock_init.assert_called_once() |
| 125 | + assert mock_init.call_args.kwargs["environment"] == SENTRY_DEFAULT_ENVIRONMENT |
| 126 | + |
| 127 | + def test_init_failure_does_not_raise( |
| 128 | + self, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture |
| 129 | + ) -> None: |
| 130 | + """Test that a failure during sentry_sdk.init does not propagate.""" |
| 131 | + monkeypatch.setenv(SENTRY_DSN_ENV_VAR, "https://key@sentry.io/123") |
| 132 | + monkeypatch.delenv(SENTRY_ENVIRONMENT_ENV_VAR, raising=False) |
| 133 | + monkeypatch.delenv(SENTRY_CA_CERTS_ENV_VAR, raising=False) |
| 134 | + mocker.patch( |
| 135 | + "sentry.sentry_sdk.init", side_effect=RuntimeError("connection failed") |
| 136 | + ) |
| 137 | + mock_logger = mocker.patch("sentry.logger") |
| 138 | + |
| 139 | + initialize_sentry() |
| 140 | + |
| 141 | + mock_logger.exception.assert_called_once_with( |
| 142 | + "Failed to initialize Sentry, continuing without error tracking" |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +class TestSentryTracesSampler: |
| 147 | + """Tests for the sentry_traces_sampler function.""" |
| 148 | + |
| 149 | + @pytest.mark.parametrize( |
| 150 | + "path", |
| 151 | + list(SENTRY_EXCLUDED_ROUTES), |
| 152 | + ids=[r.lstrip("/") or "root" for r in SENTRY_EXCLUDED_ROUTES], |
| 153 | + ) |
| 154 | + def test_excluded_routes_return_zero(self, path: str) -> None: |
| 155 | + """Test that excluded routes produce a sample rate of 0.0.""" |
| 156 | + context: dict = {"asgi_scope": {"path": path}} |
| 157 | + assert sentry_traces_sampler(context) == 0.0 |
| 158 | + |
| 159 | + def test_excluded_route_suffix_match(self) -> None: |
| 160 | + """Test that suffix matching works for excluded routes (e.g. /prometheus/metrics).""" |
| 161 | + context: dict = {"asgi_scope": {"path": "/prometheus/metrics"}} |
| 162 | + assert sentry_traces_sampler(context) == 0.0 |
| 163 | + |
| 164 | + @pytest.mark.parametrize( |
| 165 | + "path", |
| 166 | + ["/v1/query", "/v1/feedback", "/v1/query/"], |
| 167 | + ids=["query", "feedback", "query_trailing_slash"], |
| 168 | + ) |
| 169 | + def test_normal_routes_return_default_rate(self, path: str) -> None: |
| 170 | + """Test that non-excluded routes use the default sample rate.""" |
| 171 | + context: dict = {"asgi_scope": {"path": path}} |
| 172 | + assert sentry_traces_sampler(context) == SENTRY_DEFAULT_TRACES_SAMPLE_RATE |
| 173 | + |
| 174 | + def test_empty_context(self) -> None: |
| 175 | + """Test that an empty tracing context returns the default sample rate.""" |
| 176 | + assert sentry_traces_sampler({}) == SENTRY_DEFAULT_TRACES_SAMPLE_RATE |
| 177 | + |
| 178 | + def test_missing_path_in_asgi_scope(self) -> None: |
| 179 | + """Test that missing path key in asgi_scope returns the default rate.""" |
| 180 | + context: dict = {"asgi_scope": {}} |
| 181 | + assert sentry_traces_sampler(context) == SENTRY_DEFAULT_TRACES_SAMPLE_RATE |
| 182 | + |
| 183 | + def test_none_path_value(self) -> None: |
| 184 | + """Test that a None path value returns the default sample rate.""" |
| 185 | + context: dict = {"asgi_scope": {"path": None}} |
| 186 | + assert sentry_traces_sampler(context) == SENTRY_DEFAULT_TRACES_SAMPLE_RATE |
0 commit comments