2015-02-19 15:46:57 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2020-06-16 04:29:03 +00:00
|
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2015-02-19 15:46:57 +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
|
|
|
|
|
import asyncio
|
|
|
|
|
import os
|
2015-04-27 21:12:13 +00:00
|
|
|
|
import sys
|
2015-02-19 15:46:57 +00:00
|
|
|
|
import stat
|
2016-09-21 17:25:15 +00:00
|
|
|
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
2015-08-27 16:27:17 +00:00
|
|
|
|
from unittest import mock
|
2015-02-19 15:46:57 +00:00
|
|
|
|
from unittest.mock import patch, MagicMock
|
2015-08-27 16:27:17 +00:00
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
|
from gns3server.compute.qemu.qemu_vm import QemuVM
|
|
|
|
|
from gns3server.compute.qemu.qemu_error import QemuError
|
|
|
|
|
from gns3server.compute.qemu import Qemu
|
2016-05-30 08:52:39 +00:00
|
|
|
|
from gns3server.utils import force_unix_path, macaddress_to_int, int_to_macaddress
|
2016-04-15 15:57:06 +00:00
|
|
|
|
from gns3server.compute.notification_manager import NotificationManager
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
2016-10-24 19:39:35 +00:00
|
|
|
|
@pytest.fixture
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def manager(loop, port_manager):
|
|
|
|
|
|
2015-02-19 15:46:57 +00:00
|
|
|
|
m = Qemu.instance()
|
|
|
|
|
m.port_manager = port_manager
|
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-06-16 04:29:03 +00:00
|
|
|
|
def fake_qemu_img_binary(monkeypatch, tmpdir):
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
monkeypatch.setenv("PATH", str(tmpdir))
|
2015-06-06 21:15:03 +00:00
|
|
|
|
bin_path = os.path.join(os.environ["PATH"], "qemu-img")
|
2015-02-19 15:46:57 +00:00
|
|
|
|
with open(bin_path, "w+") as f:
|
|
|
|
|
f.write("1")
|
|
|
|
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
|
return bin_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-06-16 04:29:03 +00:00
|
|
|
|
def fake_qemu_binary(monkeypatch, tmpdir):
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
monkeypatch.setenv("PATH", str(tmpdir))
|
2015-04-27 21:12:13 +00:00
|
|
|
|
if sys.platform.startswith("win"):
|
2016-09-27 16:01:50 +00:00
|
|
|
|
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64w.exe")
|
2015-04-27 21:12:13 +00:00
|
|
|
|
else:
|
2015-06-10 13:49:24 +00:00
|
|
|
|
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64")
|
2015-02-19 15:46:57 +00:00
|
|
|
|
with open(bin_path, "w+") as f:
|
|
|
|
|
f.write("1")
|
|
|
|
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
|
return bin_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2020-06-16 08:57:16 +00:00
|
|
|
|
async def vm(compute_project, manager, fake_qemu_binary, fake_qemu_img_binary):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
2015-03-03 13:37:34 +00:00
|
|
|
|
manager.port_manager.console_host = "127.0.0.1"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, qemu_path=fake_qemu_binary)
|
2015-12-22 12:15:28 +00:00
|
|
|
|
vm._process_priority = "normal" # Avoid complexity for Windows tests
|
2017-07-11 11:42:47 +00:00
|
|
|
|
vm._start_ubridge = AsyncioMagicMock()
|
|
|
|
|
vm._ubridge_hypervisor = MagicMock()
|
|
|
|
|
vm._ubridge_hypervisor.is_running.return_value = True
|
2018-03-22 07:05:31 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_hardware_acceleration", False)
|
2015-12-22 12:15:28 +00:00
|
|
|
|
return vm
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
2015-02-25 14:42:01 +00:00
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def running_subprocess_mock():
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
2015-02-25 14:42:01 +00:00
|
|
|
|
mm = MagicMock()
|
|
|
|
|
mm.returncode = None
|
|
|
|
|
return mm
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 08:57:16 +00:00
|
|
|
|
async def test_vm(compute_project, manager, fake_qemu_binary):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
|
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, qemu_path=fake_qemu_binary)
|
2015-02-19 15:46:57 +00:00
|
|
|
|
assert vm.name == "test"
|
|
|
|
|
assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_vm_create(tmpdir, compute_project, manager, fake_qemu_binary):
|
|
|
|
|
|
2018-01-29 13:20:48 +00:00
|
|
|
|
fake_img = str(tmpdir / 'hello')
|
|
|
|
|
|
|
|
|
|
with open(fake_img, 'w+') as f:
|
|
|
|
|
f.write('hello')
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, qemu_path=fake_qemu_binary)
|
2019-05-18 12:19:27 +00:00
|
|
|
|
vm._hda_disk_image = fake_img
|
2018-01-29 13:20:48 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.create()
|
2018-01-29 13:20:48 +00:00
|
|
|
|
|
|
|
|
|
# tests if `create` created md5sums
|
|
|
|
|
assert os.path.exists(str(tmpdir / 'hello.md5sum'))
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 08:57:16 +00:00
|
|
|
|
async def test_vm_invalid_qemu_with_platform(compute_project, manager, fake_qemu_binary):
|
2016-01-28 15:14:26 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, qemu_path="/usr/fake/bin/qemu-system-64", platform="x86_64")
|
2016-01-28 15:14:26 +00:00
|
|
|
|
|
|
|
|
|
assert vm.qemu_path == fake_qemu_binary
|
|
|
|
|
assert vm.platform == "x86_64"
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 08:57:16 +00:00
|
|
|
|
async def test_vm_invalid_qemu_without_platform(compute_project, manager, fake_qemu_binary):
|
2016-01-28 15:14:26 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, qemu_path="/usr/fake/bin/qemu-system-x86_64")
|
2016-01-28 15:14:26 +00:00
|
|
|
|
|
|
|
|
|
assert vm.qemu_path == fake_qemu_binary
|
|
|
|
|
assert vm.platform == "x86_64"
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_is_running(vm, running_subprocess_mock):
|
2015-02-25 14:42:01 +00:00
|
|
|
|
|
|
|
|
|
vm._process = None
|
|
|
|
|
assert vm.is_running() is False
|
|
|
|
|
vm._process = running_subprocess_mock
|
|
|
|
|
assert vm.is_running()
|
|
|
|
|
vm._process.returncode = -1
|
|
|
|
|
assert vm.is_running() is False
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_start(vm, running_subprocess_mock):
|
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2016-11-09 08:47:48 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.QemuVM.start_wrap_console"):
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=running_subprocess_mock) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.start()
|
2016-11-09 08:47:48 +00:00
|
|
|
|
assert vm.is_running()
|
|
|
|
|
assert vm.command_line == ' '.join(mock.call_args[0])
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_stop(vm, running_subprocess_mock):
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
process = running_subprocess_mock
|
2015-02-19 15:46:57 +00:00
|
|
|
|
# Wait process kill success
|
|
|
|
|
future = asyncio.Future()
|
|
|
|
|
future.set_result(True)
|
|
|
|
|
process.wait.return_value = future
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2016-11-09 08:47:48 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.QemuVM.start_wrap_console"):
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
|
|
|
|
nio = Qemu.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
|
|
|
|
await vm.start()
|
2016-11-09 08:47:48 +00:00
|
|
|
|
assert vm.is_running()
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.stop()
|
2016-11-09 08:47:48 +00:00
|
|
|
|
assert vm.is_running() is False
|
|
|
|
|
process.terminate.assert_called_with()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_termination_callback(vm):
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
|
|
|
|
vm.status = "started"
|
2016-03-17 14:15:30 +00:00
|
|
|
|
with NotificationManager.instance().queue() as queue:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._termination_callback(0)
|
2016-03-17 14:15:30 +00:00
|
|
|
|
assert vm.status == "stopped"
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await queue.get(1) # Ping
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
(action, event, kwargs) = await queue.get(1)
|
2016-05-13 15:49:28 +00:00
|
|
|
|
assert action == "node.updated"
|
2016-03-17 14:15:30 +00:00
|
|
|
|
assert event == vm
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
|
|
|
|
|
2017-04-18 14:35:36 +00:00
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_termination_callback_error(vm, tmpdir):
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
|
|
|
|
with open(str(tmpdir / "qemu.log"), "w+") as f:
|
|
|
|
|
f.write("BOOMM")
|
|
|
|
|
|
|
|
|
|
vm.status = "started"
|
|
|
|
|
vm._stdout_file = str(tmpdir / "qemu.log")
|
|
|
|
|
|
2016-03-17 14:15:30 +00:00
|
|
|
|
with NotificationManager.instance().queue() as queue:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._termination_callback(1)
|
2016-03-17 14:15:30 +00:00
|
|
|
|
assert vm.status == "stopped"
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await queue.get(1) # Ping
|
2016-03-17 14:15:30 +00:00
|
|
|
|
|
|
|
|
|
(action, event, kwargs) = queue.get_nowait()
|
2016-05-13 15:49:28 +00:00
|
|
|
|
assert action == "node.updated"
|
2016-03-17 14:15:30 +00:00
|
|
|
|
assert event == vm
|
|
|
|
|
|
|
|
|
|
(action, event, kwargs) = queue.get_nowait()
|
|
|
|
|
assert action == "log.error"
|
|
|
|
|
assert event["message"] == "QEMU process has stopped, return code: 1\nBOOMM"
|
2015-06-26 12:41:58 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_reload(vm):
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.QemuVM._control_vm") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.reload()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
assert mock.called_with("system_reset")
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_suspend(vm):
|
2015-02-19 18:43:45 +00:00
|
|
|
|
|
|
|
|
|
control_vm_result = MagicMock()
|
|
|
|
|
control_vm_result.match.group.decode.return_value = "running"
|
2016-04-15 15:57:06 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.QemuVM._control_vm", return_value=control_vm_result) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.suspend()
|
2015-02-19 18:43:45 +00:00
|
|
|
|
assert mock.called_with("system_reset")
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_add_nio_binding_udp(vm):
|
|
|
|
|
|
2017-07-11 11:42:47 +00:00
|
|
|
|
nio = Qemu.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
|
|
|
|
|
assert nio.lport == 4242
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
2015-02-19 15:46:57 +00:00
|
|
|
|
assert nio.lport == 4242
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_port_remove_nio_binding(vm):
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2017-07-11 11:42:47 +00:00
|
|
|
|
nio = Qemu.instance().create_nio({"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1", "filters": {}})
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
|
|
|
|
await vm.adapter_remove_nio_binding(0)
|
2015-02-19 15:46:57 +00:00
|
|
|
|
assert vm._ethernet_adapters[0].ports[0] is None
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_close(vm, port_manager):
|
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2016-11-09 08:47:48 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.QemuVM.start_wrap_console"):
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.start()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2016-11-09 08:47:48 +00:00
|
|
|
|
console_port = vm.console
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm.close()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2016-11-09 08:47:48 +00:00
|
|
|
|
# Raise an exception if the port is not free
|
|
|
|
|
port_manager.reserve_tcp_port(console_port, vm.project)
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2016-11-09 08:47:48 +00:00
|
|
|
|
assert vm.is_running() is False
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_qemu_path(vm, tmpdir, fake_qemu_binary):
|
|
|
|
|
|
|
|
|
|
# Raise because none
|
|
|
|
|
with pytest.raises(QemuError):
|
|
|
|
|
vm.qemu_path = None
|
|
|
|
|
|
2015-04-01 16:24:36 +00:00
|
|
|
|
# Should not crash with unicode characters
|
2016-09-27 16:01:50 +00:00
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
|
path = str(tmpdir / "\u62FF" / "qemu-system-mipsw.exe")
|
|
|
|
|
else:
|
|
|
|
|
path = str(tmpdir / "\u62FF" / "qemu-system-mips")
|
2015-06-10 13:49:24 +00:00
|
|
|
|
|
2015-06-12 07:40:38 +00:00
|
|
|
|
os.makedirs(str(tmpdir / "\u62FF"))
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
# Raise because file doesn't exists
|
|
|
|
|
with pytest.raises(QemuError):
|
|
|
|
|
vm.qemu_path = path
|
|
|
|
|
|
|
|
|
|
with open(path, "w+") as f:
|
|
|
|
|
f.write("1")
|
|
|
|
|
|
|
|
|
|
# Raise because file is not executable
|
2015-04-27 21:12:13 +00:00
|
|
|
|
if not sys.platform.startswith("win"):
|
|
|
|
|
with pytest.raises(QemuError):
|
|
|
|
|
vm.qemu_path = path
|
2016-09-27 16:01:50 +00:00
|
|
|
|
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
vm.qemu_path = path
|
|
|
|
|
assert vm.qemu_path == path
|
2015-06-10 13:49:24 +00:00
|
|
|
|
assert vm.platform == "mips"
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_set_qemu_path_environ(vm, tmpdir, fake_qemu_binary):
|
|
|
|
|
|
|
|
|
|
# It should find the binary in the path
|
2015-06-10 13:49:24 +00:00
|
|
|
|
vm.qemu_path = "qemu-system-x86_64"
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
assert vm.qemu_path == fake_qemu_binary
|
2015-06-10 13:49:24 +00:00
|
|
|
|
assert vm.platform == "x86_64"
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
def test_set_qemu_path_windows(vm):
|
2015-08-26 11:47:12 +00:00
|
|
|
|
|
|
|
|
|
bin_path = os.path.join(os.environ["PATH"], "qemu-system-x86_64w.EXE")
|
|
|
|
|
open(bin_path, "w+").close()
|
2016-09-27 16:01:50 +00:00
|
|
|
|
if not sys.platform.startswith("win"):
|
|
|
|
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
2015-08-26 11:47:12 +00:00
|
|
|
|
|
|
|
|
|
vm.qemu_path = bin_path
|
|
|
|
|
|
|
|
|
|
assert vm.qemu_path == bin_path
|
|
|
|
|
assert vm.platform == "x86_64"
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
def test_set_qemu_path_old_windows(vm):
|
2016-01-14 16:40:58 +00:00
|
|
|
|
|
|
|
|
|
bin_path = os.path.join(os.environ["PATH"], "qemu.exe")
|
|
|
|
|
open(bin_path, "w+").close()
|
2016-09-27 16:01:50 +00:00
|
|
|
|
if not sys.platform.startswith("win"):
|
|
|
|
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
2016-01-14 16:40:58 +00:00
|
|
|
|
|
|
|
|
|
vm.qemu_path = bin_path
|
|
|
|
|
|
|
|
|
|
assert vm.qemu_path == bin_path
|
|
|
|
|
assert vm.platform == "i386"
|
|
|
|
|
|
|
|
|
|
|
2015-12-22 12:15:28 +00:00
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
|
def test_set_qemu_path_kvm_binary(vm, fake_qemu_binary):
|
2015-06-10 13:49:24 +00:00
|
|
|
|
|
|
|
|
|
bin_path = os.path.join(os.environ["PATH"], "qemu-kvm")
|
|
|
|
|
with open(bin_path, "w+") as f:
|
|
|
|
|
f.write("1")
|
|
|
|
|
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
|
|
|
|
|
|
# It should find the binary in the path
|
|
|
|
|
vm.qemu_path = "qemu-kvm"
|
|
|
|
|
|
2015-11-09 14:01:02 +00:00
|
|
|
|
assert vm.qemu_path.endswith("qemu-kvm")
|
2015-06-10 13:49:24 +00:00
|
|
|
|
assert vm.platform == "x86_64"
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 08:57:16 +00:00
|
|
|
|
async def test_set_platform(compute_project, manager):
|
2015-06-10 13:49:24 +00:00
|
|
|
|
|
2020-08-13 07:40:31 +00:00
|
|
|
|
manager.config_disk = None # avoids conflict with config.img support
|
2015-06-10 13:49:24 +00:00
|
|
|
|
with patch("shutil.which", return_value="/bin/qemu-system-x86_64") as which_mock:
|
2016-04-15 15:57:06 +00:00
|
|
|
|
with patch("gns3server.compute.qemu.QemuVM._check_qemu_path"):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager, platform="x86_64")
|
2015-06-10 13:49:24 +00:00
|
|
|
|
if sys.platform.startswith("win"):
|
2015-08-27 16:27:17 +00:00
|
|
|
|
which_mock.assert_called_with("qemu-system-x86_64w.exe", path=mock.ANY)
|
2015-06-10 13:49:24 +00:00
|
|
|
|
else:
|
2015-08-27 16:27:17 +00:00
|
|
|
|
which_mock.assert_called_with("qemu-system-x86_64", path=mock.ANY)
|
2015-06-10 13:49:24 +00:00
|
|
|
|
assert vm.platform == "x86_64"
|
|
|
|
|
assert vm.qemu_path == "/bin/qemu-system-x86_64"
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_disk_options(vm, tmpdir, fake_qemu_img_binary):
|
2015-07-23 08:46:43 +00:00
|
|
|
|
|
|
|
|
|
vm._hda_disk_image = str(tmpdir / "test.qcow2")
|
|
|
|
|
open(vm._hda_disk_image, "w+").close()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
options = await vm._disk_options()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
assert process.called
|
|
|
|
|
args, kwargs = process.call_args
|
2015-07-23 08:46:43 +00:00
|
|
|
|
assert args == (fake_qemu_img_binary, "create", "-o", "backing_file={}".format(vm._hda_disk_image), "-f", "qcow2", os.path.join(vm.working_dir, "hda_disk.qcow2"))
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2019-09-03 14:45:50 +00:00
|
|
|
|
assert options == ['-drive', 'file=' + os.path.join(vm.working_dir, "hda_disk.qcow2") + ',if=ide,index=0,media=disk,id=drive0']
|
2016-04-05 10:35:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_cdrom_option(vm, tmpdir, fake_qemu_img_binary):
|
2016-12-08 15:18:30 +00:00
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2016-12-08 15:18:30 +00:00
|
|
|
|
vm._cdrom_image = str(tmpdir / "test.iso")
|
|
|
|
|
open(vm._cdrom_image, "w+").close()
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
options = await vm._build_command()
|
2016-12-08 15:18:30 +00:00
|
|
|
|
|
|
|
|
|
assert ' '.join(['-cdrom', str(tmpdir / "test.iso")]) in ' '.join(options)
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_bios_option(vm, tmpdir, fake_qemu_img_binary):
|
2016-12-08 15:18:30 +00:00
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2016-12-08 15:18:30 +00:00
|
|
|
|
vm._bios_image = str(tmpdir / "test.img")
|
|
|
|
|
open(vm._bios_image, "w+").close()
|
2020-06-16 04:29:03 +00:00
|
|
|
|
options = await vm._build_command()
|
2016-12-08 15:18:30 +00:00
|
|
|
|
assert ' '.join(['-bios', str(tmpdir / "test.img")]) in ' '.join(options)
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_vnc_option(vm, fake_qemu_img_binary):
|
|
|
|
|
|
2017-06-16 08:03:33 +00:00
|
|
|
|
vm._console_type = 'vnc'
|
|
|
|
|
vm._console = 5905
|
2020-06-16 04:29:03 +00:00
|
|
|
|
options = await vm._build_command()
|
2017-06-16 08:03:33 +00:00
|
|
|
|
assert '-vnc 127.0.0.1:5' in ' '.join(options)
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_spice_option(vm, fake_qemu_img_binary):
|
|
|
|
|
|
2017-06-16 08:03:33 +00:00
|
|
|
|
vm._console_type = 'spice'
|
|
|
|
|
vm._console = 5905
|
2020-06-16 04:29:03 +00:00
|
|
|
|
options = await vm._build_command()
|
2017-06-16 08:03:33 +00:00
|
|
|
|
assert '-spice addr=127.0.0.1,port=5905,disable-ticketing' in ' '.join(options)
|
2017-06-22 10:56:28 +00:00
|
|
|
|
assert '-vga qxl' in ' '.join(options)
|
2017-06-16 08:03:33 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_disk_options_multiple_disk(vm, tmpdir, fake_qemu_img_binary):
|
2016-04-05 10:35:07 +00:00
|
|
|
|
|
|
|
|
|
vm._hda_disk_image = str(tmpdir / "test0.qcow2")
|
|
|
|
|
vm._hdb_disk_image = str(tmpdir / "test1.qcow2")
|
|
|
|
|
vm._hdc_disk_image = str(tmpdir / "test2.qcow2")
|
|
|
|
|
vm._hdd_disk_image = str(tmpdir / "test3.qcow2")
|
|
|
|
|
open(vm._hda_disk_image, "w+").close()
|
|
|
|
|
open(vm._hdb_disk_image, "w+").close()
|
|
|
|
|
open(vm._hdc_disk_image, "w+").close()
|
|
|
|
|
open(vm._hdd_disk_image, "w+").close()
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
options = await vm._disk_options()
|
2016-04-05 10:35:07 +00:00
|
|
|
|
|
|
|
|
|
assert options == [
|
2019-09-03 14:45:50 +00:00
|
|
|
|
'-drive', 'file=' + os.path.join(vm.working_dir, "hda_disk.qcow2") + ',if=ide,index=0,media=disk,id=drive0',
|
|
|
|
|
'-drive', 'file=' + os.path.join(vm.working_dir, "hdb_disk.qcow2") + ',if=ide,index=1,media=disk,id=drive1',
|
|
|
|
|
'-drive', 'file=' + os.path.join(vm.working_dir, "hdc_disk.qcow2") + ',if=ide,index=2,media=disk,id=drive2',
|
|
|
|
|
'-drive', 'file=' + os.path.join(vm.working_dir, "hdd_disk.qcow2") + ',if=ide,index=3,media=disk,id=drive3'
|
2016-04-05 10:35:07 +00:00
|
|
|
|
]
|
|
|
|
|
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
2015-04-27 21:12:13 +00:00
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_set_process_priority(vm, fake_qemu_img_binary):
|
2015-02-19 15:46:57 +00:00
|
|
|
|
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
|
|
|
|
vm._process = MagicMock()
|
|
|
|
|
vm._process.pid = 42
|
2015-12-22 12:15:28 +00:00
|
|
|
|
vm._process_priority = "low"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._set_process_priority()
|
2015-02-19 15:46:57 +00:00
|
|
|
|
assert process.called
|
|
|
|
|
args, kwargs = process.call_args
|
|
|
|
|
assert args == ("renice", "-n", "5", "-p", "42")
|
2015-02-19 19:22:30 +00:00
|
|
|
|
|
|
|
|
|
|
2015-12-22 12:15:28 +00:00
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_set_process_priority_normal(vm, fake_qemu_img_binary):
|
2015-12-22 12:15:28 +00:00
|
|
|
|
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
|
|
|
|
vm._process = MagicMock()
|
|
|
|
|
vm._process.pid = 42
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._set_process_priority()
|
2015-12-22 12:15:28 +00:00
|
|
|
|
assert not process.called
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
def test_json(vm, compute_project):
|
2015-02-19 19:22:30 +00:00
|
|
|
|
|
|
|
|
|
json = vm.__json__()
|
|
|
|
|
assert json["name"] == vm.name
|
2020-06-16 04:29:03 +00:00
|
|
|
|
assert json["project_id"] == compute_project.id
|
2015-02-20 23:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_control_vm(vm):
|
2015-02-20 23:15:56 +00:00
|
|
|
|
|
|
|
|
|
vm._process = MagicMock()
|
|
|
|
|
reader = MagicMock()
|
|
|
|
|
writer = MagicMock()
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.open_connection", return_value=(reader, writer)):
|
|
|
|
|
res = await vm._control_vm("test")
|
2015-02-20 23:15:56 +00:00
|
|
|
|
assert writer.write.called_with("test")
|
|
|
|
|
assert res is None
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_control_vm_expect_text(vm, running_subprocess_mock):
|
2015-02-20 23:15:56 +00:00
|
|
|
|
|
2015-02-25 14:42:01 +00:00
|
|
|
|
vm._process = running_subprocess_mock
|
2015-02-20 23:15:56 +00:00
|
|
|
|
reader = MagicMock()
|
|
|
|
|
writer = MagicMock()
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.open_connection", return_value=(reader, writer)):
|
2015-02-20 23:15:56 +00:00
|
|
|
|
|
|
|
|
|
future = asyncio.Future()
|
2015-03-07 03:08:00 +00:00
|
|
|
|
future.set_result(b"epic product")
|
2015-02-20 23:15:56 +00:00
|
|
|
|
reader.readline.return_value = future
|
|
|
|
|
|
2015-03-24 04:52:02 +00:00
|
|
|
|
vm._monitor = 4242
|
2020-06-16 04:29:03 +00:00
|
|
|
|
res = await vm._control_vm("test", [b"epic"])
|
2015-02-20 23:15:56 +00:00
|
|
|
|
assert writer.write.called_with("test")
|
|
|
|
|
|
|
|
|
|
assert res == "epic product"
|
2015-02-23 19:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command(vm, fake_qemu_binary):
|
2015-02-23 19:21:00 +00:00
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2015-02-23 19:21:00 +00:00
|
|
|
|
os.environ["DISPLAY"] = "0:0"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
cmd = await vm._build_command()
|
2017-07-11 11:42:47 +00:00
|
|
|
|
nio = vm._local_udp_tunnels[0][0]
|
2015-05-04 19:29:28 +00:00
|
|
|
|
assert cmd == [
|
|
|
|
|
fake_qemu_binary,
|
|
|
|
|
"-name",
|
|
|
|
|
"test",
|
|
|
|
|
"-m",
|
2015-08-07 15:03:06 +00:00
|
|
|
|
"256M",
|
2015-08-07 07:21:09 +00:00
|
|
|
|
"-smp",
|
|
|
|
|
"cpus=1",
|
2015-08-03 10:10:36 +00:00
|
|
|
|
"-boot",
|
|
|
|
|
"order=c",
|
2016-10-03 13:54:20 +00:00
|
|
|
|
"-uuid",
|
|
|
|
|
vm.id,
|
2015-05-04 19:29:28 +00:00
|
|
|
|
"-serial",
|
2016-11-09 08:47:48 +00:00
|
|
|
|
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
|
2015-05-13 08:19:50 +00:00
|
|
|
|
"-net",
|
|
|
|
|
"none",
|
2015-05-04 19:29:28 +00:00
|
|
|
|
"-device",
|
2017-07-11 11:42:47 +00:00
|
|
|
|
"e1000,mac={},netdev=gns3-0".format(vm._mac_address),
|
|
|
|
|
"-netdev",
|
2019-02-17 11:53:46 +00:00
|
|
|
|
"socket,id=gns3-0,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio.rport, nio.lport),
|
|
|
|
|
"-display",
|
|
|
|
|
"none"
|
2015-05-04 19:29:28 +00:00
|
|
|
|
]
|
2015-02-23 19:21:00 +00:00
|
|
|
|
|
2015-05-13 08:19:50 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_manual_uuid(vm):
|
2016-10-03 13:54:20 +00:00
|
|
|
|
"""
|
|
|
|
|
If user has set a uuid we keep it
|
|
|
|
|
"""
|
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="3.1.0")
|
2016-10-03 13:54:20 +00:00
|
|
|
|
vm.options = "-uuid e1c307a4-896f-11e6-81a5-3c07547807cc"
|
|
|
|
|
os.environ["DISPLAY"] = "0:0"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
cmd = await vm._build_command()
|
2016-10-03 13:54:20 +00:00
|
|
|
|
assert "e1c307a4-896f-11e6-81a5-3c07547807cc" in cmd
|
|
|
|
|
assert vm.id not in cmd
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_kvm(linux_platform, vm, fake_qemu_binary):
|
2016-09-21 17:25:15 +00:00
|
|
|
|
"""
|
|
|
|
|
Qemu 2.4 introduce an issue with KVM
|
|
|
|
|
"""
|
2018-03-22 07:05:31 +00:00
|
|
|
|
|
2016-09-21 17:25:15 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.3.2")
|
|
|
|
|
os.environ["DISPLAY"] = "0:0"
|
2018-03-22 07:05:31 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM._run_with_hardware_acceleration", return_value=True):
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
cmd = await vm._build_command()
|
2018-03-22 07:05:31 +00:00
|
|
|
|
nio = vm._local_udp_tunnels[0][0]
|
|
|
|
|
assert cmd == [
|
|
|
|
|
fake_qemu_binary,
|
|
|
|
|
"-name",
|
|
|
|
|
"test",
|
|
|
|
|
"-m",
|
|
|
|
|
"256M",
|
|
|
|
|
"-smp",
|
|
|
|
|
"cpus=1",
|
|
|
|
|
"-enable-kvm",
|
|
|
|
|
"-boot",
|
|
|
|
|
"order=c",
|
|
|
|
|
"-uuid",
|
|
|
|
|
vm.id,
|
|
|
|
|
"-serial",
|
|
|
|
|
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
|
|
|
|
|
"-net",
|
|
|
|
|
"none",
|
|
|
|
|
"-device",
|
|
|
|
|
"e1000,mac={},netdev=gns3-0".format(vm._mac_address),
|
|
|
|
|
"-netdev",
|
2019-02-22 11:04:49 +00:00
|
|
|
|
"socket,id=gns3-0,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio.rport, nio.lport),
|
|
|
|
|
"-nographic"
|
2018-03-22 07:05:31 +00:00
|
|
|
|
]
|
2016-09-21 17:25:15 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_kvm_2_4(linux_platform, vm, fake_qemu_binary):
|
2016-09-21 17:25:15 +00:00
|
|
|
|
"""
|
|
|
|
|
Qemu 2.4 introduce an issue with KVM
|
|
|
|
|
"""
|
2018-03-22 07:05:31 +00:00
|
|
|
|
|
2016-09-21 17:25:15 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.4.2")
|
|
|
|
|
os.environ["DISPLAY"] = "0:0"
|
2018-03-22 07:05:31 +00:00
|
|
|
|
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM._run_with_hardware_acceleration", return_value=True):
|
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
|
2020-06-16 04:29:03 +00:00
|
|
|
|
cmd = await vm._build_command()
|
2018-03-22 07:05:31 +00:00
|
|
|
|
nio = vm._local_udp_tunnels[0][0]
|
|
|
|
|
assert cmd == [
|
|
|
|
|
fake_qemu_binary,
|
|
|
|
|
"-name",
|
|
|
|
|
"test",
|
|
|
|
|
"-m",
|
|
|
|
|
"256M",
|
|
|
|
|
"-smp",
|
|
|
|
|
"cpus=1",
|
|
|
|
|
"-enable-kvm",
|
|
|
|
|
"-machine",
|
|
|
|
|
"smm=off",
|
|
|
|
|
"-boot",
|
|
|
|
|
"order=c",
|
|
|
|
|
"-uuid",
|
|
|
|
|
vm.id,
|
|
|
|
|
"-serial",
|
|
|
|
|
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
|
|
|
|
|
"-net",
|
|
|
|
|
"none",
|
|
|
|
|
"-device",
|
|
|
|
|
"e1000,mac={},netdev=gns3-0".format(vm._mac_address),
|
|
|
|
|
"-netdev",
|
2019-02-22 11:04:49 +00:00
|
|
|
|
"socket,id=gns3-0,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio.rport, nio.lport),
|
|
|
|
|
"-nographic"
|
2018-03-22 07:05:31 +00:00
|
|
|
|
]
|
2015-02-23 19:21:00 +00:00
|
|
|
|
|
2015-05-13 08:19:50 +00:00
|
|
|
|
|
2015-04-27 21:12:13 +00:00
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_without_display(vm):
|
2015-02-23 19:21:00 +00:00
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.5.0")
|
2015-02-23 19:21:00 +00:00
|
|
|
|
os.environ["DISPLAY"] = ""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
cmd = await vm._build_command()
|
2015-02-23 19:21:00 +00:00
|
|
|
|
assert "-nographic" in cmd
|
2015-03-07 10:56:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_two_adapters(vm, fake_qemu_binary):
|
2016-04-25 14:36:20 +00:00
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.5.0")
|
2016-05-30 08:52:39 +00:00
|
|
|
|
os.environ["DISPLAY"] = "0:0"
|
2016-04-25 14:36:20 +00:00
|
|
|
|
vm.adapters = 2
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
cmd = await vm._build_command()
|
2017-07-11 11:42:47 +00:00
|
|
|
|
nio1 = vm._local_udp_tunnels[0][0]
|
|
|
|
|
nio2 = vm._local_udp_tunnels[1][0]
|
2016-04-25 14:36:20 +00:00
|
|
|
|
assert cmd == [
|
|
|
|
|
fake_qemu_binary,
|
|
|
|
|
"-name",
|
|
|
|
|
"test",
|
|
|
|
|
"-m",
|
|
|
|
|
"256M",
|
|
|
|
|
"-smp",
|
|
|
|
|
"cpus=1",
|
|
|
|
|
"-boot",
|
|
|
|
|
"order=c",
|
2016-10-03 13:54:20 +00:00
|
|
|
|
"-uuid",
|
|
|
|
|
vm.id,
|
2016-04-25 14:36:20 +00:00
|
|
|
|
"-serial",
|
2016-11-09 08:47:48 +00:00
|
|
|
|
"telnet:127.0.0.1:{},server,nowait".format(vm._internal_console_port),
|
2016-04-25 14:36:20 +00:00
|
|
|
|
"-net",
|
|
|
|
|
"none",
|
|
|
|
|
"-device",
|
2017-07-11 11:42:47 +00:00
|
|
|
|
"e1000,mac={},netdev=gns3-0".format(vm._mac_address),
|
|
|
|
|
"-netdev",
|
|
|
|
|
"socket,id=gns3-0,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio1.rport, nio1.lport),
|
2016-04-25 14:36:20 +00:00
|
|
|
|
"-device",
|
2017-07-11 11:42:47 +00:00
|
|
|
|
"e1000,mac={},netdev=gns3-1".format(int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)),
|
|
|
|
|
"-netdev",
|
2019-02-17 11:53:46 +00:00
|
|
|
|
"socket,id=gns3-1,udp=127.0.0.1:{},localaddr=127.0.0.1:{}".format(nio2.rport, nio2.lport),
|
|
|
|
|
"-nographic"
|
2016-04-25 14:36:20 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_two_adapters_mac_address(vm):
|
2016-05-30 08:52:39 +00:00
|
|
|
|
"""
|
|
|
|
|
Should support multiple base vmac address
|
|
|
|
|
"""
|
2016-04-25 14:36:20 +00:00
|
|
|
|
|
2019-02-17 11:53:46 +00:00
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.5.0")
|
2016-04-25 14:36:20 +00:00
|
|
|
|
vm.adapters = 2
|
|
|
|
|
vm.mac_address = "00:00:ab:0e:0f:09"
|
2016-05-30 08:52:39 +00:00
|
|
|
|
mac_0 = vm._mac_address
|
2017-02-07 16:04:29 +00:00
|
|
|
|
mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)
|
2016-05-30 08:52:39 +00:00
|
|
|
|
assert mac_0[:8] == "00:00:ab"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
cmd = await vm._build_command()
|
2017-07-11 11:42:47 +00:00
|
|
|
|
assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd
|
|
|
|
|
assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd
|
2016-04-25 14:36:20 +00:00
|
|
|
|
|
2016-05-30 08:52:39 +00:00
|
|
|
|
vm.mac_address = "00:42:ab:0e:0f:0a"
|
|
|
|
|
mac_0 = vm._mac_address
|
2017-07-11 11:42:47 +00:00
|
|
|
|
mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)
|
2016-05-30 08:52:39 +00:00
|
|
|
|
assert mac_0[:8] == "00:42:ab"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
|
|
|
|
|
cmd = await vm._build_command()
|
2017-07-11 11:42:47 +00:00
|
|
|
|
assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd
|
|
|
|
|
assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd
|
2016-04-25 14:36:20 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_large_number_of_adapters(vm):
|
2017-02-07 16:04:29 +00:00
|
|
|
|
"""
|
|
|
|
|
When we have more than 28 interface we need to add a pci bridge for
|
2020-06-16 04:29:03 +00:00
|
|
|
|
additional interfaces
|
2017-02-07 16:04:29 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2017-02-17 08:55:50 +00:00
|
|
|
|
# It's supported only with Qemu 2.4 and later
|
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.4.0")
|
|
|
|
|
|
2017-02-07 16:04:29 +00:00
|
|
|
|
vm.adapters = 100
|
|
|
|
|
vm.mac_address = "00:00:ab:0e:0f:09"
|
|
|
|
|
mac_0 = vm._mac_address
|
|
|
|
|
mac_1 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 1)
|
|
|
|
|
assert mac_0[:8] == "00:00:ab"
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
cmd = await vm._build_command()
|
2017-02-07 16:04:29 +00:00
|
|
|
|
|
2017-07-11 11:42:47 +00:00
|
|
|
|
# Count if we have 100 e1000 adapters in the command
|
|
|
|
|
assert len([l for l in cmd if "e1000" in l ]) == 100
|
|
|
|
|
assert len(vm._ethernet_adapters) == 100
|
|
|
|
|
|
|
|
|
|
assert "e1000,mac={},netdev=gns3-0".format(mac_0) in cmd
|
|
|
|
|
assert "e1000,mac={},netdev=gns3-1".format(mac_1) in cmd
|
2017-02-07 16:04:29 +00:00
|
|
|
|
assert "pci-bridge,id=pci-bridge0,bus=dmi_pci_bridge0,chassis_nr=0x1,addr=0x0,shpc=off" not in cmd
|
|
|
|
|
assert "pci-bridge,id=pci-bridge1,bus=dmi_pci_bridge1,chassis_nr=0x1,addr=0x1,shpc=off" in cmd
|
|
|
|
|
assert "pci-bridge,id=pci-bridge2,bus=dmi_pci_bridge2,chassis_nr=0x1,addr=0x2,shpc=off" in cmd
|
|
|
|
|
assert "i82801b11-bridge,id=dmi_pci_bridge1" in cmd
|
|
|
|
|
|
|
|
|
|
mac_29 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 29)
|
2017-07-11 11:42:47 +00:00
|
|
|
|
assert "e1000,mac={},bus=pci-bridge1,addr=0x04,netdev=gns3-29".format(mac_29) in cmd
|
2017-02-07 16:04:29 +00:00
|
|
|
|
mac_30 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 30)
|
2017-07-11 11:42:47 +00:00
|
|
|
|
assert "e1000,mac={},bus=pci-bridge1,addr=0x05,netdev=gns3-30".format(mac_30) in cmd
|
2017-02-07 16:04:29 +00:00
|
|
|
|
mac_74 = int_to_macaddress(macaddress_to_int(vm._mac_address) + 74)
|
2017-07-11 11:42:47 +00:00
|
|
|
|
assert "e1000,mac={},bus=pci-bridge2,addr=0x11,netdev=gns3-74".format(mac_74) in cmd
|
2017-02-07 16:04:29 +00:00
|
|
|
|
|
2017-02-17 08:55:50 +00:00
|
|
|
|
# Qemu < 2.4 doesn't support large number of adapters
|
|
|
|
|
vm.manager.get_qemu_version = AsyncioMagicMock(return_value="2.0.0")
|
|
|
|
|
with pytest.raises(QemuError):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
await vm._build_command()
|
2017-02-17 08:55:50 +00:00
|
|
|
|
vm.adapters = 5
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
|
|
|
|
await vm._build_command()
|
2017-02-17 08:55:50 +00:00
|
|
|
|
|
|
|
|
|
|
2015-04-27 21:12:13 +00:00
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_build_command_with_invalid_options(vm):
|
2015-03-31 20:14:08 +00:00
|
|
|
|
|
|
|
|
|
vm.options = "'test"
|
|
|
|
|
with pytest.raises(QemuError):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._build_command()
|
2015-03-31 20:14:08 +00:00
|
|
|
|
|
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
def test_hda_disk_image(vm, images_dir):
|
2015-03-07 10:56:07 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
open(os.path.join(images_dir, "test1"), "w+").close()
|
|
|
|
|
vm.hda_disk_image = os.path.join(images_dir, "test1")
|
|
|
|
|
assert vm.hda_disk_image == force_unix_path(os.path.join(images_dir, "test1"))
|
|
|
|
|
open(os.path.join(images_dir, "QEMU", "test2"), "w+").close()
|
2016-06-02 13:19:34 +00:00
|
|
|
|
vm.hda_disk_image = "test2"
|
2016-06-07 13:34:04 +00:00
|
|
|
|
assert vm.hda_disk_image == force_unix_path(os.path.join(images_dir, "QEMU", "test2"))
|
2015-03-07 10:56:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 08:57:16 +00:00
|
|
|
|
async def test_hda_disk_image_non_linked_clone(vm, images_dir, compute_project, manager, fake_qemu_binary):
|
2016-10-24 19:39:35 +00:00
|
|
|
|
"""
|
|
|
|
|
Two non linked can't use the same image at the same time
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
open(os.path.join(images_dir, "test1"), "w+").close()
|
|
|
|
|
vm.linked_clone = False
|
|
|
|
|
vm.hda_disk_image = os.path.join(images_dir, "test1")
|
|
|
|
|
vm.manager._nodes[vm.id] = vm
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm2 = QemuVM("test", "00010203-0405-0607-0809-0a0b0c0d0eaa", compute_project, manager, qemu_path=fake_qemu_binary)
|
2016-10-24 19:39:35 +00:00
|
|
|
|
vm2.linked_clone = False
|
|
|
|
|
with pytest.raises(QemuError):
|
|
|
|
|
vm2.hda_disk_image = os.path.join(images_dir, "test1")
|
|
|
|
|
|
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
def test_hda_disk_image_ova(vm, images_dir):
|
2015-11-02 15:34:34 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
os.makedirs(os.path.join(images_dir, "QEMU", "test.ovf"))
|
|
|
|
|
open(os.path.join(images_dir, "QEMU", "test.ovf", "test.vmdk"), "w+").close()
|
2015-11-02 15:34:34 +00:00
|
|
|
|
vm.hda_disk_image = "test.ovf/test.vmdk"
|
2016-06-07 13:34:04 +00:00
|
|
|
|
assert vm.hda_disk_image == force_unix_path(os.path.join(images_dir, "QEMU", "test.ovf", "test.vmdk"))
|
2015-10-05 18:12:20 +00:00
|
|
|
|
|
2015-03-07 10:56:07 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
def test_hdb_disk_image(vm, images_dir):
|
2015-03-12 05:09:01 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
open(os.path.join(images_dir, "test1"), "w+").close()
|
|
|
|
|
vm.hdb_disk_image = os.path.join(images_dir, "test1")
|
|
|
|
|
assert vm.hdb_disk_image == force_unix_path(os.path.join(images_dir, "test1"))
|
|
|
|
|
open(os.path.join(images_dir, "QEMU", "test2"), "w+").close()
|
|
|
|
|
vm.hdb_disk_image = "test2"
|
|
|
|
|
assert vm.hdb_disk_image == force_unix_path(os.path.join(images_dir, "QEMU", "test2"))
|
2015-05-13 08:19:50 +00:00
|
|
|
|
|
2015-03-12 05:09:01 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
def test_hdc_disk_image(vm, images_dir):
|
2015-11-02 15:34:34 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
open(os.path.join(images_dir, "test1"), "w+").close()
|
|
|
|
|
vm.hdc_disk_image = os.path.join(images_dir, "test1")
|
|
|
|
|
assert vm.hdc_disk_image == force_unix_path(os.path.join(images_dir, "test1"))
|
|
|
|
|
open(os.path.join(images_dir, "QEMU", "test2"), "w+").close()
|
|
|
|
|
vm.hdc_disk_image = "test2"
|
|
|
|
|
assert vm.hdc_disk_image == force_unix_path(os.path.join(images_dir, "QEMU", "test2"))
|
2015-03-12 05:09:01 +00:00
|
|
|
|
|
2015-05-13 08:19:50 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
def test_hdd_disk_image(vm, images_dir):
|
2015-11-02 15:34:34 +00:00
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
|
open(os.path.join(images_dir, "test1"), "w+").close()
|
|
|
|
|
vm.hdd_disk_image = os.path.join(images_dir, "test1")
|
|
|
|
|
assert vm.hdd_disk_image == force_unix_path(os.path.join(images_dir, "test1"))
|
|
|
|
|
open(os.path.join(images_dir, "QEMU", "test2"), "w+").close()
|
|
|
|
|
vm.hdd_disk_image = "test2"
|
|
|
|
|
assert vm.hdd_disk_image == force_unix_path(os.path.join(images_dir, "QEMU", "test2"))
|
2015-06-12 07:40:38 +00:00
|
|
|
|
|
|
|
|
|
|
2016-09-27 16:01:50 +00:00
|
|
|
|
def test_initrd(vm, images_dir):
|
2016-01-22 18:46:05 +00:00
|
|
|
|
|
2016-09-27 16:01:50 +00:00
|
|
|
|
open(os.path.join(images_dir, "test1"), "w+").close()
|
|
|
|
|
vm.initrd = os.path.join(images_dir, "test1")
|
|
|
|
|
assert vm.initrd == force_unix_path(os.path.join(images_dir, "test1"))
|
|
|
|
|
open(os.path.join(images_dir, "QEMU", "test2"), "w+").close()
|
|
|
|
|
vm.initrd = "test2"
|
|
|
|
|
assert vm.initrd == force_unix_path(os.path.join(images_dir, "QEMU", "test2"))
|
2016-01-22 18:46:05 +00:00
|
|
|
|
|
|
|
|
|
|
2016-09-27 16:01:50 +00:00
|
|
|
|
def test_initrd_asa(vm, images_dir):
|
2016-01-22 18:46:05 +00:00
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
|
with patch("gns3server.compute.project.Project.emit") as mock:
|
2016-09-27 16:01:50 +00:00
|
|
|
|
open(os.path.join(images_dir, "asa842-initrd.gz"), "w+").close()
|
|
|
|
|
vm.initrd = os.path.join(images_dir, "asa842-initrd.gz")
|
|
|
|
|
assert vm.initrd == force_unix_path(os.path.join(images_dir, "asa842-initrd.gz"))
|
2016-01-22 18:46:05 +00:00
|
|
|
|
assert mock.called
|
|
|
|
|
|
|
|
|
|
|
2015-11-10 15:25:02 +00:00
|
|
|
|
def test_options(linux_platform, vm):
|
2015-06-12 07:40:38 +00:00
|
|
|
|
vm.kvm = False
|
|
|
|
|
vm.options = "-usb"
|
|
|
|
|
assert vm.options == "-usb"
|
|
|
|
|
assert vm.kvm is False
|
2015-07-27 14:19:15 +00:00
|
|
|
|
|
2015-11-10 15:25:02 +00:00
|
|
|
|
vm.options = "-no-kvm"
|
|
|
|
|
assert vm.options == "-no-kvm"
|
|
|
|
|
|
|
|
|
|
vm.options = "-enable-kvm"
|
|
|
|
|
assert vm.options == "-enable-kvm"
|
|
|
|
|
|
|
|
|
|
vm.options = "-icount 12"
|
|
|
|
|
assert vm.options == "-no-kvm -icount 12"
|
|
|
|
|
|
|
|
|
|
vm.options = "-icount 12 -no-kvm"
|
|
|
|
|
assert vm.options == "-icount 12 -no-kvm"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_options_windows(windows_platform, vm):
|
|
|
|
|
vm.options = "-no-kvm"
|
|
|
|
|
assert vm.options == ""
|
|
|
|
|
|
|
|
|
|
vm.options = "-enable-kvm"
|
|
|
|
|
assert vm.options == ""
|
|
|
|
|
|
2015-07-27 14:19:15 +00:00
|
|
|
|
|
|
|
|
|
def test_get_qemu_img(vm, tmpdir):
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
open(str(tmpdir / "qemu-system-x86_64"), "w+").close()
|
2015-07-27 14:19:15 +00:00
|
|
|
|
open(str(tmpdir / "qemu-img"), "w+").close()
|
2020-06-16 04:29:03 +00:00
|
|
|
|
vm._qemu_path = str(tmpdir / "qemu-system-x86_64")
|
2015-07-27 15:32:27 +00:00
|
|
|
|
assert vm._get_qemu_img() == str(tmpdir / "qemu-img")
|
2015-07-27 14:19:15 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
# def test_get_qemu_img_not_exist(vm, tmpdir):
|
|
|
|
|
#
|
|
|
|
|
# open(str(tmpdir / "qemu-system-x86_64"), "w+").close()
|
|
|
|
|
# vm._qemu_path = str(tmpdir / "qemu-system-x86_64")
|
|
|
|
|
# with pytest.raises(QemuError):
|
|
|
|
|
# vm._get_qemu_img()
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_run_with_hardware_acceleration_darwin(darwin_platform, vm):
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
2018-03-22 07:05:31 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_hardware_acceleration", False)
|
2020-06-16 04:29:03 +00:00
|
|
|
|
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is False
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_run_with_hardware_acceleration_windows(windows_platform, vm):
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
2018-03-22 07:05:31 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_hardware_acceleration", False)
|
2020-06-16 04:29:03 +00:00
|
|
|
|
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is False
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_run_with_kvm_linux(linux_platform, vm):
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
with patch("os.path.exists", return_value=True) as os_path:
|
2015-11-02 15:34:34 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_kvm", True)
|
2020-06-16 04:29:03 +00:00
|
|
|
|
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is True
|
2015-11-02 15:34:34 +00:00
|
|
|
|
os_path.assert_called_with("/dev/kvm")
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_run_with_kvm_linux_options_no_kvm(linux_platform, vm):
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
with patch("os.path.exists", return_value=True) as os_path:
|
2015-11-02 15:34:34 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_kvm", True)
|
2020-06-16 04:29:03 +00:00
|
|
|
|
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "-no-kvm") is False
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_run_with_kvm_not_x86(linux_platform, vm):
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with patch("os.path.exists", return_value=True):
|
2015-11-02 15:34:34 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_kvm", True)
|
2018-03-22 07:05:31 +00:00
|
|
|
|
with pytest.raises(QemuError):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._run_with_hardware_acceleration("qemu-system-arm", "")
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
async def test_run_with_kvm_linux_dev_kvm_missing(linux_platform, vm):
|
2015-08-07 14:49:45 +00:00
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
with patch("os.path.exists", return_value=False):
|
2015-11-02 15:34:34 +00:00
|
|
|
|
vm.manager.config.set("Qemu", "enable_kvm", True)
|
|
|
|
|
with pytest.raises(QemuError):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
await vm._run_with_hardware_acceleration("qemu-system-x86_64", "")
|