2016-05-18 12:56:23 +00:00
|
|
|
#!/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 pytest
|
2016-05-18 14:12:13 +00:00
|
|
|
from unittest.mock import MagicMock
|
2016-05-18 12:56:23 +00:00
|
|
|
|
|
|
|
from gns3server.controller.notification import Notification
|
2016-05-18 14:12:13 +00:00
|
|
|
from gns3server.controller import Controller
|
|
|
|
from tests.utils import AsyncioMagicMock
|
2016-05-18 12:56:23 +00:00
|
|
|
|
|
|
|
|
2016-05-18 14:12:13 +00:00
|
|
|
@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)
|
|
|
|
|
2016-06-11 23:31:30 +00:00
|
|
|
return async_run(project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
2016-05-18 14:12:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_emit_to_all(async_run, controller, project):
|
2016-05-18 12:56:23 +00:00
|
|
|
"""
|
|
|
|
Send an event to all if we don't have a project id in the event
|
|
|
|
"""
|
|
|
|
notif = controller.notification
|
|
|
|
with notif.queue(project) as queue:
|
|
|
|
assert len(notif._listeners[project.id]) == 1
|
2016-05-18 14:12:13 +00:00
|
|
|
async_run(queue.get(0.1)) # ping
|
2016-05-18 12:56:23 +00:00
|
|
|
notif.emit('test', {})
|
|
|
|
msg = async_run(queue.get(5))
|
|
|
|
assert msg == ('test', {}, {})
|
|
|
|
|
|
|
|
assert len(notif._listeners[project.id]) == 0
|
|
|
|
|
|
|
|
|
2016-05-18 14:12:13 +00:00
|
|
|
def test_emit_to_project(async_run, controller, project):
|
2016-05-18 12:56:23 +00:00
|
|
|
"""
|
|
|
|
Send an event to a project listeners
|
|
|
|
"""
|
|
|
|
notif = controller.notification
|
|
|
|
with notif.queue(project) as queue:
|
|
|
|
assert len(notif._listeners[project.id]) == 1
|
2016-05-18 14:12:13 +00:00
|
|
|
async_run(queue.get(0.1)) # ping
|
2016-05-18 12:56:23 +00:00
|
|
|
# This event has not listener
|
2016-05-18 14:12:13 +00:00
|
|
|
notif.emit('ignore', {"project_id": 42})
|
|
|
|
notif.emit('test', {"project_id": project.id})
|
2016-05-18 12:56:23 +00:00
|
|
|
msg = async_run(queue.get(5))
|
2016-05-18 14:12:13 +00:00
|
|
|
assert msg == ('test', {"project_id": project.id}, {})
|
2016-05-18 12:56:23 +00:00
|
|
|
|
|
|
|
assert len(notif._listeners[project.id]) == 0
|
2016-05-18 14:12:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
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', {}, {})
|
|
|
|
|
|
|
|
|
|
|
|
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"
|