Migration to FastAPI

This commit is contained in:
grossmj committed 2020-10-02 16:07:50 +09:30
1 parent c12b675691
commit eb3cb8a41f
224 files changed
+11254 -13517

No files matched your search

View File
Whitespace-only changes.
@@ -27,6 +27,7 @@ def gns3vm(controller):
return RemoteGNS3VM(controller)
@pytest.mark.asyncio
async def test_list(gns3vm, controller):
await controller.add_compute("r1", name="R1", host="r1.local", connect=False)
@@ -34,6 +35,7 @@ async def test_list(gns3vm, controller):
assert res == [{"vmname": "R1"}]
@pytest.mark.asyncio
async def test_start(gns3vm, controller):
await controller.add_compute("r1",
@@ -55,7 +57,8 @@ async def test_start(gns3vm, controller):
assert gns3vm.password == "world"
async def test_start_invalid_vm(loop, gns3vm, controller):
@pytest.mark.asyncio
async def test_start_invalid_vm(gns3vm, controller):
await controller.add_compute("r1",
name="R1",
@@ -16,23 +16,23 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import asyncio
from tests.utils import asyncio_patch, AsyncioMagicMock
from tests.utils import asyncio_patch
from gns3server.utils.asyncio import wait_run_in_executor
from unittest.mock import patch
from gns3server.controller.gns3vm.virtualbox_gns3_vm import VirtualBoxGNS3VM
@pytest.fixture
async def gns3vm(loop, controller):
@pytest.mark.asyncio
async def gns3vm(controller):
vm = VirtualBoxGNS3VM(controller)
vm.vmname = "GNS3 VM"
return vm
@pytest.mark.asyncio
async def test_look_for_interface(gns3vm):
showvminfo = """
@@ -68,6 +68,7 @@ GuestMemoryBalloon=0
# assert res == -1
@pytest.mark.asyncio
async def test_cpu_vendor_id(gns3vm):
from cpuinfo import get_cpu_info
@@ -21,7 +21,8 @@ from gns3server.controller.gns3vm.vmware_gns3_vm import VMwareGNS3VM
@pytest.fixture
async def gns3vm(loop, controller):
@pytest.mark.asyncio
async def gns3vm(controller):
vm = VMwareGNS3VM(controller)
vm.vmname = "GNS3 VM"
@@ -34,6 +35,7 @@ def vmx_path(tmpdir):
return str(tmpdir / "vmwware_vm.vmx")
@pytest.mark.asyncio
async def test_set_extra_options(gns3vm, vmx_path, windows_platform):
gns3vm._vmx_path = vmx_path
+43 -18
View File
@@ -17,11 +17,11 @@
import json
import pytest
import aiohttp
from unittest.mock import patch, MagicMock
from gns3server.controller.project import Project
from gns3server.controller.compute import Compute, ComputeConflict
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError
from tests.utils import asyncio_patch, AsyncioMagicMock
@@ -78,6 +78,7 @@ def test_name():
assert c.name == "https://azertyuiopq...@example.com:84"
@pytest.mark.asyncio
async def test_compute_httpQuery(compute):
response = MagicMock()
@@ -89,6 +90,7 @@ async def test_compute_httpQuery(compute):
assert compute._auth is None
@pytest.mark.asyncio
async def test_compute_httpQueryAuth(compute):
response = MagicMock()
@@ -104,7 +106,8 @@ async def test_compute_httpQueryAuth(compute):
assert compute._auth.password == "toor"
# async def test_compute_httpQueryNotConnected(compute, controller):
# @pytest.mark.asyncio
#async def test_compute_httpQueryNotConnected(compute, controller):
#
# controller._notification = MagicMock()
# compute._connected = False
@@ -121,7 +124,8 @@ async def test_compute_httpQueryAuth(compute):
# await compute.close()
# async def test_compute_httpQueryNotConnectedGNS3vmNotRunning(compute, controller):
# @pytest.mark.asyncio
#async def test_compute_httpQueryNotConnectedGNS3vmNotRunning(compute, controller):
# """
# We are not connected to the remote and it's a GNS3 VM. So we need to start it
# """
@@ -147,6 +151,7 @@ async def test_compute_httpQueryAuth(compute):
# await compute.close()
@pytest.mark.asyncio
async def test_compute_httpQueryNotConnectedInvalidVersion(compute):
compute._connected = False
@@ -154,12 +159,13 @@ async def test_compute_httpQueryNotConnectedInvalidVersion(compute):
response.read = AsyncioMagicMock(return_value=json.dumps({"version": "1.42.4"}).encode())
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await compute.post("/projects", {"a": "b"})
mock.assert_any_call("GET", "https://example.com:84/v2/compute/capabilities", headers={'content-type': 'application/json'}, data=None, auth=None, chunked=None, timeout=20)
await compute.close()
@pytest.mark.asyncio
async def test_compute_httpQueryNotConnectedNonGNS3Server(compute):
compute._connected = False
@@ -167,12 +173,13 @@ async def test_compute_httpQueryNotConnectedNonGNS3Server(compute):
response.read = AsyncioMagicMock(return_value=b'Blocked by super antivirus')
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await compute.post("/projects", {"a": "b"})
mock.assert_any_call("GET", "https://example.com:84/v2/compute/capabilities", headers={'content-type': 'application/json'}, data=None, auth=None, chunked=None, timeout=20)
await compute.close()
@pytest.mark.asyncio
async def test_compute_httpQueryNotConnectedNonGNS3Server2(compute):
compute._connected = False
@@ -180,22 +187,24 @@ async def test_compute_httpQueryNotConnectedNonGNS3Server2(compute):
response.read = AsyncioMagicMock(return_value=b'{}')
response.status = 200
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await compute.post("/projects", {"a": "b"})
mock.assert_any_call("GET", "https://example.com:84/v2/compute/capabilities", headers={'content-type': 'application/json'}, data=None, auth=None, chunked=None, timeout=20)
@pytest.mark.asyncio
async def test_compute_httpQueryError(compute):
response = MagicMock()
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
response.status = 404
with pytest.raises(aiohttp.web.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
await compute.post("/projects", {"a": "b"})
assert mock.called
await compute.close()
@pytest.mark.asyncio
async def test_compute_httpQueryConflictError(compute):
response = MagicMock()
@@ -208,6 +217,7 @@ async def test_compute_httpQueryConflictError(compute):
await compute.close()
@pytest.mark.asyncio
async def test_compute_httpQuery_project(compute):
response = MagicMock()
@@ -219,12 +229,14 @@ async def test_compute_httpQuery_project(compute):
await compute.close()
# FIXME: https://github.com/aio-libs/aiohttp/issues/2525
# async def test_connectNotification(compute):
# @pytest.mark.asyncio
#async def test_connectNotification(compute):
#
# ws_mock = AsyncioMagicMock()
# call = 0
#
# async def receive():
# @pytest.mark.asyncio
#async def receive():
# nonlocal call
# call += 1
# if call == 1:
@@ -256,7 +268,8 @@ async def test_compute_httpQuery_project(compute):
#
# call = 0
#
# async def receive():
# @pytest.mark.asyncio
#async def receive():
# nonlocal call
# call += 1
# if call == 1:
@@ -280,6 +293,7 @@ async def test_compute_httpQuery_project(compute):
# assert args[1]["memory_usage_percent"] == 80.7
# assert args[1]["cpu_usage_percent"] == 35.7
@pytest.mark.asyncio
async def test_json(compute):
compute.user = "test"
@@ -290,16 +304,17 @@ async def test_json(compute):
"host": "example.com",
"port": 84,
"user": "test",
"cpu_usage_percent": None,
"memory_usage_percent": None,
"disk_usage_percent": None,
"cpu_usage_percent": 0,
"memory_usage_percent": 0,
"disk_usage_percent": 0,
"connected": True,
"last_error": None,
"capabilities": {
"version": None,
"cpus": None,
"memory": None,
"disk_size": None,
"version": "",
"platform": "",
"cpus": 0,
"memory": 0,
"disk_size": 0,
"node_types": []
}
}
@@ -312,6 +327,7 @@ async def test_json(compute):
}
@pytest.mark.asyncio
async def test_downloadFile(project, compute):
response = MagicMock()
@@ -322,6 +338,7 @@ async def test_downloadFile(project, compute):
await compute.close()
@pytest.mark.asyncio
async def test_close(compute):
assert compute.connected is True
@@ -329,6 +346,7 @@ async def test_close(compute):
assert compute.connected is False
@pytest.mark.asyncio
async def test_update(compute, controller):
compute._controller._notification = MagicMock()
@@ -344,6 +362,7 @@ async def test_update(compute, controller):
assert compute._controller.save.called
@pytest.mark.asyncio
async def test_forward_get(compute):
response = MagicMock()
@@ -354,17 +373,19 @@ async def test_forward_get(compute):
await compute.close()
@pytest.mark.asyncio
async def test_forward_404(compute):
response = MagicMock()
response.status = 404
with asyncio_patch("aiohttp.ClientSession.request", return_value=response) as mock:
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
await compute.forward("GET", "qemu", "images")
assert mock.called
await compute.close()
@pytest.mark.asyncio
async def test_forward_post(compute):
response = MagicMock()
@@ -375,6 +396,7 @@ async def test_forward_post(compute):
await compute.close()
@pytest.mark.asyncio
async def test_images(compute):
"""
Will return image on compute
@@ -397,6 +419,7 @@ async def test_images(compute):
]
@pytest.mark.asyncio
async def test_list_files(project, compute):
res = [{"path": "test"}]
@@ -409,6 +432,7 @@ async def test_list_files(project, compute):
await compute.close()
@pytest.mark.asyncio
async def test_interfaces(compute):
res = [
@@ -430,6 +454,7 @@ async def test_interfaces(compute):
await compute.close()
@pytest.mark.asyncio
async def test_get_ip_on_same_subnet(controller):
compute1 = Compute("compute1", host="192.168.1.1", controller=controller)
+27 -5
View File
@@ -20,11 +20,11 @@ import uuid
import json
import pytest
import socket
import aiohttp
from unittest.mock import MagicMock, patch
from tests.utils import AsyncioMagicMock, asyncio_patch
from gns3server.controller.compute import Compute
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError
from gns3server.version import __version__
@@ -110,6 +110,7 @@ def test_import_computes_1_x(controller, controller_config_path):
assert compute.password is None
@pytest.mark.asyncio
async def test_load_projects(controller, projects_dir):
controller.save()
@@ -121,6 +122,7 @@ async def test_load_projects(controller, projects_dir):
mock_load_project.assert_called_with(os.path.join(projects_dir, "project1", "project1.gns3"), load=False)
@pytest.mark.asyncio
async def test_add_compute(controller):
controller._notification = MagicMock()
@@ -134,15 +136,17 @@ async def test_add_compute(controller):
assert len(controller.computes) == 2
@pytest.mark.asyncio
async def test_addDuplicateCompute(controller):
controller._notification = MagicMock()
c = await controller.add_compute(compute_id="test1", name="Test", connect=False)
assert len(controller.computes) == 1
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await controller.add_compute(compute_id="test2", name="Test", connect=False)
@pytest.mark.asyncio
async def test_deleteCompute(controller, controller_config_path):
c = await controller.add_compute(compute_id="test1", connect=False)
@@ -158,6 +162,7 @@ async def test_deleteCompute(controller, controller_config_path):
assert c.connected is False
@pytest.mark.asyncio
async def test_deleteComputeProjectOpened(controller, controller_config_path):
"""
When you delete a compute the project using it are close
@@ -190,6 +195,7 @@ async def test_deleteComputeProjectOpened(controller, controller_config_path):
assert project2.status == "opened"
@pytest.mark.asyncio
async def test_addComputeConfigFile(controller, controller_config_path):
await controller.add_compute(compute_id="test1", name="Test", connect=False)
@@ -209,14 +215,16 @@ async def test_addComputeConfigFile(controller, controller_config_path):
]
@pytest.mark.asyncio
async def test_getCompute(controller):
compute = await controller.add_compute(compute_id="test1", connect=False)
assert controller.get_compute("test1") == compute
with pytest.raises(aiohttp.web.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
assert controller.get_compute("dsdssd")
@pytest.mark.asyncio
async def test_has_compute(controller):
await controller.add_compute(compute_id="test1", connect=False)
@@ -224,6 +232,7 @@ async def test_has_compute(controller):
assert not controller.has_compute("test2")
@pytest.mark.asyncio
async def test_add_project(controller):
uuid1 = str(uuid.uuid4())
@@ -236,16 +245,18 @@ async def test_add_project(controller):
assert len(controller.projects) == 2
@pytest.mark.asyncio
async def test_addDuplicateProject(controller):
uuid1 = str(uuid.uuid4())
uuid2 = str(uuid.uuid4())
await controller.add_project(project_id=uuid1, name="Test")
assert len(controller.projects) == 1
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await controller.add_project(project_id=uuid2, name="Test")
@pytest.mark.asyncio
async def test_remove_project(controller):
uuid1 = str(uuid.uuid4())
@@ -255,6 +266,7 @@ async def test_remove_project(controller):
assert len(controller.projects) == 0
@pytest.mark.asyncio
async def test_addProject_with_compute(controller):
uuid1 = str(uuid.uuid4())
@@ -264,15 +276,17 @@ async def test_addProject_with_compute(controller):
await controller.add_project(project_id=uuid1, name="Test")
@pytest.mark.asyncio
async def test_getProject(controller):
uuid1 = str(uuid.uuid4())
project = await controller.add_project(project_id=uuid1, name="Test")
assert controller.get_project(uuid1) == project
with pytest.raises(aiohttp.web.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
assert controller.get_project("dsdssd")
@pytest.mark.asyncio
async def test_start(controller):
controller.gns3vm.settings = {
@@ -288,6 +302,7 @@ async def test_start(controller):
assert controller.computes["local"].name == socket.gethostname()
@pytest.mark.asyncio
async def test_start_vm(controller):
"""
Start the controller with a GNS3 VM
@@ -309,6 +324,7 @@ async def test_start_vm(controller):
assert len(controller.computes) == 2 # Local compute and vm are created
@pytest.mark.asyncio
async def test_stop(controller):
c = await controller.add_compute(compute_id="test1", connect=False)
@@ -317,6 +333,7 @@ async def test_stop(controller):
assert c.connected is False
@pytest.mark.asyncio
async def test_stop_vm(controller):
"""
Stop GNS3 VM if configured
@@ -335,6 +352,7 @@ async def test_stop_vm(controller):
assert mock.called
@pytest.mark.asyncio
async def test_suspend_vm(controller):
"""
Suspend GNS3 VM if configured
@@ -353,6 +371,7 @@ async def test_suspend_vm(controller):
assert mock.called
@pytest.mark.asyncio
async def test_keep_vm(controller):
"""
Keep GNS3 VM if configured
@@ -371,6 +390,7 @@ async def test_keep_vm(controller):
assert not mock.called
@pytest.mark.asyncio
async def test_get_free_project_name(controller):
await controller.add_project(project_id=str(uuid.uuid4()), name="Test")
@@ -380,6 +400,7 @@ async def test_get_free_project_name(controller):
assert controller.get_free_project_name("Hello") == "Hello"
@pytest.mark.asyncio
async def test_load_base_files(controller, config, tmpdir):
config.set_section_config("Server", {"configs_path": str(tmpdir)})
@@ -451,6 +472,7 @@ def test_load_templates(controller):
assert cloud_uuid == template.id
@pytest.mark.asyncio
async def test_autoidlepc(controller):
controller._computes["local"] = AsyncioMagicMock()
+1
View File
@@ -68,6 +68,7 @@ def test_json(project):
}
@pytest.mark.asyncio
async def test_update(drawing, project, controller):
controller._notification = AsyncioMagicMock()
+17 -5
View File
@@ -19,7 +19,6 @@
import os
import json
import pytest
import aiohttp
import zipfile
from pathlib import Path
@@ -30,10 +29,12 @@ from tests.utils import AsyncioMagicMock, AsyncioBytesIO
from gns3server.controller.project import Project
from gns3server.controller.export_project import export_project, _is_exportable
from gns3server.utils.asyncio import aiozipstream
from gns3server.controller.controller_error import ControllerError
@pytest.fixture
async def project(loop, controller):
@pytest.mark.asyncio
async def project(controller):
p = Project(controller=controller, name="test")
p.dump = MagicMock()
@@ -41,6 +42,7 @@ async def project(loop, controller):
@pytest.fixture
@pytest.mark.asyncio
async def node(controller, project):
compute = MagicMock()
@@ -54,6 +56,7 @@ async def node(controller, project):
return node
@pytest.mark.asyncio
async def write_file(path, z):
with open(path, 'wb') as f:
@@ -72,6 +75,7 @@ def test_exportable_files():
assert not _is_exportable("test/project-files/snapshots/test.gns3p")
@pytest.mark.asyncio
async def test_export(tmpdir, project):
path = project.path
@@ -138,6 +142,7 @@ async def test_export(tmpdir, project):
assert topo["computes"] == []
@pytest.mark.asyncio
async def test_export_vm(tmpdir, project):
"""
If data is on a remote server export it locally before
@@ -175,6 +180,7 @@ async def test_export_vm(tmpdir, project):
assert content == b"HELLO"
@pytest.mark.asyncio
async def test_export_disallow_running(tmpdir, project, node):
"""
Disallow export when a node is running
@@ -196,11 +202,12 @@ async def test_export_disallow_running(tmpdir, project, node):
json.dump(topology, f)
node._status = "started"
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
with aiozipstream.ZipFile() as z:
await export_project(z, project, str(tmpdir))
@pytest.mark.asyncio
async def test_export_disallow_some_type(tmpdir, project):
"""
Disallow export for some node type
@@ -221,7 +228,7 @@ async def test_export_disallow_some_type(tmpdir, project):
with open(os.path.join(path, "test.gns3"), 'w+') as f:
json.dump(topology, f)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
with aiozipstream.ZipFile() as z:
await export_project(z, project, str(tmpdir))
with aiozipstream.ZipFile() as z:
@@ -242,11 +249,12 @@ async def test_export_disallow_some_type(tmpdir, project):
}
with open(os.path.join(path, "test.gns3"), 'w+') as f:
json.dump(topology, f)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
with aiozipstream.ZipFile() as z:
await export_project(z, project, str(tmpdir), allow_all_nodes=True)
@pytest.mark.asyncio
async def test_export_fix_path(tmpdir, project):
"""
Fix absolute image path, except for Docker
@@ -288,6 +296,7 @@ async def test_export_fix_path(tmpdir, project):
assert topology["topology"]["nodes"][1]["properties"]["image"] == "gns3/webterm:lastest"
@pytest.mark.asyncio
async def test_export_with_images(tmpdir, project):
"""
Fix absolute image path
@@ -323,6 +332,7 @@ async def test_export_with_images(tmpdir, project):
myzip.getinfo("images/IOS/test.image")
@pytest.mark.asyncio
async def test_export_keep_compute_id(tmpdir, project):
"""
If we want to restore the same computes we could ask to keep them
@@ -362,6 +372,7 @@ async def test_export_keep_compute_id(tmpdir, project):
assert len(topo["computes"]) == 1
@pytest.mark.asyncio
async def test_export_images_from_vm(tmpdir, project):
"""
If data is on a remote server export it locally before
@@ -426,6 +437,7 @@ async def test_export_images_from_vm(tmpdir, project):
assert content == b"IMAGE"
@pytest.mark.asyncio
async def test_export_with_ignoring_snapshots(tmpdir, project):
with open(os.path.join(project.path, "test.gns3"), 'w+') as f:
+8 -3
View File
@@ -47,7 +47,8 @@ def dummy_gns3vm(controller, dummy_engine):
return vm
async def test_list(loop, controller):
@pytest.mark.asyncio
async def test_list(controller):
vm = GNS3VM(controller)
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.list", return_value=[{"vmname": "test", "vmx_path": "test"}]):
@@ -60,14 +61,16 @@ async def test_list(loop, controller):
await vm.list("hyperv")
async def test_json(loop, controller):
@pytest.mark.asyncio
async def test_json(controller):
vm = GNS3VM(controller)
assert vm.__json__() == vm._settings
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
async def test_update_settings(loop, controller):
@pytest.mark.asyncio
async def test_update_settings(controller):
vm = GNS3VM(controller)
vm.settings = {
@@ -85,6 +88,7 @@ async def test_update_settings(loop, controller):
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
@pytest.mark.asyncio
async def test_auto_start(controller, dummy_gns3vm, dummy_engine):
"""
When start the compute should be add to the controller
@@ -102,6 +106,7 @@ async def test_auto_start(controller, dummy_gns3vm, dummy_engine):
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
@pytest.mark.asyncio
async def test_auto_start_with_error(controller, dummy_gns3vm, dummy_engine):
dummy_engine.start.side_effect = GNS3VMError("Dummy error")
+19 -6
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 pytest
import os
import uuid
import json
@@ -26,6 +27,7 @@ from gns3server.controller.import_project import import_project, _move_files_to_
from gns3server.version import __version__
@pytest.mark.asyncio
async def test_import_project(tmpdir, controller):
project_id = str(uuid.uuid4())
@@ -71,6 +73,7 @@ async def test_import_project(tmpdir, controller):
assert project.name != "test"
@pytest.mark.asyncio
async def test_import_project_override(tmpdir, controller):
"""
In the case of snapshot we will import a project for
@@ -106,6 +109,7 @@ async def test_import_project_override(tmpdir, controller):
assert project.name == "test"
@pytest.mark.asyncio
async def test_import_upgrade(tmpdir, controller):
"""
Topology made for previous GNS3 version are upgraded during the process
@@ -135,6 +139,7 @@ async def test_import_upgrade(tmpdir, controller):
assert topo["version"] == __version__
@pytest.mark.asyncio
async def test_import_with_images(tmpdir, controller):
project_id = str(uuid.uuid4())
@@ -166,7 +171,8 @@ async def test_import_with_images(tmpdir, controller):
assert os.path.exists(path), path
async def test_import_iou_linux_no_vm(loop, linux_platform, tmpdir, controller):
@pytest.mark.asyncio
async def test_import_iou_linux_no_vm(linux_platform, tmpdir, controller):
"""
On non linux host IOU should be local if we don't have a GNS3 VM
"""
@@ -210,7 +216,8 @@ async def test_import_iou_linux_no_vm(loop, linux_platform, tmpdir, controller):
assert topo["topology"]["nodes"][0]["compute_id"] == "local"
async def test_import_iou_linux_with_vm(loop, linux_platform, tmpdir, controller):
@pytest.mark.asyncio
async def test_import_iou_linux_with_vm(linux_platform, tmpdir, controller):
"""
On non linux host IOU should be vm if we have a GNS3 VM configured
"""
@@ -255,7 +262,8 @@ async def test_import_iou_linux_with_vm(loop, linux_platform, tmpdir, controller
assert topo["topology"]["nodes"][0]["compute_id"] == "vm"
async def test_import_nat_non_linux(loop, windows_platform, tmpdir, controller):
@pytest.mark.asyncio
async def test_import_nat_non_linux(windows_platform, tmpdir, controller):
"""
On non linux host NAT should be moved to the GNS3 VM
"""
@@ -300,7 +308,8 @@ async def test_import_nat_non_linux(loop, windows_platform, tmpdir, controller):
assert topo["topology"]["nodes"][0]["compute_id"] == "vm"
async def test_import_iou_non_linux(loop, windows_platform, tmpdir, controller):
@pytest.mark.asyncio
async def test_import_iou_non_linux(windows_platform, tmpdir, controller):
"""
On non linux host IOU should be moved to the GNS3 VM
"""
@@ -356,7 +365,8 @@ async def test_import_iou_non_linux(loop, windows_platform, tmpdir, controller):
mock.assert_called_with(controller._computes["vm"], project_id, project.path, os.path.join('project-files', 'iou', topo["topology"]["nodes"][0]['node_id']))
async def test_import_node_id(loop, linux_platform, tmpdir, controller):
@pytest.mark.asyncio
async def test_import_node_id(linux_platform, tmpdir, controller):
"""
When importing a node, node_id should change
"""
@@ -449,7 +459,8 @@ async def test_import_node_id(loop, linux_platform, tmpdir, controller):
assert os.path.exists(os.path.join(project.path, "project-files", "iou", topo["topology"]["nodes"][0]["node_id"], "startup.cfg"))
async def test_import_keep_compute_id(loop, windows_platform, tmpdir, controller):
@pytest.mark.asyncio
async def test_import_keep_compute_id(windows_platform, tmpdir, controller):
"""
On linux host IOU should be moved to the GNS3 VM
"""
@@ -494,6 +505,7 @@ async def test_import_keep_compute_id(loop, windows_platform, tmpdir, controller
assert topo["topology"]["nodes"][0]["compute_id"] == "local"
@pytest.mark.asyncio
async def test_move_files_to_compute(tmpdir):
project_id = str(uuid.uuid4())
@@ -510,6 +522,7 @@ async def test_move_files_to_compute(tmpdir):
assert not os.path.exists(str(tmpdir / "project-files" / "docker"))
@pytest.mark.asyncio
async def test_import_project_name_and_location(tmpdir, controller):
"""
Import a project with a different location and name
+21 -5
View File
@@ -16,17 +16,18 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import aiohttp
from unittest.mock import MagicMock
from gns3server.controller.link import Link
from gns3server.controller.node import Node
from gns3server.controller.ports.ethernet_port import EthernetPort
from gns3server.controller.ports.serial_port import SerialPort
from gns3server.controller.controller_error import ControllerError
from tests.utils import AsyncioBytesIO, AsyncioMagicMock
@pytest.fixture
@pytest.mark.asyncio
async def link(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -48,6 +49,7 @@ def test_eq(project, link):
assert link != Link(project)
@pytest.mark.asyncio
async def test_add_node(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -83,6 +85,7 @@ async def test_add_node(project, compute):
assert link in node2.links
@pytest.mark.asyncio
async def test_add_node_already_connected(project, compute):
"""
Raise an error if we try to use an already connected port
@@ -104,10 +107,11 @@ async def test_add_node_already_connected(project, compute):
assert link.create.called
link2 = Link(project)
link2.create = AsyncioMagicMock()
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await link2.add_node(node1, 0, 4)
@pytest.mark.asyncio
async def test_add_node_cloud(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -123,6 +127,7 @@ async def test_add_node_cloud(project, compute):
await link.add_node(node2, 0, 4)
@pytest.mark.asyncio
async def test_add_node_cloud_to_cloud(project, compute):
"""
Cloud to cloud connection is not allowed
@@ -138,10 +143,11 @@ async def test_add_node_cloud_to_cloud(project, compute):
link._project.emit_notification = MagicMock()
await link.add_node(node1, 0, 4)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await link.add_node(node2, 0, 4)
@pytest.mark.asyncio
async def test_add_node_same_node(project, compute):
"""
Connection to the same node is not allowed
@@ -155,10 +161,11 @@ async def test_add_node_same_node(project, compute):
link._project.emit_notification = MagicMock()
await link.add_node(node1, 0, 4)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await link.add_node(node1, 0, 5)
@pytest.mark.asyncio
async def test_add_node_serial_to_ethernet(project, compute):
"""
Serial to ethernet connection is not allowed
@@ -174,10 +181,11 @@ async def test_add_node_serial_to_ethernet(project, compute):
link._project.emit_notification = MagicMock()
await link.add_node(node1, 0, 4)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await link.add_node(node2, 0, 4)
@pytest.mark.asyncio
async def test_json(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -247,6 +255,7 @@ async def test_json(project, compute):
}
@pytest.mark.asyncio
async def test_json_serial_link(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -261,6 +270,7 @@ async def test_json_serial_link(project, compute):
assert link.__json__()["link_type"] == "serial"
@pytest.mark.asyncio
async def test_default_capture_file_name(project, compute):
node1 = Node(project, compute, "Hello@", node_type="qemu")
@@ -275,8 +285,10 @@ async def test_default_capture_file_name(project, compute):
assert link.default_capture_file_name() == "Hello_0-4_to_w0rld_1-3.pcap"
@pytest.mark.asyncio
async def test_start_capture(link):
async def fake_reader():
return AsyncioBytesIO()
@@ -288,6 +300,7 @@ async def test_start_capture(link):
link._project.emit_notification.assert_called_with("link.updated", link.__json__())
@pytest.mark.asyncio
async def test_stop_capture(link):
link._capturing = True
@@ -297,6 +310,7 @@ async def test_stop_capture(link):
link._project.emit_notification.assert_called_with("link.updated", link.__json__())
@pytest.mark.asyncio
async def test_delete(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -316,6 +330,7 @@ async def test_delete(project, compute):
assert link not in node2.links
@pytest.mark.asyncio
async def test_update_filters(project, compute):
node1 = Node(project, compute, "node1", node_type="qemu")
@@ -346,6 +361,7 @@ async def test_update_filters(project, compute):
assert link.update.called
@pytest.mark.asyncio
async def test_available_filters(project, compute):
node1 = Node(project, compute, "node1", node_type="ethernet_switch")
+21
View File
@@ -101,6 +101,7 @@ def test_empty_properties(compute, project):
assert "category" not in node.properties # Controller only
@pytest.mark.asyncio
async def test_eq(compute, project, node, controller):
assert node == Node(project, compute, "demo1", node_id=node.id, node_type="qemu")
@@ -186,6 +187,7 @@ def test_init_without_uuid(project, compute):
assert node.id is not None
@pytest.mark.asyncio
async def test_create(node, compute):
node._console = 2048
@@ -206,6 +208,7 @@ async def test_create(node, compute):
assert node._properties == {"startup_script": "echo test"}
@pytest.mark.asyncio
async def test_create_image_missing(node, compute):
node._console = 2048
@@ -228,6 +231,7 @@ async def test_create_image_missing(node, compute):
#assert node._upload_missing_image.called is True
@pytest.mark.asyncio
async def test_create_base_script(node, config, compute, tmpdir):
config.set_section_config("Server", {"configs_path": str(tmpdir)})
@@ -303,6 +307,7 @@ def test_label_with_default_label_font(node):
assert node.label["style"] == None #"font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #ff0000;fill-opacity: 1.0;"
@pytest.mark.asyncio
async def test_update(node, compute, project, controller):
response = MagicMock()
@@ -326,6 +331,7 @@ async def test_update(node, compute, project, controller):
assert project.dump.called
@pytest.mark.asyncio
async def test_update_properties(node, compute, controller):
"""
properties will be updated by the answer from compute
@@ -354,6 +360,7 @@ async def test_update_properties(node, compute, controller):
#controller._notification.emit.assert_called_with("node.updated", node_notif)
@pytest.mark.asyncio
async def test_update_only_controller(node, compute):
"""
When updating property used only on controller we don't need to
@@ -374,6 +381,7 @@ async def test_update_only_controller(node, compute):
assert not node._project.emit_notification.called
@pytest.mark.asyncio
async def test_update_no_changes(node, compute):
"""
We don't call the compute node if all compute properties has not changed
@@ -391,6 +399,7 @@ async def test_update_no_changes(node, compute):
assert node.x == 43
@pytest.mark.asyncio
async def test_start(node, compute):
compute.post = AsyncioMagicMock()
@@ -399,6 +408,7 @@ async def test_start(node, compute):
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/start".format(node.project.id, node.id), timeout=240)
@pytest.mark.asyncio
async def test_start_iou(compute, project, controller):
node = Node(project, compute, "demo",
@@ -415,6 +425,7 @@ async def test_start_iou(compute, project, controller):
compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"license_check": True, "iourc_content": "aa"})
@pytest.mark.asyncio
async def test_stop(node, compute):
compute.post = AsyncioMagicMock()
@@ -423,6 +434,7 @@ async def test_stop(node, compute):
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/stop".format(node.project.id, node.id), timeout=240, dont_connect=True)
@pytest.mark.asyncio
async def test_suspend(node, compute):
compute.post = AsyncioMagicMock()
@@ -430,6 +442,7 @@ async def test_suspend(node, compute):
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/suspend".format(node.project.id, node.id), timeout=240)
@pytest.mark.asyncio
async def test_reload(node, compute):
compute.post = AsyncioMagicMock()
@@ -437,6 +450,7 @@ async def test_reload(node, compute):
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/reload".format(node.project.id, node.id), timeout=240)
@pytest.mark.asyncio
async def test_create_without_console(node, compute):
"""
None properties should be send. Because it can mean the emulator doesn't support it
@@ -458,24 +472,28 @@ async def test_create_without_console(node, compute):
assert node._properties == {"test_value": "success", "startup_script": "echo test"}
@pytest.mark.asyncio
async def test_delete(node, compute):
await node.destroy()
compute.delete.assert_called_with("/projects/{}/vpcs/nodes/{}".format(node.project.id, node.id))
@pytest.mark.asyncio
async def test_post(node, compute):
await node.post("/test", {"a": "b"})
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/test".format(node.project.id, node.id), data={"a": "b"})
@pytest.mark.asyncio
async def test_delete(node, compute):
await node.delete("/test")
compute.delete.assert_called_with("/projects/{}/vpcs/nodes/{}/test".format(node.project.id, node.id))
@pytest.mark.asyncio
async def test_dynamips_idle_pc(node, compute):
node._node_type = "dynamips"
@@ -486,6 +504,7 @@ async def test_dynamips_idle_pc(node, compute):
compute.get.assert_called_with("/projects/{}/dynamips/nodes/{}/auto_idlepc".format(node.project.id, node.id), timeout=240)
@pytest.mark.asyncio
async def test_dynamips_idlepc_proposals(node, compute):
node._node_type = "dynamips"
@@ -496,6 +515,7 @@ async def test_dynamips_idlepc_proposals(node, compute):
compute.get.assert_called_with("/projects/{}/dynamips/nodes/{}/idlepc_proposals".format(node.project.id, node.id), timeout=240)
@pytest.mark.asyncio
async def test_upload_missing_image(compute, controller, images_dir):
project = Project(str(uuid.uuid4()), controller=controller)
@@ -535,6 +555,7 @@ def test_get_port(node):
assert port is None
@pytest.mark.asyncio
async def test_parse_node_response(node):
"""
When a node is updated we notify the links connected to it
+6
View File
@@ -22,6 +22,7 @@ from tests.utils import AsyncioMagicMock
@pytest.fixture
@pytest.mark.asyncio
async def node(project):
compute = MagicMock()
@@ -33,6 +34,7 @@ async def node(project):
return await project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
@pytest.mark.asyncio
async def test_emit_to_all(controller, project):
"""
Send an event to all if we don't have a project id in the event
@@ -49,6 +51,7 @@ async def test_emit_to_all(controller, project):
assert len(notif._project_listeners[project.id]) == 0
@pytest.mark.asyncio
async def test_emit_to_project(controller, project):
"""
Send an event to a project listeners
@@ -67,6 +70,7 @@ async def test_emit_to_project(controller, project):
assert len(notif._project_listeners[project.id]) == 0
@pytest.mark.asyncio
async def test_dispatch(controller, project):
notif = controller.notification
@@ -78,6 +82,7 @@ async def test_dispatch(controller, project):
assert msg == ('test', {}, {})
@pytest.mark.asyncio
async def test_dispatch_ping(controller, project):
notif = controller.notification
@@ -89,6 +94,7 @@ async def test_dispatch_ping(controller, project):
assert msg == ('ping', {'compute_id': 12}, {})
@pytest.mark.asyncio
async def test_dispatch_node_updated(controller, node, project):
"""
When we receive a node.updated notification from compute
+57 -12
View File
@@ -20,7 +20,6 @@ import os
import sys
import uuid
import pytest
import aiohttp
from unittest.mock import MagicMock
from tests.utils import AsyncioMagicMock, asyncio_patch
from unittest.mock import patch
@@ -30,10 +29,12 @@ from gns3server.controller.project import Project
from gns3server.controller.template import Template
from gns3server.controller.node import Node
from gns3server.controller.ports.ethernet_port import EthernetPort
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError, ControllerForbiddenError
from gns3server.config import Config
@pytest.fixture
@pytest.mark.asyncio
async def node(controller, project):
compute = MagicMock()
@@ -45,6 +46,7 @@ async def node(controller, project):
return node
@pytest.mark.asyncio
async def test_affect_uuid():
p = Project(name="Test")
@@ -53,6 +55,7 @@ async def test_affect_uuid():
assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
@pytest.mark.asyncio
async def test_json():
p = Project(name="Test")
@@ -80,6 +83,7 @@ async def test_json():
}
@pytest.mark.asyncio
async def test_update(controller):
project = Project(controller=controller, name="Hello")
@@ -90,6 +94,7 @@ async def test_update(controller):
project.emit_notification.assert_any_call("project.updated", project.__json__())
@pytest.mark.asyncio
async def test_update_on_compute(controller):
variables = [{"name": "TEST", "value": "VAL1"}]
@@ -102,6 +107,7 @@ async def test_update_on_compute(controller):
compute.put.assert_any_call('/projects/{}'.format(project.id), {"variables": variables})
@pytest.mark.asyncio
async def test_path(projects_dir):
directory = projects_dir
@@ -118,10 +124,11 @@ def test_path_exist(tmpdir):
"""
os.makedirs(str(tmpdir / "demo"))
with pytest.raises(aiohttp.web.HTTPForbidden):
with pytest.raises(ControllerForbiddenError):
Project(name="Test", path=str(tmpdir / "demo"))
@pytest.mark.asyncio
async def test_init_path(tmpdir):
p = Project(path=str(tmpdir), project_id=str(uuid4()), name="Test")
@@ -129,13 +136,15 @@ async def test_init_path(tmpdir):
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
@pytest.mark.asyncio
async def test_changing_path_with_quote_not_allowed(tmpdir):
with pytest.raises(aiohttp.web.HTTPForbidden):
with pytest.raises(ControllerForbiddenError):
p = Project(project_id=str(uuid4()), name="Test")
p.path = str(tmpdir / "project\"53")
@pytest.mark.asyncio
async def test_captures_directory(tmpdir):
p = Project(path=str(tmpdir / "capturestest"), name="Test")
@@ -143,6 +152,7 @@ async def test_captures_directory(tmpdir):
assert os.path.exists(p.captures_directory)
@pytest.mark.asyncio
async def test_add_node_local(controller):
"""
For a local server we send the project path
@@ -174,6 +184,7 @@ async def test_add_node_local(controller):
project.emit_notification.assert_any_call("node.created", node.__json__())
@pytest.mark.asyncio
async def test_add_node_non_local(controller):
"""
For a non local server we do not send the project path
@@ -201,6 +212,7 @@ async def test_add_node_non_local(controller):
project.emit_notification.assert_any_call("node.created", node.__json__())
@pytest.mark.asyncio
async def test_add_node_iou(controller):
"""
Test if an application ID is allocated for IOU nodes
@@ -222,6 +234,7 @@ async def test_add_node_iou(controller):
assert node3.properties["application_id"] == 3
@pytest.mark.asyncio
async def test_add_node_iou_with_multiple_projects(controller):
"""
Test if an application ID is allocated for IOU nodes with different projects already opened
@@ -274,6 +287,7 @@ async def test_add_node_iou_with_multiple_projects(controller):
assert node12.properties["application_id"] == 3
@pytest.mark.asyncio
async def test_add_node_iou_with_multiple_projects_different_computes(controller):
"""
Test if an application ID is allocated for IOU nodes with different projects already opened
@@ -309,6 +323,7 @@ async def test_add_node_iou_with_multiple_projects_different_computes(controller
assert node6.properties["application_id"] == 4
@pytest.mark.asyncio
async def test_add_node_iou_no_id_available(controller):
"""
Test if an application ID is allocated for IOU nodes
@@ -321,13 +336,14 @@ async def test_add_node_iou_no_id_available(controller):
response = MagicMock()
compute.post = AsyncioMagicMock(return_value=response)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
for i in range(1, 513):
prop = {"properties": {"application_id": i}}
project._nodes[i] = Node(project, compute, "Node{}".format(i), node_id=i, node_type="iou", **prop)
await project.add_node(compute, "test1", None, node_type="iou")
@pytest.mark.asyncio
async def test_add_node_from_template(controller):
"""
For a local server we send the project path
@@ -361,6 +377,7 @@ async def test_add_node_from_template(controller):
project.emit_notification.assert_any_call("node.created", node.__json__())
@pytest.mark.asyncio
async def test_add_builtin_node_from_template(controller):
"""
For a local server we send the project path
@@ -394,6 +411,7 @@ async def test_add_builtin_node_from_template(controller):
project.emit_notification.assert_any_call("node.created", node.__json__())
@pytest.mark.asyncio
async def test_delete_node(controller):
"""
For a local server we send the project path
@@ -415,6 +433,7 @@ async def test_delete_node(controller):
project.emit_notification.assert_any_call("node.deleted", node.__json__())
@pytest.mark.asyncio
async def test_delete_locked_node(controller):
"""
For a local server we send the project path
@@ -431,10 +450,11 @@ async def test_delete_locked_node(controller):
node = await project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
assert node.id in project._nodes
node.locked = True
with pytest.raises(aiohttp.web_exceptions.HTTPConflict):
with pytest.raises(ControllerError):
await project.delete_node(node.id)
@pytest.mark.asyncio
async def test_delete_node_delete_link(controller):
"""
Delete a node delete all the node connected
@@ -461,6 +481,7 @@ async def test_delete_node_delete_link(controller):
project.emit_notification.assert_any_call("link.deleted", link.__json__())
@pytest.mark.asyncio
async def test_get_node(controller):
compute = MagicMock()
@@ -473,15 +494,16 @@ async def test_get_node(controller):
vm = await project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"})
assert project.get_node(vm.id) == vm
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
project.get_node("test")
# Raise an error if the project is not opened
await project.close()
with pytest.raises(aiohttp.web.HTTPForbidden):
with pytest.raises(ControllerForbiddenError):
project.get_node(vm.id)
@pytest.mark.asyncio
async def test_list_nodes(controller):
compute = MagicMock()
@@ -500,6 +522,7 @@ async def test_list_nodes(controller):
assert isinstance(project.nodes, dict)
@pytest.mark.asyncio
async def test_add_link(project):
compute = MagicMock()
@@ -522,6 +545,7 @@ async def test_add_link(project):
project.emit_notification.assert_any_call("link.created", link.__json__())
@pytest.mark.asyncio
async def test_list_links(project):
compute = MagicMock()
@@ -536,6 +560,7 @@ async def test_list_links(project):
assert len(project.links) == 1
@pytest.mark.asyncio
async def test_get_link(project):
compute = MagicMock()
@@ -546,10 +571,11 @@ async def test_get_link(project):
link = await project.add_link()
assert project.get_link(link.id) == link
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
project.get_link("test")
@pytest.mark.asyncio
async def test_delete_link(project):
compute = MagicMock()
@@ -566,6 +592,7 @@ async def test_delete_link(project):
assert len(project._links) == 0
@pytest.mark.asyncio
async def test_add_drawing(project):
project.emit_notification = MagicMock()
@@ -574,15 +601,17 @@ async def test_add_drawing(project):
project.emit_notification.assert_any_call("drawing.created", drawing.__json__())
@pytest.mark.asyncio
async def test_get_drawing(project):
drawing = await project.add_drawing(None)
assert project.get_drawing(drawing.id) == drawing
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
project.get_drawing("test")
@pytest.mark.asyncio
async def test_list_drawing(project):
await project.add_drawing(None)
@@ -592,6 +621,7 @@ async def test_list_drawing(project):
assert len(project.drawings) == 1
@pytest.mark.asyncio
async def test_delete_drawing(project):
assert len(project._drawings) == 0
@@ -603,6 +633,7 @@ async def test_delete_drawing(project):
assert len(project._drawings) == 0
@pytest.mark.asyncio
async def test_clean_pictures(project):
"""
When a project is close old pictures should be removed
@@ -617,6 +648,7 @@ async def test_clean_pictures(project):
assert not os.path.exists(os.path.join(project.pictures_directory, "test2.png"))
@pytest.mark.asyncio
async def test_clean_pictures_and_keep_supplier_logo(project):
"""
When a project is close old pictures should be removed
@@ -638,6 +670,7 @@ async def test_clean_pictures_and_keep_supplier_logo(project):
assert os.path.exists(os.path.join(project.pictures_directory, "logo.png"))
@pytest.mark.asyncio
async def test_delete(project):
assert os.path.exists(project.path)
@@ -645,6 +678,7 @@ async def test_delete(project):
assert not os.path.exists(project.path)
@pytest.mark.asyncio
async def test_dump(projects_dir):
directory = projects_dir
@@ -656,6 +690,7 @@ async def test_dump(projects_dir):
assert "00010203-0405-0607-0809-0a0b0c0d0e0f" in content
@pytest.mark.asyncio
async def test_open_close(controller):
project = Project(controller=controller, name="Test")
@@ -671,6 +706,7 @@ async def test_open_close(controller):
project.emit_notification.assert_any_call("project.closed", project.__json__())
@pytest.mark.asyncio
async def test_open_auto_start(controller):
project = Project(controller=controller, name="Test", auto_start=True)
@@ -691,6 +727,7 @@ def test_is_running(project, node):
assert project.is_running() is True
@pytest.mark.asyncio
async def test_duplicate(project, controller):
"""
Duplicate a project, the node should remain on the remote server
@@ -743,10 +780,11 @@ def test_get_snapshot(project):
snapshot = list(project.snapshots.values())[0]
assert project.get_snapshot(snapshot.id) == snapshot
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
project.get_snapshot("BLU")
@pytest.mark.asyncio
async def test_delete_snapshot(project):
os.makedirs(os.path.join(project.path, "snapshots"))
@@ -758,12 +796,13 @@ async def test_delete_snapshot(project):
await project.delete_snapshot(snapshot.id)
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
with pytest.raises(ControllerNotFoundError):
project.get_snapshot(snapshot.id)
assert not os.path.exists(os.path.join(project.path, "snapshots", "test1.gns3project"))
@pytest.mark.asyncio
async def test_snapshot(project):
"""
Create a snapshot
@@ -777,10 +816,11 @@ async def test_snapshot(project):
assert list(project.snapshots.values())[0].name == "test1"
# Raise a conflict if name is already use
with pytest.raises(aiohttp.web_exceptions.HTTPConflict):
with pytest.raises(ControllerError):
snapshot = await project.snapshot("test1")
@pytest.mark.asyncio
async def test_start_all(project):
compute = MagicMock()
@@ -797,6 +837,7 @@ async def test_start_all(project):
assert len(compute.post.call_args_list) == 10
@pytest.mark.asyncio
async def test_stop_all(project):
compute = MagicMock()
@@ -813,6 +854,7 @@ async def test_stop_all(project):
assert len(compute.post.call_args_list) == 10
@pytest.mark.asyncio
async def test_suspend_all(project):
compute = MagicMock()
@@ -829,6 +871,7 @@ async def test_suspend_all(project):
assert len(compute.post.call_args_list) == 10
@pytest.mark.asyncio
async def test_console_reset_all(project):
compute = MagicMock()
@@ -845,6 +888,7 @@ async def test_console_reset_all(project):
assert len(compute.post.call_args_list) == 10
@pytest.mark.asyncio
async def test_node_name(project):
compute = MagicMock()
@@ -870,6 +914,7 @@ async def test_node_name(project):
assert node.name == "R3"
@pytest.mark.asyncio
async def test_duplicate_node(project):
compute = MagicMock()
+5 -3
View File
@@ -18,7 +18,6 @@
import json
import pytest
import aiohttp
from tests.utils import asyncio_patch
@@ -146,7 +145,8 @@ def demo_topology():
}
# async def test_load_project(controller, tmpdir, demo_topology, http_client):
# @pytest.mark.asyncio
#async def test_load_project(controller, tmpdir, demo_topology, http_client):
#
# with open(str(tmpdir / "demo.gns3"), "w+") as f:
# json.dump(demo_topology, f)
@@ -170,6 +170,7 @@ def demo_topology():
# assert project.scene_width == 700
@pytest.mark.asyncio
async def test_open(controller, tmpdir):
simple_topology = {
@@ -208,7 +209,8 @@ async def test_open(controller, tmpdir):
assert project.scene_width == 700
# async def test_open_missing_compute(controller, tmpdir, demo_topology, http_client):
# @pytest.mark.asyncio
#async def test_open_missing_compute(controller, tmpdir, demo_topology, http_client):
# """
# If a compute is missing the project should not be open and the .gns3 should
# be the one before opening the project
+1
View File
@@ -70,6 +70,7 @@ def test_json(project):
}
@pytest.mark.asyncio
async def test_restore(project, controller):
compute = AsyncioMagicMock()
+7 -4
View File
@@ -18,16 +18,17 @@
import json
import uuid
import pytest
import aiohttp
from unittest.mock import MagicMock
from tests.utils import asyncio_patch
from gns3server.controller.project import Project
from gns3server.controller.compute import Compute
from gns3server.controller.topology import project_to_topology, load_topology, GNS3_FILE_FORMAT_REVISION
from gns3server.controller.controller_error import ControllerError
from gns3server.version import __version__
@pytest.mark.asyncio
async def test_project_to_topology_empty(tmpdir):
project = Project(name="Test")
@@ -61,6 +62,7 @@ async def test_project_to_topology_empty(tmpdir):
}
@pytest.mark.asyncio
async def test_basic_topology(controller):
project = Project(name="Test", controller=controller)
@@ -86,6 +88,7 @@ async def test_basic_topology(controller):
assert topo["topology"]["drawings"][0] == drawing.__json__(topology_dump=True)
@pytest.mark.asyncio
async def test_project_to_topology(controller):
variables = [
@@ -132,7 +135,7 @@ def test_load_topology(tmpdir):
def test_load_topology_file_error(tmpdir):
path = str(tmpdir / "test.gns3")
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
load_topology(path)
@@ -143,7 +146,7 @@ def test_load_topology_file_error_schema_error(tmpdir):
json.dump({
"revision": GNS3_FILE_FORMAT_REVISION
}, f)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
load_topology(path)
@@ -165,7 +168,7 @@ def test_load_newer_topology(tmpdir):
path = str(tmpdir / "test.gns3")
with open(path, "w+") as f:
json.dump(data, f)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
load_topology(path)
+12 -4
View File
@@ -16,15 +16,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import aiohttp
from unittest.mock import MagicMock
from tests.utils import AsyncioMagicMock
from gns3server.controller.udp_link import UDPLink
from gns3server.controller.ports.ethernet_port import EthernetPort
from gns3server.controller.node import Node
from gns3server.controller.controller_error import ControllerError
@pytest.mark.asyncio
async def test_create(project):
compute1 = MagicMock()
@@ -90,6 +91,7 @@ async def test_create(project):
}, timeout=120)
@pytest.mark.asyncio
async def test_create_one_side_failure(project):
compute1 = MagicMock()
@@ -129,13 +131,13 @@ async def test_create_one_side_failure(project):
response.json = {"udp_port": 2048}
return response
elif "/adapters" in path:
raise aiohttp.web.HTTPConflict(text="Error when creating the NIO")
raise ControllerError("Error when creating the NIO")
compute1.post.side_effect = compute1_callback
compute1.host = "example.com"
compute2.post.side_effect = compute2_callback
compute2.host = "example.org"
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
await link.add_node(node2, 3, 1)
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={
@@ -159,6 +161,7 @@ async def test_create_one_side_failure(project):
compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), timeout=120)
@pytest.mark.asyncio
async def test_delete(project):
compute1 = MagicMock()
@@ -180,6 +183,7 @@ async def test_delete(project):
compute2.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), timeout=120)
@pytest.mark.asyncio
async def test_choose_capture_side(project):
"""
The link capture should run on the optimal node
@@ -230,13 +234,14 @@ async def test_choose_capture_side(project):
await link.add_node(node_vpcs, 0, 4)
await link.add_node(node_iou, 3, 1)
with pytest.raises(aiohttp.web.HTTPConflict):
with pytest.raises(ControllerError):
link._choose_capture_side()
# If you start a node you can capture on it
node_vpcs._status = "started"
assert link._choose_capture_side()["node"] == node_vpcs
@pytest.mark.asyncio
async def test_capture(project):
compute1 = MagicMock()
@@ -265,6 +270,7 @@ async def test_capture(project):
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/stop_capture".format(project.id, node_vpcs.id))
@pytest.mark.asyncio
async def test_node_updated(project):
"""
If a node stop when capturing we stop the capture
@@ -286,6 +292,7 @@ async def test_node_updated(project):
assert link.stop_capture.called
@pytest.mark.asyncio
async def test_update(project):
compute1 = MagicMock()
@@ -365,6 +372,7 @@ async def test_update(project):
}, timeout=120)
@pytest.mark.asyncio
async def test_update_suspend(project):
compute1 = MagicMock()
compute2 = MagicMock()