From a47b7c065fa551701181071c672218af31b72a22 Mon Sep 17 00:00:00 2001 From: Wenran Xiao Date: Fri, 3 Aug 2018 11:40:20 +0800 Subject: [PATCH] Pass context in l3 flavor notifications There are some notifications pass context in l3 flavor notifications, and add context for consistency. Change-Id: I01ced41201b61c624e675375b7bcd9589b2553e0 Closes-bug: #1785539 (cherry picked from commit ae81d403f7b8508fe581647b5853c702dd2331a2) --- neutron/db/l3_db.py | 5 +- neutron/tests/unit/extensions/test_l3.py | 46 +++++++++++++++++++ neutron/tests/unit/plugins/ml2/test_plugin.py | 17 +++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py index b75f9f65e77..af89140791c 100644 --- a/neutron/db/l3_db.py +++ b/neutron/db/l3_db.py @@ -193,7 +193,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase, self._core_plugin.delete_port( context, port_id, l3_port_check=False) registry.notify(resources.FLOATING_IP, events.AFTER_DELETE, - self, **fips[0]) + self, context=context, **fips[0]) def _get_dead_floating_port_candidates(self, context): filters = {'device_id': ['PENDING'], @@ -512,6 +512,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase, registry.notify(resources.ROUTER_GATEWAY, events.AFTER_CREATE, self._create_gw_port, + context=context, gw_ips=ext_ips, network_id=new_network_id, router_id=router_id) @@ -1496,7 +1497,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase, floatingip.floating_port_id, l3_port_check=False) registry.notify(resources.FLOATING_IP, events.AFTER_DELETE, - self, **floatingip_dict) + self, context=context, **floatingip_dict) return floatingip_dict @db_api.retry_if_session_inactive() diff --git a/neutron/tests/unit/extensions/test_l3.py b/neutron/tests/unit/extensions/test_l3.py index 25d462d69e9..bb47a172c5c 100644 --- a/neutron/tests/unit/extensions/test_l3.py +++ b/neutron/tests/unit/extensions/test_l3.py @@ -1919,6 +1919,22 @@ class L3NatTestCaseBase(L3NatTestCaseMixin): network_id=n['network']['id'], subnets=[]) + def test_router_add_gateway_notifications(self): + call_count_total = 3 + with self.router() as r: + with self.network() as n: + self._set_net_external(n['network']['id']) + with mock.patch.object(registry, 'notify') as notify: + self._add_external_gateway_to_router( + r['router']['id'], n['network']['id']) + self.assertEqual(call_count_total, notify.call_count) + expected = [mock.call( + resources.ROUTER_GATEWAY, + events.AFTER_CREATE, mock.ANY, + context=mock.ANY, gw_ips=mock.ANY, + network_id=mock.ANY, router_id=mock.ANY)] + notify.assert_has_calls(expected) + def test_router_remove_interface_inuse_returns_409(self): with self.router() as r: with self.subnet() as s: @@ -3730,6 +3746,36 @@ class L3AgentDbTestCaseBase(L3NatTestCaseMixin): registry.unsubscribe(fake_method, resources.FLOATING_IP, events.PRECOMMIT_CREATE) + def test_floatingip_delete_after_event(self): + fake_method = mock.Mock() + try: + registry.subscribe(fake_method, resources.FLOATING_IP, + events.AFTER_DELETE) + with self.subnet(cidr='11.0.0.0/24') as public_sub: + self._set_net_external(public_sub['subnet']['network_id']) + f = self._make_floatingip(self.fmt, + public_sub['subnet']['network_id'], + port_id=None, + fixed_ip=None, + set_context=True) + self._delete('floatingips', f['floatingip']['id']) + fake_method.assert_called_once_with( + resources.FLOATING_IP, events.AFTER_DELETE, mock.ANY, + context=mock.ANY, description=mock.ANY, + dns_domain=mock.ANY, dns_name=mock.ANY, + fixed_ip_address=f['floatingip']['fixed_ip_address'], + floating_ip_address=f['floatingip']['floating_ip_address'], + floating_network_id=f['floatingip']['floating_network_id'], + id=f['floatingip']['id'], + port_id=f['floatingip']['port_id'], + project_id=f['floatingip']['project_id'], + router_id=f['floatingip']['router_id'], + status=f['floatingip']['status'], + tenant_id=f['floatingip']['tenant_id']) + finally: + registry.unsubscribe(fake_method, resources.FLOATING_IP, + events.AFTER_DELETE) + def test_router_create_precommit_event(self): nset = lambda *a, **k: setattr(k['router_db'], 'name', 'hello') registry.subscribe(nset, resources.ROUTER, events.PRECOMMIT_CREATE) diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index 2893bebb15d..8264192c188 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -1760,6 +1760,23 @@ class TestMl2DvrPortsV2(TestMl2PortsV2): def test_delete_port_with_floatingip_notifies_l3_plugin(self): self.test_delete_port_notifies_l3_plugin(floating_ip=True) + def test_delete_port_with_floatingip_create_precommit_event(self): + fake_method = mock.Mock() + with self.port(device_owner='network:floatingip') as port: + try: + registry.subscribe(fake_method, resources.FLOATING_IP, + events.PRECOMMIT_DELETE) + port_id = port['port']['id'] + self.plugin.delete_port(self.context, port_id) + fake_method.assert_called_once_with( + resources.FLOATING_IP, events.PRECOMMIT_DELETE, mock.ANY, + bind=mock.ANY, bindings=mock.ANY, context=mock.ANY, + id=mock.ANY, levels=mock.ANY, network=mock.ANY, + port=mock.ANY, port_db=mock.ANY) + finally: + registry.unsubscribe(fake_method, resources.FLOATING_IP, + events.PRECOMMIT_DELETE) + def test_concurrent_csnat_port_delete(self): plugin = directory.get_plugin(plugin_constants.L3) r = plugin.create_router(