Skip to content

Commit

Permalink
Mock disk usage, real values are not reliable
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Leclerc <[email protected]>
  • Loading branch information
sylvlecl committed Sep 2, 2024
1 parent 6eb8469 commit 43939cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ checksumdir~=1.2.0
pytest~=8.3.0
pytest-xdist~=3.6.0
pytest-cov~=4.0.0
pytest-mock~=3.14.0

# In this version DataFrame conversion to Excel is done using 'xlsxwriter' library.
# But Excel files reading is done using 'openpyxl' library, during testing only.
Expand Down
26 changes: 13 additions & 13 deletions tests/integration/filesystem_blueprint/test_filesystem_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import datetime
import operator
import re
import shutil
import typing as t
from pathlib import Path

from pytest_mock import MockerFixture
from starlette.testclient import TestClient

from tests.integration.conftest import RESOURCES_DIR
Expand Down Expand Up @@ -93,6 +93,7 @@ def test_lifecycle(
client: TestClient,
user_access_token: str,
admin_access_token: str,
mocker: MockerFixture
) -> None:
"""
Test the lifecycle of the filesystem endpoints.
Expand All @@ -102,7 +103,7 @@ def test_lifecycle(
caplog: pytest caplog fixture.
client: test client (tests.integration.conftest.client_fixture).
user_access_token: access token of a classic user (tests.integration.conftest.user_access_token_fixture).
admin_access_token: access token of an admin user (tests.integration.conftest.admin_access_token_fixture).
admin_access_token: access token of an admin user (tests.integration.conftestin_access_token_fixture).
"""
# NOTE: all the following paths are based on the configuration defined in the app_fixture.
archive_dir = tmp_path / "archive_dir"
Expand Down Expand Up @@ -165,26 +166,25 @@ def test_lifecycle(
err_count += 1

# Known filesystem
mocker.patch("shutil.disk_usage", return_value=(100, 200, 300))
res = client.get("/v1/filesystem/ws", headers=user_headers)
assert res.status_code == 200, res.json()
actual = sorted(res.json(), key=operator.itemgetter("name"))
# Both mount point are in the same filesystem, which is the `tmp_path` filesystem
total_bytes, used_bytes, free_bytes = shutil.disk_usage(tmp_path)
expected = [
{
"name": "default",
"path": str(default_workspace),
"total_bytes": total_bytes,
"used_bytes": used_bytes,
"free_bytes": free_bytes,
"total_bytes": 100,
"used_bytes": 200,
"free_bytes": 300,
"message": AnyDiskUsagePercent(),
},
{
"name": "ext",
"path": str(ext_workspace_path),
"total_bytes": total_bytes,
"used_bytes": used_bytes,
"free_bytes": free_bytes,
"total_bytes": 100,
"used_bytes": 200,
"free_bytes": 300,
"message": AnyDiskUsagePercent(),
},
]
Expand All @@ -206,9 +206,9 @@ def test_lifecycle(
expected = {
"name": "default",
"path": str(default_workspace),
"total_bytes": total_bytes,
"used_bytes": used_bytes,
"free_bytes": free_bytes,
"total_bytes": 100,
"used_bytes": 200,
"free_bytes": 300,
"message": AnyDiskUsagePercent(),
}
assert actual == expected
Expand Down
13 changes: 8 additions & 5 deletions tests/integration/filesystem_blueprint/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import shutil
from pathlib import Path

from pytest_mock import MockerFixture

from antarest.core.filesystem_blueprint import FileInfoDTO, FilesystemDTO, MountPointDTO


Expand Down Expand Up @@ -63,15 +65,16 @@ def test_from_path__missing_file(self) -> None:
assert dto.free_bytes == 0
assert dto.message.startswith("N/A:"), dto.message

def test_from_path__file(self, tmp_path: Path) -> None:
def test_from_path__file(self, tmp_path: Path, mocker: MockerFixture) -> None:
mocker.patch("shutil.disk_usage", return_value=(100, 200, 300))

name = "foo"
dto = MountPointDTO.from_path(name, tmp_path)
total_bytes, used_bytes, free_bytes = shutil.disk_usage(tmp_path)
assert dto.name == name
assert dto.path == tmp_path
assert dto.total_bytes == total_bytes
assert dto.used_bytes == used_bytes
assert dto.free_bytes == free_bytes
assert dto.total_bytes == 100
assert dto.used_bytes == 200
assert dto.free_bytes == 300
assert re.fullmatch(r"\d+(?:\.\d+)?% used", dto.message), dto.message


Expand Down

0 comments on commit 43939cf

Please sign in to comment.