From 94e486168e383600d9c5234c7b0c3874884aad1e Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Mon, 8 Oct 2012 04:18:11 +0000 Subject: [PATCH] Ensures port is not created when database exception occurs Fixes bug 1064261 The port creation code did not correctly treat a database error. That is, if there was an exception the port would be created and an error returned to the client. Change-Id: I6cf36d1c641b46716afb16f228b8daa631099a5d --- quantum/db/db_base_plugin_v2.py | 5 ++--- quantum/tests/unit/test_db_plugin.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/quantum/db/db_base_plugin_v2.py b/quantum/db/db_base_plugin_v2.py index 3326f73c5bc..36da230afa8 100644 --- a/quantum/db/db_base_plugin_v2.py +++ b/quantum/db/db_base_plugin_v2.py @@ -1159,9 +1159,8 @@ def create_port(self, context, port): device_owner=p['device_owner']) context.session.add(port) - # Update the allocated IP's - if ips: - with context.session.begin(subtransactions=True): + # Update the allocated IP's + if ips: for ip in ips: LOG.debug("Allocated IP %s (%s/%s/%s)", ip['ip_address'], port['network_id'], ip['subnet_id'], port.id) diff --git a/quantum/tests/unit/test_db_plugin.py b/quantum/tests/unit/test_db_plugin.py index dd8c1a4bfa8..8c9377387eb 100644 --- a/quantum/tests/unit/test_db_plugin.py +++ b/quantum/tests/unit/test_db_plugin.py @@ -1193,6 +1193,19 @@ def test_requested_split(self): for p in ports_to_delete: self._delete('ports', p['port']['id']) + def test_duplicate_ips(self): + fmt = 'json' + with self.subnet() as subnet: + # Allocate specific IP + kwargs = {"fixed_ips": [{'subnet_id': subnet['subnet']['id'], + 'ip_address': '10.0.0.5'}, + {'subnet_id': subnet['subnet']['id'], + 'ip_address': '10.0.0.5'}]} + net_id = subnet['subnet']['network_id'] + res = self._create_port(fmt, net_id=net_id, **kwargs) + port2 = self.deserialize(fmt, res) + self.assertEquals(res.status_int, 500) + def test_requested_ips_only(self): fmt = 'json' with self.subnet() as subnet: