From 8c3cb79aa54b0ffcdc840c7e95ab809835d05001 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 27 Aug 2015 22:12:48 -0700 Subject: [PATCH] Better message on allowed address pairs error Neutron was throwing a 500 error when a non-iterable was passed into allowed address pairs. This patch just catches that and converts it into a regular badrequest message. Closes-Bug: #1477829 Change-Id: I3c6f55df4912c7a9480fa097988f910b254572fd Signed-off-by: Kevin Benton --- neutron/extensions/allowedaddresspairs.py | 10 +++++++--- .../tests/unit/db/test_allowedaddresspairs_db.py | 13 ++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/neutron/extensions/allowedaddresspairs.py b/neutron/extensions/allowedaddresspairs.py index 5189e4f3305..9b73a1542ab 100644 --- a/neutron/extensions/allowedaddresspairs.py +++ b/neutron/extensions/allowedaddresspairs.py @@ -49,9 +49,13 @@ class AllowedAddressPairExhausted(nexception.BadRequest): def _validate_allowed_address_pairs(address_pairs, valid_values=None): unique_check = {} - if len(address_pairs) > cfg.CONF.max_allowed_address_pair: - raise AllowedAddressPairExhausted( - quota=cfg.CONF.max_allowed_address_pair) + try: + if len(address_pairs) > cfg.CONF.max_allowed_address_pair: + raise AllowedAddressPairExhausted( + quota=cfg.CONF.max_allowed_address_pair) + except TypeError: + raise webob.exc.HTTPBadRequest( + _("Allowed address pairs must be a list.")) for address_pair in address_pairs: # mac_address is optional, if not set we use the mac on the port diff --git a/neutron/tests/unit/db/test_allowedaddresspairs_db.py b/neutron/tests/unit/db/test_allowedaddresspairs_db.py index 2d3788a5b6e..af0ec336dc9 100644 --- a/neutron/tests/unit/db/test_allowedaddresspairs_db.py +++ b/neutron/tests/unit/db/test_allowedaddresspairs_db.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from oslo_config import cfg +from webob import exc as web_exc from neutron.api.v2 import attributes as attr from neutron.db import allowedaddresspairs_db as addr_pair_db @@ -23,7 +25,6 @@ from neutron.extensions import portsecurity as psec from neutron.extensions import securitygroup as secgroup from neutron import manager from neutron.tests.unit.db import test_db_base_plugin_v2 -from oslo_config import cfg DB_PLUGIN_KLASS = ('neutron.tests.unit.db.test_allowedaddresspairs_db.' @@ -97,6 +98,16 @@ class AllowedAddressPairDBTestCase(AllowedAddressPairTestCase): class TestAllowedAddressPairs(AllowedAddressPairDBTestCase): + def test_create_port_allowed_address_pairs_bad_format(self): + with self.network() as net: + bad_values = [False, True, None, 1.1, 1] + for value in bad_values: + self._create_port( + self.fmt, net['network']['id'], + expected_res_status=web_exc.HTTPBadRequest.code, + arg_list=(addr_pair.ADDRESS_PAIRS,), + allowed_address_pairs=value) + def test_create_port_allowed_address_pairs(self): with self.network() as net: address_pairs = [{'mac_address': '00:00:00:00:00:01',