mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-22 14:05:19 +02:00
Merge branch '3.1' into 3.1
This commit is contained in:
51 files changed
+3155
-183
No files matched your search
@@ -45,6 +45,7 @@ class TestCapabilitiesRoutes:
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vpcs_tap", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", _no_tap_probe)
|
||||
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json() == {
|
||||
@@ -73,6 +74,7 @@ class TestCapabilitiesRoutes:
|
||||
"ubridge_iol_tap": None,
|
||||
"ubridge_bridge_tap": None,
|
||||
"vpcs_tap": None,
|
||||
"ubridge_vxlan": None,
|
||||
}
|
||||
|
||||
async def test_get_on_gns3vm(self, app: FastAPI, compute_client: AsyncClient, on_gns3vm, monkeypatch) -> None:
|
||||
@@ -85,6 +87,7 @@ class TestCapabilitiesRoutes:
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vpcs_tap", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", _no_tap_probe)
|
||||
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json() == {
|
||||
@@ -113,6 +116,7 @@ class TestCapabilitiesRoutes:
|
||||
"ubridge_iol_tap": None,
|
||||
"ubridge_bridge_tap": None,
|
||||
"vpcs_tap": None,
|
||||
"ubridge_vxlan": None,
|
||||
}
|
||||
|
||||
async def test_get_reports_ubridge_tc(self, app: FastAPI, compute_client: AsyncClient, monkeypatch) -> None:
|
||||
@@ -199,6 +203,25 @@ class TestCapabilitiesRoutes:
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", _no_tap_probe)
|
||||
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["vpcs_tap"] is True
|
||||
|
||||
async def test_get_reports_ubridge_vxlan(self, app: FastAPI, compute_client: AsyncClient, monkeypatch) -> None:
|
||||
"""
|
||||
The vxlan capability is what makes a cross-compute link tunnel-
|
||||
eligible, so it travels with the other compute capabilities.
|
||||
"""
|
||||
|
||||
async def fake_vxlan_probe():
|
||||
return True
|
||||
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vxlan_support", fake_vxlan_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_iol_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_bridge_tap_support", _no_tap_probe)
|
||||
monkeypatch.setattr("gns3server.api.routes.compute.capabilities.probe_vpcs_tap", _no_tap_probe)
|
||||
response = await compute_client.get(app.url_path_for("compute:get_capabilities"))
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["ubridge_vxlan"] is True
|
||||
@@ -19,6 +19,7 @@ import pytest_asyncio
|
||||
from fastapi import FastAPI, status
|
||||
from httpx import AsyncClient
|
||||
|
||||
from gns3server.compute.builtin import Builtin
|
||||
from gns3server.compute.project import Project
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
@@ -212,3 +213,36 @@ class TestCloudNodesRoutes:
|
||||
# response = await compute_api.get("/projects/{project_id}/cloud/nodes/{node_id}/adapters/0/ports/0/pcap".format(project_id=compute_project.id, node_id=vm["node_id"]))
|
||||
# assert response.status_code == 200
|
||||
#
|
||||
|
||||
async def test_unbridged_connected_tap_update(self, app, compute_client, compute_project):
|
||||
ports = [{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap"}]
|
||||
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._start_ubridge"):
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_cloud", project_id=compute_project.id),
|
||||
json={"name": "Cloud TAP", "ports_mapping": ports},
|
||||
)
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
node = Builtin.instance().get_node(data["node_id"], project_id=compute_project.id)
|
||||
node.nios[0] = object()
|
||||
response = await compute_client.put(
|
||||
app.url_path_for("compute:update_cloud", project_id=compute_project.id, node_id=node.id),
|
||||
json={"usage": "Updated usage", "ports_mapping": data["ports_mapping"]},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["usage"] == "Updated usage"
|
||||
|
||||
@pytest.mark.parametrize("name", ["tap-interface-too-long", "tap bad", "/dev/tap0"])
|
||||
async def test_invalid_bridged_tap_rejected_before_save(self, app, compute_client, compute_project, name):
|
||||
with asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._start_ubridge") as start:
|
||||
response = await compute_client.post(
|
||||
app.url_path_for("compute:create_cloud", project_id=compute_project.id),
|
||||
json={
|
||||
"name": "Invalid TAP",
|
||||
"ports_mapping": [
|
||||
{"interface": name, "name": name, "port_number": 0, "type": "tap", "bridge": "virbr0"}
|
||||
],
|
||||
},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
start.assert_not_called()
|
||||
@@ -370,6 +370,36 @@ class TestQemuNodesRoutes:
|
||||
assert response.json()["hdb_disk_image"] == "linux载.img"
|
||||
assert response.json()["ram"] == 1024
|
||||
|
||||
async def test_cdrom_interfaces_update_and_read_back(
|
||||
self, app: FastAPI, compute_client: AsyncClient, qemu_vm: dict
|
||||
) -> None:
|
||||
url = app.url_path_for("compute:update_qemu_node", project_id=qemu_vm["project_id"], node_id=qemu_vm["node_id"])
|
||||
response = await compute_client.put(url, json={"cdrom_interface": "sata", "cdrom2_interface": "sata"})
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["cdrom_interface"] == "sata"
|
||||
assert response.json()["cdrom2_interface"] == "sata"
|
||||
response = await compute_client.get(url)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert response.json()["cdrom_interface"] == "sata"
|
||||
assert response.json()["cdrom2_interface"] == "sata"
|
||||
|
||||
@pytest.mark.parametrize("name", ["cdrom_interface", "cdrom2_interface"])
|
||||
async def test_cdrom_interface_rejection_does_not_partially_update(self, app, compute_client, qemu_vm, name):
|
||||
url = app.url_path_for("compute:update_qemu_node", project_id=qemu_vm["project_id"], node_id=qemu_vm["node_id"])
|
||||
original = (await compute_client.get(url)).json()
|
||||
with patch("gns3server.compute.qemu.qemu_vm.QemuVM.is_running", return_value=True):
|
||||
response = await compute_client.put(url, json={"ram": original["ram"] + 128, name: "sata"})
|
||||
assert response.status_code == status.HTTP_409_CONFLICT
|
||||
saved = (await compute_client.get(url)).json()
|
||||
assert saved["ram"] == original["ram"]
|
||||
assert saved[name] == original[name]
|
||||
|
||||
@pytest.mark.parametrize("name", ["cdrom_interface", "cdrom2_interface"])
|
||||
async def test_cdrom_interface_rejects_null(self, app, compute_client, qemu_vm, name):
|
||||
url = app.url_path_for("compute:update_qemu_node", project_id=qemu_vm["project_id"], node_id=qemu_vm["node_id"])
|
||||
response = await compute_client.put(url, json={name: None})
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
|
||||
|
||||
async def test_qemu_nio_create_udp(self, app: FastAPI, compute_client: AsyncClient, qemu_vm: dict) -> None:
|
||||
|
||||
params = {"type": "nio_udp", "lport": 4242, "rport": 4343, "rhost": "127.0.0.1"}
|
||||
|
||||
@@ -476,3 +476,40 @@ class TestImageRoutes:
|
||||
assert len(templates) == 1
|
||||
assert templates[0].name == "Empty VM"
|
||||
await templates_repo.delete_template(templates[0].template_id)
|
||||
|
||||
|
||||
class TestIsoImages:
|
||||
async def test_iso_uploads_can_be_used_together_in_a_template(self, app, client, config, images_dir):
|
||||
config.settings.Server.allow_raw_images = False
|
||||
references = []
|
||||
for index, name in enumerate(("installer.ISO", "drivers.iso")):
|
||||
content = bytes(16 * 2048) + b"\x01CD001\x01" + bytes([index]) * 2041
|
||||
response = await client.post(
|
||||
app.url_path_for("upload_image", image_path=name),
|
||||
params={"subdirectory": "installers"} if index == 0 else {},
|
||||
content=content,
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED, response.text
|
||||
assert response.json()["image_type"] == "qemu"
|
||||
relative = "installers/" + name if index == 0 else name
|
||||
assert response.json()["path"] == os.path.join(images_dir, "QEMU", relative)
|
||||
assert response.json()["reference"] == relative
|
||||
assert Qemu.instance().get_abs_image_path(relative) == response.json()["path"]
|
||||
references.append(relative)
|
||||
|
||||
response = await client.post(
|
||||
app.url_path_for("create_template"),
|
||||
json={
|
||||
"name": "Windows installer with drivers",
|
||||
"template_type": "qemu",
|
||||
"compute_id": "local",
|
||||
"cdrom_image": references[0],
|
||||
"cdrom2_image": references[1],
|
||||
},
|
||||
)
|
||||
assert response.status_code == status.HTTP_201_CREATED, response.text
|
||||
template_id = response.json()["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.json()["cdrom_image"] == references[0]
|
||||
assert response.json()["cdrom2_image"] == references[1]
|
||||
@@ -458,7 +458,14 @@ class TestReplayFrameDetail:
|
||||
"end": None,
|
||||
"frame_count": 0,
|
||||
"sources": [
|
||||
{"node_id": "n1", "link_id": link.id, "marker": "icmp", "data_link_type": "DLT_EN10MB", "count": 0}
|
||||
{
|
||||
"node_id": "n1",
|
||||
"link_id": link.id,
|
||||
"marker": "icmp",
|
||||
"data_link_type": "DLT_EN10MB",
|
||||
"count": 0,
|
||||
"error": None,
|
||||
}
|
||||
],
|
||||
"frames": [],
|
||||
}
|
||||
@@ -1112,6 +1112,28 @@ class TestDockerTemplate:
|
||||
|
||||
|
||||
class TestQemuTemplate:
|
||||
async def test_cdrom_interfaces_persist_on_template_update(self, app, client):
|
||||
params = {
|
||||
"name": "SATA installer",
|
||||
"compute_id": "local",
|
||||
"template_type": "qemu",
|
||||
"cdrom_interface": "sata",
|
||||
"cdrom2_interface": "sata",
|
||||
}
|
||||
response = await client.post(app.url_path_for("create_template"), json=params)
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
template_id = response.json()["template_id"]
|
||||
url = app.url_path_for("get_template", template_id=template_id)
|
||||
saved = (await client.get(url)).json()
|
||||
assert saved["cdrom_interface"] == saved["cdrom2_interface"] == "sata"
|
||||
response = await client.put(
|
||||
app.url_path_for("update_template", template_id=template_id), json={"cdrom_interface": "ide"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
saved = (await client.get(url)).json()
|
||||
assert saved["cdrom_interface"] == "ide"
|
||||
assert saved["cdrom2_interface"] == "sata"
|
||||
|
||||
async def test_qemu_template_create(self, app: FastAPI, client: AsyncClient) -> None:
|
||||
|
||||
params = {
|
||||
|
||||
@@ -15,19 +15,34 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import struct
|
||||
import uuid
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from pydantic import ValidationError
|
||||
|
||||
from gns3server.compute.base_manager import BaseManager
|
||||
from gns3server.compute.builtin.nodes.cloud import Cloud
|
||||
from gns3server.compute.error import NodeError
|
||||
from gns3server.compute.nios.nio_udp import NIOUDP
|
||||
from gns3server.compute.ubridge.ubridge_error import UbridgeError
|
||||
from gns3server.schemas.compute.cloud_nodes import Cloud as CloudSchema
|
||||
from gns3server.schemas.compute.cloud_nodes import CloudUpdate, TAPPort
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_bridge_detection():
|
||||
# Interface inventories are mocked: never mix them with the host's sysfs.
|
||||
with patch("gns3server.utils.interfaces.is_interface_bridge", return_value=False):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def nio():
|
||||
|
||||
@@ -256,3 +271,142 @@ async def test_linux_ethernet_raw_add_nio_bridge(linux_platform, compute_project
|
||||
call(f"bridge start {cloud._id}-0"),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bridge", [None, "virbr0"])
|
||||
@pytest.mark.parametrize("existing_master", [False, True])
|
||||
async def test_tap_bridge_recreated(linux_platform, compute_project, manager, nio, bridge, existing_master):
|
||||
ports = [{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap"}]
|
||||
if bridge:
|
||||
ports[0]["bridge"] = bridge
|
||||
cloud = Cloud("cloud1", str(uuid.uuid4()), compute_project, manager, ports=ports)
|
||||
# Round-trip through the API schema, as happens when a project is restored.
|
||||
restored_ports = CloudSchema(**cloud.asdict()).model_dump()["ports_mapping"]
|
||||
restored = Cloud("cloud1", cloud.id, compute_project, manager, ports=restored_ports)
|
||||
with (
|
||||
asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._ubridge_send") as send,
|
||||
asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._start_ubridge"),
|
||||
patch.object(restored, "_interfaces", return_value=[{"name": "virbr0"}]),
|
||||
patch("gns3server.utils.interfaces.is_interface_bridge", return_value=True),
|
||||
patch("gns3server.compute.builtin.nodes.cloud.os.path.exists", return_value=existing_master),
|
||||
patch("gns3server.compute.builtin.nodes.cloud.os.path.realpath", return_value="/sys/class/net/virbr0"),
|
||||
):
|
||||
restored._nios[0] = nio
|
||||
await restored.start()
|
||||
tap_call = call(f'bridge add_nio_tap {cloud.id}-0 "tap0"')
|
||||
assert tap_call in send.call_args_list
|
||||
attach_call = call('brctl addif "virbr0" "tap0"')
|
||||
if bridge:
|
||||
assert send.call_args_list.index(tap_call) < send.call_args_list.index(attach_call)
|
||||
else:
|
||||
assert attach_call not in send.call_args_list
|
||||
|
||||
|
||||
@pytest.mark.parametrize("failure", ["missing", "not_bridge", "other_master", "invalid_name", "non_linux"])
|
||||
async def test_tap_bridge_validation(linux_platform, compute_project, manager, nio, failure):
|
||||
ports = [{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap", "bridge": "virbr0"}]
|
||||
if failure == "invalid_name":
|
||||
ports[0]["interface"] = 'tap0"\nbridge delete all'
|
||||
cloud = Cloud("cloud1", str(uuid.uuid4()), compute_project, manager, ports=ports)
|
||||
with (
|
||||
asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._ubridge_send") as send,
|
||||
patch.object(cloud, "_interfaces", return_value=[] if failure == "missing" else [{"name": "virbr0"}]),
|
||||
patch("gns3server.utils.interfaces.is_interface_bridge", return_value=failure != "not_bridge"),
|
||||
patch("gns3server.compute.builtin.nodes.cloud.os.path.exists", return_value=failure == "other_master"),
|
||||
patch("gns3server.compute.builtin.nodes.cloud.os.path.realpath", return_value="/sys/class/net/another0"),
|
||||
patch("gns3server.compute.builtin.nodes.cloud.sys.platform", "darwin" if failure == "non_linux" else "linux"),
|
||||
):
|
||||
with pytest.raises(NodeError):
|
||||
await cloud._add_ubridge_connection(nio, 0)
|
||||
assert not any("add_nio_tap" in c.args[0] or "brctl addif" in c.args[0] for c in send.call_args_list)
|
||||
|
||||
|
||||
async def test_tap_bridge_attachment_failure(linux_platform, compute_project, manager, nio):
|
||||
ports = [{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap", "bridge": "virbr0"}]
|
||||
cloud = Cloud("cloud1", str(uuid.uuid4()), compute_project, manager, ports=ports)
|
||||
cloud.status = "started"
|
||||
|
||||
async def send(command):
|
||||
if command.startswith("brctl addif"):
|
||||
raise UbridgeError("Operation not permitted")
|
||||
|
||||
with (
|
||||
asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._ubridge_send", side_effect=send),
|
||||
asyncio_patch("gns3server.compute.builtin.nodes.cloud.Cloud._stop_ubridge") as stop,
|
||||
patch.object(cloud, "_interfaces", return_value=[{"name": "virbr0"}]),
|
||||
patch("gns3server.utils.interfaces.is_interface_bridge", return_value=True),
|
||||
patch("gns3server.compute.builtin.nodes.cloud.os.path.exists", return_value=False),
|
||||
):
|
||||
await cloud.add_nio(nio, 0)
|
||||
stop.assert_called_once()
|
||||
assert cloud.status == "stopped"
|
||||
|
||||
|
||||
async def test_cloud_lists_linux_bridges(linux_platform, compute_project, manager):
|
||||
cloud = Cloud(
|
||||
"cloud1",
|
||||
str(uuid.uuid4()),
|
||||
compute_project,
|
||||
manager,
|
||||
ports=[{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap"}],
|
||||
)
|
||||
with (
|
||||
patch(
|
||||
"gns3server.utils.interfaces.interfaces",
|
||||
return_value=[
|
||||
{"name": "eth0", "type": "ethernet", "special": False},
|
||||
{"name": "virbr0", "type": "ethernet", "special": True},
|
||||
],
|
||||
),
|
||||
patch("gns3server.utils.interfaces.is_interface_bridge", side_effect=lambda name: name == "virbr0"),
|
||||
):
|
||||
interfaces = CloudSchema(**cloud.asdict()).model_dump()["interfaces"]
|
||||
assert [(interface["name"], interface["is_bridge"]) for interface in interfaces] == [
|
||||
("eth0", False),
|
||||
("virbr0", True),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("stored_bridge", [None, "omitted"])
|
||||
async def test_connected_tap_mapping_round_trip(compute_project, manager, stored_bridge):
|
||||
port = {"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap"}
|
||||
if stored_bridge is None:
|
||||
port["bridge"] = None
|
||||
cloud = Cloud("cloud1", str(uuid.uuid4()), compute_project, manager, ports=[port])
|
||||
response = CloudSchema(**cloud.asdict()).model_dump(mode="json")
|
||||
cloud._nios[0] = object()
|
||||
ports = jsonable_encoder(CloudUpdate(ports_mapping=response["ports_mapping"]), exclude_unset=True)["ports_mapping"]
|
||||
cloud.ports_mapping = ports
|
||||
assert cloud.ports_mapping == [{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap"}]
|
||||
ports[0]["bridge"] = "virbr0"
|
||||
with pytest.raises(NodeError, match="already connected"):
|
||||
cloud.ports_mapping = ports
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["tap-interface-too-long", "tap bad", 'tap0"\nbridge delete all', "/dev/tap0"])
|
||||
async def test_bridged_tap_name_validation_preserves_legacy_names(name):
|
||||
port = {"interface": name, "name": name, "port_number": 0, "type": "tap"}
|
||||
assert TAPPort(**port).interface == name
|
||||
with pytest.raises(ValidationError, match="bridged TAP name"):
|
||||
TAPPort(**port, bridge="virbr0")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"caps,legacy_allowed,bridge_allowed",
|
||||
[
|
||||
(0, False, False),
|
||||
(1 << 12, False, False),
|
||||
(1 << 13, True, False),
|
||||
((1 << 12) | (1 << 13), True, True),
|
||||
],
|
||||
)
|
||||
async def test_bridged_tap_requires_net_admin(linux_platform, tmp_path, caps, legacy_allowed, bridge_allowed):
|
||||
executable = tmp_path / "ubridge"
|
||||
executable.write_text("")
|
||||
with (
|
||||
patch("os.geteuid", return_value=1000),
|
||||
patch("os.listxattr", return_value=["security.capability"]),
|
||||
patch("os.getxattr", return_value=struct.pack("<IIIII", 0x2000001, caps, 0, 0, 0)),
|
||||
):
|
||||
assert BaseManager.has_privileged_access(executable) is legacy_allowed
|
||||
assert BaseManager.has_privileged_access(executable, require_net_admin=True) is bridge_allowed
|
||||
@@ -22,6 +22,7 @@ fallback and cleanup.
|
||||
"""
|
||||
|
||||
import os
|
||||
import socket
|
||||
import uuid
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
@@ -32,6 +33,7 @@ from gns3server.compute.compute_error import ComputeError
|
||||
from gns3server.compute.docker import Docker
|
||||
from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error
|
||||
from gns3server.compute.docker.docker_vm import DockerVM
|
||||
from gns3server.compute.error import NodeError
|
||||
from gns3server.compute.nios.nio_bridge import NIOBridge
|
||||
from gns3server.compute.ubridge.ubridge_error import UbridgeError, UbridgeNamespaceError
|
||||
from tests.utils import AsyncioMagicMock
|
||||
@@ -1468,3 +1470,147 @@ async def test_stop_clears_relay_bridge_registry(vm):
|
||||
await vm.stop()
|
||||
assert vm._bridges == set()
|
||||
assert vm._ubridge_tc_caps is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Cross-compute VXLAN tunnel
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
VXLAN_SPEC = {"name": "gvl01234567abcd", "vni": 1048577, "remote": "192.168.1.2", "dstport": 4789}
|
||||
|
||||
|
||||
def test_create_nio_bridge_carries_vxlan_spec(vm):
|
||||
"""
|
||||
The vxlan sub-spec of a cross-compute NIO survives the factory and the
|
||||
asdict round-trip (the node replays the NIO from asdict on restart).
|
||||
"""
|
||||
|
||||
nio = vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
|
||||
assert isinstance(nio, NIOBridge)
|
||||
assert nio.vxlan == VXLAN_SPEC
|
||||
assert nio.asdict()["vxlan"] == VXLAN_SPEC
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_ubridge_kernel_connection_with_vxlan(vm):
|
||||
"""
|
||||
A vxlan-carrying NIO creates this end's tunnel device (lower device
|
||||
resolved locally from the peer address) and enslaves it into the link's
|
||||
kernel bridge next to the anchor.
|
||||
"""
|
||||
|
||||
async def send(command):
|
||||
if command.startswith("vxlan show"):
|
||||
raise UbridgeError("VXLAN does not exist")
|
||||
|
||||
vm._ubridge_hypervisor = MagicMock()
|
||||
vm._ubridge_hypervisor.send = AsyncioMagicMock(side_effect=send)
|
||||
vm._namespace = 42
|
||||
nio = vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
|
||||
|
||||
with patch.object(DockerVM, "_vxlan_lowerdev", return_value="eth0"):
|
||||
await vm._add_ubridge_connection(nio, 0)
|
||||
|
||||
host_ifc, _ = vm._veth_names(0, 0)
|
||||
sends = [c.args[0] for c in vm._ubridge_hypervisor.send.call_args_list]
|
||||
show = f'vxlan show "{VXLAN_SPEC["name"]}"'
|
||||
create = (
|
||||
f'vxlan create "{VXLAN_SPEC["name"]}" {VXLAN_SPEC["vni"]} '
|
||||
f"remote={VXLAN_SPEC['remote']} dstport={VXLAN_SPEC['dstport']} dev=eth0"
|
||||
)
|
||||
assert sends.index(show) < sends.index(create)
|
||||
# The tunnel device is enslaved after the node's anchor, and the anchor
|
||||
# still goes up.
|
||||
assert sends.index(f'brctl addif "{BRIDGE}" "{host_ifc}"') < sends.index(
|
||||
f'brctl addif "{BRIDGE}" "{VXLAN_SPEC["name"]}"'
|
||||
)
|
||||
assert f'link set "{host_ifc}" up' in sends
|
||||
# A failed show (device absent) must not trigger a delete.
|
||||
assert f'vxlan delete "{VXLAN_SPEC["name"]}"' not in sends
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_ubridge_kernel_connection_replaces_vxlan_leftover(vm):
|
||||
"""
|
||||
A device found under this link's deterministic name is this link's own
|
||||
leftover (a crashed run, a node restart): deleted and re-created, which
|
||||
makes wiring idempotent.
|
||||
"""
|
||||
|
||||
vm._ubridge_hypervisor = MagicMock()
|
||||
vm._namespace = 42
|
||||
nio = vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
|
||||
|
||||
with patch.object(DockerVM, "_vxlan_lowerdev", return_value="eth0"):
|
||||
await vm._add_ubridge_connection(nio, 0)
|
||||
|
||||
sends = [c.args[0] for c in vm._ubridge_hypervisor.send.call_args_list]
|
||||
show = f'vxlan show "{VXLAN_SPEC["name"]}"'
|
||||
delete = f'vxlan delete "{VXLAN_SPEC["name"]}"'
|
||||
create = (
|
||||
f'vxlan create "{VXLAN_SPEC["name"]}" {VXLAN_SPEC["vni"]} '
|
||||
f"remote={VXLAN_SPEC['remote']} dstport={VXLAN_SPEC['dstport']} dev=eth0"
|
||||
)
|
||||
assert sends.index(show) < sends.index(delete) < sends.index(create)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remove_kernel_nio_deletes_vxlan_device(vm):
|
||||
"""
|
||||
Link teardown deletes this end's tunnel device (the kernel un-enrolls it
|
||||
from the bridge on unregister) before the bridge delete.
|
||||
"""
|
||||
|
||||
vm._ubridge_hypervisor = MagicMock()
|
||||
vm._ubridge_send = AsyncioMagicMock()
|
||||
vm.status = "started"
|
||||
host_ifc, _ = vm._veth_names(0, 0)
|
||||
vm._kernel_veths[(0, 0)] = host_ifc
|
||||
vm._ethernet_adapters[0].add_nio(
|
||||
0, vm.manager.create_nio({"type": "nio_bridge", "bridge": BRIDGE, "vxlan": dict(VXLAN_SPEC)})
|
||||
)
|
||||
|
||||
await vm.adapter_remove_nio_binding(0)
|
||||
|
||||
vm._ubridge_send.assert_has_calls(
|
||||
[
|
||||
call(f'brctl delif "{BRIDGE}" "{host_ifc}"'),
|
||||
call(f'vxlan delete "{VXLAN_SPEC["name"]}"'),
|
||||
call(f'brctl delete "{BRIDGE}"'),
|
||||
],
|
||||
any_order=False,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vxlan_lowerdev_unresolvable_peer_raises(vm):
|
||||
"""
|
||||
A peer that cannot be resolved (or routed to) is a wiring error of this
|
||||
link — the controller retries and then degrades it to the relay.
|
||||
"""
|
||||
|
||||
with patch("gns3server.compute.kernel_datapath.socket.getaddrinfo", side_effect=socket.gaierror("nope")):
|
||||
with pytest.raises(NodeError):
|
||||
await vm._vxlan_lowerdev("192.0.2.1")
|
||||
|
||||
|
||||
def test_interface_with_address_matches_the_owning_interface():
|
||||
"""
|
||||
The route's chosen source address is matched back to its interface,
|
||||
scope suffixes stripped (link-local IPv6).
|
||||
"""
|
||||
|
||||
class _Addr:
|
||||
def __init__(self, address):
|
||||
self.address = address
|
||||
|
||||
with patch(
|
||||
"gns3server.compute.kernel_datapath.psutil.net_if_addrs",
|
||||
return_value={
|
||||
"eth0": [_Addr("192.168.1.1")],
|
||||
"eth1": [_Addr("fe80::1%eth1")],
|
||||
},
|
||||
):
|
||||
assert DockerVM._interface_with_address("192.168.1.1") == "eth0"
|
||||
assert DockerVM._interface_with_address("fe80::1%eth1") == "eth1"
|
||||
assert DockerVM._interface_with_address("10.0.0.1") is None
|
||||
@@ -15,9 +15,11 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
@@ -1039,3 +1041,225 @@ async def test_run_with_kvm_linux_dev_kvm_missing(linux_platform, vm):
|
||||
vm.manager.config.settings.Qemu.require_hardware_acceleration = True
|
||||
with pytest.raises(QemuError):
|
||||
await vm._run_with_hardware_acceleration("qemu-system-x86_64", "")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_two_cdrom_images(vm, images_dir):
|
||||
installer = Path(images_dir) / "installer.iso"
|
||||
drivers = Path(images_dir) / "virtio,drivers.iso"
|
||||
installer.touch()
|
||||
drivers.touch()
|
||||
vm.cdrom_image = str(installer)
|
||||
vm.cdrom2_image = str(drivers)
|
||||
assert vm._cdrom_option() == [
|
||||
"-cdrom",
|
||||
str(installer),
|
||||
"-drive",
|
||||
f"file={str(drivers).replace(',', ',,')},if=ide,index=3,media=cdrom,readonly=on",
|
||||
]
|
||||
assert vm.asdict()["cdrom2_image"].endswith("virtio,drivers.iso")
|
||||
vm.cdrom2_image = ""
|
||||
assert vm._cdrom_option() == ["-cdrom", str(installer)]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("config_disk", [False, True])
|
||||
async def test_second_cdrom_ide_conflict(vm, images_dir, config_disk):
|
||||
iso = Path(images_dir) / "drivers.iso"
|
||||
iso.touch()
|
||||
vm.cdrom2_image = str(iso)
|
||||
vm._hdd_disk_interface = "ide"
|
||||
vm._create_config_disk = config_disk
|
||||
if not config_disk:
|
||||
vm._hdd_disk_image = str(iso)
|
||||
with pytest.raises(QemuError, match="second CDROM"):
|
||||
vm._cdrom_option()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_first_cdrom_ide_conflict(vm, images_dir):
|
||||
iso = Path(images_dir) / "installer.iso"
|
||||
iso.touch()
|
||||
vm.cdrom_image = str(iso)
|
||||
vm._hdc_disk_image = str(iso)
|
||||
vm._hdc_disk_interface = "ide"
|
||||
with pytest.raises(QemuError, match="IDE hdc disk"):
|
||||
vm._cdrom_option()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("interface", ["virtio", "scsi", "sata", "nvme"])
|
||||
@pytest.mark.parametrize("cdrom_count", [1, 2])
|
||||
async def test_four_non_ide_disks_with_cdroms(vm, images_dir, interface, cdrom_count):
|
||||
iso = Path(images_dir) / "installer.iso"
|
||||
iso.touch()
|
||||
vm.cdrom_image = str(iso)
|
||||
for drive in "abcd":
|
||||
disk = Path(images_dir) / f"hd{drive}.qcow2"
|
||||
disk.touch()
|
||||
setattr(vm, f"_hd{drive}_disk_image", str(disk))
|
||||
setattr(vm, f"_hd{drive}_disk_interface", interface)
|
||||
expected = ["-cdrom", str(iso)]
|
||||
if cdrom_count == 2:
|
||||
vm.cdrom2_image = str(iso)
|
||||
expected.extend(["-drive", f"file={iso},if=ide,index=3,media=cdrom,readonly=on"])
|
||||
assert vm._cdrom_option() == expected
|
||||
vm.linked_clone = False
|
||||
with patch.object(vm, "_qemu_img_exec", new_callable=AsyncioMagicMock, return_value=0):
|
||||
disk_options = await vm._disk_options()
|
||||
assert sum(option.startswith("file=") for option in disk_options) == 4
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cdrom_monitor(vm):
|
||||
reader = asyncio.StreamReader()
|
||||
reader.feed_data(b"QEMU monitor\r\n(qemu) ")
|
||||
writer = MagicMock()
|
||||
writer.drain = AsyncioMagicMock()
|
||||
state = {"blocks": "ide1-cd0: installer.iso\r\nide1-cd1: drivers.iso\r\n", "error": ""}
|
||||
|
||||
def respond(data):
|
||||
command = data.decode("utf-8").strip()
|
||||
output = state["blocks"] if command == "info block" else state["error"]
|
||||
reader.feed_data(data.rstrip(b"\n") + b"\r\n" + output.encode("utf-8") + b"(qemu) ")
|
||||
|
||||
writer.write.side_effect = respond
|
||||
with (
|
||||
patch.object(vm, "is_running", return_value=True),
|
||||
patch.object(
|
||||
vm, "_open_qemu_monitor_connection_vm", new_callable=AsyncioMagicMock, return_value=(reader, writer)
|
||||
),
|
||||
):
|
||||
yield state, writer
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("filename", ["drivers with spaces.iso", "drivers-雪.iso", "drivers (qemu) .iso"])
|
||||
async def test_second_cdrom_update(vm, images_dir, cdrom_monitor, filename):
|
||||
iso = Path(images_dir) / filename
|
||||
iso.touch()
|
||||
state, writer = cdrom_monitor
|
||||
await vm.update_property("cdrom2_image", str(iso))
|
||||
assert writer.write.call_args_list[0] == mock.call(b"info block\n")
|
||||
command = writer.write.call_args_list[1].args[0].decode("ascii")
|
||||
encoded_filename = json.loads(command.removeprefix("change ide1-cd1 -f ").strip())
|
||||
if encoded_filename.startswith("json:"):
|
||||
assert json.loads(encoded_filename[5:])["file"]["filename"] == str(iso)
|
||||
else:
|
||||
assert encoded_filename == str(iso)
|
||||
assert vm.cdrom2_image == str(iso)
|
||||
writer.close.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_second_cdrom_eject(vm, cdrom_monitor):
|
||||
state, writer = cdrom_monitor
|
||||
await vm.update_property("cdrom2_image", "")
|
||||
writer.write.assert_called_with(b"eject -f ide1-cd1\n")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_second_cdrom_hot_add_requires_restart(vm, images_dir, cdrom_monitor):
|
||||
iso = Path(images_dir) / "drivers.iso"
|
||||
iso.touch()
|
||||
state, writer = cdrom_monitor
|
||||
state["blocks"] = "ide1-cd0: installer.iso\r\n"
|
||||
vm._command_line_changed = False
|
||||
with pytest.raises(QemuError, match="Stop the VM"):
|
||||
await vm.update_property("cdrom2_image", str(iso))
|
||||
assert vm.cdrom2_image == ""
|
||||
assert not vm._command_line_changed
|
||||
assert writer.write.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_other_iso_does_not_block_media_swap(vm, images_dir, cdrom_monitor):
|
||||
iso = Path(images_dir) / "installer.iso"
|
||||
iso.touch()
|
||||
vm._cdrom2_image = str(Path(images_dir) / "deleted.iso")
|
||||
await vm.update_property("cdrom_image", str(iso))
|
||||
assert vm.cdrom_image == str(iso)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("error", ["Error: media change failed", "change: filename expected"])
|
||||
@pytest.mark.parametrize("original_deleted", [False, True])
|
||||
async def test_cdrom_monitor_error_preserves_previous_image(vm, images_dir, cdrom_monitor, error, original_deleted):
|
||||
original = Path(images_dir) / "original.iso"
|
||||
original.touch()
|
||||
replacement = Path(images_dir) / "replacement.iso"
|
||||
replacement.touch()
|
||||
vm.cdrom2_image = str(original)
|
||||
if original_deleted:
|
||||
original.unlink()
|
||||
state, writer = cdrom_monitor
|
||||
state["error"] = error + "\r\n"
|
||||
with pytest.raises(QemuError, match=error):
|
||||
await vm.update_property("cdrom2_image", str(replacement))
|
||||
assert vm.cdrom2_image == str(original)
|
||||
writer.close.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("slot", [0, 1])
|
||||
async def test_sata_cdrom_with_ide_disks(vm, images_dir, slot):
|
||||
iso = Path(images_dir) / "installer,drivers.iso"
|
||||
iso.touch()
|
||||
image_name = "cdrom_image" if slot == 0 else "cdrom2_image"
|
||||
interface_name = "cdrom_interface" if slot == 0 else "cdrom2_interface"
|
||||
setattr(vm, image_name, str(iso))
|
||||
setattr(vm, interface_name, "sata")
|
||||
vm._hdc_disk_image = vm._hdd_disk_image = str(iso)
|
||||
vm._hdc_disk_interface = vm._hdd_disk_interface = "ide"
|
||||
assert vm._cdrom_option() == [
|
||||
"-device",
|
||||
f"ich9-ahci,id=cdrom-ahci{slot}",
|
||||
"-drive",
|
||||
f"file={str(iso).replace(',', ',,')},if=none,id=sata-cdrom{slot},media=cdrom,readonly=on",
|
||||
"-device",
|
||||
f"ide-cd,drive=sata-cdrom{slot},bus=cdrom-ahci{slot}.0",
|
||||
]
|
||||
assert vm.asdict()[interface_name] == "sata"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("name", ["cdrom_interface", "cdrom2_interface"])
|
||||
async def test_cdrom_interface_change_requires_stopped_vm(vm, name):
|
||||
with patch.object(vm, "is_running", return_value=True):
|
||||
with pytest.raises(QemuError, match="Stop the VM"):
|
||||
await vm.update_property(name, "sata")
|
||||
assert getattr(vm, name) == "ide"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("slot", [0, 1])
|
||||
async def test_sata_cdrom_media_update(vm, images_dir, cdrom_monitor, slot):
|
||||
state, writer = cdrom_monitor
|
||||
setattr(vm, "_cdrom_interface" if slot == 0 else "_cdrom2_interface", "sata")
|
||||
state["blocks"] = f"sata-cdrom{slot} (#block123): old.iso\r\n"
|
||||
iso = Path(images_dir) / "new.iso"
|
||||
iso.touch()
|
||||
await vm.update_property("cdrom_image" if slot == 0 else "cdrom2_image", str(iso))
|
||||
writer.write.assert_called_with(f'change sata-cdrom{slot} -f "{iso}"\n'.encode())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("name", ["cdrom_image", "cdrom2_image"])
|
||||
async def test_cdrom_broken_symlink_error_includes_target(vm, images_dir, name):
|
||||
target = Path(images_dir) / "missing.iso"
|
||||
link = Path(images_dir) / "linked.iso"
|
||||
link.symlink_to(target)
|
||||
setattr(vm, f"_{name}", str(link))
|
||||
with pytest.raises(QemuError, match="linked to") as error:
|
||||
vm._cdrom_option()
|
||||
assert str(target) in str(error.value)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("slot", [0, 1])
|
||||
async def test_sata_cdrom_eject(vm, cdrom_monitor, slot):
|
||||
state, writer = cdrom_monitor
|
||||
setattr(vm, "_cdrom_interface" if slot == 0 else "_cdrom2_interface", "sata")
|
||||
state["blocks"] = f"sata-cdrom{slot}: empty\r\n"
|
||||
await vm.update_property("cdrom_image" if slot == 0 else "cdrom2_image", "")
|
||||
writer.write.assert_called_with(f"eject -f sata-cdrom{slot}\n".encode())
|
||||
@@ -30,8 +30,10 @@ from gns3server.compute.ubridge.tc_probe import (
|
||||
probe_iol_tap_support,
|
||||
probe_tap_support,
|
||||
probe_tc_capabilities,
|
||||
probe_vxlan_support,
|
||||
)
|
||||
from gns3server.compute.ubridge.ubridge_error import UbridgeError
|
||||
from gns3server.config import Config
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
@@ -93,6 +95,7 @@ def probe_env(monkeypatch, tmp_path):
|
||||
tc_probe._tap_cache.clear()
|
||||
tc_probe._iol_tap_cache.clear()
|
||||
tc_probe._bridge_tap_cache.clear()
|
||||
tc_probe._vxlan_cache.clear()
|
||||
FakeHypervisor.last = None
|
||||
# a real file on disk: the cache is keyed on its (path, mtime, size,
|
||||
# ctime)
|
||||
@@ -280,3 +283,51 @@ async def test_probe_bridge_tap_support_missing_binary(monkeypatch):
|
||||
monkeypatch.setattr(tc_probe.shutil, "which", lambda name: None)
|
||||
assert await probe_bridge_tap_support() is None
|
||||
assert FakeHypervisor.spawned == 0
|
||||
|
||||
|
||||
class _FixedUuid:
|
||||
"""A uuid4 stand-in whose .int/.hex pin the probe's random draws."""
|
||||
|
||||
def __init__(self, value):
|
||||
self.int = value
|
||||
self.hex = f"{value:032x}"
|
||||
|
||||
|
||||
def _probe_create_port():
|
||||
"""The dstport of the one `vxlan create` the probe issued."""
|
||||
|
||||
create = next(c for c in FakeHypervisor.last.commands if c.startswith("vxlan create "))
|
||||
return int(create.rsplit("dstport=", 1)[1])
|
||||
|
||||
|
||||
async def test_probe_vxlan_avoids_the_configured_dstport(monkeypatch, probe_env):
|
||||
"""
|
||||
A scratch draw landing exactly on the configured dstport must move: on
|
||||
that port the scratch VNI shares a demux table with the live links'
|
||||
devices, and an unlucky VNI draw would make the probe report the
|
||||
capability unknown for the process's lifetime (the result is cached).
|
||||
"""
|
||||
|
||||
# int % 10000 == 9999 -> the scratch window draws its very last value
|
||||
monkeypatch.setattr(tc_probe.uuid, "uuid4", lambda: _FixedUuid(29999))
|
||||
settings = Config.instance().settings.Server
|
||||
monkeypatch.setattr(settings, "vxlan_dstport", 29999)
|
||||
|
||||
assert await probe_vxlan_support() is True
|
||||
assert _probe_create_port() == 20000
|
||||
|
||||
|
||||
async def test_probe_vxlan_scratch_port_stays_off_the_configured_one(monkeypatch, probe_env):
|
||||
"""
|
||||
Whatever the configured dstport, the probe's socket never lands on it
|
||||
and stays inside its scratch window.
|
||||
"""
|
||||
|
||||
settings = Config.instance().settings.Server
|
||||
for configured in (4789, 20000, 25000, 29999):
|
||||
monkeypatch.setattr(settings, "vxlan_dstport", configured)
|
||||
tc_probe._vxlan_cache.clear()
|
||||
assert await probe_vxlan_support() is True
|
||||
port = _probe_create_port()
|
||||
assert 20000 <= port <= 29999, port
|
||||
assert port != configured, (port, configured)
|
||||
@@ -628,3 +628,52 @@ def test_no_appliance_metadata_when_appliance_has_none():
|
||||
template = ApplianceToTemplate().new_template(appliance, None, "local")
|
||||
|
||||
assert "appliance_metadata" not in template
|
||||
|
||||
|
||||
@pytest.mark.parametrize("version", [6, 8])
|
||||
def test_second_cdrom_survives_appliance_conversion(version):
|
||||
from copy import deepcopy
|
||||
|
||||
from gns3server.schemas.controller.appliances import ApplianceVersionImages
|
||||
|
||||
slots = {"cdrom_image": "installer.iso", "cdrom2_image": "drivers.iso"}
|
||||
validated = ApplianceVersionImages.model_validate(slots).model_dump(exclude_none=True)
|
||||
assert validated == slots
|
||||
appliance = deepcopy(VYOS_V8)
|
||||
appliance["versions"][0]["images"] = validated
|
||||
if version == 6:
|
||||
appliance["registry_version"] = 6
|
||||
appliance["qemu"] = {"arch": "x86_64", "ram": 512, "adapters": 1, "kvm": "allow"}
|
||||
converted = ApplianceToTemplate().new_template(appliance, appliance["versions"][0], "local")
|
||||
assert converted["cdrom_image"] == "installer.iso"
|
||||
assert converted["cdrom2_image"] == "drivers.iso"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["cdrom_interface", "cdrom2_interface"])
|
||||
def test_v8_cdrom_interface_survives_validation_and_conversion(name):
|
||||
import copy
|
||||
|
||||
appliance = copy.deepcopy(VYOS_V8)
|
||||
appliance["settings"][0]["template_properties"][name] = "sata"
|
||||
validated = ApplianceModel.model_validate(appliance).model_dump(mode="json", exclude_none=True)
|
||||
template = ApplianceToTemplate().new_template(validated, validated["versions"][1], "local")
|
||||
assert template[name] == "sata"
|
||||
|
||||
|
||||
def test_legacy_cdrom_interfaces_are_qemu_settings():
|
||||
from gns3server.schemas.controller.appliances import Qemu
|
||||
|
||||
settings = Qemu.model_validate(
|
||||
{
|
||||
"adapter_type": "e1000",
|
||||
"adapters": 1,
|
||||
"ram": 512,
|
||||
"arch": "x86_64",
|
||||
"console_type": "telnet",
|
||||
"kvm": "allow",
|
||||
"cdrom_interface": "sata",
|
||||
"cdrom2_interface": "sata",
|
||||
}
|
||||
)
|
||||
assert settings.model_dump()["cdrom_interface"] == "sata"
|
||||
assert settings.model_dump()["cdrom2_interface"] == "sata"
|
||||
@@ -413,3 +413,49 @@ async def test_export_with_ignoring_snapshots(tmpdir, project):
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / "zipfile.zip")) as myzip:
|
||||
assert os.path.join("snapshots", "snap.gns3project") not in [f.filename for f in myzip.filelist]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_export_two_isos_with_identical_filenames(tmpdir, project):
|
||||
from gns3server.compute.qemu import Qemu
|
||||
from gns3server.controller.import_project import _import_images
|
||||
|
||||
root = Path(str(tmpdir)) / "QEMU"
|
||||
installer = root / "windows/install.iso"
|
||||
drivers = root / "drivers/install.iso"
|
||||
for image, data in ((installer, b"installer"), (drivers, b"drivers")):
|
||||
image.parent.mkdir(parents=True)
|
||||
image.write_bytes(data)
|
||||
topology = {
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"node_type": "qemu",
|
||||
"properties": {
|
||||
"cdrom_image": str(installer),
|
||||
"cdrom2_image": str(drivers),
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Path(project.path, "test.gns3").write_text(json.dumps(topology))
|
||||
archive = str(tmpdir / "iso-project.zip")
|
||||
with aiozipstream.ZipFile() as stream:
|
||||
with patch.object(Qemu, "get_images_directory", return_value=str(root)):
|
||||
await export_project(stream, project, str(tmpdir), include_images=True)
|
||||
await write_file(archive, stream)
|
||||
with zipfile.ZipFile(archive) as stream:
|
||||
properties = json.loads(stream.read("project.gns3"))["topology"]["nodes"][0]["properties"]
|
||||
assert properties["cdrom_image"] != properties["cdrom2_image"]
|
||||
assert stream.read("images/QEMU/" + properties["cdrom_image"]) == b"installer"
|
||||
assert stream.read("images/QEMU/" + properties["cdrom2_image"]) == b"drivers"
|
||||
assert len(stream.namelist()) == len(set(stream.namelist()))
|
||||
destination = Path(str(tmpdir)) / "unpacked"
|
||||
stream.extractall(destination)
|
||||
controller = MagicMock()
|
||||
imported_images = Path(str(tmpdir)) / "imported-images"
|
||||
controller.images_path.return_value = str(imported_images)
|
||||
await _import_images(controller, str(destination / "images"))
|
||||
assert (imported_images / "QEMU" / properties["cdrom_image"]).read_bytes() == b"installer"
|
||||
assert (imported_images / "QEMU" / properties["cdrom2_image"]).read_bytes() == b"drivers"
|
||||
@@ -20,18 +20,21 @@ Controller-side kernel-datapath link selection: eligibility of Docker-to-
|
||||
Docker links, NIO emission and the filters/markers guards.
|
||||
"""
|
||||
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from gns3server.config import Config
|
||||
from gns3server.controller.controller_error import ControllerError
|
||||
from gns3server.controller.compute import ComputeError
|
||||
from gns3server.controller.controller_error import ComputeConflictError, ControllerError
|
||||
from gns3server.controller.link import FILTERS
|
||||
from gns3server.controller.node import Node
|
||||
from gns3server.controller.ports.ethernet_port import EthernetPort
|
||||
from gns3server.controller.ports.serial_port import SerialPort
|
||||
from gns3server.controller.udp_link import UDPLink
|
||||
from gns3server.utils.kernel_anchor import kernel_anchor_name, kernel_cascade_names
|
||||
from gns3server.utils.vxlan import vxlan_device_name
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
|
||||
@@ -1174,3 +1177,350 @@ async def test_node_started_repushes_both_cascade_ends(project):
|
||||
|
||||
sw1.put.assert_called_once_with("/adapters/0/ports/0/nio", data=entries[0][3], timeout=120)
|
||||
sw2.put.assert_called_once_with("/adapters/0/ports/0/nio", data=entries[1][3], timeout=120)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Cross-compute VXLAN links
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _cross_compute_enabled():
|
||||
settings = Config.instance().settings.Server
|
||||
original = settings.enable_vxlan_cross_compute
|
||||
settings.enable_vxlan_cross_compute = True
|
||||
try:
|
||||
yield settings
|
||||
finally:
|
||||
settings.enable_vxlan_cross_compute = original
|
||||
|
||||
|
||||
def _vxlan_compute(compute_id, vxlan=True):
|
||||
compute = MagicMock()
|
||||
compute.id = compute_id
|
||||
compute.capabilities = {"ubridge_vxlan": vxlan}
|
||||
return compute
|
||||
|
||||
|
||||
async def _cross_compute_link(project, vxlan1=True, vxlan2=True, same_host=False):
|
||||
"""A docker↔docker link whose endpoints live on two computes, with the
|
||||
peer resolution (and the relay fallback's port reservation) mocked."""
|
||||
|
||||
compute1 = _vxlan_compute("compute-1", vxlan1)
|
||||
compute2 = _vxlan_compute("compute-2", vxlan2)
|
||||
|
||||
async def subnet_callback(other):
|
||||
return ("192.168.1.1", "192.168.1.1" if same_host else "192.168.1.2")
|
||||
|
||||
compute1.get_ip_on_same_subnet.side_effect = subnet_callback
|
||||
|
||||
async def post_callback(path, data=None, **kwargs):
|
||||
response = MagicMock()
|
||||
response.json = {"udp_port": 1024}
|
||||
return response
|
||||
|
||||
compute1.post.side_effect = post_callback
|
||||
compute2.post.side_effect = post_callback
|
||||
node1 = _node(project, compute1, "docker1")
|
||||
node2 = _node(project, compute2, "docker2")
|
||||
link = UDPLink(project)
|
||||
await link.add_node(node1, 0, 0, batch=True, dump=False)
|
||||
await link.add_node(node2, 0, 0, batch=True, dump=False)
|
||||
return link, node1, node2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vxlan_cross_compute_disabled_by_config(project):
|
||||
"""
|
||||
Cross-compute links stay on the relay until the deployment opts in —
|
||||
the VXLAN tunnel caps the usable MTU at the underlay minus the outer
|
||||
headers, so it is opt-in.
|
||||
"""
|
||||
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
assert link._kernel_datapath_eligible(node1, node2) is False
|
||||
|
||||
with _cross_compute_enabled():
|
||||
assert link._kernel_datapath_eligible(node1, node2) is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vxlan_cross_compute_requires_the_vxlan_capability(project):
|
||||
"""
|
||||
Both computes' uBridge must be able to create the tunnel device; an
|
||||
unreported/failed probe (None) or an old build (False) keeps the link
|
||||
on the relay, exactly like the other capability gates.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
for vxlan1, vxlan2 in ((False, True), (True, False), (True, None), (None, None)):
|
||||
link, node1, node2 = await _cross_compute_link(project, vxlan1=vxlan1, vxlan2=vxlan2)
|
||||
assert link._kernel_datapath_eligible(node1, node2) is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_vxlan_cross_compute_switch_stays_on_relay(project):
|
||||
"""
|
||||
The switch fast path absorbs the peer's anchor into the switch's own
|
||||
bridge — a one-host shape; a switch link across computes keeps the
|
||||
relay.
|
||||
"""
|
||||
|
||||
switch = _node(project, _vxlan_compute("compute-1"), "switch1", node_type="ethernet_switch")
|
||||
peer = _node(project, _vxlan_compute("compute-2"), "docker2")
|
||||
link = await _link(project, switch, peer)
|
||||
with _cross_compute_enabled():
|
||||
assert link._kernel_datapath_eligible(switch, peer) is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prepare_vxlan_link_emits_tunnel_specs(project):
|
||||
"""
|
||||
Both ends get one nio_bridge sharing the per-link bridge name, each
|
||||
carrying the same VNI/dstport (the pair must agree, and neither end can
|
||||
ask the other) and the peer's address, with per-endpoint deterministic
|
||||
device names.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
entries = await link._prepare()
|
||||
|
||||
assert len(entries) == 2
|
||||
expected_bridge = "gns3" + link.id.replace("-", "")[:11]
|
||||
dstport = Config.instance().settings.Server.vxlan_dstport
|
||||
specs = [entry[3] for entry in entries]
|
||||
assert all(spec["type"] == "nio_bridge" for spec in specs)
|
||||
assert all(spec["bridge"] == expected_bridge for spec in specs)
|
||||
vxlan1, vxlan2 = specs[0]["vxlan"], specs[1]["vxlan"]
|
||||
assert vxlan1["vni"] == vxlan2["vni"]
|
||||
assert vxlan1["dstport"] == vxlan2["dstport"] == dstport
|
||||
assert vxlan1["remote"] == "192.168.1.2"
|
||||
assert vxlan2["remote"] == "192.168.1.1"
|
||||
assert vxlan1["name"] == vxlan_device_name(link.id, node1.id)
|
||||
assert vxlan2["name"] == vxlan_device_name(link.id, node2.id)
|
||||
assert vxlan1["name"] != vxlan2["name"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prepare_vxlan_same_host_pair_falls_back_to_relay(project):
|
||||
"""
|
||||
Equal resolved addresses mean both computes share one host: the kernel
|
||||
shares one VXLAN socket per dstport, so a tunnel pair cannot live there
|
||||
— such links keep the relay, where the ends differ by UDP port.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _cross_compute_link(project, same_host=True)
|
||||
entries = await link._prepare()
|
||||
|
||||
assert all(entry[3]["type"] == "nio_udp" for entry in entries)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_commit_vxlan_retries_with_a_fresh_vni(project):
|
||||
"""
|
||||
A refused commit (reactively: the VNI was already registered on the
|
||||
host's shared socket) re-prepares the link with a bumped salt — a fresh
|
||||
VNI, identical on both ends — and recommits.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
vnis = []
|
||||
|
||||
async def commit(entries):
|
||||
vnis.append(entries[0][3]["vxlan"]["vni"])
|
||||
if len(vnis) == 1:
|
||||
raise ComputeError("Could not create vxlan gvl...: File exists")
|
||||
|
||||
with patch.object(link, "_commit_nios", commit):
|
||||
await link.create()
|
||||
|
||||
assert len(vnis) == 2
|
||||
assert vnis[0] != vnis[1]
|
||||
assert link._vxlan_downgraded is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_commit_vxlan_degrades_to_relay_after_retries(project):
|
||||
"""
|
||||
When every VXLAN attempt fails, the link degrades to the UDP relay —
|
||||
the opt-in datapath must never make a link uncreatable.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
commits = []
|
||||
|
||||
async def commit(entries):
|
||||
commits.append(entries)
|
||||
if any(entry[3].get("vxlan") for entry in entries):
|
||||
raise ComputeError("Could not create vxlan gvl...: File exists")
|
||||
|
||||
with patch.object(link, "_commit_nios", commit):
|
||||
await link.create()
|
||||
|
||||
assert link._vxlan_downgraded is True
|
||||
assert len(commits) == 4 # the three VXLAN attempts, then the relay
|
||||
assert all(entry[3].get("vxlan") for entry in commits[0])
|
||||
assert all(entry[3]["type"] == "nio_udp" for entry in commits[-1])
|
||||
|
||||
|
||||
async def _prepared_vxlan_link(project):
|
||||
"""A cross-compute link with its tunnel NIOs prepared (the shape a node
|
||||
start replays) — call inside ``_cross_compute_enabled`` so the heal's
|
||||
re-preparation stays on the tunnel too. _bind_members mirrors the
|
||||
project-open finalize (the node's link back-reference is what lets the
|
||||
heal find the conflicted link)."""
|
||||
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
await link._prepare()
|
||||
link._bind_members()
|
||||
link._created = True
|
||||
return link, node1, node2
|
||||
|
||||
|
||||
def _vni_conflict(node, link):
|
||||
"""The compute's 409 when the kernel refuses the link's VNI on the
|
||||
shared dstport socket — the wording KernelDatapathMixin emits."""
|
||||
|
||||
device = vxlan_device_name(link.id, node.id)
|
||||
return ComputeConflictError(
|
||||
"http://compute", {"message": f"Could not create VXLAN device {device} to 192.168.1.2 via eth0: File exists"}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_node_start_self_heals_a_vni_conflict(project):
|
||||
"""
|
||||
A link wired while its nodes were stopped hits its VNI collision at
|
||||
node start (the kernel's refusal is the first time anyone asks). The
|
||||
start stops the half-started node, re-wires the link on a fresh VNI —
|
||||
both ends, through the per-compute batch — and retries.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _prepared_vxlan_link(project)
|
||||
salt = link._vxlan_salt
|
||||
starts = []
|
||||
|
||||
async def post_callback(path, data=None, **kwargs):
|
||||
if path.endswith("/start"):
|
||||
starts.append(path)
|
||||
if len(starts) == 1:
|
||||
raise _vni_conflict(node1, link)
|
||||
response = MagicMock()
|
||||
response.json = {"udp_port": 1024}
|
||||
return response
|
||||
|
||||
node1.compute.post.side_effect = post_callback
|
||||
await node1.start()
|
||||
|
||||
assert len(starts) == 2
|
||||
assert link._vxlan_downgraded is False
|
||||
assert link._vxlan_salt == salt + 1
|
||||
# the half-started node was stopped before the retry (a start on a
|
||||
# running container early-returns without wiring)
|
||||
assert any(call.args[0].endswith("/stop") for call in node1.compute.post.call_args_list)
|
||||
# the fresh-VNI NIOs were re-dispatched to both computes
|
||||
for compute in (node1.compute, node2.compute):
|
||||
batches = [c for c in compute.post.call_args_list if str(c.args[0]).endswith("/nios/batch")]
|
||||
assert len(batches) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_node_start_degrades_the_link_after_a_second_conflict(project):
|
||||
"""
|
||||
A conflict that survives the fresh VNI degrades the link to the relay
|
||||
— the start must succeed no later than that round, the same guarantee
|
||||
create() and the project-open batch path give.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _prepared_vxlan_link(project)
|
||||
starts = []
|
||||
|
||||
async def post_callback(path, data=None, **kwargs):
|
||||
if path.endswith("/start"):
|
||||
starts.append(path)
|
||||
if len(starts) <= 2:
|
||||
raise _vni_conflict(node1, link)
|
||||
response = MagicMock()
|
||||
response.json = {"udp_port": 1024}
|
||||
return response
|
||||
|
||||
node1.compute.post.side_effect = post_callback
|
||||
await node1.start()
|
||||
|
||||
assert len(starts) == 3
|
||||
assert link._vxlan_downgraded is True
|
||||
assert all(nio["type"] == "nio_udp" for nio in link._link_data)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_node_start_does_not_retry_other_failures(project):
|
||||
"""
|
||||
Only the VXLAN device conflict heals: any other start failure surfaces
|
||||
unchanged, with a single attempt and no stop/redispatch side effects.
|
||||
"""
|
||||
|
||||
with _cross_compute_enabled():
|
||||
link, node1, node2 = await _prepared_vxlan_link(project)
|
||||
salt = link._vxlan_salt
|
||||
|
||||
async def post_callback(path, data=None, **kwargs):
|
||||
if path.endswith("/start"):
|
||||
raise ComputeError("DockerError: image not found")
|
||||
response = MagicMock()
|
||||
response.json = {}
|
||||
return response
|
||||
|
||||
node1.compute.post.side_effect = post_callback
|
||||
with pytest.raises(ComputeError):
|
||||
await node1.start()
|
||||
|
||||
starts = [c for c in node1.compute.post.call_args_list if str(c.args[0]).endswith("/start")]
|
||||
assert len(starts) == 1
|
||||
assert not any(call.args[0].endswith("/stop") for call in node1.compute.post.call_args_list)
|
||||
assert link._vxlan_salt == salt
|
||||
assert link._vxlan_downgraded is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_heal_tries_the_full_vni_candidate_count(project):
|
||||
"""
|
||||
The project-open heal gives a refused link the same VNI candidate count
|
||||
an interactive creation gets (fresh-salt rounds), degrading to the
|
||||
relay only after they run out.
|
||||
"""
|
||||
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
rounds = []
|
||||
|
||||
async def redispatch(links, downgrade):
|
||||
rounds.append(downgrade)
|
||||
return links if not downgrade else []
|
||||
|
||||
with patch.object(project, "_redispatch_vxlan_links", redispatch):
|
||||
await project._heal_refused_vxlan_links([link])
|
||||
assert rounds == [False, False, True]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_heal_stops_at_the_first_successful_vni(project):
|
||||
"""
|
||||
A link healed by the first fresh-VNI round needs no further rounds —
|
||||
no pointless second redispatch, no relay degradation.
|
||||
"""
|
||||
|
||||
link, node1, node2 = await _cross_compute_link(project)
|
||||
rounds = []
|
||||
|
||||
async def redispatch(links, downgrade):
|
||||
rounds.append(downgrade)
|
||||
return []
|
||||
|
||||
with patch.object(project, "_redispatch_vxlan_links", redispatch):
|
||||
await project._heal_refused_vxlan_links([link])
|
||||
assert rounds == [False]
|
||||
@@ -30,7 +30,13 @@ Unit tests for the tag-keyed aggregate replay module (sharkd edition):
|
||||
invalidation, LRU bound (idle-only eviction), single spawn under
|
||||
concurrent acquires, timeout/stale-reply aborts — against the real sharkd
|
||||
where installed, plus the frame detail end-to-end (hex + renamed tree +
|
||||
filter expressions, same-microsecond disambiguation).
|
||||
filter expressions, same-microsecond disambiguation)
|
||||
* remote capture sources — markers whose capture node runs on another
|
||||
compute are fetched over HTTP into the bounded cache: merged timelines
|
||||
span both computes, a compute with no file is a zero-count source (not an
|
||||
error), an unreachable compute flags its source instead of shrinking the
|
||||
inventory, unchanged refetches keep the cached file identity (resident
|
||||
sharkd sessions survive), and the local compute is never queried.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
@@ -38,6 +44,7 @@ import os
|
||||
import shutil
|
||||
import struct
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -45,6 +52,7 @@ import pytest
|
||||
|
||||
from gns3server.controller import marker_replay
|
||||
from gns3server.controller.controller_error import (
|
||||
ComputeError,
|
||||
ControllerBadRequestError,
|
||||
ControllerError,
|
||||
ControllerNotFoundError,
|
||||
@@ -880,3 +888,254 @@ class TestDecodeFrame:
|
||||
assert seen <= known, f"unknown sharkd keys appeared: {seen - known}"
|
||||
finally:
|
||||
await manager.close_all()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# remote capture sources (fetch from the compute hosting the capture node)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _pcap_bytes(frames, magic=PCAP_MAGIC_US, snaplen=65535):
|
||||
"""In-memory twin of _write_pcap: the bytes a compute serves for a marker
|
||||
pcap."""
|
||||
|
||||
buf = bytearray(struct.pack("<IHHiIII", magic, 2, 4, 0, 0, snaplen, 1))
|
||||
for sec, frac, payload in frames:
|
||||
buf += struct.pack("<IIII", sec, frac, len(payload), len(payload))
|
||||
buf += payload
|
||||
return bytes(buf)
|
||||
|
||||
|
||||
class _FakeRemoteContent:
|
||||
def __init__(self, chunks):
|
||||
self._chunks = chunks
|
||||
|
||||
def iter_any(self):
|
||||
async def _gen():
|
||||
for chunk in self._chunks:
|
||||
yield chunk
|
||||
|
||||
return _gen()
|
||||
|
||||
|
||||
class _FakeRemoteResponse:
|
||||
"""Duck-typed aiohttp response: streaming content + release()."""
|
||||
|
||||
def __init__(self, body):
|
||||
self.status = 200
|
||||
self.content = _FakeRemoteContent([body] if body else [])
|
||||
|
||||
async def release(self):
|
||||
pass
|
||||
|
||||
|
||||
class _FakeRemoteCompute:
|
||||
"""Duck-typed Compute: answers GETs on /projects/{id}/files/{rel} from a
|
||||
dict (missing key → 404), or raises a fixed failure on every call."""
|
||||
|
||||
def __init__(self, compute_id="remote-1", files=None, failure=None):
|
||||
self.id = compute_id
|
||||
self.requests = []
|
||||
self.files = dict(files or {})
|
||||
self.failure = failure
|
||||
|
||||
async def http_query(self, method, path, stream=False, **kwargs):
|
||||
self.requests.append((method, path))
|
||||
if self.failure is not None:
|
||||
raise self.failure
|
||||
rel = path.split("/files/", 1)[1]
|
||||
if rel not in self.files:
|
||||
raise ControllerNotFoundError(f"{method} {path} not found")
|
||||
return _FakeRemoteResponse(self.files[rel])
|
||||
|
||||
|
||||
def _remote_project(tmp_path, markers, computes_by_node, markers_dir=None):
|
||||
"""_fake_project plus id and get_node resolving to the fake computes."""
|
||||
|
||||
project = _fake_project(tmp_path, markers, markers_dir)
|
||||
project.id = "proj-1"
|
||||
|
||||
def get_node(node_id):
|
||||
try:
|
||||
return SimpleNamespace(compute=computes_by_node[node_id])
|
||||
except KeyError:
|
||||
raise ControllerNotFoundError(f"Node ID {node_id} doesn't exist")
|
||||
|
||||
project.get_node = get_node
|
||||
return project
|
||||
|
||||
|
||||
_REL = "project-files/markers/remotenode_linkB_icmp.pcap"
|
||||
|
||||
|
||||
class TestRemoteSources:
|
||||
@pytest.fixture(autouse=True)
|
||||
def _fresh_cache(self, monkeypatch, tmp_path):
|
||||
cache_dir = tmp_path / "marker-cache"
|
||||
monkeypatch.setattr(marker_replay, "_remote_cache", {})
|
||||
monkeypatch.setattr(marker_replay, "_remote_locks", {})
|
||||
monkeypatch.setattr(marker_replay, "_remote_cache_dir", str(cache_dir))
|
||||
return cache_dir
|
||||
|
||||
def _markers(self, tag=7):
|
||||
return {
|
||||
"linkA/icmp": _marker_entry(tag=tag, enabled=False, node_id="localnode"),
|
||||
"linkB/icmp": _marker_entry(tag=tag, enabled=False, node_id="remotenode"),
|
||||
}
|
||||
|
||||
async def test_remote_source_fetched_and_merged(self, tmp_path, monkeypatch):
|
||||
_fake_columns(monkeypatch, {})
|
||||
_write_pcap(tmp_path / "localnode_linkA_icmp.pcap", [(1693472000, 100000, _icmp_frame())])
|
||||
compute = _FakeRemoteCompute(files={_REL: _pcap_bytes([(1693472000, 200000, _icmp_frame())])})
|
||||
project = _remote_project(tmp_path, self._markers(), {"remotenode": compute})
|
||||
|
||||
timeline = await build_timeline(project, tag=7)
|
||||
|
||||
assert timeline["frame_count"] == 2
|
||||
assert [f["node_id"] for f in timeline["frames"]] == ["localnode", "remotenode"] # merged by ts
|
||||
assert all(s["count"] == 1 for s in timeline["sources"])
|
||||
assert not any("error" in s for s in timeline["sources"])
|
||||
# the remote pcap was fetched through the compute project-files API
|
||||
assert compute.requests == [("GET", f"/projects/proj-1/files/{_REL}")]
|
||||
|
||||
async def test_remote_missing_file_is_zero_count_without_error(self, tmp_path):
|
||||
compute = _FakeRemoteCompute() # no files: the marker never matched anything
|
||||
project = _remote_project(tmp_path, self._markers(), {"remotenode": compute})
|
||||
|
||||
timeline = await build_timeline(project, tag=7)
|
||||
|
||||
by_node = {s["node_id"]: s for s in timeline["sources"]}
|
||||
assert by_node["remotenode"]["count"] == 0
|
||||
assert "error" not in by_node["remotenode"]
|
||||
assert timeline["frame_count"] == 0
|
||||
|
||||
async def test_unreachable_compute_flags_source(self, tmp_path, monkeypatch):
|
||||
_fake_columns(monkeypatch, {})
|
||||
_write_pcap(tmp_path / "localnode_linkA_icmp.pcap", [(1693472000, 100000, _icmp_frame())])
|
||||
compute = _FakeRemoteCompute(failure=ComputeError("Cannot connect to compute 'remote-1'"))
|
||||
project = _remote_project(tmp_path, self._markers(), {"remotenode": compute})
|
||||
|
||||
timeline = await build_timeline(project, tag=7)
|
||||
|
||||
by_node = {s["node_id"]: s for s in timeline["sources"]}
|
||||
assert by_node["localnode"]["count"] == 1
|
||||
assert "error" not in by_node["localnode"]
|
||||
assert by_node["remotenode"]["count"] == 0
|
||||
assert "remote-1" in by_node["remotenode"]["error"]
|
||||
# the timeline still carries the reachable sources' frames
|
||||
assert timeline["frame_count"] == 1
|
||||
|
||||
async def test_local_compute_is_never_queried(self, tmp_path, monkeypatch):
|
||||
_fake_columns(monkeypatch, {})
|
||||
_write_pcap(tmp_path / "localnode_linkA_icmp.pcap", [(1693472000, 0, _icmp_frame())])
|
||||
compute = _FakeRemoteCompute(compute_id="local")
|
||||
markers = {
|
||||
"linkA/icmp": _marker_entry(tag=7, enabled=False, node_id="localnode"), # file on disk
|
||||
"linkB/icmp": _marker_entry(tag=7, enabled=False, node_id="localnode2"), # no file ever
|
||||
}
|
||||
project = _remote_project(tmp_path, markers, {"localnode": compute, "localnode2": compute})
|
||||
|
||||
timeline = await build_timeline(project, tag=7)
|
||||
|
||||
assert compute.requests == []
|
||||
by_link = {s["link_id"]: s for s in timeline["sources"]}
|
||||
assert by_link["linkA"]["count"] == 1
|
||||
assert by_link["linkB"]["count"] == 0
|
||||
assert "error" not in by_link["linkB"]
|
||||
|
||||
async def test_unchanged_refetch_preserves_cache_file(self, tmp_path, monkeypatch):
|
||||
_fake_columns(monkeypatch, {})
|
||||
compute = _FakeRemoteCompute(files={_REL: _pcap_bytes([(1693472000, 0, _icmp_frame())])})
|
||||
markers = {"linkB/icmp": _marker_entry(tag=7, enabled=False, node_id="remotenode")}
|
||||
project = _remote_project(tmp_path, markers, {"remotenode": compute})
|
||||
|
||||
await build_timeline(project, tag=7)
|
||||
entry = marker_replay._remote_cache[("proj-1", "remote-1", _REL)]
|
||||
first = os.stat(entry["path"])
|
||||
|
||||
await build_timeline(project, tag=7) # refetch with identical bytes
|
||||
assert os.stat(entry["path"]).st_ino == first.st_ino # same file: resident sessions stay valid
|
||||
|
||||
compute.files[_REL] = _pcap_bytes([(1693473000, 0, _icmp_frame())]) # rebuilt capture
|
||||
await build_timeline(project, tag=7)
|
||||
assert os.stat(entry["path"]).st_ino != first.st_ino # replaced copy
|
||||
|
||||
async def test_cache_evicts_idle_entries(self, tmp_path, monkeypatch):
|
||||
_fake_columns(monkeypatch, {})
|
||||
monkeypatch.setattr(marker_replay, "_REMOTE_CACHE_MAX", 2)
|
||||
compute = _FakeRemoteCompute(
|
||||
files={
|
||||
f"project-files/markers/remotenode_link{i}_icmp.pcap": _pcap_bytes([(1693472000 + i, 0, _icmp_frame())])
|
||||
for i in range(3)
|
||||
}
|
||||
)
|
||||
markers = {f"link{i}/icmp": _marker_entry(tag=7, enabled=False, node_id="remotenode") for i in range(3)}
|
||||
project = _remote_project(tmp_path, markers, {"remotenode": compute})
|
||||
|
||||
await build_timeline(project, tag=7)
|
||||
|
||||
assert len(marker_replay._remote_cache) == 2
|
||||
assert all(os.path.exists(entry["path"]) for entry in marker_replay._remote_cache.values())
|
||||
|
||||
async def test_decode_remote_missing_is_not_matched_404(self, tmp_path):
|
||||
compute = _FakeRemoteCompute()
|
||||
project = _remote_project(tmp_path, self._markers(), {"remotenode": compute})
|
||||
|
||||
with pytest.raises(ControllerNotFoundError, match="nothing ever matched"):
|
||||
await decode_frame(
|
||||
project, tag=7, ts="1693472000.000000", node_id="remotenode", link_id="linkB", marker="icmp"
|
||||
)
|
||||
|
||||
async def test_decode_remote_unreachable_raises_compute_error(self, tmp_path):
|
||||
compute = _FakeRemoteCompute(failure=ComputeError("Cannot connect to compute 'remote-1'"))
|
||||
project = _remote_project(tmp_path, self._markers(), {"remotenode": compute})
|
||||
|
||||
with pytest.raises(ComputeError, match="Could not fetch capture file"):
|
||||
await decode_frame(
|
||||
project, tag=7, ts="1693472000.000000", node_id="remotenode", link_id="linkB", marker="icmp"
|
||||
)
|
||||
|
||||
async def test_decode_remote_frame_uses_cached_copy(self, tmp_path, monkeypatch):
|
||||
compute = _FakeRemoteCompute(files={_REL: _pcap_bytes([(1693472000, 123456, _icmp_frame())])})
|
||||
project = _remote_project(tmp_path, self._markers(), {"remotenode": compute})
|
||||
|
||||
class _FakeManager:
|
||||
def __init__(self):
|
||||
self.seen_pcap = None
|
||||
|
||||
@asynccontextmanager
|
||||
async def session(self, pcap):
|
||||
self.seen_pcap = pcap
|
||||
|
||||
async def rpc(method, params):
|
||||
assert (method, params) == ("frame", {"frame": 1, "proto": True})
|
||||
return {"tree": []}
|
||||
|
||||
yield SimpleNamespace(rpc=rpc)
|
||||
|
||||
manager = _FakeManager()
|
||||
monkeypatch.setattr(marker_replay, "_get_manager", lambda: manager)
|
||||
|
||||
detail = await decode_frame(
|
||||
project, tag=7, ts="1693472000.123456", node_id="remotenode", link_id="linkB", marker="icmp"
|
||||
)
|
||||
|
||||
assert detail["hex"] == _icmp_frame().hex()
|
||||
cached = {entry["path"] for entry in marker_replay._remote_cache.values()}
|
||||
assert manager.seen_pcap in cached
|
||||
|
||||
async def test_close_sessions_removes_cache_dir(self, tmp_path, monkeypatch):
|
||||
_fake_columns(monkeypatch, {})
|
||||
compute = _FakeRemoteCompute(files={_REL: _pcap_bytes([(1693472000, 0, _icmp_frame())])})
|
||||
project = _remote_project(
|
||||
tmp_path, {"linkB/icmp": _marker_entry(tag=7, enabled=False, node_id="remotenode")}, {"remotenode": compute}
|
||||
)
|
||||
|
||||
await build_timeline(project, tag=7)
|
||||
cache_dir = marker_replay._remote_cache_dir
|
||||
assert os.path.isdir(cache_dir)
|
||||
|
||||
await marker_replay.close_sessions()
|
||||
|
||||
assert not os.path.exists(cache_dir)
|
||||
assert marker_replay._remote_cache == {}
|
||||
@@ -0,0 +1,56 @@
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from alembic import command, config
|
||||
from alembic.operations import Operations
|
||||
from alembic.runtime.migration import MigrationContext
|
||||
from fastapi import FastAPI
|
||||
|
||||
from gns3server.db.models import Base
|
||||
from gns3server.db.tasks import connect_to_db
|
||||
from gns3server.db_migrations.versions import e2a7c9d4b601_add_cdrom_interfaces as migration
|
||||
|
||||
|
||||
def test_cdrom_migration_defaults_idempotence_and_downgrade():
|
||||
engine = sa.create_engine("sqlite://")
|
||||
with engine.begin() as connection:
|
||||
connection.exec_driver_sql("CREATE TABLE qemu_templates (id INTEGER PRIMARY KEY)")
|
||||
connection.exec_driver_sql("INSERT INTO qemu_templates (id) VALUES (1)")
|
||||
with Operations.context(MigrationContext.configure(connection)):
|
||||
migration.upgrade()
|
||||
migration.upgrade()
|
||||
assert connection.exec_driver_sql("SELECT cdrom_interface, cdrom2_interface FROM qemu_templates").one() == (
|
||||
"ide",
|
||||
"ide",
|
||||
)
|
||||
migration.downgrade()
|
||||
assert [column["name"] for column in sa.inspect(connection).get_columns("qemu_templates")] == ["id"]
|
||||
engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("versioned", [False, True])
|
||||
async def test_database_startup_adds_cdrom_interfaces(tmp_path, monkeypatch, versioned):
|
||||
path = tmp_path / "controller.db"
|
||||
engine = sa.create_engine(f"sqlite:///{path}")
|
||||
with engine.connect() as connection:
|
||||
Base.metadata.create_all(connection)
|
||||
connection.exec_driver_sql("ALTER TABLE qemu_templates DROP COLUMN cdrom_interface")
|
||||
connection.exec_driver_sql("ALTER TABLE qemu_templates DROP COLUMN cdrom2_interface")
|
||||
if versioned:
|
||||
cfg = config.Config()
|
||||
cfg.set_main_option("script_location", "gns3server:db_migrations")
|
||||
cfg.attributes["connection"] = connection
|
||||
command.stamp(cfg, migration.down_revision)
|
||||
connection.commit()
|
||||
engine.dispose()
|
||||
monkeypatch.setenv("GNS3_DATABASE_URI", f"sqlite+aiosqlite:///{path}")
|
||||
app = FastAPI()
|
||||
await connect_to_db(app)
|
||||
try:
|
||||
async with app.state._db_engine.connect() as connection:
|
||||
columns = await connection.run_sync(lambda conn: sa.inspect(conn).get_columns("qemu_templates"))
|
||||
assert {"cdrom_interface", "cdrom2_interface"} <= {column["name"] for column in columns}
|
||||
revision = await connection.scalar(sa.text("SELECT version_num FROM alembic_version"))
|
||||
assert revision == migration.revision
|
||||
finally:
|
||||
await app.state._db_engine.dispose()
|
||||
@@ -187,3 +187,53 @@ async def test_list_images(tmpdir, config):
|
||||
"path": "qemu_image.qcow2",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("subdirectory", [None, "installers/linux"])
|
||||
async def test_iso_upload_and_discovery(tmp_path, config, subdirectory):
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from gns3server.utils.images import discover_images, inspect_image_file, write_image
|
||||
|
||||
config.settings.Server.images_path = str(tmp_path)
|
||||
config.settings.Server.additional_images_paths = []
|
||||
# ISO 9660 primary volume descriptor; no raw-image fallback is needed.
|
||||
content = b"\0" * (16 * 2048) + b"\x01CD001\x01" + b"\0" * 2041
|
||||
|
||||
async def stream():
|
||||
# Exercise a descriptor split across upload chunks.
|
||||
for offset in range(0, len(content), 13):
|
||||
yield content[offset : offset + 13]
|
||||
|
||||
repo = AsyncMock()
|
||||
repo.get_image_by_checksum.return_value = None
|
||||
repo.save_verified_image.side_effect = lambda image: image
|
||||
result = await write_image(
|
||||
"installer.ISO", str(tmp_path / "installer.ISO"), stream(), repo, subdirectory=subdirectory
|
||||
)
|
||||
expected = tmp_path / "QEMU" / (subdirectory or "") / "installer.ISO"
|
||||
assert result["path"] == str(expected)
|
||||
assert result["image_type"] == "qemu"
|
||||
assert expected.read_bytes() == content
|
||||
assert inspect_image_file(expected)["checksum"] == result["checksum"]
|
||||
assert [image["path"] for image in await discover_images("qemu")] == [str(expected)]
|
||||
|
||||
|
||||
def test_iso_extension_alone_is_not_a_valid_image():
|
||||
from gns3server.utils.images import InvalidImageError, check_valid_image_header
|
||||
|
||||
with pytest.raises(InvalidImageError):
|
||||
check_valid_image_header("fake.iso", b"not an ISO image")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("images_root", ["images", "QEMU/images"])
|
||||
def test_image_reference_uses_configured_root(tmp_path, config, images_root):
|
||||
from gns3server.db.models.images import Image
|
||||
|
||||
root = tmp_path / images_root
|
||||
config.settings.Server.images_path = str(root)
|
||||
image = Image(image_type="qemu", path=str(root / "QEMU/installers/linux.iso"))
|
||||
assert image.reference == "installers/linux.iso"
|
||||
image.path = str(tmp_path / "extra/QEMU/linux.iso")
|
||||
assert image.reference == image.path
|
||||
@@ -0,0 +1,71 @@
|
||||
#
|
||||
# Copyright (C) 2026 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Cross-compute VXLAN link identity: the derived VNI and device name.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
|
||||
from gns3server.utils.vxlan import VXLAN_NAME_PREFIX, VXLAN_VNI_MAX, VXLAN_VNI_MIN, derive_vni, vxlan_device_name
|
||||
|
||||
|
||||
def test_derive_vni_is_deterministic_and_bounded():
|
||||
link_id = str(uuid.uuid4())
|
||||
vni = derive_vni(link_id)
|
||||
assert derive_vni(link_id) == vni
|
||||
assert VXLAN_VNI_MIN <= vni <= VXLAN_VNI_MAX
|
||||
|
||||
|
||||
def test_derive_vni_salt_moves_the_value():
|
||||
link_id = str(uuid.uuid4())
|
||||
values = {derive_vni(link_id, salt) for salt in range(16)}
|
||||
# Every salt lands in range; a bumped salt is a fresh point of the space
|
||||
# (a collision here would defeat the retry, and 16 draws out of ~16.8M
|
||||
# values collapsing is a hash failure, not bad luck).
|
||||
assert all(VXLAN_VNI_MIN <= v <= VXLAN_VNI_MAX for v in values)
|
||||
assert len(values) == 16
|
||||
|
||||
|
||||
def test_derive_vni_avoids_low_well_known_vnis():
|
||||
# Flannel starts at 1, Docker overlay networks at 256: derived links
|
||||
# must never land in the low range whatever the link id.
|
||||
for _ in range(200):
|
||||
assert derive_vni(str(uuid.uuid4()), uuid.uuid4().int) >= VXLAN_VNI_MIN
|
||||
|
||||
|
||||
def test_vxlan_device_name_deterministic_and_within_ifnamsiz():
|
||||
link_id = str(uuid.uuid4())
|
||||
node_id = str(uuid.uuid4())
|
||||
name = vxlan_device_name(link_id, node_id)
|
||||
assert name == vxlan_device_name(link_id, node_id)
|
||||
assert name.startswith(VXLAN_NAME_PREFIX)
|
||||
# IFNAMSIZ (16) includes the NUL terminator: the kernel accepts 15 chars.
|
||||
assert len(name) <= 15
|
||||
|
||||
|
||||
def test_vxlan_device_name_unique_per_endpoint_and_link():
|
||||
link1 = str(uuid.uuid4())
|
||||
link2 = str(uuid.uuid4())
|
||||
node1 = str(uuid.uuid4())
|
||||
node2 = str(uuid.uuid4())
|
||||
names = {
|
||||
vxlan_device_name(link1, node1),
|
||||
vxlan_device_name(link1, node2),
|
||||
vxlan_device_name(link2, node1),
|
||||
}
|
||||
assert len(names) == 3
|
||||
Reference in new issue
Block a user