mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 22:11:14 +02:00
Notification feed for the hypervisor
This commit is contained in:
15 files changed
+425
-144
No files matched your search
@@ -32,6 +32,7 @@ from gns3server.hypervisor.qemu.qemu_vm import QemuVM
|
||||
from gns3server.hypervisor.qemu.qemu_error import QemuError
|
||||
from gns3server.hypervisor.qemu import Qemu
|
||||
from gns3server.utils import force_unix_path
|
||||
from gns3server.hypervisor.notification_manager import NotificationManager
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -136,41 +137,43 @@ def test_stop(loop, vm, running_subprocess_mock):
|
||||
process.terminate.assert_called_with()
|
||||
|
||||
|
||||
def test_termination_callback(vm):
|
||||
def test_termination_callback(vm, async_run):
|
||||
|
||||
vm.status = "started"
|
||||
queue = vm.project.get_listen_queue()
|
||||
|
||||
vm._termination_callback(0)
|
||||
assert vm.status == "stopped"
|
||||
with NotificationManager.instance().queue() as queue:
|
||||
vm._termination_callback(0)
|
||||
assert vm.status == "stopped"
|
||||
|
||||
(action, event) = queue.get_nowait()
|
||||
assert action == "vm.stopped"
|
||||
assert event == vm
|
||||
async_run(queue.get(0)) # Ping
|
||||
|
||||
with pytest.raises(asyncio.queues.QueueEmpty):
|
||||
queue.get_nowait()
|
||||
(action, event, kwargs) = async_run(queue.get(0))
|
||||
assert action == "vm.stopped"
|
||||
assert event == vm
|
||||
|
||||
|
||||
def test_termination_callback_error(vm, tmpdir):
|
||||
def test_termination_callback_error(vm, tmpdir, async_run):
|
||||
|
||||
with open(str(tmpdir / "qemu.log"), "w+") as f:
|
||||
f.write("BOOMM")
|
||||
|
||||
vm.status = "started"
|
||||
vm._stdout_file = str(tmpdir / "qemu.log")
|
||||
queue = vm.project.get_listen_queue()
|
||||
|
||||
vm._termination_callback(1)
|
||||
assert vm.status == "stopped"
|
||||
with NotificationManager.instance().queue() as queue:
|
||||
vm._termination_callback(1)
|
||||
assert vm.status == "stopped"
|
||||
|
||||
(action, event) = queue.get_nowait()
|
||||
assert action == "vm.stopped"
|
||||
assert event == vm
|
||||
|
||||
(action, event) = queue.get_nowait()
|
||||
assert action == "log.error"
|
||||
assert event["message"] == "QEMU process has stopped, return code: 1\nBOOMM"
|
||||
async_run(queue.get(0)) # Ping
|
||||
|
||||
(action, event, kwargs) = queue.get_nowait()
|
||||
assert action == "vm.stopped"
|
||||
assert event == vm
|
||||
|
||||
(action, event, kwargs) = queue.get_nowait()
|
||||
assert action == "log.error"
|
||||
assert event["message"] == "QEMU process has stopped, return code: 1\nBOOMM"
|
||||
|
||||
|
||||
def test_reload(loop, vm):
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 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 uuid
|
||||
|
||||
from gns3server.hypervisor.notification_manager import NotificationManager
|
||||
|
||||
|
||||
def test_queue(async_run):
|
||||
NotificationManager.reset()
|
||||
notifications = NotificationManager.instance()
|
||||
with notifications.queue() as queue:
|
||||
assert len(notifications._listeners) == 1
|
||||
|
||||
res = async_run(queue.get(5))
|
||||
assert res[0] == "ping"
|
||||
|
||||
notifications.emit("test", {"a": 1})
|
||||
res = async_run(queue.get(5))
|
||||
assert res == ('test', {"a": 1}, {})
|
||||
|
||||
assert len(notifications._listeners) == 0
|
||||
|
||||
|
||||
def test_queue_json(async_run):
|
||||
NotificationManager.reset()
|
||||
notifications = NotificationManager.instance()
|
||||
with notifications.queue() as queue:
|
||||
assert len(notifications._listeners) == 1
|
||||
|
||||
res = async_run(queue.get(5))
|
||||
assert "ping" in res
|
||||
|
||||
notifications.emit("test", {"a": 1})
|
||||
res = async_run(queue.get_json(5))
|
||||
assert res == '{"action": "test", "event": {"a": 1}}'
|
||||
|
||||
assert len(notifications._listeners) == 0
|
||||
|
||||
|
||||
def test_queue_json_meta(async_run):
|
||||
NotificationManager.reset()
|
||||
project_id = str(uuid.uuid4())
|
||||
notifications = NotificationManager.instance()
|
||||
with notifications.queue() as queue:
|
||||
assert len(notifications._listeners) == 1
|
||||
|
||||
res = async_run(queue.get(5))
|
||||
assert "ping" in res
|
||||
|
||||
notifications.emit("test", {"a": 1}, project_id=project_id)
|
||||
res = async_run(queue.get_json(5))
|
||||
assert res == '{"action": "test", "event": {"a": 1}, "project_id": "' + project_id + '"}'
|
||||
|
||||
assert len(notifications._listeners) == 0
|
||||
|
||||
|
||||
|
||||
def test_queue_ping(async_run):
|
||||
"""
|
||||
If we don't send a message during a long time (0.5 seconds)
|
||||
a ping is send
|
||||
"""
|
||||
NotificationManager.reset()
|
||||
notifications = NotificationManager.instance()
|
||||
with notifications.queue() as queue:
|
||||
assert len(notifications._listeners) == 1
|
||||
|
||||
res = async_run(queue.get(5))
|
||||
assert res[0] == "ping"
|
||||
|
||||
res = async_run(queue.get(0.5))
|
||||
assert res[0] == "ping"
|
||||
assert res[1]["cpu_usage_percent"] is not None
|
||||
assert len(notifications._listeners) == 0
|
||||
@@ -25,6 +25,7 @@ from unittest.mock import patch
|
||||
|
||||
from tests.utils import asyncio_patch
|
||||
from gns3server.hypervisor.project import Project
|
||||
from gns3server.hypervisor.notification_manager import NotificationManager
|
||||
from gns3server.hypervisor.vpcs import VPCS, VPCSVM
|
||||
from gns3server.config import Config
|
||||
|
||||
@@ -256,3 +257,15 @@ def test_list_files(tmpdir, loop):
|
||||
"md5sum": "098f6bcd4621d373cade4e832627b4f6"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_emit(async_run):
|
||||
|
||||
with NotificationManager.instance().queue() as queue:
|
||||
(action, event, context) = async_run(queue.get(0.5)) # Ping
|
||||
|
||||
project = Project(project_id=str(uuid4()))
|
||||
project.emit("test", {})
|
||||
(action, event, context) = async_run(queue.get(0.5))
|
||||
assert action == "test"
|
||||
assert context["project_id"] == project.id
|
||||
@@ -28,6 +28,7 @@ from unittest.mock import patch, MagicMock
|
||||
from gns3server.hypervisor.vpcs.vpcs_vm import VPCSVM
|
||||
from gns3server.hypervisor.vpcs.vpcs_error import VPCSError
|
||||
from gns3server.hypervisor.vpcs import VPCS
|
||||
from gns3server.hypervisor.notification_manager import NotificationManager
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -82,35 +83,38 @@ def test_vm_invalid_vpcs_path(vm, manager, loop):
|
||||
assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0e"
|
||||
|
||||
|
||||
def test_start(loop, vm):
|
||||
def test_start(loop, vm, async_run):
|
||||
process = MagicMock()
|
||||
process.returncode = None
|
||||
queue = vm.project.get_listen_queue()
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
|
||||
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
||||
vm.port_add_nio_binding(0, nio)
|
||||
loop.run_until_complete(asyncio.async(vm.start()))
|
||||
assert mock_exec.call_args[0] == (vm.vpcs_path,
|
||||
'-p',
|
||||
str(vm.console),
|
||||
'-m', '1',
|
||||
'-i',
|
||||
'1',
|
||||
'-F',
|
||||
'-R',
|
||||
'-s',
|
||||
'4242',
|
||||
'-c',
|
||||
'4243',
|
||||
'-t',
|
||||
'127.0.0.1')
|
||||
assert vm.is_running()
|
||||
assert vm.command_line == ' '.join(mock_exec.call_args[0])
|
||||
(action, event) = queue.get_nowait()
|
||||
assert action == "vm.started"
|
||||
assert event == vm
|
||||
|
||||
with NotificationManager.instance().queue() as queue:
|
||||
async_run(queue.get(0)) # Ping
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process) as mock_exec:
|
||||
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
||||
vm.port_add_nio_binding(0, nio)
|
||||
loop.run_until_complete(asyncio.async(vm.start()))
|
||||
assert mock_exec.call_args[0] == (vm.vpcs_path,
|
||||
'-p',
|
||||
str(vm.console),
|
||||
'-m', '1',
|
||||
'-i',
|
||||
'1',
|
||||
'-F',
|
||||
'-R',
|
||||
'-s',
|
||||
'4242',
|
||||
'-c',
|
||||
'4243',
|
||||
'-t',
|
||||
'127.0.0.1')
|
||||
assert vm.is_running()
|
||||
assert vm.command_line == ' '.join(mock_exec.call_args[0])
|
||||
(action, event, kwargs) = async_run(queue.get(0))
|
||||
assert action == "vm.started"
|
||||
assert event == vm
|
||||
|
||||
|
||||
def test_start_0_6_1(loop, vm):
|
||||
@@ -120,7 +124,6 @@ def test_start_0_6_1(loop, vm):
|
||||
"""
|
||||
process = MagicMock()
|
||||
process.returncode = None
|
||||
queue = vm.project.get_listen_queue()
|
||||
vm._vpcs_version = parse_version("0.6.1")
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
||||
@@ -142,12 +145,10 @@ def test_start_0_6_1(loop, vm):
|
||||
'-t',
|
||||
'127.0.0.1')
|
||||
assert vm.is_running()
|
||||
(action, event) = queue.get_nowait()
|
||||
assert action == "vm.started"
|
||||
assert event == vm
|
||||
|
||||
|
||||
def test_stop(loop, vm):
|
||||
|
||||
def test_stop(loop, vm, async_run):
|
||||
process = MagicMock()
|
||||
|
||||
# Wait process kill success
|
||||
@@ -156,28 +157,31 @@ def test_stop(loop, vm):
|
||||
process.wait.return_value = future
|
||||
process.returncode = None
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
||||
vm.port_add_nio_binding(0, nio)
|
||||
with NotificationManager.instance().queue() as queue:
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM._check_requirements", return_value=True):
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||
nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_udp", "lport": 4242, "rport": 4243, "rhost": "127.0.0.1"})
|
||||
vm.port_add_nio_binding(0, nio)
|
||||
|
||||
loop.run_until_complete(asyncio.async(vm.start()))
|
||||
assert vm.is_running()
|
||||
loop.run_until_complete(asyncio.async(vm.start()))
|
||||
assert vm.is_running()
|
||||
|
||||
queue = vm.project.get_listen_queue()
|
||||
|
||||
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
||||
loop.run_until_complete(asyncio.async(vm.stop()))
|
||||
assert vm.is_running() is False
|
||||
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
||||
loop.run_until_complete(asyncio.async(vm.stop()))
|
||||
assert vm.is_running() is False
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
process.send_signal.assert_called_with(1)
|
||||
else:
|
||||
process.terminate.assert_called_with()
|
||||
if sys.platform.startswith("win"):
|
||||
process.send_signal.assert_called_with(1)
|
||||
else:
|
||||
process.terminate.assert_called_with()
|
||||
|
||||
(action, event) = queue.get_nowait()
|
||||
assert action == "vm.stopped"
|
||||
assert event == vm
|
||||
async_run(queue.get(0)) # Ping
|
||||
async_run(queue.get(0)) # Started
|
||||
|
||||
(action, event, kwargs) = async_run(queue.get(0))
|
||||
assert action == "vm.stopped"
|
||||
assert event == vm
|
||||
|
||||
|
||||
def test_reload(loop, vm):
|
||||
|
||||
Reference in new issue
Block a user