From 62a3bc72f69f9d79926d1ecbed8e5ced53cfef8d Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Mon, 2 Mar 2026 19:00:36 +0000 Subject: [PATCH] fixes disk usage tests --- app/tests/web/routers/test_default.py | 19 ++++++++++++++----- app/tests/web/test_main.py | 6 +++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/tests/web/routers/test_default.py b/app/tests/web/routers/test_default.py index 32e1221..70ce508 100644 --- a/app/tests/web/routers/test_default.py +++ b/app/tests/web/routers/test_default.py @@ -1,5 +1,6 @@ +import shutil from http import HTTPStatus -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch import pytest 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 # after metrics calculation - await measure_regular_metrics( - get_settings.DATABASE_PATH, 60 * 60 * 24 * 31 * 12 * 100 - ) + # Mock shutil.disk_usage since /aa-api/database is a Docker-only path + 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") assert 'disk_utilization{type="used"}' 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 - 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") assert 'database_metrics{query="count_archives"} 100.0' in r3.text assert 'database_metrics{query="count_archive_urls"} 1000.0' in r3.text diff --git a/app/tests/web/test_main.py b/app/tests/web/test_main.py index a8a2c4f..a6e09d0 100644 --- a/app/tests/web/test_main.py +++ b/app/tests/web/test_main.py @@ -44,11 +44,11 @@ def test_alembic(db_session): side_effect=Exception("mocked error"), ) 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"): client_with_auth.delete("/url/123") - # creates one empty and one from above - assert len(EXCEPTION_COUNTER.collect()[0].samples) == 2 + # the exception should have created new counter samples + assert len(EXCEPTION_COUNTER.collect()[0].samples) == samples_before + 2 def test_serve_local_archive_logic(get_settings):