2
0
mirror of https://github.com/GNS3/gns3-server.git synced 2025-03-13 09:03:34 +02:00

Improve tests for pruning images

This commit is contained in:
grossmj 2025-01-03 19:56:41 +07:00
parent e17a79e261
commit ac4c5f5985
No known key found for this signature in database
GPG Key ID: 1E7DD6DBB53FF3D7
3 changed files with 22 additions and 7 deletions
gns3server/db/repositories
tests/api/routes/controller

View File

@ -171,7 +171,7 @@ class TemplatesRepository(BaseRepository):
await self._db_session.refresh(template_in_db)
return template_in_db
async def get_template_images(self, template_id: UUID) -> int:
async def get_template_images(self, template_id: UUID) -> List[models.Image]:
"""
Return all images attached to a template.
"""

View File

@ -261,10 +261,13 @@ class TestImageRoutes:
async def test_prune_images(self, app: FastAPI, client: AsyncClient, db_session: AsyncSession) -> None:
images_repo = ImagesRepository(db_session)
images_in_db = await images_repo.get_images()
assert len(images_in_db) != 0
response = await client.delete(app.url_path_for("prune_images"))
assert response.status_code == status.HTTP_204_NO_CONTENT
images_repo = ImagesRepository(db_session)
images_in_db = await images_repo.get_images()
assert len(images_in_db) == 0

View File

@ -16,6 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import shutil
import pytest
import uuid
import unittest.mock
@ -145,29 +147,39 @@ class TestTemplateRoutes:
tmpdir: str,
) -> None:
path = os.path.join(tmpdir, "test.qcow2")
with open(path, "wb+") as f:
image1 = os.path.join(tmpdir, "image1.qcow2")
with open(image1, "wb+") as f:
f.write(b'\x42\x42\x42\x42')
image2 = os.path.join(tmpdir, "image2.qcow2")
with open(image2, "wb+") as f:
f.write(b'\x42\x42\x42\x42')
images_repo = ImagesRepository(db_session)
await images_repo.add_image("test.qcow2", "qemu", 42, path, "e342eb86c1229b6c154367a5476969b5", "md5")
await images_repo.add_image("image1.qcow2", "qemu", 42, image1, "e342eb86c1229b6c154367a5476969b5", "md5")
await images_repo.add_image("image2.qcow2", "qemu", 42, image2, "e342eb86c1229b6c154367a5476969b5", "md5")
template_id = str(uuid.uuid4())
params = {"template_id": template_id,
"name": "QEMU_TEMPLATE",
"compute_id": "local",
"hda_disk_image": "test.qcow2",
"hda_disk_image": "image1.qcow2",
"hdb_disk_image": "image2.qcow2",
"template_type": "qemu"}
response = await client.post(app.url_path_for("create_template"), json=params)
assert response.status_code == status.HTTP_201_CREATED
templates_repo = TemplatesRepository(db_session)
images = await templates_repo.get_template_images(response.json().get("template_id"))
assert len(images) == 2
response = await client.delete(
app.url_path_for("delete_template", template_id=template_id),
params={"prune_images": True}
)
assert response.status_code == status.HTTP_204_NO_CONTENT
images_repo = ImagesRepository(db_session)
images = await images_repo.get_images()
assert len(images) == 0