]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix foreign key constraint error on ml2_dvr_port_bindings
authorarmando-migliaccio <armamig@gmail.com>
Fri, 19 Sep 2014 19:11:11 +0000 (12:11 -0700)
committerarmando-migliaccio <armamig@gmail.com>
Fri, 19 Sep 2014 19:11:24 +0000 (12:11 -0700)
Get the port before creating the DVR binding. This should guarantee that, if
the port is found, no foreign key violation is triggered.

Closes-bug: #1371696

Change-Id: I90c3d6460c14ef27823be95bc26aae38eba4879a

neutron/plugins/ml2/plugin.py
neutron/tests/unit/ml2/test_ml2_plugin.py

index e6a8d9040cad4ea28270bf956674dfd286d029e3..8547d18d8f6328a708604100b5ad4f3a6d6dd8ad 100644 (file)
@@ -890,10 +890,14 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
             router_id != device_id)
         if update_required:
             with session.begin(subtransactions=True):
+                try:
+                    orig_port = super(Ml2Plugin, self).get_port(context, id)
+                except exc.PortNotFound:
+                    LOG.debug("DVR Port %s has been deleted concurrently", id)
+                    return
                 if not binding:
                     binding = db.ensure_dvr_port_binding(
                         session, id, host, router_id=device_id)
-                orig_port = super(Ml2Plugin, self).get_port(context, id)
                 network = self.get_network(context, orig_port['network_id'])
                 mech_context = driver_context.DvrPortContext(self,
                     context, orig_port, network,
index ca0a061f946f33dcc89141a38a64a61baf5f37b9..e70aa3c4a5538f3a0236a33df69f690ed0093c84 100644 (file)
@@ -377,6 +377,17 @@ class TestMl2PortBinding(Ml2PluginV2TestCase,
                                  mech_context._binding.router_id)
                 self.assertEqual(host_id, mech_context._binding.host)
 
+    def test_update_dvr_port_binding_on_non_existent_port(self):
+        plugin = manager.NeutronManager.get_plugin()
+        port = {
+            'id': 'foo_port_id',
+            'binding:host_id': 'foo_host',
+        }
+        with mock.patch.object(ml2_db, 'ensure_dvr_port_binding') as mock_dvr:
+            plugin.update_dvr_port_binding(
+                self.context, 'foo_port_id', {'port': port})
+        self.assertFalse(mock_dvr.called)
+
 
 class TestMl2PortBindingNoSG(TestMl2PortBinding):
     HAS_PORT_FILTER = False