fix: Dynamips create_nio(node, nio_settings) takes extra arg + add tests

Dynamips.create_nio requires the node as first positional argument
(unlike every other manager which takes only nio_settings). The batch
handler now detects this via parameter-count inspection (3 vs 2) and
passes node when needed.

Also add Dynamips to _add_nio_binding dispatch: routers use
slot_add_nio_binding(slot, port, nio), switches/hubs fall back to
add_nio(nio, port_number).

Add tests covering Dynamips router dispatch, switch dispatch, and the
create_nio signature detection to prevent regression.
This commit is contained in:
YueGuobin committed 2026-08-11 00:54:53 +08:00
1 parent c366f20340
commit ed0f1bb4de
2 files changed
+70 -1

No files matched your search

+54
View File
@@ -224,3 +224,57 @@ class TestComputeProjectRoutes:
project_id=project.id,
file_path=file_path), content=b"world")
assert response.status_code == status.HTTP_403_FORBIDDEN
class TestBatchNIOEdgeCases:
@pytest.mark.asyncio
async def test_dynamips_router_dispatch_to_slot_add_nio_binding(self):
"""_add_nio_binding dispatches Dynamips router to slot_add_nio_binding."""
from unittest.mock import AsyncMock, MagicMock
from gns3server.api.routes.compute.projects import _add_nio_binding
node = MagicMock()
type(node.manager).__name__ = "Dynamips"
node.slot_add_nio_binding = AsyncMock()
nio = MagicMock()
await _add_nio_binding(node, 0, 0, nio)
node.slot_add_nio_binding.assert_called_once_with(0, 0, nio)
@pytest.mark.asyncio
async def test_dynamips_switch_dispatch_to_add_nio(self):
"""_add_nio_binding dispatches Dynamips switch to add_nio."""
from unittest.mock import AsyncMock, MagicMock
from gns3server.api.routes.compute.projects import _add_nio_binding
node = MagicMock()
type(node.manager).__name__ = "Dynamips"
del node.slot_add_nio_binding # no slot_add_nio → switch path
node.add_nio = AsyncMock()
nio = MagicMock()
await _add_nio_binding(node, 0, 0, nio)
node.add_nio.assert_called_once_with(nio, 0)
@pytest.mark.asyncio
async def test_dynamips_create_nio_passes_node_arg(self):
"""
Dynamips.create_nio(self, node, nio_settings) takes an extra 'node'
positional; the batch handler detects this via parameter count.
"""
import inspect
# Dynamips-style 3-param signature (self + node + nio_settings)
async def create_nio_with_node(self, node, nio_settings):
pass
sig = inspect.signature(create_nio_with_node)
assert len(sig.parameters) == 3
# Standard base 2-param signature (self + nio_settings)
async def create_nio_base(self, nio_settings):
pass
sig2 = inspect.signature(create_nio_base)
assert len(sig2.parameters) == 2