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
This commit is contained in:
zhouhenglc 2019-08-02 10:29:38 +08:00
parent f41f2a22cf
commit 45612ca52f
2 changed files with 61 additions and 0 deletions

View File

@ -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

View File

@ -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'])