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

@ -88,6 +88,58 @@ class TestNodeRoutes:
assert response.json()[0]["name"] == "test" 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( async def test_get_node(
self, self,
app: FastAPI, app: FastAPI,
@ -131,6 +183,7 @@ class TestNodeRoutes:
"name": "test", "name": "test",
"node_type": "vpcs", "node_type": "vpcs",
"compute_id": "example.com", "compute_id": "example.com",
"tags": ["tag1", "tag2"],
"properties": { "properties": {
"startup_script": "echo test" "startup_script": "echo test"
} }
@ -139,6 +192,7 @@ class TestNodeRoutes:
assert response.status_code == 200 assert response.status_code == 200
assert response.json()["name"] == "test" assert response.json()["name"] == "test"
assert "name" not in response.json()["properties"] assert "name" not in response.json()["properties"]
assert response.json()["tags"] == ["tag1", "tag2"]
async def test_start_all_nodes( async def test_start_all_nodes(

View File

@ -41,15 +41,18 @@ class TestTemplateRoutes:
async def test_route_exist(self, app: FastAPI, client: AsyncClient) -> None: async def test_route_exist(self, app: FastAPI, client: AsyncClient) -> None:
new_template = {"base_script_file": "vpcs_base_config.txt", new_template = {
"category": "guest", "base_script_file": "vpcs_base_config.txt",
"console_auto_start": False, "category": "guest",
"console_type": "telnet", "console_auto_start": False,
"default_name_format": "PC{0}", "console_type": "telnet",
"name": "VPCS_TEST", "default_name_format": "PC{0}",
"compute_id": "local", "name": "VPCS_TEST",
"symbol": ":/symbols/vpcs_guest.svg", "compute_id": "local",
"template_type": "vpcs"} "symbol": ":/symbols/vpcs_guest.svg",
"template_type": "vpcs",
"tags": ["tag1", "tag2"]
}
response = await client.post(app.url_path_for("create_template"), json=new_template) response = await client.post(app.url_path_for("create_template"), json=new_template)
assert response.status_code == status.HTTP_201_CREATED assert response.status_code == status.HTTP_201_CREATED
@ -61,6 +64,36 @@ class TestTemplateRoutes:
assert response.status_code == status.HTTP_200_OK assert response.status_code == status.HTTP_200_OK
assert len(response.json()) > 0 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: async def test_template_get(self, app: FastAPI, client: AsyncClient) -> None:
template_id = str(uuid.uuid4()) template_id = str(uuid.uuid4())
@ -105,11 +138,14 @@ class TestTemplateRoutes:
async def test_template_update(self, app: FastAPI, client: AsyncClient) -> None: async def test_template_update(self, app: FastAPI, client: AsyncClient) -> None:
template_id = str(uuid.uuid4()) template_id = str(uuid.uuid4())
params = {"template_id": template_id, params = {
"name": "VPCS_TEST", "template_id": template_id,
"version": "3.0", "name": "VPCS_TEST",
"compute_id": "local", "version": "3.0",
"template_type": "vpcs"} "compute_id": "local",
"template_type": "vpcs",
"tags": ["tag1", "tag2"]
}
response = await client.post(app.url_path_for("create_template"), json=params) response = await client.post(app.url_path_for("create_template"), json=params)
assert response.status_code == status.HTTP_201_CREATED 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)) response = await client.get(app.url_path_for("get_template", template_id=template_id))
assert response.status_code == status.HTTP_200_OK assert response.status_code == status.HTTP_200_OK
assert response.json()["template_id"] == template_id assert response.json()["template_id"] == template_id
assert response.json()["tags"] == ["tag1", "tag2"]
params = {"name": "VPCS_TEST_RENAMED", "console_auto_start": True} 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) 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_name_format": "Ethernet{0}",
"port_segment_size": 0, "port_segment_size": 0,
"first_port_name": None, "first_port_name": None,
"tags": [],
"custom_adapters": [], "custom_adapters": [],
"console_auto_start": False, "console_auto_start": False,
"ports": [ "ports": [
@ -176,6 +177,7 @@ def test_json(node, compute):
"port_segment_size": 0, "port_segment_size": 0,
"first_port_name": None, "first_port_name": None,
"custom_adapters": [], "custom_adapters": [],
"tags": [],
"console_auto_start": False, "console_auto_start": False,
} }