mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-20 05:05:14 +02:00
Refactor tests
* Use pytest-aiohttp * Use the async def / await syntax. * Fix tests to run with Python 3.8
This commit is contained in:
1 parent
f498ab06b4
commit
d3ea67da24
87 files changed
+3697
-3528
No files matched your search
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
# Copyright (C) 2020 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
|
||||
@@ -18,81 +18,78 @@
|
||||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from gns3server.controller.notification import Notification
|
||||
from gns3server.controller import Controller
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project(async_run):
|
||||
return async_run(Controller.instance().add_project(name="Test"))
|
||||
async def node(project):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def node(project, async_run):
|
||||
compute = MagicMock()
|
||||
compute.id = "remote1"
|
||||
compute.host = "example.org"
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
return async_run(project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
return await project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
|
||||
|
||||
|
||||
def test_emit_to_all(async_run, controller, project):
|
||||
async def test_emit_to_all(controller, project):
|
||||
"""
|
||||
Send an event to all if we don't have a project id in the event
|
||||
"""
|
||||
|
||||
notif = controller.notification
|
||||
with notif.project_queue(project.id) as queue:
|
||||
assert len(notif._project_listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
await queue.get(0.1) # ping
|
||||
notif.project_emit('test', {})
|
||||
msg = async_run(queue.get(5))
|
||||
msg = await queue.get(5)
|
||||
assert msg == ('test', {}, {})
|
||||
|
||||
assert len(notif._project_listeners[project.id]) == 0
|
||||
|
||||
|
||||
def test_emit_to_project(async_run, controller, project):
|
||||
async def test_emit_to_project(controller, project):
|
||||
"""
|
||||
Send an event to a project listeners
|
||||
"""
|
||||
|
||||
notif = controller.notification
|
||||
with notif.project_queue(project.id) as queue:
|
||||
assert len(notif._project_listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
await queue.get(0.1) # ping
|
||||
# This event has not listener
|
||||
notif.project_emit('ignore', {"project_id": 42})
|
||||
notif.project_emit('test', {"project_id": project.id})
|
||||
msg = async_run(queue.get(5))
|
||||
msg = await queue.get(5)
|
||||
assert msg == ('test', {"project_id": project.id}, {})
|
||||
|
||||
assert len(notif._project_listeners[project.id]) == 0
|
||||
|
||||
|
||||
def test_dispatch(async_run, controller, project):
|
||||
async def test_dispatch(controller, project):
|
||||
|
||||
notif = controller.notification
|
||||
with notif.project_queue(project.id) as queue:
|
||||
assert len(notif._project_listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
async_run(notif.dispatch("test", {}, project_id=project.id, compute_id=1))
|
||||
msg = async_run(queue.get(5))
|
||||
await queue.get(0.1) # ping
|
||||
await notif.dispatch("test", {}, project_id=project.id, compute_id=1)
|
||||
msg = await queue.get(5)
|
||||
assert msg == ('test', {}, {})
|
||||
|
||||
|
||||
def test_dispatch_ping(async_run, controller, project):
|
||||
async def test_dispatch_ping(controller, project):
|
||||
|
||||
notif = controller.notification
|
||||
with notif.project_queue(project.id) as queue:
|
||||
assert len(notif._project_listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
async_run(notif.dispatch("ping", {}, project_id=project.id, compute_id=12))
|
||||
msg = async_run(queue.get(5))
|
||||
await queue.get(0.1) # ping
|
||||
await notif.dispatch("ping", {}, project_id=project.id, compute_id=12)
|
||||
msg = await queue.get(5)
|
||||
assert msg == ('ping', {'compute_id': 12}, {})
|
||||
|
||||
|
||||
def test_dispatch_node_updated(async_run, controller, node, project):
|
||||
async def test_dispatch_node_updated(controller, node, project):
|
||||
"""
|
||||
When we receive a node.updated notification from compute
|
||||
we need to update the client
|
||||
@@ -101,25 +98,26 @@ def test_dispatch_node_updated(async_run, controller, node, project):
|
||||
notif = controller.notification
|
||||
with notif.project_queue(project.id) as queue:
|
||||
assert len(notif._project_listeners[project.id]) == 1
|
||||
async_run(queue.get(0.1)) # ping
|
||||
async_run(notif.dispatch("node.updated", {
|
||||
await queue.get(0.1) # ping
|
||||
await notif.dispatch("node.updated", {
|
||||
"node_id": node.id,
|
||||
"project_id": project.id,
|
||||
"name": "hello",
|
||||
"startup_config": "ip 192"
|
||||
},
|
||||
project_id=project.id,
|
||||
compute_id=1))
|
||||
compute_id=1)
|
||||
assert node.name == "hello"
|
||||
action, event, _ = async_run(queue.get(5))
|
||||
action, event, _ = await queue.get(5)
|
||||
assert action == "node.updated"
|
||||
assert event["name"] == "hello"
|
||||
assert event["properties"]["startup_config"] == "ip 192"
|
||||
|
||||
|
||||
def test_various_notification(controller, node):
|
||||
|
||||
notif = controller.notification
|
||||
notif.project_emit("log.info", {"message": "Image uploaded"})
|
||||
notif.project_emit("log.warning", {"message": "Warning ASA 8 is not officialy supported by GNS3"})
|
||||
notif.project_emit("log.warning", {"message": "Warning ASA 8 is not officially supported by GNS3"})
|
||||
notif.project_emit("log.error", {"message": "Permission denied on /tmp"})
|
||||
notif.project_emit("node.updated", node.__json__())
|
||||
Reference in new issue
Block a user