|
| 1 | +"""wait_for_opensearch must surface the real failure reason. |
| 2 | +
|
| 3 | +Previously the readiness probe gated on ``client.ping()`` (HEAD /), which |
| 4 | +opensearchpy implements by swallowing transport/auth/connection errors and |
| 5 | +returning ``False`` — collapsing a rejected JWT (401/403), a connection |
| 6 | +refusal, and a TLS error into a single uninformative "ping failed" log. |
| 7 | +
|
| 8 | +These tests pin the new behaviour: the probe calls ``cluster.health()`` |
| 9 | +(which raises), the per-attempt warning carries ``status_code``/``info``, and |
| 10 | +the final ``OpenSearchNotReadyError`` message embeds the last error. |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | +from unittest.mock import AsyncMock, MagicMock |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +ROOT = Path(__file__).resolve().parent.parent.parent |
| 20 | +SRC = ROOT / "src" |
| 21 | +if str(SRC) not in sys.path: |
| 22 | + sys.path.insert(0, str(SRC)) |
| 23 | + |
| 24 | +from utils.opensearch_utils import ( # noqa: E402 |
| 25 | + OpenSearchNotReadyError, |
| 26 | + wait_for_opensearch, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class _FakeTransportError(Exception): |
| 31 | + """Mimics opensearchpy's TransportError: carries status_code + info.""" |
| 32 | + |
| 33 | + def __init__(self, status_code, error, info): |
| 34 | + super().__init__(error) |
| 35 | + self.status_code = status_code |
| 36 | + self.error = error |
| 37 | + self.info = info |
| 38 | + |
| 39 | + |
| 40 | +def _client_with_health(health_side_effect): |
| 41 | + client = MagicMock() |
| 42 | + client.cluster.health = AsyncMock(side_effect=health_side_effect) |
| 43 | + return client |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_ready_when_health_green(): |
| 48 | + client = _client_with_health([{"status": "green"}]) |
| 49 | + # Should return without raising; no sleeps on first-attempt success. |
| 50 | + await wait_for_opensearch(client, max_retries=1) |
| 51 | + client.cluster.health.assert_awaited_once() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.asyncio |
| 55 | +async def test_auth_failure_is_surfaced(monkeypatch): |
| 56 | + err = _FakeTransportError(403, "AuthorizationException", {"reason": "invalid jwt"}) |
| 57 | + client = _client_with_health(err) |
| 58 | + |
| 59 | + captured = [] |
| 60 | + monkeypatch.setattr( |
| 61 | + "utils.opensearch_utils.logger.warning", |
| 62 | + lambda msg, **kw: captured.append((msg, kw)), |
| 63 | + ) |
| 64 | + |
| 65 | + with pytest.raises(OpenSearchNotReadyError) as excinfo: |
| 66 | + # tiny delays so the retry loop doesn't actually sleep meaningfully |
| 67 | + await wait_for_opensearch(client, max_retries=2, base_delay=0.0, max_delay=0.0) |
| 68 | + |
| 69 | + # The real reason must reach the final exception message... |
| 70 | + assert "403" in str(excinfo.value) or "AuthorizationException" in str(excinfo.value) |
| 71 | + # ...and the per-attempt warning must carry structured detail. |
| 72 | + assert captured, "expected at least one readiness-failure warning" |
| 73 | + _, kw = captured[0] |
| 74 | + assert kw["status_code"] == 403 |
| 75 | + assert kw["info"] == {"reason": "invalid jwt"} |
| 76 | + assert kw["error_type"] == "_FakeTransportError" |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.asyncio |
| 80 | +async def test_connection_error_without_status_code(monkeypatch): |
| 81 | + # A plain ConnectionError-style failure has no status_code attribute. |
| 82 | + client = _client_with_health(ConnectionError("connection refused")) |
| 83 | + |
| 84 | + captured = [] |
| 85 | + monkeypatch.setattr( |
| 86 | + "utils.opensearch_utils.logger.warning", |
| 87 | + lambda msg, **kw: captured.append((msg, kw)), |
| 88 | + ) |
| 89 | + |
| 90 | + with pytest.raises(OpenSearchNotReadyError) as excinfo: |
| 91 | + await wait_for_opensearch(client, max_retries=1, base_delay=0.0, max_delay=0.0) |
| 92 | + |
| 93 | + assert "connection refused" in str(excinfo.value) |
| 94 | + _, kw = captured[0] |
| 95 | + assert kw["status_code"] is None |
| 96 | + assert kw["error_type"] == "ConnectionError" |
0 commit comments