Add/fix tests

This commit is contained in:
grossmj 2026-02-23 23:27:36 +08:00
parent a9525503c6
commit 4d35477e46
No known key found for this signature in database
GPG Key ID: 1E7DD6DBB53FF3D7
3 changed files with 110 additions and 17 deletions

View File

@ -86,7 +86,59 @@ class TestNodeRoutes:
response = await client.get(app.url_path_for("get_nodes", project_id=project.id))
assert response.status_code == status.HTTP_200_OK
assert response.json()[0]["name"] == "test"
@pytest.mark.parametrize(
"tags, expected_match",
(
([], True),
(["tag1"], True),
(["tag1", "tag2"], True),
(["tag42"], False),
(["tag1", "tag3"], False),
),
)
async def test_list_nodes_with_tags(
self,
app: FastAPI,
client: AsyncClient,
project: Project,
compute: Compute,
tags: list,
expected_match: bool
) -> None:
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
await client.post(app.url_path_for("create_node", project_id=project.id), json={
"name": "test",
"node_type": "vpcs",
"compute_id": "example.com",
"tags": ["tag1", "tag2"],
"properties": {
"startup_script": "echo test"
}
})
await client.post(app.url_path_for("create_node", project_id=project.id), json={
"name": "test2",
"node_type": "vpcs",
"compute_id": "example.com",
"tags": ["tag3", "tag4"],
"properties": {
"startup_script": "echo test"
}
})
params = {"tags": tags}
response = await client.get(app.url_path_for("get_nodes", project_id=project.id), params=params)
assert response.status_code == status.HTTP_200_OK
if expected_match:
assert len(response.json()) > 0
else:
assert len(response.json()) == 0
async def test_get_node(
self,
@ -131,6 +183,7 @@ class TestNodeRoutes:
"name": "test",
"node_type": "vpcs",
"compute_id": "example.com",
"tags": ["tag1", "tag2"],
"properties": {
"startup_script": "echo test"
}
@ -139,8 +192,9 @@ class TestNodeRoutes:
assert response.status_code == 200
assert response.json()["name"] == "test"
assert "name" not in response.json()["properties"]
assert response.json()["tags"] == ["tag1", "tag2"]
async def test_start_all_nodes(
self,
app: FastAPI,

View File

@ -41,15 +41,18 @@ class TestTemplateRoutes:
async def test_route_exist(self, app: FastAPI, client: AsyncClient) -> None:
new_template = {"base_script_file": "vpcs_base_config.txt",
"category": "guest",
"console_auto_start": False,
"console_type": "telnet",
"default_name_format": "PC{0}",
"name": "VPCS_TEST",
"compute_id": "local",
"symbol": ":/symbols/vpcs_guest.svg",
"template_type": "vpcs"}
new_template = {
"base_script_file": "vpcs_base_config.txt",
"category": "guest",
"console_auto_start": False,
"console_type": "telnet",
"default_name_format": "PC{0}",
"name": "VPCS_TEST",
"compute_id": "local",
"symbol": ":/symbols/vpcs_guest.svg",
"template_type": "vpcs",
"tags": ["tag1", "tag2"]
}
response = await client.post(app.url_path_for("create_template"), json=new_template)
assert response.status_code == status.HTTP_201_CREATED
@ -61,6 +64,36 @@ class TestTemplateRoutes:
assert response.status_code == status.HTTP_200_OK
assert len(response.json()) > 0
@pytest.mark.parametrize(
"tags, expected_match",
(
([], True),
(["tag1"], True),
(["tag1", "tag2"], True),
(["tag42"], False),
(["tag1", "tag3"], False),
),
)
async def test_template_list_with_tags(
self,
app: FastAPI,
client: AsyncClient,
tags: list,
expected_match: bool
) -> None:
params = {"tags": tags}
response = await client.get(app.url_path_for("get_templates"), params=params)
assert response.status_code == status.HTTP_200_OK
if expected_match:
if not tags:
assert len(response.json()) == 8
else:
assert response.json()[0]["name"] == "VPCS_TEST"
assert len(response.json()) == 1
else:
assert len(response.json()) == 0
async def test_template_get(self, app: FastAPI, client: AsyncClient) -> None:
template_id = str(uuid.uuid4())
@ -105,11 +138,14 @@ class TestTemplateRoutes:
async def test_template_update(self, app: FastAPI, client: AsyncClient) -> None:
template_id = str(uuid.uuid4())
params = {"template_id": template_id,
"name": "VPCS_TEST",
"version": "3.0",
"compute_id": "local",
"template_type": "vpcs"}
params = {
"template_id": template_id,
"name": "VPCS_TEST",
"version": "3.0",
"compute_id": "local",
"template_type": "vpcs",
"tags": ["tag1", "tag2"]
}
response = await client.post(app.url_path_for("create_template"), json=params)
assert response.status_code == status.HTTP_201_CREATED
@ -117,6 +153,7 @@ class TestTemplateRoutes:
response = await client.get(app.url_path_for("get_template", template_id=template_id))
assert response.status_code == status.HTTP_200_OK
assert response.json()["template_id"] == template_id
assert response.json()["tags"] == ["tag1", "tag2"]
params = {"name": "VPCS_TEST_RENAMED", "console_auto_start": True}
response = await client.put(app.url_path_for("update_template", template_id=template_id), json=params)

View File

@ -139,6 +139,7 @@ def test_json(node, compute):
"port_name_format": "Ethernet{0}",
"port_segment_size": 0,
"first_port_name": None,
"tags": [],
"custom_adapters": [],
"console_auto_start": False,
"ports": [
@ -176,6 +177,7 @@ def test_json(node, compute):
"port_segment_size": 0,
"first_port_name": None,
"custom_adapters": [],
"tags": [],
"console_auto_start": False,
}