From e9e4395d578e40bb59272b409c7ca3617ec1e6e3 Mon Sep 17 00:00:00 2001 From: yangjianfeng Date: Sat, 6 Feb 2021 02:13:04 +0000 Subject: [PATCH] Fix incorrect exception catch when update floating ip port forwarding Closes-Bug: #1912596 Change-Id: Ib1a95dd41d25f39f3378a6728d752a6589b9b61c --- neutron/services/portforwarding/pf_plugin.py | 3 +- .../test_floating_ip_port_forwarding.py | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/neutron/services/portforwarding/pf_plugin.py b/neutron/services/portforwarding/pf_plugin.py index e736b858851..f118e66205a 100644 --- a/neutron/services/portforwarding/pf_plugin.py +++ b/neutron/services/portforwarding/pf_plugin.py @@ -33,6 +33,7 @@ from neutron_lib.objects import exceptions as obj_exc from neutron_lib.plugins import constants from neutron_lib.plugins import directory from oslo_config import cfg +from oslo_db import exception as oslo_db_exc from oslo_log import log as logging from neutron._i18n import _ @@ -432,7 +433,7 @@ class PortForwardingPlugin(fip_pf.PortForwardingPluginBase): pf_obj.update_fields(port_forwarding, reset_changes=True) self._check_port_forwarding_update(context, pf_obj) pf_obj.update() - except obj_exc.NeutronDbObjectDuplicateEntry: + except oslo_db_exc.DBDuplicateEntry: (__, conflict_params) = self._find_existing_port_forwarding( context, floatingip_id, pf_obj.to_dict()) message = _("A duplicate port forwarding entry with same " diff --git a/neutron/tests/unit/extensions/test_floating_ip_port_forwarding.py b/neutron/tests/unit/extensions/test_floating_ip_port_forwarding.py index 972b55ea8bb..594dde2432f 100644 --- a/neutron/tests/unit/extensions/test_floating_ip_port_forwarding.py +++ b/neutron/tests/unit/extensions/test_floating_ip_port_forwarding.py @@ -71,6 +71,21 @@ class FloatingIPPorForwardingTestCase(test_l3.L3BaseForIntTests, return fip_pf_req.get_response(self.ext_api) + def _update_fip_port_forwarding(self, fmt, floating_ip_id, + port_forwarding_id, **kwargs): + port_forwarding = {} + for k, v in kwargs.items(): + port_forwarding[k] = v + data = {'port_forwarding': port_forwarding} + + fip_pf_req = self._req( + 'PUT', 'floatingips', data, + fmt or self.fmt, id=floating_ip_id, + sub_id=port_forwarding_id, + subresource='port_forwardings') + + return fip_pf_req.get_response(self.ext_api) + def test_create_floatingip_port_forwarding_with_port_number_0(self): with self.network() as ext_net: network_id = ext_net['network']['id'] @@ -136,3 +151,46 @@ class FloatingIPPorForwardingTestCase(test_l3.L3BaseForIntTests, pf_body = self.deserialize(self.fmt, res) self.assertEqual( "blablablabla", pf_body['port_forwarding']['description']) + + def test_update_floatingip_port_forwarding_with_dup_internal_port(self): + with self.network() as ext_net: + network_id = ext_net['network']['id'] + self._set_net_external(network_id) + with self.subnet(ext_net, cidr='10.10.10.0/24'), \ + self.router() as router, \ + self.subnet(cidr='11.0.0.0/24') as private_subnet, \ + self.port(private_subnet) as port: + self._add_external_gateway_to_router( + router['router']['id'], + network_id) + self._router_interface_action( + 'add', router['router']['id'], + private_subnet['subnet']['id'], + None) + fip1 = self._make_floatingip( + self.fmt, + network_id) + self.assertIsNone(fip1['floatingip'].get('port_id')) + self._create_fip_port_forwarding( + self.fmt, fip1['floatingip']['id'], + 2222, 22, + 'tcp', + port['port']['fixed_ips'][0]['ip_address'], + port['port']['id'], + description="blablablabla") + fip2 = self._make_floatingip( + self.fmt, + network_id) + fip_pf_response = self._create_fip_port_forwarding( + self.fmt, fip2['floatingip']['id'], + 2222, 23, + 'tcp', + port['port']['fixed_ips'][0]['ip_address'], + port['port']['id'], + description="blablablabla") + update_res = self._update_fip_port_forwarding( + self.fmt, fip2['floatingip']['id'], + fip_pf_response.json['port_forwarding']['id'], + **{'internal_port': 22}) + self.assertEqual(exc.HTTPBadRequest.code, + update_res.status_int)