refactor: remove platform-specific code for Darwin and Windows. Clean up Qemu options handling

This commit is contained in:
grossmj committed 2026-10-31 16:49:23 +07:00
1 parent 4d1b9e4700
commit bad48286a5
20 files changed
+77 -532

No files matched your search

+9 -10
View File
@@ -171,7 +171,7 @@ async def test_json_without_ports(on_gns3vm, compute_project, manager):
}
async def test_kernel_datapath_interfaces_are_hidden(linux_platform, compute_project, manager):
async def test_kernel_datapath_interfaces_are_hidden(compute_project, manager):
"""
Kernel-datapath interfaces (node anchors, cascade veths, VXLAN devices)
are neither pre-filled as cloud ports nor listed as host interfaces,
@@ -221,7 +221,7 @@ async def test_update_port_mappings(on_gns3vm, compute_project):
assert cloud.ports_mapping == ports1
async def test_linux_ethernet_raw_add_nio(linux_platform, compute_project, nio):
async def test_linux_ethernet_raw_add_nio(compute_project, nio):
ports = [{"interface": "eth0", "name": "eth0", "port_number": 0, "type": "ethernet"}]
cloud = Cloud("cloud1", str(uuid.uuid4()), compute_project, MagicMock(), ports=ports)
cloud.status = "started"
@@ -243,7 +243,7 @@ async def test_linux_ethernet_raw_add_nio(linux_platform, compute_project, nio):
)
async def test_linux_ethernet_raw_add_nio_bridge(linux_platform, compute_project, nio):
async def test_linux_ethernet_raw_add_nio_bridge(compute_project, nio):
"""
Bridge can't be connected directly to a cloud we use a tap in the middle
"""
@@ -275,7 +275,7 @@ async def test_linux_ethernet_raw_add_nio_bridge(linux_platform, compute_project
@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):
async def test_tap_bridge_recreated(compute_project, manager, nio, bridge, existing_master):
ports = [{"interface": "tap0", "name": "tap0", "port_number": 0, "type": "tap"}]
if bridge:
ports[0]["bridge"] = bridge
@@ -302,8 +302,8 @@ async def test_tap_bridge_recreated(linux_platform, compute_project, manager, ni
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):
@pytest.mark.parametrize("failure", ["missing", "not_bridge", "other_master", "invalid_name"])
async def test_tap_bridge_validation(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'
@@ -314,14 +314,13 @@ async def test_tap_bridge_validation(linux_platform, compute_project, manager, n
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):
async def test_tap_bridge_attachment_failure(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"
@@ -342,7 +341,7 @@ async def test_tap_bridge_attachment_failure(linux_platform, compute_project, ma
assert cloud.status == "stopped"
async def test_cloud_lists_linux_bridges(linux_platform, compute_project, manager):
async def test_cloud_lists_linux_bridges(compute_project, manager):
cloud = Cloud(
"cloud1",
str(uuid.uuid4()),
@@ -400,7 +399,7 @@ async def test_bridged_tap_name_validation_preserves_legacy_names(name):
((1 << 12) | (1 << 13), True, True),
],
)
async def test_bridged_tap_requires_net_admin(linux_platform, tmp_path, caps, legacy_allowed, bridge_allowed):
async def test_bridged_tap_requires_net_admin(tmp_path, caps, legacy_allowed, bridge_allowed):
executable = tmp_path / "ubridge"
executable.write_text("")
with (
+1 -55
View File
@@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import uuid
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock
from gns3server.compute.builtin.nodes.nat import Nat
@@ -40,57 +40,3 @@ def test_json_gns3vm(on_gns3vm, compute_project):
},
],
}
def test_json_darwin(darwin_platform, compute_project):
with patch(
"gns3server.utils.interfaces.interfaces",
return_value=[
{"name": "eth0", "special": False, "type": "ethernet"},
{"name": "vmnet8", "special": True, "type": "ethernet"},
],
):
nat = Nat("nat1", str(uuid.uuid4()), compute_project, MagicMock())
assert nat.asdict() == {
"name": "nat1",
"usage": "",
"node_id": nat.id,
"project_id": compute_project.id,
"status": "started",
"ports_mapping": [{"interface": "vmnet8", "name": "nat0", "port_number": 0, "type": "ethernet"}],
"interfaces": [
{
"name": "vmnet8",
"type": "ethernet",
"special": True,
"ip_addresses": [],
},
],
}
def test_json_windows_with_full_name_of_interface(windows_platform, project):
with patch(
"gns3server.utils.interfaces.interfaces",
return_value=[{"name": "VMware Network Adapter VMnet8", "special": True, "type": "ethernet"}],
):
nat = Nat("nat1", str(uuid.uuid4()), project, MagicMock())
assert nat.asdict() == {
"name": "nat1",
"usage": "",
"node_id": nat.id,
"project_id": project.id,
"status": "started",
"ports_mapping": [
{"interface": "VMware Network Adapter VMnet8", "name": "nat0", "port_number": 0, "type": "ethernet"}
],
"interfaces": [
{
"name": "VMware Network Adapter VMnet8",
"type": "ethernet",
"special": True,
"ip_addresses": [],
},
],
}
+1 -5
View File
@@ -17,7 +17,6 @@
import os
import platform
import stat
import sys
from unittest.mock import patch
import pytest
@@ -29,10 +28,7 @@ from tests.utils import asyncio_patch
@pytest.fixture
def fake_qemu_img_binary(tmpdir):
if sys.platform.startswith("win"):
bin_path = str(tmpdir / "qemu-img.EXE")
else:
bin_path = str(tmpdir / "qemu-img")
bin_path = str(tmpdir / "qemu-img")
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
+17 -48
View File
@@ -18,7 +18,6 @@ import asyncio
import json
import os
import stat
import sys
from pathlib import Path
from unittest import mock
from unittest.mock import MagicMock, patch
@@ -46,10 +45,7 @@ async def manager(port_manager):
def fake_qemu_img_binary(monkeypatch, tmpdir):
monkeypatch.setenv("PATH", str(tmpdir))
if sys.platform.startswith("win"):
bin_path = os.path.join(os.environ["PATH"], "qemu-img.EXE")
else:
bin_path = os.path.join(os.environ["PATH"], "qemu-img")
bin_path = os.path.join(os.environ["PATH"], "qemu-img")
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
@@ -72,7 +68,7 @@ async def vm(compute_project, manager, fake_qemu_binary, fake_qemu_img_binary):
manager.port_manager.console_host = "127.0.0.1"
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, qemu_path=fake_qemu_binary)
vm._process_priority = "normal" # Avoid complexity for Windows tests
vm._process_priority = "normal"
vm._replicate_network_connection_state = False # to avoid delay when starting/stopping the VM
vm._start_ubridge = AsyncioMagicMock()
vm._ubridge_hypervisor = MagicMock()
@@ -432,7 +428,6 @@ async def test_bios_option(vm, tmpdir, fake_qemu_img_binary):
assert " ".join(["-bios", str(tmpdir / "test.img")]) in " ".join(options)
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Test not working on Windows")
@pytest.mark.asyncio
async def test_uefi_boot_mode_option(vm, tmpdir, images_dir, fake_qemu_img_binary):
@@ -657,7 +652,7 @@ async def test_build_command_manual_uuid(vm):
@pytest.mark.asyncio
async def test_build_command_kvm_below_6_2(linux_platform, vm, fake_qemu_binary):
async def test_build_command_kvm_below_6_2(vm, fake_qemu_binary):
"""
Qemu 2.4 introduce an issue with KVM
"""
@@ -821,7 +816,6 @@ async def test_build_command_with_invalid_options(vm):
await vm._build_command()
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.mark.asyncio
async def test_build_command_with_forbidden_options(vm):
@@ -843,7 +837,7 @@ async def _build_command_with_kvm(vm, clocksource):
@pytest.mark.asyncio
@pytest.mark.parametrize("clocksource", ["hyperv_clocksource_tsc_page", "hyperv_clocksource_msr"])
async def test_build_command_kvmclock_off_on_hyperv_host(linux_platform, vm, clocksource):
async def test_build_command_kvmclock_off_on_hyperv_host(vm, clocksource):
"""
A host running on Hyper-V without a usable TSC falls back to a Hyper-V clocksource:
kvmclock runs away in KVM guests there (Linux guests panic with soft lockups) and
@@ -852,16 +846,16 @@ async def test_build_command_kvmclock_off_on_hyperv_host(linux_platform, vm, clo
"""
cmd = await _build_command_with_kvm(vm, clocksource)
assert "-enable-kvm" in cmd
assert "accel=kvm" in cmd
assert cmd[cmd.index("-cpu") + 1] == "qemu64,kvmclock=off,tsc-deadline=off"
@pytest.mark.asyncio
@pytest.mark.parametrize("clocksource", ["tsc", None])
async def test_build_command_kvmclock_kept_on_other_hosts(linux_platform, vm, clocksource):
async def test_build_command_kvmclock_kept_on_other_hosts(vm, clocksource):
cmd = await _build_command_with_kvm(vm, clocksource)
assert "-enable-kvm" in cmd
assert "accel=kvm" in cmd
assert "-cpu" not in cmd
@@ -875,7 +869,7 @@ async def test_build_command_kvmclock_kept_without_kvm(vm):
@pytest.mark.asyncio
async def test_build_command_kvmclock_off_on_user_cpu(linux_platform, vm):
async def test_build_command_kvmclock_off_on_user_cpu(vm):
"""
A -cpu given in the additional options keeps its model and gets the features turned off.
"""
@@ -894,7 +888,7 @@ async def test_build_command_kvmclock_off_on_user_cpu(linux_platform, vm):
("max,-kvmclock,+tsc-deadline", "max,-kvmclock,+tsc-deadline"),
],
)
async def test_build_command_kvmclock_user_choice_kept(linux_platform, vm, cpu, expected):
async def test_build_command_kvmclock_user_choice_kept(vm, cpu, expected):
vm.options = f"-cpu {cpu}"
cmd = await _build_command_with_kvm(vm, "hyperv_clocksource_tsc_page")
@@ -903,7 +897,7 @@ async def test_build_command_kvmclock_user_choice_kept(linux_platform, vm, cpu,
@pytest.mark.asyncio
async def test_build_command_kvmclock_off_i386(linux_platform, vm):
async def test_build_command_kvmclock_off_i386(vm):
vm._platform = "i386"
cmd = await _build_command_with_kvm(vm, "hyperv_clocksource_tsc_page")
@@ -918,7 +912,7 @@ async def test_build_command_kvmclock_off_i386(linux_platform, vm):
("enabled", "hyperv_clocksource_tsc_page", False),
],
)
async def test_build_command_kvmclock_setting(linux_platform, vm, setting, clocksource, kvmclock_off):
async def test_build_command_kvmclock_setting(vm, setting, clocksource, kvmclock_off):
vm.manager.config.settings.Qemu.kvmclock = setting
cmd = await _build_command_with_kvm(vm, clocksource)
@@ -1031,7 +1025,7 @@ def test_initrd_asa(vm, images_dir):
assert mock.called
def test_options(linux_platform, vm):
def test_options(vm):
vm.kvm = False
vm.options = "-usb"
assert vm.options == "-usb"
@@ -1044,23 +1038,12 @@ def test_options(linux_platform, vm):
assert vm.options == "-machine accel=kvm"
def test_options_windows(windows_platform, vm):
vm.options = "-no-kvm"
assert vm.options == "-machine accel=tcg"
vm.options = "-enable-kvm"
assert vm.options == ""
def test_get_qemu_img(vm, tmpdir):
open(str(tmpdir / "qemu-system-x86_64"), "w+").close()
open(str(tmpdir / "qemu-img"), "w+").close()
vm._qemu_path = str(tmpdir / "qemu-system-x86_64")
if sys.platform.startswith("win"):
assert vm._get_qemu_img() == str(tmpdir / "qemu-img.EXE")
else:
assert vm._get_qemu_img() == str(tmpdir / "qemu-img")
assert vm._get_qemu_img() == str(tmpdir / "qemu-img")
# def test_get_qemu_img_not_exist(vm, tmpdir):
@@ -1072,21 +1055,7 @@ def test_get_qemu_img(vm, tmpdir):
@pytest.mark.asyncio
async def test_run_with_hardware_acceleration_darwin(darwin_platform, vm):
vm.manager.config.settings.Qemu.enable_hardware_acceleration = False
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is False
@pytest.mark.asyncio
async def test_run_with_hardware_acceleration_windows(windows_platform, vm):
vm.manager.config.settings.Qemu.enable_hardware_acceleration = False
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is False
@pytest.mark.asyncio
async def test_run_with_kvm_linux(linux_platform, vm):
async def test_run_with_kvm(vm):
with patch("os.path.exists", return_value=True) as os_path:
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
@@ -1095,7 +1064,7 @@ async def test_run_with_kvm_linux(linux_platform, vm):
@pytest.mark.asyncio
async def test_run_with_kvm_linux_options_no_kvm(linux_platform, vm):
async def test_run_with_kvm_options_no_kvm(vm):
with patch("os.path.exists", return_value=True) as os_path:
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
@@ -1103,7 +1072,7 @@ async def test_run_with_kvm_linux_options_no_kvm(linux_platform, vm):
@pytest.mark.asyncio
async def test_run_with_kvm_not_x86(linux_platform, vm):
async def test_run_with_kvm_not_x86(vm):
with patch("os.path.exists", return_value=True):
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
@@ -1113,7 +1082,7 @@ async def test_run_with_kvm_not_x86(linux_platform, vm):
@pytest.mark.asyncio
async def test_run_with_kvm_linux_dev_kvm_missing(linux_platform, vm):
async def test_run_with_kvm_dev_kvm_missing(vm):
with patch("os.path.exists", return_value=False):
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True