From 45612ca52f0a318d437904b1599095e971a831b9 Mon Sep 17 00:00:00 2001 From: zhouhenglc Date: Fri, 2 Aug 2019 10:29:38 +0800 Subject: [PATCH] fix update neutron resource with incorrect body key server return 500 In current neutron, when I update resource with incorrect body, neutron server return 500 NeutronError. It should be fixed and BadRequest (400) should be returned. This patch fixed it. Change-Id: I024b8ef73be69af069e4516dc501df3f5c145419 Partial-bug: #1838587 --- neutron/pecan_wsgi/controllers/resource.py | 4 ++ neutron/tests/unit/plugins/ml2/test_plugin.py | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/neutron/pecan_wsgi/controllers/resource.py b/neutron/pecan_wsgi/controllers/resource.py index 827bff31930..6092d8bf15f 100644 --- a/neutron/pecan_wsgi/controllers/resource.py +++ b/neutron/pecan_wsgi/controllers/resource.py @@ -59,6 +59,10 @@ class ItemController(utils.NeutronPecanController): @utils.when(index, method='PUT') def put(self, *args, **kwargs): neutron_context = request.context['neutron_context'] + if "resources" not in request.context: + msg = (_("Unable to find '%s' in request body") % + request.context['resource']) + raise webob.exc.HTTPBadRequest(msg) resources = request.context['resources'] # Bulk update is not supported, 'resources' always contains a single # elemenet diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index ae8329f116c..40c7d6eaa53 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -501,6 +501,26 @@ class TestMl2NetworksV2(test_plugin.TestNetworksV2, portbindings.VIF_TYPE_BINDING_FAILED], negative_search=True) + def test_update_network_with_empty_body(self): + with self.network() as network: + network_id = network["network"]["id"] + network_req = self.new_update_request("networks", None, + network_id) + res = network_req.get_response(self.api) + self.assertEqual(webob.exc.HTTPBadRequest.code, res.status_int) + self.assertIn("network", res.json['NeutronError']['message']) + + def test_update_network_with_incorrect_resource_body(self): + with self.network() as network: + network_id = network["network"]["id"] + incorrect_body = {"incorrect": {}} + network_req = self.new_update_request("networks", + incorrect_body, + network_id) + res = network_req.get_response(self.api) + self.assertEqual(webob.exc.HTTPBadRequest.code, res.status_int) + self.assertIn("network", res.json['NeutronError']['message']) + class TestMl2NetworksV2AgentMechDrivers(Ml2PluginV2TestCase): @@ -707,6 +727,25 @@ class TestMl2SubnetsV2(test_plugin.TestSubnetsV2, self.assertEqual(s3['subnet']['id'], port['fixed_ips'][0]['subnet_id']) + def test_update_subnet_with_empty_body(self): + with self.subnet() as subnet: + subnet_id = subnet["subnet"]["id"] + subnet_req = self.new_update_request("subnets", None, + subnet_id) + res = subnet_req.get_response(self.api) + self.assertEqual(webob.exc.HTTPBadRequest.code, res.status_int) + self.assertIn("subnet", res.json['NeutronError']['message']) + + def test_update_subnet_with_incorrect_resource_body(self): + with self.subnet() as subnet: + subnet_id = subnet["subnet"]["id"] + incorrect_body = {"incorrect": {}} + subnet_req = self.new_update_request("subnets", incorrect_body, + subnet_id) + res = subnet_req.get_response(self.api) + self.assertEqual(webob.exc.HTTPBadRequest.code, res.status_int) + self.assertIn("subnet", res.json['NeutronError']['message']) + def test_subnet_after_update_callback(self): after_update = mock.Mock() registry.subscribe(after_update, resources.SUBNET, events.AFTER_UPDATE) @@ -1077,6 +1116,24 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase): res_ports = self._list('ports')['ports'] self.assertEqual([], res_ports) + def test_update_port_with_empty_body(self): + with self.port() as port: + port_id = port["port"]["id"] + port_req = self.new_update_request("ports", None, port_id) + res = port_req.get_response(self.api) + self.assertEqual(webob.exc.HTTPBadRequest.code, res.status_int) + self.assertIn("port", res.json['NeutronError']['message']) + + def test_update_port_with_incorrect_resource_body(self): + with self.port() as port: + port_id = port["port"]["id"] + incorrect_body = {"incorrect": {}} + port_req = self.new_update_request("ports", incorrect_body, + port_id) + res = port_req.get_response(self.api) + self.assertEqual(webob.exc.HTTPBadRequest.code, res.status_int) + self.assertIn("port", res.json['NeutronError']['message']) + def test_update_port_status_build(self): with self.port() as port: self.assertEqual('DOWN', port['port']['status'])