mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-22 14:05:19 +02:00
refactor: remove local gns3vm support and remove more platform-specific code
This commit is contained in:
36 files changed
+63
-2739
No files matched your search
Whitespace-only changes.
@@ -1,80 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2020 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/>.
|
||||
|
||||
import pytest
|
||||
from pydantic import SecretStr
|
||||
|
||||
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
|
||||
from gns3server.controller.gns3vm.remote_gns3_vm import RemoteGNS3VM
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def gns3vm(controller):
|
||||
|
||||
return RemoteGNS3VM(controller)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list(gns3vm, controller):
|
||||
|
||||
await controller.add_compute("r1", name="R1", host="r1.local", connect=False)
|
||||
res = await gns3vm.list()
|
||||
assert res == [{"vmname": "R1"}]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start(gns3vm, controller):
|
||||
|
||||
await controller.add_compute(
|
||||
"r1",
|
||||
name="R1",
|
||||
protocol="https",
|
||||
host="r1.local",
|
||||
port=8484,
|
||||
user="hello",
|
||||
password=SecretStr("world"),
|
||||
connect=False,
|
||||
)
|
||||
|
||||
gns3vm.vmname = "R1"
|
||||
await gns3vm.start()
|
||||
assert gns3vm.running
|
||||
assert gns3vm.protocol == "https"
|
||||
assert gns3vm.ip_address == "r1.local"
|
||||
assert gns3vm.port == 8484
|
||||
assert gns3vm.user == "hello"
|
||||
assert gns3vm.password.get_secret_value() == "world"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_invalid_vm(gns3vm, controller):
|
||||
|
||||
await controller.add_compute(
|
||||
"r1",
|
||||
connect=False,
|
||||
name="R1",
|
||||
protocol="https",
|
||||
host="r1.local",
|
||||
port=8484,
|
||||
user="hello",
|
||||
password=SecretStr("world"),
|
||||
)
|
||||
|
||||
gns3vm.vmname = "R2"
|
||||
with pytest.raises(GNS3VMError):
|
||||
await gns3vm.start()
|
||||
assert not gns3vm.running
|
||||
@@ -1,78 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2020 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/>.
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
|
||||
from gns3server.controller.gns3vm.virtualbox_gns3_vm import VirtualBoxGNS3VM
|
||||
from gns3server.utils.asyncio import wait_run_in_executor
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def gns3vm(controller):
|
||||
|
||||
vm = VirtualBoxGNS3VM(controller)
|
||||
vm.vmname = "GNS3 VM"
|
||||
return vm
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_look_for_interface(gns3vm):
|
||||
|
||||
showvminfo = """
|
||||
nic1="hostonly"
|
||||
nictype1="82540EM"
|
||||
nicspeed1="0"
|
||||
nic2="nat"
|
||||
nictype2="82540EM"
|
||||
nicspeed2="0"
|
||||
nic3="none"
|
||||
nic4="none"
|
||||
nic5="none"
|
||||
nic6="none"
|
||||
nic7="none"
|
||||
nic8="none"
|
||||
vcpwidth=1024
|
||||
vcpheight=768
|
||||
vcprate=512
|
||||
vcpfps=25
|
||||
GuestMemoryBalloon=0
|
||||
"""
|
||||
|
||||
with asyncio_patch(
|
||||
"gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM._execute", return_value=showvminfo
|
||||
) as mock:
|
||||
res = await gns3vm._look_for_interface("nat")
|
||||
mock.assert_called_with("showvminfo", ["GNS3 VM", "--machinereadable"])
|
||||
assert res == 2
|
||||
|
||||
# with asyncio_patch("gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM._execute") as mock:
|
||||
# mock.side_effect = execute_mock
|
||||
# res = await gns3vm._look_for_interface("dummy")
|
||||
# assert mock.called
|
||||
# assert res == -1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cpu_vendor_id(gns3vm):
|
||||
|
||||
from cpuinfo import get_cpu_info
|
||||
|
||||
cpu_info = await wait_run_in_executor(get_cpu_info)
|
||||
vendor_id = cpu_info.get("vendor_id_raw")
|
||||
assert vendor_id # vendor id should not be empty
|
||||
@@ -1,35 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2020 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/>.
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
|
||||
from gns3server.controller.gns3vm.vmware_gns3_vm import VMwareGNS3VM
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def gns3vm(controller):
|
||||
|
||||
vm = VMwareGNS3VM(controller)
|
||||
vm.vmname = "GNS3 VM"
|
||||
return vm
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vmx_path(tmpdir):
|
||||
|
||||
return str(tmpdir / "vmwware_vm.vmx")
|
||||
@@ -342,8 +342,6 @@ async def test_getProject(controller):
|
||||
@pytest.mark.asyncio
|
||||
async def test_start(controller):
|
||||
|
||||
controller.gns3vm.settings = {"enable": False, "engine": "vmware", "vmname": "GNS3 VM"}
|
||||
|
||||
# with asyncio_patch("gns3server.controller.compute.Compute.connect") as mock:
|
||||
with asyncio_patch("gns3server.controller.Controller._install_builtin_disks", return_value=[]):
|
||||
await controller.start()
|
||||
@@ -352,25 +350,6 @@ async def test_start(controller):
|
||||
assert controller.computes["local"].name == f"{socket.gethostname()} (controller)"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_vm(controller):
|
||||
"""
|
||||
Start the controller with a GNS3 VM
|
||||
"""
|
||||
|
||||
controller.gns3vm.settings = {"enable": True, "engine": "vmware", "vmname": "GNS3 VM"}
|
||||
|
||||
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.start") as mock:
|
||||
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network"):
|
||||
with asyncio_patch("gns3server.controller.compute.Compute.connect"):
|
||||
with asyncio_patch("gns3server.controller.Controller._install_builtin_disks", return_value=[]):
|
||||
await controller.start()
|
||||
assert mock.called
|
||||
assert "local" in controller.computes
|
||||
assert "vm" in controller.computes
|
||||
assert len(controller.computes) == 2 # Local compute and vm are created
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop(controller):
|
||||
|
||||
@@ -380,48 +359,6 @@ async def test_stop(controller):
|
||||
assert c.connected is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop_vm(controller):
|
||||
"""
|
||||
Stop GNS3 VM if configured
|
||||
"""
|
||||
|
||||
controller.gns3vm.settings = {"enable": True, "engine": "vmware", "when_exit": "stop", "vmname": "GNS3 VM"}
|
||||
|
||||
controller.gns3vm.current_engine().running = True
|
||||
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.stop") as mock:
|
||||
await controller.stop()
|
||||
assert mock.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_suspend_vm(controller):
|
||||
"""
|
||||
Suspend GNS3 VM if configured
|
||||
"""
|
||||
|
||||
controller.gns3vm.settings = {"enable": True, "engine": "vmware", "when_exit": "suspend", "vmname": "GNS3 VM"}
|
||||
|
||||
controller.gns3vm.current_engine().running = True
|
||||
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.suspend") as mock:
|
||||
await controller.stop()
|
||||
assert mock.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_keep_vm(controller):
|
||||
"""
|
||||
Keep GNS3 VM if configured
|
||||
"""
|
||||
|
||||
controller.gns3vm.settings = {"enable": True, "engine": "vmware", "when_exit": "keep", "vmname": "GNS3 VM"}
|
||||
|
||||
controller.gns3vm.current_engine().running = True
|
||||
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.suspend") as mock:
|
||||
await controller.stop()
|
||||
assert not mock.called
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_free_project_name(controller):
|
||||
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2020 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/>.
|
||||
|
||||
|
||||
import pytest
|
||||
from pydantic import SecretStr
|
||||
|
||||
from gns3server.controller.gns3vm import GNS3VM
|
||||
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
|
||||
from tests.utils import AsyncioMagicMock, asyncio_patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_engine():
|
||||
|
||||
engine = AsyncioMagicMock()
|
||||
engine.running = False
|
||||
engine.ip_address = "vm.local"
|
||||
engine.protocol = "https"
|
||||
engine.port = 8442
|
||||
engine.user = "hello"
|
||||
engine.password = SecretStr("world")
|
||||
return engine
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_gns3vm(controller, dummy_engine):
|
||||
|
||||
vm = GNS3VM(controller)
|
||||
vm._settings["engine"] = "dummy"
|
||||
vm._settings["vmname"] = "Test VM"
|
||||
vm._settings["enable"] = True
|
||||
vm._engines["dummy"] = dummy_engine
|
||||
return vm
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list(controller):
|
||||
|
||||
vm = GNS3VM(controller)
|
||||
with asyncio_patch(
|
||||
"gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.list",
|
||||
return_value=[{"vmname": "test", "vmx_path": "test"}],
|
||||
):
|
||||
res = await vm.list("vmware")
|
||||
assert res == [{"vmname": "test"}] # Information specific to VMware is stripped
|
||||
with asyncio_patch(
|
||||
"gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM.list", return_value=[{"vmname": "test"}]
|
||||
):
|
||||
res = await vm.list("virtualbox")
|
||||
assert res == [{"vmname": "test"}]
|
||||
with pytest.raises(NotImplementedError):
|
||||
await vm.list("hyperv")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_json(controller):
|
||||
|
||||
vm = GNS3VM(controller)
|
||||
assert vm.asdict() == vm._settings
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_settings(controller):
|
||||
|
||||
vm = GNS3VM(controller)
|
||||
vm.settings = {"enable": True, "engine": "vmware", "vmname": "GNS3 VM"}
|
||||
|
||||
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.start"):
|
||||
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network"):
|
||||
await vm.auto_start_vm()
|
||||
assert "vm" in controller.computes
|
||||
await vm.update_settings({"enable": False})
|
||||
assert "vm" not in controller.computes
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auto_start(controller, dummy_gns3vm, dummy_engine):
|
||||
"""
|
||||
When start the compute should be add to the controller
|
||||
"""
|
||||
|
||||
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network"):
|
||||
await dummy_gns3vm.auto_start_vm()
|
||||
assert dummy_engine.start.called
|
||||
assert controller.computes["vm"].name == "GNS3 VM (Test VM)"
|
||||
assert controller.computes["vm"].host == "vm.local"
|
||||
assert controller.computes["vm"].port == 80
|
||||
assert controller.computes["vm"].protocol == "https"
|
||||
assert controller.computes["vm"].user == "hello"
|
||||
assert controller.computes["vm"].password.get_secret_value() == "world"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auto_start_with_error(controller, dummy_gns3vm, dummy_engine):
|
||||
|
||||
dummy_engine.start.side_effect = GNS3VMError("Dummy error")
|
||||
await dummy_gns3vm.auto_start_vm()
|
||||
assert dummy_engine.start.called
|
||||
assert controller.computes["vm"].name == "GNS3 VM (Test VM)"
|
||||
Reference in new issue
Block a user