2016-08-24 09:36:32 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-06-16 04:29:03 +00:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2016-08-24 09:36:32 +00:00
|
|
|
#
|
|
|
|
# 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
|
2020-06-16 09:33:42 +00:00
|
|
|
import sys
|
2016-09-01 13:36:41 +00:00
|
|
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
2016-08-24 09:36:32 +00:00
|
|
|
|
|
|
|
from gns3server.controller.gns3vm import GNS3VM
|
2016-09-01 13:36:41 +00:00
|
|
|
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
|
|
|
|
|
2017-01-27 09:41:39 +00:00
|
|
|
|
2016-09-01 13:36:41 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def dummy_engine():
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-09-01 13:36:41 +00:00
|
|
|
engine = AsyncioMagicMock()
|
|
|
|
engine.running = False
|
|
|
|
engine.ip_address = "vm.local"
|
|
|
|
engine.protocol = "https"
|
|
|
|
engine.port = 8442
|
|
|
|
engine.user = "hello"
|
|
|
|
engine.password = "world"
|
|
|
|
return engine
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def dummy_gns3vm(controller, dummy_engine):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-09-01 13:36:41 +00:00
|
|
|
vm = GNS3VM(controller)
|
|
|
|
vm._settings["engine"] = "dummy"
|
|
|
|
vm._settings["vmname"] = "Test VM"
|
|
|
|
vm._settings["enable"] = True
|
|
|
|
vm._engines["dummy"] = dummy_engine
|
|
|
|
return vm
|
2016-08-24 09:36:32 +00:00
|
|
|
|
|
|
|
|
2024-05-09 10:53:02 +00:00
|
|
|
async def test_list(controller):
|
2016-08-24 09:36:32 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = GNS3VM(controller)
|
2016-08-24 09:36:32 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.list", return_value=[{"vmname": "test", "vmx_path": "test"}]):
|
2020-06-16 04:29:03 +00:00
|
|
|
res = await vm.list("vmware")
|
|
|
|
assert res == [{"vmname": "test"}] # Information specific to VMware is stripped
|
2016-08-24 09:36:32 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.virtualbox_gns3_vm.VirtualBoxGNS3VM.list", return_value=[{"vmname": "test"}]):
|
2020-06-16 04:29:03 +00:00
|
|
|
res = await vm.list("virtualbox")
|
2016-08-24 09:36:32 +00:00
|
|
|
assert res == [{"vmname": "test"}]
|
|
|
|
with pytest.raises(NotImplementedError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.list("hyperv")
|
2016-08-24 09:36:32 +00:00
|
|
|
|
|
|
|
|
2024-05-09 10:53:02 +00:00
|
|
|
async def test_json(controller):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-08-24 09:36:32 +00:00
|
|
|
vm = GNS3VM(controller)
|
|
|
|
assert vm.__json__() == vm._settings
|
2016-08-26 12:09:18 +00:00
|
|
|
|
|
|
|
|
2020-06-16 09:33:42 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
|
2024-05-09 10:53:02 +00:00
|
|
|
async def test_update_settings(controller):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-08-26 12:09:18 +00:00
|
|
|
vm = GNS3VM(controller)
|
|
|
|
vm.settings = {
|
|
|
|
"enable": True,
|
2017-01-27 09:41:39 +00:00
|
|
|
"engine": "vmware",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-08-26 12:09:18 +00:00
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-08-26 12:09:18 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.start"):
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network"):
|
|
|
|
await vm.auto_start_vm()
|
2016-08-26 12:09:18 +00:00
|
|
|
assert "vm" in controller.computes
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.update_settings({"enable": False})
|
2016-08-26 12:09:18 +00:00
|
|
|
assert "vm" not in controller.computes
|
2016-09-01 13:36:41 +00:00
|
|
|
|
|
|
|
|
2020-06-16 09:39:49 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_auto_start(controller, dummy_gns3vm, dummy_engine):
|
2016-09-01 13:36:41 +00:00
|
|
|
"""
|
|
|
|
When start the compute should be add to the controller
|
|
|
|
"""
|
2017-12-06 02:38:48 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network"):
|
|
|
|
await dummy_gns3vm.auto_start_vm()
|
2016-09-01 13:36:41 +00:00
|
|
|
assert dummy_engine.start.called
|
|
|
|
assert controller.computes["vm"].name == "GNS3 VM (Test VM)"
|
|
|
|
assert controller.computes["vm"].host == "vm.local"
|
2020-05-05 03:49:51 +00:00
|
|
|
assert controller.computes["vm"].port == 80
|
2016-09-01 13:36:41 +00:00
|
|
|
assert controller.computes["vm"].protocol == "https"
|
|
|
|
assert controller.computes["vm"].user == "hello"
|
|
|
|
assert controller.computes["vm"].password == "world"
|
|
|
|
|
2020-06-16 11:26:10 +00:00
|
|
|
|
2020-06-16 09:46:58 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_auto_start_with_error(controller, dummy_gns3vm, dummy_engine):
|
2016-09-01 13:36:41 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
dummy_engine.start.side_effect = GNS3VMError("Dummy error")
|
|
|
|
await dummy_gns3vm.auto_start_vm()
|
2016-09-01 13:36:41 +00:00
|
|
|
assert dummy_engine.start.called
|
|
|
|
assert controller.computes["vm"].name == "GNS3 VM (Test VM)"
|