From ed499b6af1ee8cc3003a991eea2ab4d0fdd461c3 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Wed, 10 Sep 2014 02:15:02 -0700 Subject: [PATCH] Fail on None before iteration attempt Check for a 'None' value before trying to iterate over it in the bulk code. Also eliminates an unneccessary anonymous recursive function in the same bulk handling code. Closes-Bug: #1368055 Change-Id: Id4aca81e4882a3cdf9c790bdea0b0b515abc9a8c --- neutron/api/v2/base.py | 16 +++++++--------- neutron/tests/unit/test_api_v2.py | 13 ++++++++++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/neutron/api/v2/base.py b/neutron/api/v2/base.py index 48c607256..a8bf20555 100644 --- a/neutron/api/v2/base.py +++ b/neutron/api/v2/base.py @@ -576,20 +576,18 @@ class Controller(object): raise webob.exc.HTTPBadRequest(_("Resource body required")) LOG.debug(_("Request body: %(body)s"), {'body': body}) - prep_req_body = lambda x: Controller.prepare_request_body( - context, - x if resource in x else {resource: x}, - is_create, - resource, - attr_info, - allow_bulk) if collection in body: if not allow_bulk: raise webob.exc.HTTPBadRequest(_("Bulk operation " "not supported")) - bulk_body = [prep_req_body(item) for item in body[collection]] - if not bulk_body: + if not body[collection]: raise webob.exc.HTTPBadRequest(_("Resources required")) + bulk_body = [ + Controller.prepare_request_body( + context, item if resource in item else {resource: item}, + is_create, resource, attr_info, allow_bulk + ) for item in body[collection] + ] return {collection: bulk_body} res_dict = body.get(resource) diff --git a/neutron/tests/unit/test_api_v2.py b/neutron/tests/unit/test_api_v2.py index cd08390d9..e2ee2aacb 100644 --- a/neutron/tests/unit/test_api_v2.py +++ b/neutron/tests/unit/test_api_v2.py @@ -888,14 +888,21 @@ class JSONV2TestCase(APIv2TestBase, testlib_api.WebTestCase): content_type='application/' + self.fmt) self.assertEqual(res.status_int, exc.HTTPCreated.code) - def test_create_bulk_no_networks(self): - data = {'networks': []} - res = self.api.post(_get_path('networks', fmt=self.fmt), + def _test_create_bulk_failure(self, resource, data): + # TODO(kevinbenton): update the rest of the failure cases to use + # this. + res = self.api.post(_get_path(resource, fmt=self.fmt), self.serialize(data), content_type='application/' + self.fmt, expect_errors=True) self.assertEqual(res.status_int, exc.HTTPBadRequest.code) + def test_create_bulk_networks_none(self): + self._test_create_bulk_failure('networks', {'networks': None}) + + def test_create_bulk_networks_empty_list(self): + self._test_create_bulk_failure('networks', {'networks': []}) + def test_create_bulk_missing_attr(self): data = {'ports': [{'what': 'who', 'tenant_id': _uuid()}]} res = self.api.post(_get_path('ports', fmt=self.fmt), -- 2.45.2