Use subnet id instead of wrong built-in id()

In _validate_subnet(), built-in id() is used as param for exceptions,
this patch fixes it via using proper subnet id.

Closes-Bug: #1213930

Change-Id: I9a88f4dc7b771047f4061fb94ba65f0515afa745
This commit is contained in:
ZhiQiang Fan 2013-08-20 00:05:15 +08:00
parent 7095354c81
commit d5002844db
2 changed files with 32 additions and 3 deletions

View File

@ -1046,7 +1046,7 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
if attributes.is_attr_set(s.get('dns_nameservers')): if attributes.is_attr_set(s.get('dns_nameservers')):
if len(s['dns_nameservers']) > cfg.CONF.max_dns_nameservers: if len(s['dns_nameservers']) > cfg.CONF.max_dns_nameservers:
raise q_exc.DNSNameServersExhausted( raise q_exc.DNSNameServersExhausted(
subnet_id=id, subnet_id=s.get('id', _('new subnet')),
quota=cfg.CONF.max_dns_nameservers) quota=cfg.CONF.max_dns_nameservers)
for dns in s['dns_nameservers']: for dns in s['dns_nameservers']:
try: try:
@ -1060,7 +1060,7 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
if attributes.is_attr_set(s.get('host_routes')): if attributes.is_attr_set(s.get('host_routes')):
if len(s['host_routes']) > cfg.CONF.max_subnet_host_routes: if len(s['host_routes']) > cfg.CONF.max_subnet_host_routes:
raise q_exc.HostRoutesExhausted( raise q_exc.HostRoutesExhausted(
subnet_id=id, subnet_id=s.get('id', _('new subnet')),
quota=cfg.CONF.max_subnet_host_routes) quota=cfg.CONF.max_subnet_host_routes)
# check if the routes are all valid # check if the routes are all valid
for rt in s['host_routes']: for rt in s['host_routes']:
@ -1154,6 +1154,7 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
# and 'allocation_pools' fields. # and 'allocation_pools' fields.
s['ip_version'] = db_subnet.ip_version s['ip_version'] = db_subnet.ip_version
s['cidr'] = db_subnet.cidr s['cidr'] = db_subnet.cidr
s['id'] = db_subnet.id
self._validate_subnet(s) self._validate_subnet(s)
if 'gateway_ip' in s and s['gateway_ip'] is not None: if 'gateway_ip' in s and s['gateway_ip'] is not None:

View File

@ -482,7 +482,7 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase):
def _do_side_effect(self, patched_plugin, orig, *args, **kwargs): def _do_side_effect(self, patched_plugin, orig, *args, **kwargs):
"""Invoked by test cases for injecting failures in plugin.""" """Invoked by test cases for injecting failures in plugin."""
def second_call(*args, **kwargs): def second_call(*args, **kwargs):
raise q_exc.NeutronException raise q_exc.NeutronException()
patched_plugin.side_effect = second_call patched_plugin.side_effect = second_call
return orig(*args, **kwargs) return orig(*args, **kwargs)
@ -3433,6 +3433,34 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase):
res = req.get_response(self.api) res = req.get_response(self.api)
self.assertEqual(res.status_int, 204) self.assertEqual(res.status_int, 204)
def _helper_test_validate_subnet(self, option, exception):
cfg.CONF.set_override(option, 0)
with self.network() as network:
subnet = {'network_id': network['network']['id'],
'cidr': '10.0.2.0/24',
'ip_version': 4,
'tenant_id': network['network']['tenant_id'],
'gateway_ip': '10.0.2.1',
'dns_nameservers': ['8.8.8.8'],
'host_routes': [{'destination': '135.207.0.0/16',
'nexthop': '1.2.3.4'}]}
plugin = NeutronManager.get_plugin()
e = self.assertRaises(exception,
plugin._validate_subnet, subnet)
self.assertThat(
str(e),
matchers.Not(matchers.Contains('built-in function id')))
def test_validate_subnet_dns_nameservers_exhausted(self):
self._helper_test_validate_subnet(
'max_dns_nameservers',
q_exc.DNSNameServersExhausted)
def test_validate_subnet_host_routes_exhausted(self):
self._helper_test_validate_subnet(
'max_subnet_host_routes',
q_exc.HostRoutesExhausted)
class DbModelTestCase(base.BaseTestCase): class DbModelTestCase(base.BaseTestCase):
"""DB model tests.""" """DB model tests."""