Correctly process node.updated event on controller

This commit is contained in:
Julien Duponchelle committed 2016-05-18 16:12:57 +02:00
1 parent 694e1a2e68
commit fa0af7f4a2
9 files changed
+118 -33

No files matched your search

+2 -2
View File
@@ -152,7 +152,7 @@ def test_connectNotification(compute, async_run):
call += 1
if call == 1:
response = MagicMock()
response.data = '{"action": "test", "event": {"a": 1}, "project_id": "42"}'
response.data = '{"action": "test", "event": {"a": 1}}'
response.tp = aiohttp.MsgType.text
return response
else:
@@ -166,7 +166,7 @@ def test_connectNotification(compute, async_run):
ws_mock.receive = receive
async_run(compute._connect_notification())
compute._controller.notification.emit.assert_called_with('test', {'a': 1}, compute_id=compute.id, project_id='42')
compute._controller.notification.dispatch.assert_called_with('test', {'a': 1}, compute_id=compute.id)
assert compute._connected is False
-1
View File
@@ -156,4 +156,3 @@ def test_getProject(controller, async_run):
assert controller.get_project(uuid1) == project
with pytest.raises(aiohttp.web.HTTPNotFound):
assert controller.get_project("dsdssd")
+69 -10
View File
@@ -16,20 +16,36 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from unittest.mock import MagicMock
from gns3server.controller.notification import Notification
from gns3server.controller.project import Project
from gns3server.controller import Controller
from tests.utils import AsyncioMagicMock
def test_emit_to_all(async_run, controller):
@pytest.fixture
def project(async_run):
return async_run(Controller.instance().add_project())
@pytest.fixture
def node(project, async_run):
compute = MagicMock()
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
return async_run(project.add_node(compute, None, name="test", node_type="vpcs", properties={"startup_config": "test.cfg"}))
def test_emit_to_all(async_run, controller, project):
"""
Send an event to all if we don't have a project id in the event
"""
project = Project()
notif = controller.notification
with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1
async_run(queue.get(0.1)) #  ping
async_run(queue.get(0.1)) # ping
notif.emit('test', {})
msg = async_run(queue.get(5))
assert msg == ('test', {}, {})
@@ -37,19 +53,62 @@ def test_emit_to_all(async_run, controller):
assert len(notif._listeners[project.id]) == 0
def test_emit_to_project(async_run, controller):
def test_emit_to_project(async_run, controller, project):
"""
Send an event to a project listeners
"""
project = Project()
notif = controller.notification
with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1
async_run(queue.get(0.1)) #  ping
async_run(queue.get(0.1)) # ping
# This event has not listener
notif.emit('ignore', {}, project_id=42)
notif.emit('test', {}, project_id=project.id)
notif.emit('ignore', {"project_id": 42})
notif.emit('test', {"project_id": project.id})
msg = async_run(queue.get(5))
assert msg == ('test', {"project_id": project.id}, {})
assert len(notif._listeners[project.id]) == 0
def test_dispatch(async_run, controller, project):
notif = controller.notification
with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1
async_run(queue.get(0.1)) # ping
notif.dispatch("test", {}, compute_id=1)
msg = async_run(queue.get(5))
assert msg == ('test', {}, {})
assert len(notif._listeners[project.id]) == 0
def test_dispatch_ping(async_run, controller, project):
notif = controller.notification
with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1
async_run(queue.get(0.1)) # ping
notif.dispatch("ping", {}, compute_id=12)
msg = async_run(queue.get(5))
assert msg == ('ping', {'compute_id': 12}, {})
def test_dispatch_node_updated(async_run, controller, node, project):
"""
When we receive a node.updated notification from compute
we need to update the client
"""
notif = controller.notification
with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1
async_run(queue.get(0.1)) # ping
notif.dispatch("node.updated", {
"node_id": node.id,
"project_id": project.id,
"name": "hello",
"startup_config": "ip 192"
},
compute_id=1)
assert node.name == "hello"
action, event, _ = async_run(queue.get(5))
assert action == "node.updated"
assert event["name"] == "hello"
assert event["properties"]["startup_config"] == "ip 192"
-1
View File
@@ -193,4 +193,3 @@ def test_getLink(async_run):
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
project.get_link("test")
@@ -82,6 +82,7 @@ def test_list_projects(http_controller, tmpdir):
projects = response.json
assert projects[0]["name"] == "test"
def test_commit_project(http_controller, project):
with asyncio_patch("gns3server.controller.project.Project.commit", return_value=True) as mock:
response = http_controller.post("/projects/{project_id}/commit".format(project_id=project.id), example=True)