From fc3cecf2ac59ce37a7a215751c06adb869d67a54 Mon Sep 17 00:00:00 2001 From: "Mauro S. M. Rodrigues" Date: Sun, 23 Jun 2013 23:52:56 -0400 Subject: [PATCH] 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 --- nova/api/openstack/compute/contrib/console_output.py | 6 +++++- .../openstack/compute/contrib/test_console_output.py | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/nova/api/openstack/compute/contrib/console_output.py b/nova/api/openstack/compute/contrib/console_output.py index 07b3f25567b7..f8cdb24c6de5 100644 --- a/nova/api/openstack/compute/contrib/console_output.py +++ b/nova/api/openstack/compute/contrib/console_output.py @@ -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')) diff --git a/nova/tests/api/openstack/compute/contrib/test_console_output.py b/nova/tests/api/openstack/compute/contrib/test_console_output.py index 14b61abb7878..17bfb88a678e 100644 --- a/nova/tests/api/openstack/compute/contrib/test_console_output.py +++ b/nova/tests/api/openstack/compute/contrib/test_console_output.py @@ -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)