From f3630dd130d6e2e90b84416c9084e515604718ed Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Tue, 5 May 2015 11:50:21 +1000 Subject: [PATCH] Catch additional type conversion errors When converting strings to integers in quota we catch ValueError which will handle an input which is not a valid number, eg 'abc'. Extend this to covert TypeErrors which will handle things like a None value being passed. Partial-Bug: #1451637 Change-Id: I7e3e694d539113beeb99001aeb80012ac4c1124d --- cinder/api/contrib/quotas.py | 2 +- cinder/tests/unit/api/contrib/test_quotas.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cinder/api/contrib/quotas.py b/cinder/api/contrib/quotas.py index 32667cad3..d1f9efca7 100644 --- a/cinder/api/contrib/quotas.py +++ b/cinder/api/contrib/quotas.py @@ -59,7 +59,7 @@ class QuotaSetsController(wsgi.Controller): def _validate_quota_limit(self, limit): try: limit = int(limit) - except ValueError: + except (ValueError, TypeError): msg = _("Quota limit must be specified as an integer value.") raise webob.exc.HTTPBadRequest(explanation=msg) diff --git a/cinder/tests/unit/api/contrib/test_quotas.py b/cinder/tests/unit/api/contrib/test_quotas.py index fa01c1e71..9e24657c0 100644 --- a/cinder/tests/unit/api/contrib/test_quotas.py +++ b/cinder/tests/unit/api/contrib/test_quotas.py @@ -88,11 +88,16 @@ class QuotaSetsControllerTest(test.TestCase): self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, self.req, 'foo', body) - def test_update_invalid_key_value(self): + def test_update_invalid_value_key_value(self): body = {'quota_set': {'gigabytes': "should_be_int"}} self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, self.req, 'foo', body) + def test_update_invalid_type_key_value(self): + body = {'quota_set': {'gigabytes': None}} + self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, + self.req, 'foo', body) + def test_update_multi_value_with_bad_data(self): orig_quota = self.controller.show(self.req, 'foo') body = make_body(gigabytes=2000, snapshots=15, volumes="should_be_int", -- 2.45.2