From e50acf811c0d0dd48d02495d9e771facb81fa84e Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 25 Jul 2016 18:02:22 +0200 Subject: [PATCH] If we don't have a GNS3 VM on linux don't move file to it --- gns3server/controller/__init__.py | 6 ++++ gns3server/controller/import_project.py | 5 +-- tests/controller/test_controller.py | 8 ++++- tests/controller/test_import_project.py | 47 +++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 73e4adf2e..449945dea 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -244,6 +244,12 @@ class Controller: raise aiohttp.web.HTTPNotFound(text="You try to use a node on the GNS3 VM server but the GNS3 is not configured") raise aiohttp.web.HTTPNotFound(text="Compute ID {} doesn't exist".format(compute_id)) + def has_compute(self, compute_id): + """ + Return True if the compute exist in the controller + """ + return compute_id in self._computes + @asyncio.coroutine def add_project(self, project_id=None, name=None, **kwargs): """ diff --git a/gns3server/controller/import_project.py b/gns3server/controller/import_project.py index 2c63f4da5..274c7194c 100644 --- a/gns3server/controller/import_project.py +++ b/gns3server/controller/import_project.py @@ -74,8 +74,9 @@ def import_project(controller, project_id, stream, location=None, name=None, kee # Modify the compute id of the node depending of compute capacity if not keep_compute_id: - # For some VM type we move them to the GNS3 VM if it's not a Linux host - if not sys.platform.startswith("linux"): + # For some VM type we move them to the GNS3 VM if possible + # unless it's a linux host without GNS3 VM + if not sys.platform.startswith("linux") or controller.has_compute("vm"): for node in topology["topology"]["nodes"]: if node["node_type"] in ("docker", "qemu", "iou"): node["compute_id"] = "vm" diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index b36dba317..ddc89759d 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -192,6 +192,13 @@ def test_getCompute(controller, async_run): assert controller.get_compute("dsdssd") +def test_has_compute(controller, async_run): + compute = async_run(controller.add_compute(compute_id="test1")) + + assert controller.has_compute("test1") + assert not controller.has_compute("test2") + + def test_initControllerLocal(controller, controller_config_path, async_run): """ The local node is the controller itself you can not change the informations @@ -344,4 +351,3 @@ def test_get_free_project_name(controller, async_run): async_run(controller.add_project(project_id=str(uuid.uuid4()), name="Test-1")) assert controller.get_free_project_name("Test") == "Test-2" assert controller.get_free_project_name("Hello") == "Hello" - diff --git a/tests/controller/test_import_project.py b/tests/controller/test_import_project.py index 6f11babc8..c736af375 100644 --- a/tests/controller/test_import_project.py +++ b/tests/controller/test_import_project.py @@ -131,9 +131,9 @@ def test_import_with_images(tmpdir, async_run, controller): assert os.path.exists(path), path -def test_import_iou_linux(linux_platform, async_run, tmpdir, controller): +def test_import_iou_linux_no_vm(linux_platform, async_run, tmpdir, controller): """ - On non linux host IOU should be local + On non linux host IOU should be local if we don't have a GNS3 VM """ project_id = str(uuid.uuid4()) @@ -172,6 +172,49 @@ def test_import_iou_linux(linux_platform, async_run, tmpdir, controller): assert topo["topology"]["nodes"][0]["compute_id"] == "local" +def test_import_iou_linux_with_vm(linux_platform, async_run, tmpdir, controller): + """ + On non linux host IOU should be vm if we have a GNS3 VM configured + """ + 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", + "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: + project = async_run(import_project(controller, project_id, f)) + + with open(os.path.join(project.path, "test.gns3")) as f: + topo = json.load(f) + assert topo["topology"]["nodes"][0]["compute_id"] == "vm" + + def test_import_iou_non_linux(windows_platform, async_run, tmpdir, controller): """ On non linux host IOU should be moved to the GNS3 VM