From: Jamie Lennox <jamielennox@redhat.com>
Date: Tue, 5 May 2015 01:50:21 +0000 (+1000)
Subject: Catch additional type conversion errors
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=f3630dd130d6e2e90b84416c9084e515604718ed;p=openstack-build%2Fcinder-build.git

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

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",