|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from aikido_zen.helpers.token import Token |
| 9 | +from .listen_for_config_updates import listen_for_config_updates |
| 10 | + |
| 11 | + |
| 12 | +def make_event(event="config-updated", data=None): |
| 13 | + return SimpleNamespace( |
| 14 | + event=event, data=json.dumps(data) if data is not None else "" |
| 15 | + ) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def connection_manager(): |
| 20 | + return MagicMock( |
| 21 | + token=Token("123"), |
| 22 | + serverless=None, |
| 23 | + conf=MagicMock(last_updated_at=0), |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def test_no_token(caplog): |
| 28 | + with patch( |
| 29 | + "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" |
| 30 | + ) as mock_connect: |
| 31 | + listen_for_config_updates( |
| 32 | + connection_manager=MagicMock(token=None, serverless=None), |
| 33 | + event_scheduler=MagicMock(), |
| 34 | + ) |
| 35 | + |
| 36 | + assert "No token provided, not listening for config updates" in caplog.text |
| 37 | + mock_connect.assert_not_called() |
| 38 | + |
| 39 | + |
| 40 | +def test_serverless_environment(caplog): |
| 41 | + with patch( |
| 42 | + "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" |
| 43 | + ) as mock_connect: |
| 44 | + listen_for_config_updates( |
| 45 | + connection_manager=MagicMock(token=Token("123"), serverless=True), |
| 46 | + event_scheduler=MagicMock(), |
| 47 | + ) |
| 48 | + |
| 49 | + assert ( |
| 50 | + "Running in serverless environment, not listening for config updates" |
| 51 | + in caplog.text |
| 52 | + ) |
| 53 | + mock_connect.assert_not_called() |
| 54 | + |
| 55 | + |
| 56 | +def test_connects_to_sse_with_token(connection_manager): |
| 57 | + with patch( |
| 58 | + "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" |
| 59 | + ) as mock_connect: |
| 60 | + listen_for_config_updates( |
| 61 | + connection_manager=connection_manager, event_scheduler=MagicMock() |
| 62 | + ) |
| 63 | + |
| 64 | + mock_connect.assert_called_once() |
| 65 | + assert mock_connect.call_args.kwargs["token"] is connection_manager.token |
| 66 | + assert callable(mock_connect.call_args.kwargs["on_event"]) |
| 67 | + |
| 68 | + |
| 69 | +def get_on_event(connection_manager): |
| 70 | + with patch( |
| 71 | + "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" |
| 72 | + ) as mock_connect: |
| 73 | + listen_for_config_updates( |
| 74 | + connection_manager=connection_manager, event_scheduler=MagicMock() |
| 75 | + ) |
| 76 | + return mock_connect.call_args.kwargs["on_event"] |
| 77 | + |
| 78 | + |
| 79 | +def test_ignores_events_that_are_not_config_updated(connection_manager): |
| 80 | + on_event = get_on_event(connection_manager) |
| 81 | + |
| 82 | + with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: |
| 83 | + on_event(make_event(event="ping")) |
| 84 | + |
| 85 | + mock_get_config.assert_not_called() |
| 86 | + connection_manager.update_service_config.assert_not_called() |
| 87 | + |
| 88 | + |
| 89 | +def test_ignores_config_updated_event_with_invalid_json(connection_manager, caplog): |
| 90 | + caplog.set_level(logging.DEBUG, logger="Zen") |
| 91 | + on_event = get_on_event(connection_manager) |
| 92 | + |
| 93 | + with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: |
| 94 | + on_event(SimpleNamespace(event="config-updated", data="not json")) |
| 95 | + |
| 96 | + mock_get_config.assert_not_called() |
| 97 | + connection_manager.update_service_config.assert_not_called() |
| 98 | + assert "SSE config-updated event has invalid payload" in caplog.text |
| 99 | + |
| 100 | + |
| 101 | +def test_ignores_config_updated_event_missing_config_updated_at(connection_manager): |
| 102 | + on_event = get_on_event(connection_manager) |
| 103 | + |
| 104 | + with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: |
| 105 | + on_event(make_event(data={"foo": "bar"})) |
| 106 | + |
| 107 | + mock_get_config.assert_not_called() |
| 108 | + connection_manager.update_service_config.assert_not_called() |
| 109 | + |
| 110 | + |
| 111 | +def test_ignores_config_updated_event_that_is_not_newer(connection_manager): |
| 112 | + connection_manager.conf.last_updated_at = 100 |
| 113 | + on_event = get_on_event(connection_manager) |
| 114 | + |
| 115 | + with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: |
| 116 | + on_event(make_event(data={"configUpdatedAt": 100})) |
| 117 | + |
| 118 | + mock_get_config.assert_not_called() |
| 119 | + connection_manager.update_service_config.assert_not_called() |
| 120 | + |
| 121 | + |
| 122 | +def test_fetches_and_applies_new_config_on_newer_event(connection_manager): |
| 123 | + connection_manager.conf.last_updated_at = 100 |
| 124 | + on_event = get_on_event(connection_manager) |
| 125 | + |
| 126 | + new_config = {"endpoints": [], "configUpdatedAt": 200} |
| 127 | + with patch( |
| 128 | + "aikido_zen.background_process.realtime.get_config", return_value=new_config |
| 129 | + ) as mock_get_config: |
| 130 | + on_event(make_event(data={"configUpdatedAt": 200})) |
| 131 | + |
| 132 | + mock_get_config.assert_called_once_with(connection_manager.token) |
| 133 | + connection_manager.update_service_config.assert_called_once_with( |
| 134 | + {**new_config, "success": True} |
| 135 | + ) |
| 136 | + connection_manager.update_firewall_lists.assert_called_once() |
| 137 | + |
| 138 | + |
| 139 | +def test_updates_last_updated_at_so_a_second_stale_event_is_ignored( |
| 140 | + connection_manager, |
| 141 | +): |
| 142 | + connection_manager.conf.last_updated_at = 100 |
| 143 | + on_event = get_on_event(connection_manager) |
| 144 | + |
| 145 | + new_config = {"endpoints": [], "configUpdatedAt": 200} |
| 146 | + with patch( |
| 147 | + "aikido_zen.background_process.realtime.get_config", return_value=new_config |
| 148 | + ) as mock_get_config: |
| 149 | + on_event(make_event(data={"configUpdatedAt": 200})) |
| 150 | + # A second event claiming to be newer than the original lastUpdatedAt |
| 151 | + # (100), but not newer than what we just fetched (200), should now be |
| 152 | + # ignored without fetching again. |
| 153 | + on_event(make_event(data={"configUpdatedAt": 150})) |
| 154 | + |
| 155 | + mock_get_config.assert_called_once() |
| 156 | + |
| 157 | + |
| 158 | +def test_handles_get_config_failure_gracefully(connection_manager, caplog): |
| 159 | + connection_manager.conf.last_updated_at = 100 |
| 160 | + on_event = get_on_event(connection_manager) |
| 161 | + |
| 162 | + with patch( |
| 163 | + "aikido_zen.background_process.realtime.get_config", |
| 164 | + side_effect=ValueError("Request timed out"), |
| 165 | + ): |
| 166 | + on_event(make_event(data={"configUpdatedAt": 200})) |
| 167 | + |
| 168 | + connection_manager.update_service_config.assert_not_called() |
| 169 | + assert "Failed to fetch config after SSE event" in caplog.text |
0 commit comments