async def test_import_iou_non_linux(loop, windows_platform, tmpdir, controller): """ On non linux host IOU should be moved to the GNS3 VM """ project_id = str(uuid.uuid4()) controller._computes["vm"] = AsyncioMagicMock() topology = { "project_id": str(uuid.uuid4()), "name": "test", "type": "topology", "topology": { "nodes": [{ "compute_id": "local", "node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "node_type": "iou", "name": "test", "properties": {} }, { "compute_id": "local", "node_type": "vpcs", "name": "test2", "properties": {} }], "links": [], "computes": [], "drawings": [] }, "revision": 5, "version": "2.0.0" } with open(str(tmpdir / "project.gns3"), 'w+') as f: json.dump(topology, f) zip_path = str(tmpdir / "project.zip") with zipfile.ZipFile(zip_path, 'w') as myzip: myzip.write(str(tmpdir / "project.gns3"), "project.gns3") with open(zip_path, "rb") as f: with asyncio_patch( "gns3server.controller.import_project._move_files_to_compute" ) as mock: project = await import_project(controller, project_id, f) controller._computes["vm"].post.assert_called_with( '/projects', data={ 'name': 'test', 'project_id': project_id }) with open(os.path.join(project.path, "test.gns3")) as f: topo = json.load(f) assert topo["topology"]["nodes"][0]["compute_id"] == "vm" assert topo["topology"]["nodes"][1]["compute_id"] == "local" 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): """ When importing a node, node_id should change """ project_id = str(uuid.uuid4()) controller._computes["local"] = AsyncioMagicMock() topology = { "project_id": str(uuid.uuid4()), "name": "test", "type": "topology", "topology": { "nodes": [{ "compute_id": "local", "node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "node_type": "iou", "name": "test", "properties": {} }, { "compute_id": "local", "node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "node_type": "iou", "name": "test2", "properties": {} }], "links": [{ "link_id": "b570a150-c09f-47d9-8d32-9ca5b03234d6", "nodes": [{ "adapter_number": 0, "node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "port_number": 0 }, { "adapter_number": 0, "node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "port_number": 0 }] }], "computes": [], "drawings": [{ "drawing_id": "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3", "rotation": 0, "x": -210, "y": -108, "z": 0 }] }, "revision": 5, "version": "2.0.0" } with open(str(tmpdir / "project.gns3"), 'w+') as f: json.dump(topology, f) # Fake .gns3project zip_path = str(tmpdir / "project.zip") with zipfile.ZipFile(zip_path, 'w') as myzip: myzip.write(str(tmpdir / "project.gns3"), "project.gns3") myzip.writestr( "project-files/iou/0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b/startup.cfg", "test") myzip.writestr( "project-files/iou/c3ae286c-c81f-40d9-a2d0-5874b2f2478d/startup.cfg", "test") with open(zip_path, "rb") as f: project = await import_project(controller, project_id, f) with open(os.path.join(project.path, "test.gns3")) as f: topo = json.load(f) # Node id should have change assert topo["topology"]["nodes"][0]["node_id"] not in [ "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d" ] # Link should have change link = topo["topology"]["links"][0] assert link["link_id"] != "b570a150-c09f-47d9-8d32-9ca5b03234d6" assert link["nodes"][0]["node_id"] not in [ "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d" ] # Drawing id should change assert topo["topology"]["drawings"][0][ "drawing_id"] != "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3" # Node files should have moved to the new node id assert not os.path.exists( os.path.join(project.path, "project-files", "iou", "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "startup.cfg")) assert not os.path.exists( os.path.join(project.path, "project-files", "iou", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "startup.cfg")) assert os.path.exists( os.path.join(project.path, "project-files", "iou", topo["topology"]["nodes"][0]["node_id"], "startup.cfg"))
def test_stop(node, compute, project, async_run): compute.post = AsyncioMagicMock() async_run(node.stop()) compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/stop".format(node.project.id, node.id), timeout=240, dont_connect=True)
def test_reload(node, compute, project, async_run): compute.post = AsyncioMagicMock() async_run(node.reload()) compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/reload".format(node.project.id, node.id), timeout=240)
def vm(project, manager, ubridge_path): vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) vm._vpcs_version = parse_version("0.9") vm._start_ubridge = AsyncioMagicMock() return vm
def compute(): s = AsyncioMagicMock() s.id = "http://test.com:42" return s
def response(): request = AsyncioMagicMock() return Response(request=request)
def test_export_images_from_vm(tmpdir, project, async_run): """ If data is on a remote server export it locally before sending it in the archive. """ compute = MagicMock() compute.id = "vm" compute.list_files = AsyncioMagicMock(return_value=[ {"path": "vm-1/dynamips/test"} ]) # Fake file that will be download from the vm mock_response = AsyncioMagicMock() mock_response.content = AsyncioBytesIO() async_run(mock_response.content.write(b"HELLO")) mock_response.content.seek(0) mock_response.status = 200 compute.download_file = AsyncioMagicMock(return_value=mock_response) mock_response = AsyncioMagicMock() mock_response.content = AsyncioBytesIO() async_run(mock_response.content.write(b"IMAGE")) mock_response.content.seek(0) mock_response.status = 200 compute.download_image = AsyncioMagicMock(return_value=mock_response) project._project_created_on_compute.add(compute) path = project.path os.makedirs(os.path.join(path, "vm-1", "dynamips")) topology = { "topology": { "nodes": [ { "compute_id": "vm", "properties": { "image": "test.image" }, "node_type": "dynamips" } ] } } # The .gns3 should be renamed project.gns3 in order to simplify import with open(os.path.join(path, "test.gns3"), 'w+') as f: f.write(json.dumps(topology)) with aiozipstream.ZipFile() as z: async_run(export_project(z, project, str(tmpdir), include_images=True)) assert compute.list_files.called async_run(write_file(str(tmpdir / 'zipfile.zip'), z)) with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip: with myzip.open("vm-1/dynamips/test") as myfile: content = myfile.read() assert content == b"HELLO" with myzip.open("images/dynamips/test.image") as myfile: content = myfile.read() assert content == b"IMAGE"
async def test_reload_all_nodes(controller_api, project, compute): compute.post = AsyncioMagicMock() response = await controller_api.post("/projects/{}/nodes/reload".format( project.id)) assert response.status == 204
async def test_delete_node(controller_api, project, node, compute): compute.post = AsyncioMagicMock() response = await controller_api.delete("/projects/{}/nodes/{}".format( project.id, node.id)) assert response.status == 204
def vm(project, manager, ubridge_path): vm = TraceNGVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) vm._start_ubridge = AsyncioMagicMock() vm._ubridge_hypervisor = MagicMock() vm._ubridge_hypervisor.is_running.return_value = True return vm
async def test_json(project, compute): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 1, 3)] link = Link(project) link.create = AsyncioMagicMock() await link.add_node(node1, 0, 4) await link.add_node(node2, 1, 3) assert link.__json__() == { "link_id": link.id, "project_id": project.id, "nodes": [ { "node_id": node1.id, "adapter_number": 0, "port_number": 4, 'label': { 'text': '0/4', 'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;' } }, { "node_id": node2.id, "adapter_number": 1, "port_number": 3, 'label': { 'text': '1/3', 'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;' } } ], "filters": {}, "link_style": {}, "suspend": False, "link_type": "ethernet", "capturing": False, "capture_file_name": None, "capture_file_path": None, "capture_compute_id": None } assert link.__json__(topology_dump=True) == { "link_id": link.id, "nodes": [ { "node_id": node1.id, "adapter_number": 0, "port_number": 4, 'label': { 'text': '0/4', 'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;' } }, { "node_id": node2.id, "adapter_number": 1, "port_number": 3, 'label': { 'text': '1/3', 'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;' } } ], "link_style": {}, "filters": {}, "suspend": False }
def test_json(async_run, project, compute, link): node1 = Node(project, compute, "node1", node_type="qemu") node1._ports = [EthernetPort("E0", 0, 0, 4)] node2 = Node(project, compute, "node2", node_type="qemu") node2._ports = [EthernetPort("E0", 0, 1, 3)] link = Link(project) link.create = AsyncioMagicMock() async_run(link.add_node(node1, 0, 4)) async_run(link.add_node(node2, 1, 3)) assert link.__json__() == { "link_id": link.id, "project_id": project.id, "nodes": [{ "node_id": node1.id, "adapter_number": 0, "port_number": 4, 'label': { 'y': -10, 'text': '0/4', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } }, { "node_id": node2.id, "adapter_number": 1, "port_number": 3, 'label': { 'y': -10, 'text': '1/3', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } }], "filters": {}, "suspend": False, "link_type": "ethernet", "capturing": False, "capture_file_name": None, "capture_file_path": None } assert link.__json__(topology_dump=True) == { "link_id": link.id, "nodes": [{ "node_id": node1.id, "adapter_number": 0, "port_number": 4, 'label': { 'y': -10, 'text': '0/4', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } }, { "node_id": node2.id, "adapter_number": 1, "port_number": 3, 'label': { 'y': -10, 'text': '1/3', 'x': -10, 'rotation': 0, 'style': 'font-size: 10; font-style: Verdana' } }], "filters": {}, "suspend": False }
def test_open_auto_start(async_run, controller): project = Project(controller=controller, status="closed", name="Test") project.auto_start = True project.start_all = AsyncioMagicMock() async_run(project.open()) assert project.start_all.called
def test_add_node_iou_with_multiple_projects(async_run, controller): """ Test if an application ID is allocated for IOU nodes with different projects already opened """ compute = MagicMock() compute.id = "local" project1 = async_run( controller.add_project(project_id=str(uuid.uuid4()), name="test1")) project1.emit_notification = MagicMock() project2 = async_run( controller.add_project(project_id=str(uuid.uuid4()), name="test2")) project2.emit_notification = MagicMock() project3 = async_run( controller.add_project(project_id=str(uuid.uuid4()), name="test3")) project3.emit_notification = MagicMock() response = MagicMock() compute.post = AsyncioMagicMock(return_value=response) node1 = async_run( project1.add_node(compute, "test1", None, node_type="iou")) node2 = async_run( project1.add_node(compute, "test2", None, node_type="iou")) node3 = async_run( project1.add_node(compute, "test3", None, node_type="iou")) node4 = async_run( project2.add_node(compute, "test4", None, node_type="iou")) node5 = async_run( project2.add_node(compute, "test5", None, node_type="iou")) node6 = async_run( project2.add_node(compute, "test6", None, node_type="iou")) node7 = async_run( project3.add_node(compute, "test7", None, node_type="iou")) node8 = async_run( project3.add_node(compute, "test8", None, node_type="iou")) node9 = async_run( project3.add_node(compute, "test9", None, node_type="iou")) assert node1.properties["application_id"] == 1 assert node2.properties["application_id"] == 2 assert node3.properties["application_id"] == 3 assert node4.properties["application_id"] == 4 assert node5.properties["application_id"] == 5 assert node6.properties["application_id"] == 6 assert node7.properties["application_id"] == 7 assert node8.properties["application_id"] == 8 assert node9.properties["application_id"] == 9 controller.remove_project(project1) project4 = async_run( controller.add_project(project_id=str(uuid.uuid4()), name="test4")) project4.emit_notification = MagicMock() node10 = async_run( project3.add_node(compute, "test10", None, node_type="iou")) node11 = async_run( project3.add_node(compute, "test11", None, node_type="iou")) node12 = async_run( project3.add_node(compute, "test12", None, node_type="iou")) assert node10.properties["application_id"] == 1 assert node11.properties["application_id"] == 2 assert node12.properties["application_id"] == 3