1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 17:28:08 +00:00

Correctly process node.updated event on controller

This commit is contained in:
Julien Duponchelle 2016-05-18 16:12:13 +02:00
parent 694e1a2e68
commit fa0af7f4a2
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
9 changed files with 118 additions and 33 deletions

View File

@ -188,4 +188,3 @@ class Controller:
if not hasattr(Controller, '_instance') or Controller._instance is None: if not hasattr(Controller, '_instance') or Controller._instance is None:
Controller._instance = Controller() Controller._instance = Controller()
return Controller._instance return Controller._instance

View File

@ -196,7 +196,7 @@ class Compute:
msg = json.loads(response.data) msg = json.loads(response.data)
action = msg.pop("action") action = msg.pop("action")
event = msg.pop("event") event = msg.pop("event")
self._controller.notification.emit(action, event, compute_id=self.id, **msg) self._controller.notification.dispatch(action, event, compute_id=self.id)
def _getUrl(self, path): def _getUrl(self, path):
return "{}://{}:{}/v2/compute{}".format(self._protocol, self._host, self._port, path) return "{}://{}:{}/v2/compute{}".format(self._protocol, self._host, self._port, path)

View File

@ -102,7 +102,7 @@ class Node:
data = self._node_data() data = self._node_data()
data["node_id"] = self._id data["node_id"] = self._id
response = yield from self._compute.post("/projects/{}/{}/nodes".format(self._project.id, self._node_type), data=data) response = yield from self._compute.post("/projects/{}/{}/nodes".format(self._project.id, self._node_type), data=data)
self._parse_node_response(response) self.parse_node_response(response.json)
@asyncio.coroutine @asyncio.coroutine
def update(self, name=None, console=None, console_type="telnet", properties={}): def update(self, name=None, console=None, console_type="telnet", properties={}):
@ -128,20 +128,26 @@ class Node:
data = self._node_data() data = self._node_data()
response = yield from self.put(None, data=data) response = yield from self.put(None, data=data)
self._parse_node_response(response) self.parse_node_response(response.json)
def _parse_node_response(self, response): def parse_node_response(self, response):
""" """
Update the object with the remote node object Update the object with the remote node object
""" """
for key, value in response.json.items(): for key, value in response.items():
if key == "console": if key == "console":
self._console = value self._console = value
elif key == "node_directory": elif key == "node_directory":
self._node_directory = value self._node_directory = value
elif key == "command_line": elif key == "command_line":
self._command_line = value self._command_line = value
elif key in ["console_type", "name", "node_id", "project_id", "status"]: elif key == "status":
self._status = value
elif key == "console_type":
self._console_type = value
elif key == "name":
self._name = value
elif key in ["node_id", "project_id"]:
pass pass
else: else:
self._properties[key] = value self._properties[key] = value

View File

@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import aiohttp
from contextlib import contextmanager from contextlib import contextmanager
from ..notification_queue import NotificationQueue from ..notification_queue import NotificationQueue
@ -42,21 +43,44 @@ class Notification:
yield queue yield queue
self._listeners[project.id].remove(queue) self._listeners[project.id].remove(queue)
def emit(self, action, event, **kwargs): def dispatch(self, action, event, compute_id):
"""
Notification received from compute node. Send it directly
to clients or process it
:param action: Action name
:param event: Event to send
:param compute_id: Compute id of the sender
"""
if action == "node.updated":
try:
# Update controller node data and send the event node.updated
project = self._controller.get_project(event["project_id"])
node = project.get_node(event["node_id"])
node.parse_node_response(event)
self.emit("node.updated", node.__json__())
except aiohttp.web.HTTPNotFound:
return
elif action == "ping":
event["compute_id"] = compute_id
self.emit(action, event)
else:
self.emit(action, event)
def emit(self, action, event):
""" """
Send a notification to clients scoped by projects Send a notification to clients scoped by projects
:param action: Action name :param action: Action name
:param event: Event to send :param event: Event to send
:param kwargs: Add this meta to the notification
""" """
if "project_id" in kwargs: if "project_id" in event:
project_id = kwargs.pop("project_id") self._send_event_to_project(event["project_id"], action, event)
self._send_event_to_project(project_id, action, event, **kwargs)
else: else:
self._send_event_to_all(action, event, **kwargs) self._send_event_to_all(action, event)
def _send_event_to_project(self, project_id, action, event, **kwargs): def _send_event_to_project(self, project_id, action, event):
""" """
Send an event to all the client listening for notifications for Send an event to all the client listening for notifications for
this project this project
@ -64,24 +88,22 @@ class Notification:
:param project: Project where we need to send the event :param project: Project where we need to send the event
:param action: Action name :param action: Action name
:param event: Event to send :param event: Event to send
:param kwargs: Add this meta to the notification
""" """
try: try:
project_listeners = self._listeners[project_id] project_listeners = self._listeners[project_id]
except KeyError: except KeyError:
return return
for listener in project_listeners: for listener in project_listeners:
listener.put_nowait((action, event, kwargs)) listener.put_nowait((action, event, {}))
def _send_event_to_all(self, action, event, **kwargs): def _send_event_to_all(self, action, event):
""" """
Send an event to all the client listening for notifications on all Send an event to all the client listening for notifications on all
projects projects
:param action: Action name :param action: Action name
:param event: Event to send :param event: Event to send
:param kwargs: Add this meta to the notification
""" """
for project_listeners in self._listeners.values(): for project_listeners in self._listeners.values():
for listener in project_listeners: for listener in project_listeners:
listener.put_nowait((action, event, kwargs)) listener.put_nowait((action, event, {}))

