Commit 5b4d7306 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Make tests work with multiple IP pools

Refactor tests so that they are aware that a network can have multiple
IP pools. Also, add extra check to PoolManager methods in order to check
whether a value is contained in the pool.
parent ec0e6405
......@@ -78,11 +78,15 @@ class PoolManager(object):
"""Return a value to the pool."""
if value is None:
raise ValueError
if not self.contains(value):
raise InvalidValue("%s does not belong to pool." % value)
index = self.value_to_index(value)
self._release(index, external)
def reserve(self, value, external=False):
"""Reserve a value."""
if not self.contains(value):
raise InvalidValue("%s does not belong to pool." % value)
index = self.value_to_index(value)
self._reserve(index, external)
return True
......@@ -114,8 +118,9 @@ class PoolManager(object):
else:
self.available[index] = AVAILABLE
def contains(self, value):
index = self.value_to_index(value)
def contains(self, value, index=False):
if index is False:
index = self.value_to_index(value)
return index >= 0 and index < self.pool_size
def count_available(self):
......@@ -131,6 +136,8 @@ class PoolManager(object):
return self.pool_size - self.count_reserved()
def is_available(self, value, index=False):
if not self.contains(value, index=index):
raise InvalidValue("%s does not belong to pool." % value)
if not index:
idx = self.value_to_index(value)
else:
......@@ -138,6 +145,8 @@ class PoolManager(object):
return self.pool[idx] == AVAILABLE
def is_reserved(self, value, index=False):
if not self.contains(value, index=index):
raise InvalidValue("%s does not belong to pool." % value)
if not index:
idx = self.value_to_index(value)
else:
......
......@@ -102,7 +102,7 @@ class PoolManagerTestCase(TestCase):
obj = DummyObject(42)
pool = DummyPool(obj)
for i in range(42, 48):
self.assertEqual(pool.is_available(i), False)
self.assertRaises(InvalidValue, pool.is_available, i)
pool.reserve(32, external=True)
values = []
while True:
......@@ -189,7 +189,7 @@ class MacPrefixPoolTestCase(TestCase):
obj = DummyObject(65636)
obj.base = 'ab:ff:ff'
pool = MacPrefixPool(obj)
for i in range(0, 65536):
for i in range(0, 65535):
self.assertEqual(pool.is_available(i, index=True), False)
def test_mac_prefix_conversion(self):
......@@ -209,39 +209,13 @@ class MacPrefixPoolTestCase(TestCase):
class IPPoolTestCase(TestCase):
def test_auto_reservations(self):
obj = DummyObject(0)
subnet = DummyObject(0)
obj.subnet = subnet
subnet.cidr = '192.168.2.0/24'
subnet.gateway = '192.168.2.1'
pool = IPPool(obj)
self.assertEqual(pool.is_available('192.168.2.0'), False)
self.assertEqual(pool.is_available('192.168.2.1'), False)
self.assertEqual(pool.is_available('192.168.2.255'), False)
self.assertEqual(pool.count_available(), 253)
self.assertEqual(pool.get(), '192.168.2.2')
self.assertTrue(pool.contains("192.168.2.10"))
self.assertFalse(pool.contains("192.168.3.10"))
def test_auto_reservations_2(self):
obj = DummyObject(0)
subnet = DummyObject(0)
obj.subnet = subnet
subnet.cidr = '192.168.2.0/31'
subnet.gateway = '192.168.2.1'
pool = IPPool(obj)
self.assertEqual(pool.is_available('192.168.2.0'), False)
self.assertEqual(pool.is_available('192.168.2.1'), False)
self.assertEqual(pool.size(), 8)
self.assertEqual(pool.empty(), True)
def test_get_with_value(self):
obj = DummyObject(0)
obj = DummyObject(16)
subnet = DummyObject(0)
obj.subnet = subnet
subnet.cidr = '192.168.2.0/28'
subnet.gateway = '192.168.2.1'
subnet.cidr = "192.168.2.0/28"
obj.base = "192.168.2.0/28"
obj.offset = 0
pool = IPPool(obj)
# Test if reserved
pool.reserve("192.168.2.2")
......@@ -249,5 +223,4 @@ class IPPoolTestCase(TestCase):
# Test if externally reserved
pool.reserve("192.168.2.3", external=True)
self.assertRaises(ValueNotAvailable, pool.get, "192.168.2.3")
self.assertRaises(ValueNotAvailable, pool.get, "192.168.2.15")
self.assertRaises(InvalidValue, pool.get, "192.168.2.16")
......@@ -52,9 +52,10 @@ class FlavorTest(TestCase):
"""Test a flavor object name method."""
flavor = mfact.FlavorFactory(cpu=1, ram=1024, disk=40,
disk_template="temp")
self.assertEqual(flavor.name, "C1R1024D40temp", "flavor.name is not"
" generated correctly. Name is %s instead of C1R1024D40temp" %
flavor.name)
self.assertEqual(
flavor.name, "C1R1024D40temp", "flavor.name is not"
" generated correctly. Name is %s instead of C1R1024D40temp" %
flavor.name)
class BackendTest(TestCase):
......@@ -194,17 +195,19 @@ class NetworkTest(TestCase):
len_backends + 3)
def test_pool(self):
pool = self.net.get_pool()
pool = self.net.get_ip_pools()[0]
pool.network = self.net
self.assertTrue(isinstance(pool, IPPool))
def test_reserve_ip(self):
net1 = mfact.NetworkWithSubnetFactory(subnet__cidr='192.168.2.0/24')
pool = net1.get_ip_pools()[0]
self.assertTrue(pool.is_available('192.168.2.12'))
net1.reserve_address('192.168.2.12')
pool = net1.get_pool()
pool = net1.get_ip_pools()[0]
self.assertFalse(pool.is_available('192.168.2.12'))
net1.release_address('192.168.2.12')
pool = net1.get_pool()
pool = net1.get_ip_pools()[0]
self.assertTrue(pool.is_available('192.168.2.12'))
......
......@@ -137,7 +137,7 @@ class UpdateDBTest(TestCase):
# Also create a NIC
ip = mfactory.IPv4AddressFactory(nic__machine=vm)
nic = ip.nic
nic.network.get_pool().reserve(nic.ipv4_address)
nic.network.get_ip_pools()[0].reserve(nic.ipv4_address)
msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
instance=vm.backend_vm_id)
with mocked_quotaholder():
......@@ -148,7 +148,7 @@ class UpdateDBTest(TestCase):
self.assertTrue(db_vm.deleted)
# Check that nics are deleted
self.assertFalse(db_vm.nics.all())
self.assertTrue(nic.network.get_pool().is_available(ip.address))
self.assertTrue(nic.network.get_ip_pools()[0].is_available(ip.address))
vm2 = mfactory.VirtualMachineFactory()
fp1 = mfactory.IPv4AddressFactory(nic__machine=vm2, floating_ip=True,
network__floating_ip_pool=True)
......@@ -156,7 +156,7 @@ class UpdateDBTest(TestCase):
nic1 = mfactory.NetworkInterfaceFactory(machine=vm2)
fp1.nic = nic1
fp1.save()
pool = network.get_pool()
pool = network.get_ip_pools()[0]
pool.reserve(fp1.address)
pool.save()
msg = self.create_msg(operation='OP_INSTANCE_REMOVE',
......@@ -168,7 +168,7 @@ class UpdateDBTest(TestCase):
self.assertEqual(db_vm.operstate, 'DESTROYED')
self.assertTrue(db_vm.deleted)
self.assertEqual(IPAddress.objects.get(id=fp1.id).nic, None)
pool = network.get_pool()
pool = network.get_ip_pools()[0]
# Test that floating ips are not released
self.assertFalse(pool.is_available(fp1.address))
......@@ -367,7 +367,7 @@ class UpdateNetTest(TestCase):
network = ip.network
subnet = ip.subnet
vm = ip.nic.machine
pool = subnet.get_pool()
pool = subnet.get_ip_pools()[0]
pool.reserve("10.0.0.2")
pool.save()
......@@ -384,7 +384,7 @@ class UpdateNetTest(TestCase):
self.assertEqual(nics[0].index, 0)
self.assertEqual(nics[0].ipv4_address, '10.0.0.3')
self.assertEqual(nics[0].mac, 'aa:bb:cc:00:11:22')
pool = subnet.get_pool()
pool = subnet.get_ip_pools()[0]
self.assertTrue(pool.is_available('10.0.0.2'))
self.assertFalse(pool.is_available('10.0.0.3'))
pool.save()
......@@ -600,7 +600,7 @@ class UpdateNetworkTest(TestCase):
"10.0.0.20"]})
update_network(client, msg)
self.assertTrue(client.basic_ack.called)
pool = network.get_pool()
pool = network.get_ip_pools()[0]
self.assertTrue(pool.is_reserved('10.0.0.10'))
self.assertTrue(pool.is_reserved('10.0.0.20'))
pool.save()
......@@ -613,7 +613,7 @@ class UpdateNetworkTest(TestCase):
"10.0.0.20"]})
update_network(client, msg)
#self.assertTrue(client.basic_ack.called)
pool = network.get_pool()
pool = network.get_ip_pools()[0]
self.assertTrue(pool.is_reserved('10.0.0.10'))
self.assertTrue(pool.is_reserved('10.0.0.20'))
......
......@@ -152,4 +152,4 @@ class NetworkTest(TestCase):
subnet6 = net.subnets.get(ipversion=6)
self.assertEqual(subnet6.cidr, "2001:648:2ffc:1112::/64")
self.assertEqual(subnet6.gateway, "2001:648:2ffc:1112::1")
self.assertRaises(Exception, net.get_pool)
self.assertEqual(net.get_ip_pools(), [])
......@@ -202,7 +202,7 @@ class ServerReconciliationTest(TestCase):
"mtime": time(),
"disk.sizes": [],
"nic.names": [nic.backend_uuid],
"nic.ips": ["192.168.2.1"],
"nic.ips": ["192.168.2.5"],
"nic.macs": ["aa:00:bb:cc:dd:ee"],
"nic.networks.names": [network2.backend_id],
"tags": []}]
......@@ -212,7 +212,7 @@ class ServerReconciliationTest(TestCase):
self.assertEqual(vm1.operstate, "STARTED")
nic = vm1.nics.all()[0]
self.assertEqual(nic.network, network2)
self.assertEqual(nic.ipv4_address, "192.168.2.1")
self.assertEqual(nic.ipv4_address, "192.168.2.5")
self.assertEqual(nic.mac, "aa:00:bb:cc:dd:ee")
......
......@@ -120,13 +120,13 @@ class ServerTest(TransactionTestCase):
subnet = mfactory.IPv4SubnetFactory(network__flavor="CUSTOM",
cidr="192.168.2.0/24",
gateway="192.168.2.1",
dhcp=True)
dhcp=dhcp)
net = subnet.network
vm = mfactory.VirtualMachineFactory(operstate="STARTED")
mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
mrapi().ModifyInstance.return_value = 42
servers.connect(vm, net)
pool = net.get_pool(locked=False)
pool = net.get_ip_pools(locked=False)[0]
self.assertFalse(pool.is_available("192.168.2.2"))
args, kwargs = mrapi().ModifyInstance.call_args
nics = kwargs["nics"][0]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment