]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix to throw correct error code for bad attribute
authorSudipta Biswas <sbiswas7@in.ibm.com>
Tue, 5 Aug 2014 14:10:06 +0000 (19:40 +0530)
committerSudipta Biswas <sbiswas7@in.ibm.com>
Fri, 8 Aug 2014 05:00:23 +0000 (10:30 +0530)
Currently the neutron network API throws up error code 500 for
the extended attribute for segmentation id. This can be reproduced
if the user types in a random string in place of an integer value
for the segmentation id. The proper behavior should throw an error
code 400 with the appropriate failure message. This patch fixes the
same issue and covers it with a test case.

Change-Id: I4735e20f5b8b23c5b2a9d896415c2e84561a279c
Closes-bug: #1348056

neutron/extensions/providernet.py
neutron/tests/unit/test_extension_pnet.py

index 944de104fb6690fef7d0d827e178a3ee92c96a28..2dc966ae729bc050e7ba6eb487148e9964977c9c 100644 (file)
@@ -35,7 +35,7 @@ EXTENDED_ATTRIBUTES_2_0 = {
                            'enforce_policy': True,
                            'is_visible': True},
         SEGMENTATION_ID: {'allow_post': True, 'allow_put': True,
-                          'convert_to': int,
+                          'convert_to': attributes.convert_to_int,
                           'enforce_policy': True,
                           'default': attributes.ATTR_NOT_SPECIFIED,
                           'is_visible': True},
index e6959a122753c2fbe6082ecd2598d5a4a8dcfbe0..211fe7151cc8cedea3ebd03dec491a68258c3920 100644 (file)
@@ -122,6 +122,18 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase):
                             expect_errors=expect_errors)
         return res, data
 
+    def _post_network_with_bad_provider_attrs(self, ctx, bad_data,
+                                              expect_errors=False):
+        data = self._prepare_net_data()
+        data.update(bad_data)
+        env = {'neutron.context': ctx}
+        res = self.api.post(test_api_v2._get_path('networks', fmt=self.fmt),
+                            self.serialize({'network': data}),
+                            content_type='application/' + self.fmt,
+                            extra_environ=env,
+                            expect_errors=expect_errors)
+        return res, data
+
     def test_network_create_with_provider_attrs(self):
         ctx = context.get_admin_context()
         ctx.tenant_id = 'an_admin'
@@ -135,6 +147,14 @@ class ProvidernetExtensionTestCase(testlib_api.WebTestCase):
                                                    network=exp_input)
         self.assertEqual(res.status_int, web_exc.HTTPCreated.code)
 
+    def test_network_create_with_bad_provider_attrs_400(self):
+        ctx = context.get_admin_context()
+        ctx.tenant_id = 'an_admin'
+        bad_data = {pnet.SEGMENTATION_ID: "abc"}
+        res, _1 = self._post_network_with_bad_provider_attrs(ctx, bad_data,
+                                                             True)
+        self.assertEqual(web_exc.HTTPBadRequest.code, res.status_int)
+
     def test_network_update_with_provider_attrs(self):
         ctx = context.get_admin_context()
         ctx.tenant_id = 'an_admin'