mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
Add project.created, project.opened and project.deleted controller notification stream.
Move project.updated and project.closed from project notification to controller notification stream.
This commit is contained in:
4 files changed
+103
-73
No files matched your search
@@ -213,7 +213,9 @@ async def test_compute_httpQuery_project(compute):
|
||||
response = MagicMock()
|
||||
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
|
||||
response.status = 200
|
||||
project = Project(name="Test")
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification') as mock_notification:
|
||||
project = Project(name="Test")
|
||||
mock_notification.assert_called()
|
||||
await compute.post("/projects", project)
|
||||
mock.assert_called_with("POST", "https://example.com:84/v2/compute/projects", data=json.dumps(project.__json__()), headers={'content-type': 'application/json'}, auth=None, chunked=None, timeout=20)
|
||||
await compute.close()
|
||||
|
||||
@@ -47,15 +47,19 @@ async def node(controller, project):
|
||||
|
||||
async def test_affect_uuid():
|
||||
|
||||
p = Project(name="Test")
|
||||
assert len(p.id) == 36
|
||||
p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f', name="Test 2")
|
||||
assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification') as mock_notification:
|
||||
p = Project(name="Test")
|
||||
mock_notification.assert_called()
|
||||
assert len(p.id) == 36
|
||||
p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f', name="Test 2")
|
||||
assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
|
||||
|
||||
|
||||
async def test_json():
|
||||
|
||||
p = Project(name="Test")
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification') as mock_notification:
|
||||
p = Project(name="Test")
|
||||
mock_notification.assert_called()
|
||||
|
||||
assert p.__json__() == {
|
||||
"name": "Test",
|
||||
@@ -83,11 +87,11 @@ async def test_json():
|
||||
async def test_update(controller):
|
||||
|
||||
project = Project(controller=controller, name="Hello")
|
||||
project.emit_notification = MagicMock()
|
||||
project.emit_controller_notification = MagicMock()
|
||||
assert project.name == "Hello"
|
||||
await project.update(name="World")
|
||||
assert project.name == "World"
|
||||
project.emit_notification.assert_any_call("project.updated", project.__json__())
|
||||
project.emit_controller_notification.assert_any_call("project.updated", project.__json__())
|
||||
|
||||
|
||||
async def test_update_on_compute(controller):
|
||||
@@ -106,7 +110,9 @@ async def test_path(projects_dir):
|
||||
|
||||
directory = projects_dir
|
||||
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
|
||||
p = Project(project_id=str(uuid4()), name="Test")
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification') as mock_notification:
|
||||
p = Project(project_id=str(uuid4()), name="Test")
|
||||
mock_notification.assert_called()
|
||||
assert p.path == os.path.join(directory, p.id)
|
||||
assert os.path.exists(os.path.join(directory, p.id))
|
||||
|
||||
@@ -124,23 +130,27 @@ def test_path_exist(tmpdir):
|
||||
|
||||
async def test_init_path(tmpdir):
|
||||
|
||||
p = Project(path=str(tmpdir), project_id=str(uuid4()), name="Test")
|
||||
assert p.path == str(tmpdir)
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification') as mock_notification:
|
||||
p = Project(path=str(tmpdir), project_id=str(uuid4()), name="Test")
|
||||
mock_notification.assert_called()
|
||||
assert p.path == str(tmpdir)
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
||||
async def test_changing_path_with_quote_not_allowed(tmpdir):
|
||||
|
||||
with pytest.raises(aiohttp.web.HTTPForbidden):
|
||||
p = Project(project_id=str(uuid4()), name="Test")
|
||||
p.path = str(tmpdir / "project\"53")
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification'):
|
||||
p = Project(project_id=str(uuid4()), name="Test")
|
||||
p.path = str(tmpdir / "project\"53")
|
||||
|
||||
|
||||
async def test_captures_directory(tmpdir):
|
||||
|
||||
p = Project(path=str(tmpdir / "capturestest"), name="Test")
|
||||
assert p.captures_directory == str(tmpdir / "capturestest" / "project-files" / "captures")
|
||||
assert os.path.exists(p.captures_directory)
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification'):
|
||||
p = Project(path=str(tmpdir / "capturestest"), name="Test")
|
||||
assert p.captures_directory == str(tmpdir / "capturestest" / "project-files" / "captures")
|
||||
assert os.path.exists(p.captures_directory)
|
||||
|
||||
|
||||
async def test_add_node_local(controller):
|
||||
@@ -649,36 +659,39 @@ async def test_dump(projects_dir):
|
||||
|
||||
directory = projects_dir
|
||||
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
|
||||
p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f', name="Test")
|
||||
p.dump()
|
||||
with open(os.path.join(directory, p.id, "Test.gns3")) as f:
|
||||
content = f.read()
|
||||
assert "00010203-0405-0607-0809-0a0b0c0d0e0f" in content
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification'):
|
||||
p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f', name="Test")
|
||||
p.dump()
|
||||
with open(os.path.join(directory, p.id, "Test.gns3")) as f:
|
||||
content = f.read()
|
||||
assert "00010203-0405-0607-0809-0a0b0c0d0e0f" in content
|
||||
|
||||
|
||||
async def test_open_close(controller):
|
||||
|
||||
project = Project(controller=controller, name="Test")
|
||||
assert project.status == "opened"
|
||||
await project.close()
|
||||
project.start_all = AsyncioMagicMock()
|
||||
await project.open()
|
||||
assert not project.start_all.called
|
||||
assert project.status == "opened"
|
||||
project.emit_notification = MagicMock()
|
||||
await project.close()
|
||||
assert project.status == "closed"
|
||||
project.emit_notification.assert_any_call("project.closed", project.__json__())
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification'):
|
||||
project = Project(controller=controller, name="Test")
|
||||
assert project.status == "opened"
|
||||
await project.close()
|
||||
project.start_all = AsyncioMagicMock()
|
||||
await project.open()
|
||||
assert not project.start_all.called
|
||||
assert project.status == "opened"
|
||||
project.emit_controller_notification = MagicMock()
|
||||
await project.close()
|
||||
assert project.status == "closed"
|
||||
project.emit_controller_notification.assert_any_call("project.closed", project.__json__())
|
||||
|
||||
|
||||
async def test_open_auto_start(controller):
|
||||
|
||||
project = Project(controller=controller, name="Test", auto_start=True)
|
||||
assert project.status == "opened"
|
||||
await project.close()
|
||||
project.start_all = AsyncioMagicMock()
|
||||
await project.open()
|
||||
assert project.start_all.called
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification'):
|
||||
project = Project(controller=controller, name="Test", auto_start=True)
|
||||
assert project.status == "opened"
|
||||
await project.close()
|
||||
project.start_all = AsyncioMagicMock()
|
||||
await project.open()
|
||||
assert project.start_all.called
|
||||
|
||||
|
||||
def test_is_running(project, node):
|
||||
|
||||
@@ -19,7 +19,7 @@ import json
|
||||
import uuid
|
||||
import pytest
|
||||
import aiohttp
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
from gns3server.controller.project import Project
|
||||
@@ -30,35 +30,36 @@ from gns3server.version import __version__
|
||||
|
||||
async def test_project_to_topology_empty(tmpdir):
|
||||
|
||||
project = Project(name="Test")
|
||||
topo = project_to_topology(project)
|
||||
assert topo == {
|
||||
"project_id": project.id,
|
||||
"name": "Test",
|
||||
"auto_start": False,
|
||||
"auto_close": True,
|
||||
"auto_open": False,
|
||||
"scene_width": 2000,
|
||||
"scene_height": 1000,
|
||||
"revision": GNS3_FILE_FORMAT_REVISION,
|
||||
"zoom": 100,
|
||||
"show_grid": False,
|
||||
"show_interface_labels": False,
|
||||
"show_layers": False,
|
||||
"snap_to_grid": False,
|
||||
"grid_size": 75,
|
||||
"drawing_grid_size": 25,
|
||||
"topology": {
|
||||
"nodes": [],
|
||||
"links": [],
|
||||
"computes": [],
|
||||
"drawings": []
|
||||
},
|
||||
"type": "topology",
|
||||
"supplier": None,
|
||||
"variables": None,
|
||||
"version": __version__
|
||||
}
|
||||
with patch('gns3server.controller.project.Project.emit_controller_notification'):
|
||||
project = Project(name="Test")
|
||||
topo = project_to_topology(project)
|
||||
assert topo == {
|
||||
"project_id": project.id,
|
||||
"name": "Test",
|
||||
"auto_start": False,
|
||||
"auto_close": True,
|
||||
"auto_open": False,
|
||||
"scene_width": 2000,
|
||||
"scene_height": 1000,
|
||||
"revision": GNS3_FILE_FORMAT_REVISION,
|
||||
"zoom": 100,
|
||||
"show_grid": False,
|
||||
"show_interface_labels": False,
|
||||
"show_layers": False,
|
||||
"snap_to_grid": False,
|
||||
"grid_size": 75,
|
||||
"drawing_grid_size": 25,
|
||||
"topology": {
|
||||
"nodes": [],
|
||||
"links": [],
|
||||
"computes": [],
|
||||
"drawings": []
|
||||
},
|
||||
"type": "topology",
|
||||
"supplier": None,
|
||||
"variables": None,
|
||||
"version": __version__
|
||||
}
|
||||
|
||||
|
||||
async def test_basic_topology(controller):
|
||||
|
||||
Reference in new issue
Block a user