From 66860655b93d949d3058527f2018a0e8cf629a55 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 2 Mar 2015 15:35:36 +0100 Subject: [PATCH] If a VM is already loaded, we return a VM instead of creating it twice Partial fix for #81 --- gns3server/modules/base_manager.py | 3 +++ tests/modules/test_manager.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index e318e0fc..05e9621f 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -200,6 +200,9 @@ class BaseManager: :param vm_id: restore a VM identifier """ + if vm_id in self._vms: + return self._vms[vm_id] + project = ProjectManager.instance().get_project(project_id) if vm_id and isinstance(vm_id, int): with (yield from BaseManager._convert_lock): diff --git a/tests/modules/test_manager.py b/tests/modules/test_manager.py index 7ee5fb2b..32a0130a 100644 --- a/tests/modules/test_manager.py +++ b/tests/modules/test_manager.py @@ -34,6 +34,20 @@ def test_create_vm_new_topology(loop, project, port_manager): assert vm in project.vms +def test_create_twice_same_vm_new_topology(loop, project, port_manager): + + project._vms = set() + VPCS._instance = None + vpcs = VPCS.instance() + vpcs.port_manager = port_manager + vm_id = str(uuid.uuid4()) + vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id, console=2222)) + assert vm in project.vms + assert len(project.vms) == 1 + vm = loop.run_until_complete(vpcs.create_vm("PC 2", project.id, vm_id, console=2222)) + assert len(project.vms) == 1 + + def test_create_vm_new_topology_without_uuid(loop, project, port_manager): VPCS._instance = None