fixes disk usage tests

This commit is contained in:
msramalho
2026-03-02 19:00:36 +00:00
parent f0bb6c265d
commit 62a3bc72f6
2 changed files with 17 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import shutil
from http import HTTPStatus from http import HTTPStatus
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@@ -85,9 +86,14 @@ async def test_prometheus_metrics(test_data, client_with_token, get_settings):
assert 'disk_utilization{type="used"}' not in r.text assert 'disk_utilization{type="used"}' not in r.text
# after metrics calculation # after metrics calculation
await measure_regular_metrics( # Mock shutil.disk_usage since /aa-api/database is a Docker-only path
get_settings.DATABASE_PATH, 60 * 60 * 24 * 31 * 12 * 100 mock_usage = shutil._ntuple_diskusage(2**40, 2**38, 2**39)
) with patch(
"app.web.utils.metrics.shutil.disk_usage", return_value=mock_usage
):
await measure_regular_metrics(
get_settings.DATABASE_PATH, 60 * 60 * 24 * 31 * 12 * 100
)
r2 = client_with_token.get("/metrics") r2 = client_with_token.get("/metrics")
assert 'disk_utilization{type="used"}' in r2.text assert 'disk_utilization{type="used"}' in r2.text
assert 'disk_utilization{type="free"}' in r2.text assert 'disk_utilization{type="free"}' in r2.text
@@ -109,7 +115,10 @@ async def test_prometheus_metrics(test_data, client_with_token, get_settings):
) )
# 30s window, should not change the gauges nor the total in the counters # 30s window, should not change the gauges nor the total in the counters
await measure_regular_metrics(get_settings.DATABASE_PATH, 30) with patch(
"app.web.utils.metrics.shutil.disk_usage", return_value=mock_usage
):
await measure_regular_metrics(get_settings.DATABASE_PATH, 30)
r3 = client_with_token.get("/metrics") r3 = client_with_token.get("/metrics")
assert 'database_metrics{query="count_archives"} 100.0' in r3.text assert 'database_metrics{query="count_archives"} 100.0' in r3.text
assert 'database_metrics{query="count_archive_urls"} 1000.0' in r3.text assert 'database_metrics{query="count_archive_urls"} 1000.0' in r3.text

View File

@@ -44,11 +44,11 @@ def test_alembic(db_session):
side_effect=Exception("mocked error"), side_effect=Exception("mocked error"),
) )
def test_logging_middleware(m1, client_with_auth): def test_logging_middleware(m1, client_with_auth):
assert len(EXCEPTION_COUNTER.collect()[0].samples) == 0 samples_before = len(EXCEPTION_COUNTER.collect()[0].samples)
with pytest.raises(Exception, match="mocked error"): with pytest.raises(Exception, match="mocked error"):
client_with_auth.delete("/url/123") client_with_auth.delete("/url/123")
# creates one empty and one from above # the exception should have created new counter samples
assert len(EXCEPTION_COUNTER.collect()[0].samples) == 2 assert len(EXCEPTION_COUNTER.collect()[0].samples) == samples_before + 2
def test_serve_local_archive_logic(get_settings): def test_serve_local_archive_logic(get_settings):