View File

@ -152,7 +152,7 @@ def test_connectNotification(compute, async_run):
call += 1 call += 1
if call == 1: if call == 1:
response = MagicMock() response = MagicMock()
response.data = '{"action": "test", "event": {"a": 1}, "project_id": "42"}' response.data = '{"action": "test", "event": {"a": 1}}'
response.tp = aiohttp.MsgType.text response.tp = aiohttp.MsgType.text
return response return response
else: else:
@ -166,7 +166,7 @@ def test_connectNotification(compute, async_run):
ws_mock.receive = receive ws_mock.receive = receive
async_run(compute._connect_notification()) 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 assert compute._connected is False

View File

@ -156,4 +156,3 @@ def test_getProject(controller, async_run):
assert controller.get_project(uuid1) == project assert controller.get_project(uuid1) == project
with pytest.raises(aiohttp.web.HTTPNotFound): with pytest.raises(aiohttp.web.HTTPNotFound):
assert controller.get_project("dsdssd") assert controller.get_project("dsdssd")

View File

@ -16,20 +16,36 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest import pytest
from unittest.mock import MagicMock
from gns3server.controller.notification import Notification 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 Send an event to all if we don't have a project id in the event
""" """
project = Project()
notif = controller.notification notif = controller.notification
with notif.queue(project) as queue: with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1 assert len(notif._listeners[project.id]) == 1
async_run(queue.get(0.1)) #  ping async_run(queue.get(0.1)) # ping
notif.emit('test', {}) notif.emit('test', {})
msg = async_run(queue.get(5)) msg = async_run(queue.get(5))
assert msg == ('test', {}, {}) assert msg == ('test', {}, {})
@ -37,19 +53,62 @@ def test_emit_to_all(async_run, controller):
assert len(notif._listeners[project.id]) == 0 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 Send an event to a project listeners
""" """
project = Project()
notif = controller.notification notif = controller.notification
with notif.queue(project) as queue: with notif.queue(project) as queue:
assert len(notif._listeners[project.id]) == 1 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 # This event has not listener
notif.emit('ignore', {}, project_id=42) notif.emit('ignore', {"project_id": 42})
notif.emit('test', {}, project_id=project.id) 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)) msg = async_run(queue.get(5))
assert msg == ('test', {}, {}) 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"

View File

@ -193,4 +193,3 @@ def test_getLink(async_run):
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound): with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
project.get_link("test") project.get_link("test")

View File

@ -82,6 +82,7 @@ def test_list_projects(http_controller, tmpdir):
projects = response.json projects = response.json
assert projects[0]["name"] == "test" assert projects[0]["name"] == "test"
def test_commit_project(http_controller, project): def test_commit_project(http_controller, project):
with asyncio_patch("gns3server.controller.project.Project.commit", return_value=True) as mock: 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) response = http_controller.post("/projects/{project_id}/commit".format(project_id=project.id), example=True)