From 4d35477e4651983f509a25053b1cb99ade5e9fce Mon Sep 17 00:00:00 2001 From: grossmj Date: Mon, 23 Feb 2026 23:27:36 +0800 Subject: [PATCH] Add/fix tests --- tests/api/routes/controller/test_nodes.py | 60 ++++++++++++++++- tests/api/routes/controller/test_templates.py | 65 +++++++++++++++---- tests/controller/test_node.py | 2 + 3 files changed, 110 insertions(+), 17 deletions(-) diff --git a/tests/api/routes/controller/test_nodes.py b/tests/api/routes/controller/test_nodes.py index 46f42e4af..32bc80918 100644 --- a/tests/api/routes/controller/test_nodes.py +++ b/tests/api/routes/controller/test_nodes.py @@ -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, diff --git a/tests/api/routes/controller/test_templates.py b/tests/api/routes/controller/test_templates.py index 25ce4922d..38f01fb59 100644 --- a/tests/api/routes/controller/test_templates.py +++ b/tests/api/routes/controller/test_templates.py @@ -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) diff --git a/tests/controller/test_node.py b/tests/controller/test_node.py index 2192444b2..27a02d4d7 100644 --- a/tests/controller/test_node.py +++ b/tests/controller/test_node.py @@ -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, }