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:
Controller._instance = Controller()
return Controller._instance

View File

@ -196,7 +196,7 @@ class Compute:
msg = json.loads(response.data)
action = msg.pop("action")
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):
return "{}://{}:{}/v2/compute{}".format(self._protocol, self._host, self._port, path)

View File

@ -102,7 +102,7 @@ class Node:
data = self._node_data()
data["node_id"] = self._id
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
def update(self, name=None, console=None, console_type="telnet", properties={}):
@ -128,20 +128,26 @@ class Node:
data = self._node_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
"""
for key, value in response.json.items():
for key, value in response.items():
if key == "console":
self._console = value
elif key == "node_directory":
self._node_directory = value
elif key == "command_line":
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
else:
self._properties[key] = value

View File

@ -15,6 +15,7 @@
# 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 aiohttp
from contextlib import contextmanager
from ..notification_queue import NotificationQueue
@ -42,21 +43,44 @@ class Notification:
yield 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
:param action: Action name
:param event: Event to send
:param kwargs: Add this meta to the notification
"""
if "project_id" in kwargs:
project_id = kwargs.pop("project_id")
self._send_event_to_project(project_id, action, event, **kwargs)
if "project_id" in event:
self._send_event_to_project(event["project_id"], action, event)
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
this project
@ -64,24 +88,22 @@ class Notification:
:param project: Project where we need to send the event
:param action: Action name
:param event: Event to send
:param kwargs: Add this meta to the notification
"""
try:
project_listeners = self._listeners[project_id]
except KeyError:
return
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
projects
:param action: Action name
:param event: Event to send
:param kwargs: Add this meta to the notification
"""
for project_listeners in self._listeners.values():
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
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

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")

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"

View File

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

View File

@ -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)