Avoid exception deleting PTG with no subnets

The resource_mapping driver's PTG cleanup code no longer assumes PTG
has at least one subnet, which can occur when subnet allocation fails
when the PTG is created. Unit test for subnet exhaustion added.

Closes-bug: 1416177
Change-Id: I45529255723282b6e0d8a884cac2bd604ea9b61a
This commit is contained in:
Robert Kukura 2015-03-05 19:47:22 -05:00
parent 75dd238e50
commit 985f435acb
3 changed files with 32 additions and 6 deletions

View File

@ -334,14 +334,15 @@ class ResourceMappingDriver(api.PolicyDriver):
context._plugin_context.session, policy_target_group)
return ipaddress
def _cleanup_network_service_policy(self, context, subnet, ptg_id,
def _cleanup_network_service_policy(self, context, subnets, ptg_id,
ipaddress=None):
if not ipaddress:
ipaddress = self._get_ptg_policy_ipaddress_mapping(
context._plugin_context.session, ptg_id)
if ipaddress:
if ipaddress and subnets:
# TODO(rkukura): Loop on subnets?
self._restore_ip_to_allocation_pool(
context, subnet, ipaddress.ipaddress)
context, subnets[0], ipaddress.ipaddress)
self._delete_policy_ipaddress_mapping(
context._plugin_context.session, ptg_id)
@ -434,7 +435,7 @@ class ResourceMappingDriver(api.PolicyDriver):
if old_nsp:
self._cleanup_network_service_policy(
context,
context.current['subnets'][0],
context.current['subnets'],
context.current['id'])
if new_nsp:
self._handle_network_service_policy(context)
@ -498,7 +499,7 @@ class ResourceMappingDriver(api.PolicyDriver):
@log.log
def delete_policy_target_group_postcommit(self, context):
self._cleanup_network_service_policy(context,
context.current['subnets'][0],
context.current['subnets'],
context.current['id'],
context.nsp_cleanup_ipaddress)
self._cleanup_redirect_action(context)

View File

@ -105,6 +105,10 @@ class TestPolicyTargetGroup(OneConvergenceGBPDriverTestCase,
# One Convergence driver shares the same implicit subnet
self.assertEqual(count + 1, new_count)
def test_ip_pool_exhaustion(self):
# One Convergence driver shares the same implicit subnet
pass
def test_oneconvergence_controller_api_invoked(self):
with contextlib.nested(
mock.patch.object(MockNVSDApiClient, 'create_endpointgroup'),

View File

@ -784,7 +784,28 @@ class TestPolicyTargetGroup(ResourceMappingTestCase):
self.assertEqual('L2PolicyUpdateOfPolicyTargetGroupNotSupported',
data['NeutronError']['type'])
# TODO(rkukura): Test ip_pool exhaustion.
def test_ip_pool_exhaustion(self):
# Create L3P with only a single subnet in pool, and an L2P
# using this L3P.
l3p = self.create_l3_policy(name="l3p", ip_pool="10.0.0.0/24",
subnet_prefix_length=24)
l3p_id = l3p['l3_policy']['id']
l2p = self.create_l2_policy(name="l2p", l3_policy_id=l3p_id)
l2p_id = l2p['l2_policy']['id']
# Create PTG, which will be allocated this subnet.
self.create_policy_target_group(name="ptg1", l2_policy_id=l2p_id)
# Create 2nd PTG, which should fail due to subnet exhaustion.
res = self.create_policy_target_group(name="ptg2", l2_policy_id=l2p_id,
expected_res_status=503)
self.assertEqual('NoSubnetAvailable',
res['NeutronError']['type'])
# Verify 2nd PTG was not created.
self.assertFalse(self._list('policy_target_groups',
query_params='name=ptg2')
['policy_target_groups'])
class TestL2Policy(ResourceMappingTestCase):