Notif forwarded from hypervisor to controller

This commit is contained in:
Julien Duponchelle committed 2016-03-18 16:56:23 +01:00
1 parent de61ed316c
commit bc14d5d78e
8 files changed
+174 -46

No files matched your search

+46 -1
View File
@@ -77,7 +77,7 @@ def test_removeProject(controller, async_run):
def test_addProject_with_hypervisor(controller, async_run):
uuid1 = str(uuid.uuid4())
hypervisor = Hypervisor("test1")
hypervisor = Hypervisor("test1", controller=MagicMock())
hypervisor.post = MagicMock()
controller._hypervisors = {"test1": hypervisor}
@@ -92,3 +92,48 @@ def test_getProject(controller, async_run):
assert controller.getProject(uuid1) == project
with pytest.raises(aiohttp.web.HTTPNotFound):
assert controller.getProject("dsdssd")
def test_emit(controller, async_run):
project1 = MagicMock()
uuid1 = str(uuid.uuid4())
controller._projects[uuid1] = project1
project2 = MagicMock()
uuid2 = str(uuid.uuid4())
controller._projects[uuid2] = project2
# Notif without project should be send to all projects
controller.emit("test", {})
assert project1.emit.called
assert project2.emit.called
def test_emit_to_project(controller, async_run):
project1 = MagicMock()
uuid1 = str(uuid.uuid4())
controller._projects[uuid1] = project1
project2 = MagicMock()
uuid2 = str(uuid.uuid4())
controller._projects[uuid2] = project2
# Notif with project should be send to this project
controller.emit("test", {}, project_id=uuid1)
project1.emit.assert_called_with('test', {})
assert not project2.emit.called
def test_emit_to_project_not_exists(controller, async_run):
project1 = MagicMock()
uuid1 = str(uuid.uuid4())
controller._projects[uuid1] = project1
project2 = MagicMock()
uuid2 = str(uuid.uuid4())
controller._projects[uuid2] = project2
# Notif with project should be send to this project
controller.emit("test", {}, project_id="4444444")
assert not project1.emit.called
assert not project2.emit.called
+33 -3
View File
@@ -19,6 +19,7 @@
import pytest
import json
import aiohttp
import asyncio
from unittest.mock import patch, MagicMock
from gns3server.controller.project import Project
@@ -29,7 +30,7 @@ from tests.utils import asyncio_patch, AsyncioMagicMock
@pytest.fixture
def hypervisor():
hypervisor = Hypervisor("my_hypervisor_id", protocol="https", host="example.com", port=84)
hypervisor = Hypervisor("my_hypervisor_id", protocol="https", host="example.com", port=84, controller=MagicMock())
hypervisor._connected = True
return hypervisor
@@ -46,10 +47,10 @@ def test_hypervisor_local(hypervisor):
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
with pytest.raises(HypervisorError):
s = Hypervisor("local")
s = Hypervisor("local", controller=MagicMock())
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
s = Hypervisor("test")
s = Hypervisor("test", controller=MagicMock())
def test_hypervisor_httpQuery(hypervisor, async_run):
@@ -139,6 +140,35 @@ def test_hypervisor_httpQuery_project(hypervisor, async_run):
mock.assert_called_with("POST", "https://example.com:84/v2/hypervisor/projects", data=json.dumps(project.__json__()), headers={'content-type': 'application/json'}, auth=None)
def test_connectNotification(hypervisor, async_run):
ws_mock = AsyncioMagicMock()
call = 0
@asyncio.coroutine
def receive():
nonlocal call
call += 1
if call == 1:
response = MagicMock()
response.data = '{"action": "test", "event": {"a": 1}, "project_id": "42"}'
response.tp = aiohttp.MsgType.text
return response
else:
response = MagicMock()
response.tp = aiohttp.MsgType.closed
return response
hypervisor._controller = MagicMock()
hypervisor._session = AsyncioMagicMock(return_value=ws_mock)
hypervisor._session.ws_connect = AsyncioMagicMock(return_value=ws_mock)
ws_mock.receive = receive
async_run(hypervisor._connectNotification())
hypervisor._controller.emit.assert_called_with('test', {'a': 1}, hypervisor_id=hypervisor.id, project_id='42')
assert hypervisor._connected is False
def test_json(hypervisor):
hypervisor.user = "test"
assert hypervisor.__json__() == {
+2 -1
View File
@@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from unittest.mock import MagicMock
from gns3server.controller.link import Link
from gns3server.controller.vm import VM
@@ -30,7 +31,7 @@ def project():
@pytest.fixture
def hypervisor():
return Hypervisor("example.com")
return Hypervisor("example.com", controller=MagicMock())
def test_addVM(async_run, project, hypervisor):