1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 09:18:08 +00:00

VPCS Mac address / project

This commit is contained in:
Julien Duponchelle 2015-01-22 18:47:27 +01:00
parent 72c6182062
commit 2c50bb607f
2 changed files with 23 additions and 3 deletions

View File

@ -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)

View File

@ -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