Fix to integer cast of length in console output extension

This fixes a small issue when you try to use console output length as a
float point value (as a float point itself not as string).
The extension was behaving as expected due to impossibility to fulfil the
request, returning bad request, but now this check in completelly done in api
level (it was partially done before) instead of wait until it fails behind the
rpc call.

Naturally a test was added to explicit it and avoid future regression.

related to blueprint nova-v3-api

Change-Id: Ifd7ce90dfd88acc8b22e7d190eafc3dd686186e9
This commit is contained in:
Mauro S. M. Rodrigues
2013-06-23 23:52:56 -04:00
parent d376043bef
commit fc3cecf2ac
2 changed files with 14 additions and 2 deletions

View File

@@ -52,7 +52,11 @@ class ConsoleOutputController(wsgi.Controller):
if length is not None:
try:
int(length)
# NOTE(maurosr): cast length into a string before cast into an
# integer to avoid thing like: int(2.5) which is 2 instead of
# raise ValueError like it would when we try int("2.5"). This
# can be removed once we have api validation landed.
int(str(length))
except ValueError:
raise webob.exc.HTTPBadRequest(_('Length in request body must '
'be an integer value'))

View File

@@ -101,7 +101,6 @@ class ConsoleOutputExtensionTest(test.TestCase):
req.body = jsonutils.dumps(body)
req.headers["content-type"] = "application/json"
res = req.get_response(self.app)
output = jsonutils.loads(res.body)
self.assertEqual(res.status_int, 400)
def test_get_text_console_no_instance(self):
@@ -149,3 +148,12 @@ class ConsoleOutputExtensionTest(test.TestCase):
res = req.get_response(self.app)
self.assertEqual(res.status_int, 409)
def test_get_console_output_with_length_as_float(self):
body = {'os-getConsoleOutput': {'length': 2.5}}
req = webob.Request.blank('/v2/fake/servers/1/action')
req.method = "POST"
req.body = jsonutils.dumps(body)
req.headers["content-type"] = "application/json"
res = req.get_response(self.app)
self.assertEqual(res.status_int, 400)