diff --git a/gns3server/modules/vpcs/__init__.py b/gns3server/modules/vpcs/__init__.py index dbabf8ca..046eff7f 100644 --- a/gns3server/modules/vpcs/__init__.py +++ b/gns3server/modules/vpcs/__init__.py @@ -31,15 +31,16 @@ class VPCS(BaseManager): def __init__(self): super().__init__() - self._free_mac_ids = list(range(0, 255)) + self._free_mac_ids = {} self._used_mac_ids = {} @asyncio.coroutine def create_vm(self, *args, **kwargs): vm = yield from super().create_vm(*args, **kwargs) + self._free_mac_ids.setdefault(vm.project.uuid, list(range(0, 255))) try: - self._used_mac_ids[vm.uuid] = self._free_mac_ids.pop(0) + self._used_mac_ids[vm.uuid] = self._free_mac_ids[vm.project.uuid].pop(0) except IndexError: raise VPCSError("No mac address available") return vm @@ -47,8 +48,9 @@ class VPCS(BaseManager): @asyncio.coroutine def delete_vm(self, uuid, *args, **kwargs): + vm = self.get_vm(uuid) i = self._used_mac_ids[uuid] - self._free_mac_ids.insert(0, i) + self._free_mac_ids[vm.project.uuid].insert(0, i) del self._used_mac_ids[uuid] yield from super().delete_vm(uuid, *args, **kwargs) diff --git a/tests/modules/vpcs/test_vpcs_manager.py b/tests/modules/vpcs/test_vpcs_manager.py index a239c1c1..7632ef4e 100644 --- a/tests/modules/vpcs/test_vpcs_manager.py +++ b/tests/modules/vpcs/test_vpcs_manager.py @@ -44,6 +44,24 @@ def test_get_mac_id(loop, project, port_manager): assert vpcs.get_mac_id(vm3_uuid) == 0 +def test_get_mac_id_multiple_project(loop, port_manager): + # Cleanup the VPCS object + VPCS._instance = None + vpcs = VPCS.instance() + vpcs.port_manager = port_manager + vm1_uuid = str(uuid.uuid4()) + vm2_uuid = str(uuid.uuid4()) + vm3_uuid = str(uuid.uuid4()) + project1 = ProjectManager.instance().create_project() + project2 = ProjectManager.instance().create_project() + loop.run_until_complete(vpcs.create_vm("PC 1", project1.uuid, vm1_uuid)) + loop.run_until_complete(vpcs.create_vm("PC 2", project1.uuid, vm2_uuid)) + loop.run_until_complete(vpcs.create_vm("PC 2", project2.uuid, vm3_uuid)) + assert vpcs.get_mac_id(vm1_uuid) == 0 + assert vpcs.get_mac_id(vm2_uuid) == 1 + assert vpcs.get_mac_id(vm3_uuid) == 0 + + def test_get_mac_id_no_id_available(loop, project, port_manager): # Cleanup the VPCS object VPCS._instance = None