|
2 | 2 | import pytest |
3 | 3 | from starlette.testclient import TestClient |
4 | 4 |
|
| 5 | +from fides.api.models.application_config import ApplicationConfig |
5 | 6 | from fides.api.util.endpoint_utils import API_PREFIX |
6 | 7 | from fides.config import FidesConfig |
7 | 8 |
|
8 | 9 |
|
| 10 | +@pytest.fixture(scope="function") |
| 11 | +def memory_watchdog_enabled(db): |
| 12 | + """Fixture to enable memory watchdog for tests.""" |
| 13 | + ApplicationConfig.create_or_update( |
| 14 | + db, |
| 15 | + data={"api_set": {"execution": {"memory_watchdog_enabled": True}}}, |
| 16 | + ) |
| 17 | + yield |
| 18 | + ApplicationConfig.create_or_update( |
| 19 | + db, |
| 20 | + data={"api_set": {"execution": {"memory_watchdog_enabled": False}}}, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="function") |
| 25 | +def memory_watchdog_disabled(db): |
| 26 | + """Fixture to explicitly disable memory watchdog for tests.""" |
| 27 | + ApplicationConfig.create_or_update( |
| 28 | + db, |
| 29 | + data={"api_set": {"execution": {"memory_watchdog_enabled": False}}}, |
| 30 | + ) |
| 31 | + yield |
| 32 | + # Reset is handled by default behavior (memory_watchdog_enabled defaults to False) |
| 33 | + |
| 34 | + |
9 | 35 | def test_db_reset_dev_mode_enabled( |
10 | 36 | test_config: FidesConfig, |
11 | 37 | test_client: TestClient, |
@@ -36,3 +62,65 @@ def test_db_reset_dev_mode_disabled( |
36 | 62 |
|
37 | 63 | assert response.status_code == 501 |
38 | 64 | assert response.json()["detail"] == error_message |
| 65 | + |
| 66 | + |
| 67 | +def test_heap_dump_when_disabled( |
| 68 | + test_config: FidesConfig, |
| 69 | + test_client: TestClient, |
| 70 | +) -> None: |
| 71 | + """Test that heap dump returns 405 when memory_watchdog_enabled is False (default).""" |
| 72 | + response = test_client.post( |
| 73 | + test_config.cli.server_url + API_PREFIX + "/admin/heap-dump/", |
| 74 | + headers=test_config.user.auth_header, |
| 75 | + ) |
| 76 | + |
| 77 | + assert response.status_code == 405 |
| 78 | + assert response.json()["detail"] == ( |
| 79 | + "Heap dump functionality is not enabled. " |
| 80 | + "Set memory_watchdog_enabled to true in application configuration." |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.usefixtures("memory_watchdog_disabled") |
| 85 | +def test_heap_dump_explicitly_disabled( |
| 86 | + test_config: FidesConfig, |
| 87 | + test_client: TestClient, |
| 88 | +) -> None: |
| 89 | + """Test that heap dump returns 405 when memory_watchdog_enabled is explicitly set to False.""" |
| 90 | + response = test_client.post( |
| 91 | + test_config.cli.server_url + API_PREFIX + "/admin/heap-dump/", |
| 92 | + headers=test_config.user.auth_header, |
| 93 | + ) |
| 94 | + |
| 95 | + assert response.status_code == 405 |
| 96 | + assert response.json()["detail"] == ( |
| 97 | + "Heap dump functionality is not enabled. " |
| 98 | + "Set memory_watchdog_enabled to true in application configuration." |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.usefixtures("memory_watchdog_enabled") |
| 103 | +def test_heap_dump_logs_heap_stats( |
| 104 | + test_config: FidesConfig, |
| 105 | + test_client: TestClient, |
| 106 | + loguru_caplog, |
| 107 | +) -> None: |
| 108 | + """Test that heap dump endpoint logs heap statistics.""" |
| 109 | + response = test_client.post( |
| 110 | + test_config.cli.server_url + API_PREFIX + "/admin/heap-dump/", |
| 111 | + headers=test_config.user.auth_header, |
| 112 | + ) |
| 113 | + |
| 114 | + assert response.status_code == 200 |
| 115 | + |
| 116 | + # Verify that the heap dump was logged |
| 117 | + log_output = loguru_caplog.text |
| 118 | + |
| 119 | + # Check for the info message that heap dump was triggered |
| 120 | + assert "Manual heap dump triggered via API" in log_output |
| 121 | + |
| 122 | + # Check for key sections of the heap dump in logs |
| 123 | + assert "MEMORY DUMP" in log_output |
| 124 | + assert "PROCESS MEMORY STATS" in log_output |
| 125 | + assert "OBJECT TYPE COUNTS (Top 10)" in log_output |
| 126 | + assert "GARBAGE COLLECTOR STATS" in log_output |
0 commit comments