mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02:00
Merge branch '3.1' into update-dependencies
This commit is contained in:
34 files changed
+1284
-334
No files matched your search
@@ -211,6 +211,34 @@ class TestDockerNodesRoutes:
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["type"] == "nio_udp"
|
||||
|
||||
async def test_docker_nio_batch_create(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
"""
|
||||
Exercise the project-wide batch NIO endpoint: bind two NIOs on the same
|
||||
docker node in a single request (the path used during project open).
|
||||
"""
|
||||
|
||||
params = {
|
||||
"nios": [
|
||||
{
|
||||
"node_id": vm["node_id"],
|
||||
"adapter_number": 0,
|
||||
"port_number": 0,
|
||||
"nio": {"type": "nio_udp", "lport": 4242, "rport": 4343, "rhost": "127.0.0.1"},
|
||||
},
|
||||
{
|
||||
"node_id": vm["node_id"],
|
||||
"adapter_number": 1,
|
||||
"port_number": 0,
|
||||
"nio": {"type": "nio_udp", "lport": 4243, "rport": 4344, "rhost": "127.0.0.1"},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
url = app.url_path_for("compute:create_batch_nios", project_id=vm["project_id"])
|
||||
response = await compute_client.post(url, json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.json()["added"] == 2
|
||||
|
||||
|
||||
async def test_docker_update_nio(self, app: FastAPI, compute_client: AsyncClient, vm: dict) -> None:
|
||||
|
||||
|
||||
@@ -224,3 +224,121 @@ 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_is_async_and_needs_await(self):
|
||||
"""
|
||||
Dynamips.create_nio is async (returns a coroutine) unlike the sync
|
||||
base version. The batch handler must await it.
|
||||
"""
|
||||
import inspect
|
||||
import asyncio as _asyncio
|
||||
|
||||
class _FakeDynamips:
|
||||
async def create_nio(self, node, nio_settings):
|
||||
return {"type": "nio_udp", "node": node}
|
||||
|
||||
class _FakeBase:
|
||||
def create_nio(self, nio_settings):
|
||||
return {"type": "nio_udp"}
|
||||
|
||||
dyn = _FakeDynamips()
|
||||
base = _FakeBase()
|
||||
assert len(inspect.signature(dyn.create_nio).parameters) == 2 # Dynamips
|
||||
assert len(inspect.signature(base.create_nio).parameters) == 1 # standard
|
||||
assert inspect.iscoroutinefunction(dyn.create_nio)
|
||||
assert not inspect.iscoroutinefunction(base.create_nio)
|
||||
|
||||
# Verify the batch logic: 2 params → await, 1 param → no await
|
||||
d_result = await dyn.create_nio("r1", {"type": "nio_udp"})
|
||||
b_result = base.create_nio({"type": "nio_udp"})
|
||||
assert d_result["node"] == "r1"
|
||||
assert b_result["type"] == "nio_udp"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_qemu_dispatch_to_adapter_add_nio_binding(self):
|
||||
"""_add_nio_binding dispatches Qemu to adapter_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__ = "Qemu"
|
||||
node.adapter_add_nio_binding = AsyncMock()
|
||||
nio = MagicMock()
|
||||
|
||||
await _add_nio_binding(node, 0, 0, nio)
|
||||
node.adapter_add_nio_binding.assert_called_once_with(0, nio)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_iou_dispatch_to_adapter_add_nio_binding(self):
|
||||
"""_add_nio_binding dispatches IOU to adapter_add_nio_binding(adapter, port, nio)."""
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from gns3server.api.routes.compute.projects import _add_nio_binding
|
||||
|
||||
node = MagicMock()
|
||||
type(node.manager).__name__ = "IOU"
|
||||
node.adapter_add_nio_binding = AsyncMock()
|
||||
nio = MagicMock()
|
||||
|
||||
await _add_nio_binding(node, 1, 2, nio)
|
||||
node.adapter_add_nio_binding.assert_called_once_with(1, 2, nio)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vpcs_dispatch_to_port_add_nio_binding(self):
|
||||
"""_add_nio_binding dispatches VPCS to port_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__ = "VPCS"
|
||||
node.port_add_nio_binding = AsyncMock()
|
||||
nio = MagicMock()
|
||||
|
||||
await _add_nio_binding(node, 0, 3, nio)
|
||||
node.port_add_nio_binding.assert_called_once_with(3, nio)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_builtin_dispatch_to_add_nio(self):
|
||||
"""_add_nio_binding dispatches Builtin nodes to add_nio(nio, port)."""
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from gns3server.api.routes.compute.projects import _add_nio_binding
|
||||
|
||||
node = MagicMock()
|
||||
type(node.manager).__name__ = "Builtin"
|
||||
node.add_nio = AsyncMock()
|
||||
nio = MagicMock()
|
||||
|
||||
await _add_nio_binding(node, 0, 0, nio)
|
||||
node.add_nio.assert_called_once_with(nio, 0)
|
||||
@@ -1209,7 +1209,7 @@ async def test_stop(vm):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
||||
vm._permissions_fixed = False
|
||||
await vm.stop()
|
||||
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
|
||||
mock_query.assert_called_with("POST", "containers/e90e34656842/kill")
|
||||
assert mock.stop.called
|
||||
assert vm._ubridge_hypervisor is None
|
||||
assert vm._fix_permissions.called
|
||||
@@ -1222,7 +1222,7 @@ async def test_stop_paused_container(vm):
|
||||
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause") as mock_unpause:
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
||||
await vm.stop()
|
||||
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
|
||||
mock_query.assert_called_with("POST", "containers/e90e34656842/kill")
|
||||
assert mock_unpause.called
|
||||
|
||||
|
||||
@@ -1442,7 +1442,7 @@ async def test_add_ubridge_connection(vm):
|
||||
call.send('docker move_to_ns tap-gns3-e0 42 eth0'),
|
||||
call.send('bridge add_nio_udp bridge0 4242 127.0.0.1 4343'),
|
||||
call.send('bridge start_capture bridge0 "/tmp/capture.pcap"'),
|
||||
call.send('bridge start bridge0')
|
||||
call.send('bridge start bridge0'),
|
||||
]
|
||||
assert 'bridge0' in vm._bridges
|
||||
# We need to check any_order otherwise mock is confused by asyncio
|
||||
@@ -1894,7 +1894,7 @@ async def test_stop_exited_container_no_stop_query(vm):
|
||||
vm._permissions_fixed = False
|
||||
await vm.stop()
|
||||
assert not any(
|
||||
call.args[:2] == ("POST", "containers/e90e34656842/stop")
|
||||
call.args[:2] == ("POST", "containers/e90e34656842/kill")
|
||||
for call in mock_query.mock_calls
|
||||
)
|
||||
assert vm.status == "stopped"
|
||||
@@ -367,6 +367,79 @@ async def test_apply_markers_skips_already_installed(compute_project, manager):
|
||||
assert not any("add_packet_filter" in c for c in cmds) # skipped, not re-added
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_apply_markers_deletes_removed(compute_project, manager):
|
||||
# Reconcile: a marker no longer in nio.markers is deleted from uBridge
|
||||
# (filter + registry), not left as an orphan still matching packets.
|
||||
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
||||
node._ubridge_send = AsyncioMagicMock()
|
||||
node._ubridge_hypervisor = MagicMock()
|
||||
node._ubridge_hypervisor.is_running.return_value = True
|
||||
node._marker_filter_bridges["m", "L1"] = "VPCS-10"
|
||||
node._marker_specs["m", "L1"] = {"bpf": "icmp", "enabled": True}
|
||||
nio = NIOUDP(1234, "127.0.0.1", 4321)
|
||||
nio.markers = {} # marker removed
|
||||
with patch("gns3server.compute.marker.marker_manager.MarkerManager") as mm:
|
||||
mm.instance.return_value.unregister = MagicMock()
|
||||
await node._ubridge_apply_markers("VPCS-10", nio)
|
||||
cmds = [c.args[0] for c in node._ubridge_send.call_args_list]
|
||||
assert any("delete_packet_filter VPCS-10 m" in c for c in cmds)
|
||||
assert ("m", "L1") not in node._marker_filter_bridges
|
||||
assert ("m", "L1") not in node._marker_specs
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_apply_markers_rebuilds_changed_bpf(compute_project, manager):
|
||||
# Reconcile: a marker whose bpf changed is rebuilt (delete + re-add), so
|
||||
# uBridge ends up with the new expression — not the stale original.
|
||||
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
||||
node._ubridge_send = AsyncioMagicMock()
|
||||
node._ubridge_hypervisor = MagicMock()
|
||||
node._ubridge_hypervisor.is_running.return_value = True
|
||||
node._marker_filter_bridges["m", "L1"] = "VPCS-10"
|
||||
node._marker_specs["m", "L1"] = {"bpf": "icmp", "tag": None, "direction": None,
|
||||
"data_link_type": None, "enabled": True}
|
||||
nio = NIOUDP(1234, "127.0.0.1", 4321)
|
||||
nio.markers = {"m": {"bpf": "tcp", "tag": None, "link_id": "L1",
|
||||
"direction": None, "enabled": True}}
|
||||
with patch("gns3server.compute.marker.marker_manager.MarkerManager") as mm:
|
||||
mm.instance.return_value.register = MagicMock()
|
||||
await node._ubridge_apply_markers("VPCS-10", nio)
|
||||
cmds = [c.args[0] for c in node._ubridge_send.call_args_list]
|
||||
assert any("delete_packet_filter VPCS-10 m" in c for c in cmds) # old removed
|
||||
assert any("add_packet_filter VPCS-10 m mark" in c and "tcp" in c for c in cmds) # new added
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_apply_markers_preserves_markers_on_other_bridges(compute_project, manager):
|
||||
# Regression: reconciling one NIO must not delete markers installed on this
|
||||
# node's OTHER bridges/NIOs. _marker_filter_bridges is node-wide, but
|
||||
# `desired` only carries the current NIO's markers — the delete pass must be
|
||||
# scoped to the current bridge, or updating one link wipes every other link's
|
||||
# markers + pcaps.
|
||||
node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
||||
node._ubridge_send = AsyncioMagicMock()
|
||||
node._ubridge_hypervisor = MagicMock()
|
||||
node._ubridge_hypervisor.is_running.return_value = True
|
||||
# Two links, each with a marker on its own bridge:
|
||||
node._marker_filter_bridges["m", "L1"] = "VPCS-10"
|
||||
node._marker_specs["m", "L1"] = {"bpf": "icmp", "enabled": True}
|
||||
node._marker_filter_bridges["m", "L2"] = "VPCS-20"
|
||||
node._marker_specs["m", "L2"] = {"bpf": "tcp", "enabled": True}
|
||||
# Update only the VPCS-10 NIO; "m" is gone from this link — but the marker
|
||||
# on VPCS-20 (L2) must survive untouched.
|
||||
nio = NIOUDP(1234, "127.0.0.1", 4321)
|
||||
nio.markers = {}
|
||||
with patch("gns3server.compute.marker.marker_manager.MarkerManager") as mm:
|
||||
mm.instance.return_value.unregister = MagicMock()
|
||||
await node._ubridge_apply_markers("VPCS-10", nio)
|
||||
cmds = [c.args[0] for c in node._ubridge_send.call_args_list]
|
||||
assert any("delete_packet_filter VPCS-10 m" in c for c in cmds) # L1 removed on its bridge
|
||||
assert not any("VPCS-20" in c for c in cmds) # other bridge untouched
|
||||
assert ("m", "L1") not in node._marker_filter_bridges
|
||||
assert ("m", "L2") in node._marker_filter_bridges # L2 preserved
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop_ubridge_clears_marker_bridges(compute_project, manager):
|
||||
# uBridge stopping drops every marker filter — the map must clear so the next
|
||||
|
||||
@@ -120,6 +120,33 @@ async def test_dispatch_node_updated(controller, node, project):
|
||||
assert event["properties"]["startup_config"] == "ip 192"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dispatch_marker_routed_to_marker_channel(controller, project):
|
||||
"""
|
||||
marker.* events are dispatched to the dedicated marker channel, not the
|
||||
main project queue, so high-frequency matches cannot block topology events.
|
||||
"""
|
||||
|
||||
notif = controller.notification
|
||||
with notif.project_queue(project.id) as project_q, \
|
||||
notif.project_marker_queue(project.id) as marker_q:
|
||||
assert len(notif._project_marker_listeners[project.id]) == 1
|
||||
await project_q.get(0.1) # consume initial ping
|
||||
await marker_q.get(0.1) # consume initial ping
|
||||
|
||||
await notif.dispatch("marker.match", {"link_id": "abc"},
|
||||
project_id=project.id, compute_id=1)
|
||||
|
||||
# marker.match lands on the marker channel...
|
||||
msg = await marker_q.get(5)
|
||||
assert msg == ('marker.match', {"link_id": "abc"}, {})
|
||||
# ...and does NOT land on the main project queue (times out -> ping)
|
||||
msg = await project_q.get(0.1)
|
||||
assert msg[0] == "ping"
|
||||
|
||||
assert len(notif._project_marker_listeners[project.id]) == 0
|
||||
|
||||
|
||||
def test_various_notification(controller, node):
|
||||
|
||||
notif = controller.notification
|
||||
|
||||
Reference in new issue
Block a user