Merge lp:~salvatore-orlando/neutron/bug814518 into lp:neutron/diablo

Proposed by Salvatore Orlando
Status: Merged
Approved by: Salvatore Orlando
Approved revision: 31
Merged at revision: 34
Proposed branch: lp:~salvatore-orlando/neutron/bug814518
Merge into: lp:neutron/diablo
Diff against target: 72 lines (+31/-3)
3 files modified
quantum/api/api_common.py (+7/-1)
tests/unit/test_api.py (+21/-0)
tests/unit/testlib_api.py (+3/-2)
To merge this branch: bzr merge lp:~salvatore-orlando/neutron/bug814518
Reviewer Review Type Date Requested Status
dan wendlandt Approve
Brad Hall (community) Approve
Review via email: mp+70018@code.launchpad.net

Description of the change

Fixing the issue causing a 400 error to not be raised if an invalid request body was specified.
The issue occurred in port creation, as the request body is optional; an invalid request body was simply ignored.

Also adding unit tests for creating a port without a request body in order to further improve code coverage.

To post a comment you must log in.
Revision history for this message
Brad Hall (bgh) wrote :

Looks good to me

review: Approve
Revision history for this message
dan wendlandt (danwent) wrote :

Change looks good.

This does remind me of something I was wondering when looking at the code earlier, though. Why do we call _deserialize() on the body for each iteration of the loop?

review: Approve
Revision history for this message
Salvatore Orlando (salvatore-orlando) wrote :

The _deserialize call is for parsing the request body from xml/json into a dictionary.
It is invoked only for operations which require a request body.

I think the Openstack API has a more sophisticated mechanism in which header/body deserialization is performed withing the wsgi middleware. We can do something similar in Quantum, but I don't think it is a high priority task at this stage.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'quantum/api/api_common.py'
2--- quantum/api/api_common.py 2011-07-21 16:24:24 +0000
3+++ quantum/api/api_common.py 2011-08-01 14:41:39 +0000
4@@ -43,7 +43,13 @@
5 des_body = self._deserialize(req.body,
6 req.best_match_content_type())
7 data = des_body and des_body.get(self._resource_name, None)
8- param_value = data and data.get(param_name, None)
9+ if not data:
10+ msg = ("Failed to parse request. Resource: " +
11+ self._resource_name + " not found in request body")
12+ for line in msg.split('\n'):
13+ LOG.error(line)
14+ raise exc.HTTPBadRequest(msg)
15+ param_value = data.get(param_name, None)
16 if not param_value:
17 # 2- parse request headers
18 # prepend param name with a 'x-' prefix
19
20=== modified file 'tests/unit/test_api.py'
21--- tests/unit/test_api.py 2011-07-29 23:39:45 +0000
22+++ tests/unit/test_api.py 2011-08-01 14:41:39 +0000
23@@ -285,6 +285,21 @@
24 self.assertEqual(show_port_res.status_int, 430)
25 LOG.debug("_test_show_port_portnotfound - format:%s - END", format)
26
27+ def _test_create_port_noreqbody(self, format):
28+ LOG.debug("_test_create_port_noreqbody - format:%s - START", format)
29+ content_type = "application/%s" % format
30+ network_id = self._create_network(format)
31+ port_id = self._create_port(network_id, None, format,
32+ custom_req_body='')
33+ show_port_req = testlib.show_port_request(self.tenant_id,
34+ network_id, port_id, format)
35+ show_port_res = show_port_req.get_response(self.api)
36+ self.assertEqual(show_port_res.status_int, 200)
37+ port_data = self._port_serializer.deserialize(
38+ show_port_res.body, content_type)
39+ self.assertEqual(port_id, port_data['port']['id'])
40+ LOG.debug("_test_create_port_noreqbody - format:%s - END", format)
41+
42 def _test_create_port(self, format):
43 LOG.debug("_test_create_port - format:%s - START", format)
44 content_type = "application/%s" % format
45@@ -741,6 +756,12 @@
46 def test_create_port_xml(self):
47 self._test_create_port('xml')
48
49+ def test_create_port_noreqbody_json(self):
50+ self._test_create_port_noreqbody('json')
51+
52+ def test_create_port_noreqbody_xml(self):
53+ self._test_create_port_noreqbody('xml')
54+
55 def test_create_port_networknotfound_json(self):
56 self._test_create_port_networknotfound('json')
57
58
59=== modified file 'tests/unit/testlib_api.py'
60--- tests/unit/testlib_api.py 2011-07-18 22:51:22 +0000
61+++ tests/unit/testlib_api.py 2011-08-01 14:41:39 +0000
62@@ -77,9 +77,10 @@
63 method = 'POST'
64 path = "/tenants/%(tenant_id)s/networks/" \
65 "%(network_id)s/ports.%(format)s" % locals()
66- data = custom_req_body or {'port': {'port-state': '%s' % port_state}}
67+ data = custom_req_body or port_state and \
68+ {'port': {'port-state': '%s' % port_state}}
69 content_type = "application/%s" % format
70- body = Serializer().serialize(data, content_type)
71+ body = data and Serializer().serialize(data, content_type)
72 return create_request(path, body, content_type, method)
73
74

Subscribers

People subscribed via source and target branches