|
2 | 2 | Unit tests for the PGJsonb returner (pgjsonb). |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import logging |
| 6 | + |
5 | 7 | import pytest |
6 | 8 |
|
| 9 | +import salt.exceptions |
7 | 10 | import salt.returners.pgjsonb as pgjsonb |
8 | 11 | from tests.support.mock import MagicMock, call, patch |
9 | 12 |
|
@@ -148,3 +151,169 @@ def test_save_load_uses_plain_insert_on_pre_pg_95(): |
148 | 151 |
|
149 | 152 | sql = cur.execute.call_args.args[0] |
150 | 153 | assert "ON CONFLICT" not in sql |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 157 | +def test__purge_jobs_logs_via_salt_logger_and_reraises_on_db_error(caplog): |
| 158 | + """When the DELETE inside ``_purge_jobs`` fails, the error must reach |
| 159 | + Salt's logger (not stderr), the transaction is rolled back, and the |
| 160 | + original ``DatabaseError`` is re-raised.""" |
| 161 | + cursor = MagicMock() |
| 162 | + cursor.execute.side_effect = [psycopg2.DatabaseError("boom"), None] |
| 163 | + serv = MagicMock() |
| 164 | + serv.return_value.__enter__.return_value = cursor |
| 165 | + |
| 166 | + with patch.object(pgjsonb, "_get_serv", serv): |
| 167 | + with caplog.at_level(logging.ERROR, logger="salt.returners.pgjsonb"): |
| 168 | + with pytest.raises(psycopg2.DatabaseError): |
| 169 | + pgjsonb._purge_jobs("2026-01-01") |
| 170 | + |
| 171 | + cursor.execute.assert_any_call("ROLLBACK") |
| 172 | + assert any("failed to purge jids" in r.message for r in caplog.records) |
| 173 | + |
| 174 | + |
| 175 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 176 | +def test__archive_jobs_logs_via_salt_logger_and_reraises_on_db_error(caplog): |
| 177 | + """When the CREATE TABLE inside ``_archive_jobs`` fails, the error |
| 178 | + reaches Salt's logger, the transaction is rolled back, and the |
| 179 | + original ``DatabaseError`` is re-raised.""" |
| 180 | + cursor = MagicMock() |
| 181 | + cursor.execute.side_effect = [psycopg2.DatabaseError("boom"), None] |
| 182 | + serv = MagicMock() |
| 183 | + serv.return_value.__enter__.return_value = cursor |
| 184 | + |
| 185 | + with patch.object(pgjsonb, "_get_serv", serv): |
| 186 | + with caplog.at_level(logging.ERROR, logger="salt.returners.pgjsonb"): |
| 187 | + with pytest.raises(psycopg2.DatabaseError): |
| 188 | + pgjsonb._archive_jobs("2026-01-01") |
| 189 | + |
| 190 | + cursor.execute.assert_any_call("ROLLBACK") |
| 191 | + assert any("failed to create archive table" in r.message for r in caplog.records) |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 195 | +def test__get_serv_logs_via_salt_logger_and_reraises_on_yield_error(caplog): |
| 196 | + """When the caller of ``_get_serv()`` raises a DatabaseError inside |
| 197 | + the ``with`` block, ``_get_serv`` must log via Salt's logger, |
| 198 | + issue ROLLBACK on the connection, and re-raise.""" |
| 199 | + fake_conn = MagicMock() |
| 200 | + fake_conn.server_version = 90500 |
| 201 | + fake_cursor = MagicMock() |
| 202 | + fake_conn.cursor.return_value = fake_cursor |
| 203 | + |
| 204 | + with patch.object(pgjsonb, "_get_options", return_value={}): |
| 205 | + with patch("psycopg2.connect", return_value=fake_conn): |
| 206 | + with caplog.at_level(logging.ERROR, logger="salt.returners.pgjsonb"): |
| 207 | + with pytest.raises(psycopg2.DatabaseError): |
| 208 | + with pgjsonb._get_serv(): |
| 209 | + raise psycopg2.DatabaseError("boom") |
| 210 | + |
| 211 | + fake_cursor.execute.assert_any_call("ROLLBACK") |
| 212 | + assert any("_get_serv" in r.message for r in caplog.records) |
| 213 | + |
| 214 | + |
| 215 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 216 | +def test_returner_logs_on_connection_failure_without_raising(caplog): |
| 217 | + """When the database is unreachable, ``returner`` must not propagate |
| 218 | + the SaltMasterError. The drop is logged at CRITICAL with jid and id |
| 219 | + so operators can correlate it to the lost return.""" |
| 220 | + serv = MagicMock(side_effect=salt.exceptions.SaltMasterError("pg down")) |
| 221 | + with patch.object(pgjsonb, "_get_serv", serv): |
| 222 | + with caplog.at_level(logging.CRITICAL, logger="salt.returners.pgjsonb"): |
| 223 | + pgjsonb.returner( |
| 224 | + { |
| 225 | + "fun": "test.ping", |
| 226 | + "jid": "20260505000000000001", |
| 227 | + "id": "minion-1", |
| 228 | + "return": True, |
| 229 | + } |
| 230 | + ) |
| 231 | + |
| 232 | + assert any( |
| 233 | + "PostgreSQL unavailable" in r.message |
| 234 | + and "20260505000000000001" in r.message |
| 235 | + and "minion-1" in r.message |
| 236 | + for r in caplog.records |
| 237 | + ) |
| 238 | + |
| 239 | + |
| 240 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 241 | +def test_returner_logs_on_database_error_without_raising(caplog): |
| 242 | + """A DatabaseError during the INSERT into ``salt_returns`` must not |
| 243 | + propagate; ``returner`` logs and drops the return so that one bad row |
| 244 | + cannot escape into syndic-aggregate paths that lack an outer catch.""" |
| 245 | + cur = MagicMock() |
| 246 | + cur.execute.side_effect = psycopg2.DatabaseError("bad row") |
| 247 | + serv = MagicMock() |
| 248 | + serv.return_value.__enter__.return_value = cur |
| 249 | + |
| 250 | + with patch.object(pgjsonb, "_get_serv", serv): |
| 251 | + with caplog.at_level(logging.ERROR, logger="salt.returners.pgjsonb"): |
| 252 | + pgjsonb.returner( |
| 253 | + { |
| 254 | + "fun": "test.ping", |
| 255 | + "jid": "20260505000000000002", |
| 256 | + "id": "minion-2", |
| 257 | + "return": True, |
| 258 | + } |
| 259 | + ) |
| 260 | + |
| 261 | + assert any( |
| 262 | + "failed to store return" in r.message |
| 263 | + and "20260505000000000002" in r.message |
| 264 | + and "minion-2" in r.message |
| 265 | + for r in caplog.records |
| 266 | + ) |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 270 | +def test_event_return_inserts_each_event_into_salt_events(): |
| 271 | + """``event_return`` writes one row to ``salt_events`` per queued event, |
| 272 | + carrying the event tag and the master id. Also pins that ``_get_serv`` |
| 273 | + is opened with ``commit=True`` only -- passing the events list as the |
| 274 | + positional ``ret`` argument was a copy-paste leftover from |
| 275 | + ``returner(ret)``.""" |
| 276 | + events = [ |
| 277 | + {"tag": "salt/job/1/new", "data": {"jid": "1", "fun": "test.ping"}}, |
| 278 | + {"tag": "salt/auth", "data": {"id": "minion-1", "act": "accept"}}, |
| 279 | + ] |
| 280 | + cur = MagicMock() |
| 281 | + serv = MagicMock() |
| 282 | + serv.return_value.__enter__.return_value = cur |
| 283 | + |
| 284 | + with patch.object(pgjsonb, "_get_serv", serv): |
| 285 | + with patch.dict(pgjsonb.__opts__, {"id": "master-A"}): |
| 286 | + with patch("salt.returners.pgjsonb.time.time", return_value=1700000000.0): |
| 287 | + pgjsonb.event_return(events) |
| 288 | + |
| 289 | + serv.assert_called_once_with(commit=True) |
| 290 | + |
| 291 | + assert cur.execute.call_count == len(events) |
| 292 | + for executed_call, event in zip(cur.execute.call_args_list, events): |
| 293 | + sql, params = executed_call.args |
| 294 | + assert "INSERT INTO salt_events" in sql |
| 295 | + tag, _data_json, master_id, ts = params |
| 296 | + assert tag == event["tag"] |
| 297 | + assert master_id == "master-A" |
| 298 | + assert ts == 1700000000.0 |
| 299 | + |
| 300 | + |
| 301 | +@pytest.mark.skipif(not pgjsonb.HAS_PG, reason="psycopg2 not installed") |
| 302 | +def test_event_return_logs_on_database_error_without_raising(caplog): |
| 303 | + """A DatabaseError mid-batch must not propagate out of ``event_return``; |
| 304 | + the queue length is logged so operators see how many events were lost.""" |
| 305 | + events = [{"tag": f"tag-{i}", "data": {}} for i in range(3)] |
| 306 | + cur = MagicMock() |
| 307 | + cur.execute.side_effect = psycopg2.DatabaseError("bad event") |
| 308 | + serv = MagicMock() |
| 309 | + serv.return_value.__enter__.return_value = cur |
| 310 | + |
| 311 | + with patch.object(pgjsonb, "_get_serv", serv): |
| 312 | + with patch.dict(pgjsonb.__opts__, {"id": "master-A"}): |
| 313 | + with caplog.at_level(logging.ERROR, logger="salt.returners.pgjsonb"): |
| 314 | + pgjsonb.event_return(events) |
| 315 | + |
| 316 | + assert any( |
| 317 | + "failed to store" in r.message and "3 event" in r.message |
| 318 | + for r in caplog.records |
| 319 | + ) |
0 commit comments