]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Move volume size validation to api layer.
authorEoghan Glynn <eglynn@redhat.com>
Wed, 15 Aug 2012 13:10:03 +0000 (14:10 +0100)
committerEoghan Glynn <eglynn@redhat.com>
Wed, 15 Aug 2012 16:06:16 +0000 (17:06 +0100)
As per the discussion on this patch:

  ihttps://review.openstack.org/10540

Change-Id: I5d3031a08aa1e2d17b5fb233ebaec938c66fa88b

cinder/api/openstack/volume/volumes.py
cinder/tests/api/openstack/volume/test_volumes.py
cinder/tests/test_volume.py
cinder/volume/api.py

index 82dbe461ba22352b13e0be400d01cba35afc8fdd..0525a4f3540eac704c9d85f5ccb125ee1f6853c9 100644 (file)
@@ -242,15 +242,7 @@ class VolumeController(object):
 
         volume = body['volume']
 
-        def as_int(s):
-            try:
-                return int(s)
-            except ValueError:
-                return s
-
-        # NOTE(eglynn): we're tolerant of non-int sizes here, as type
-        # integrity is enforced later in the creation codepath
-        size = as_int(volume['size'])
+        size = volume['size']
 
         LOG.audit(_("Create volume of %s GB"), size, context=context)
 
index 2a63b561e670dc9e4ab3b8f33310f5b20495230e..2c69ea55384d2c879195423ee6404ab37107f98d 100644 (file)
@@ -64,10 +64,10 @@ class VolumeApiTest(test.TestCase):
         self.stubs.Set(volume_api.API, 'get', fakes.stub_volume_get)
         self.stubs.Set(volume_api.API, 'delete', fakes.stub_volume_delete)
 
-    def _do_test_volume_create(self, size):
+    def test_volume_create(self):
         self.stubs.Set(volume_api.API, "create", fakes.stub_volume_create)
 
-        vol = {"size": size,
+        vol = {"size": 100,
                "display_name": "Volume Test Name",
                "display_description": "Volume Test Desc",
                "availability_zone": "zone1:host1"}
@@ -91,12 +91,6 @@ class VolumeApiTest(test.TestCase):
                                'size': 100}}
         self.assertEqual(res_dict, expected)
 
-    def test_volume_create_int_size(self):
-        self._do_test_volume_create(100)
-
-    def test_volume_create_str_size(self):
-        self._do_test_volume_create('100')
-
     def test_volume_creation_fails_with_bad_size(self):
         vol = {"size": '',
                "display_name": "Volume Test Name",
@@ -139,7 +133,7 @@ class VolumeApiTest(test.TestCase):
                             'metadata': {},
                             'id': '1',
                             'created_at': datetime.datetime(1, 1, 1, 1, 1, 1),
-                            'size': 1}
+                            'size': '1'}
                     }
         body = {"volume": vol}
         req = fakes.HTTPRequest.blank('/v1/volumes')
index 0cdd3596f818b9a6c9494c756c655c8851e13a95..7ab934bbcc0b44b6d84520807152cab3a5ee61de 100644 (file)
@@ -38,6 +38,7 @@ from cinder.openstack.common import log as os_logging
 from cinder.openstack.common import importutils
 from cinder.openstack.common import rpc
 import cinder.policy
+from cinder import quota
 from cinder import test
 import cinder.volume.api
 
@@ -549,6 +550,43 @@ class VolumeTestCase(test.TestCase):
             db.volume_destroy(self.context, volume_id)
             os.unlink(dst_path)
 
+    def _do_test_create_volume_with_size(self, size):
+        def fake_allowed_volumes(context, requested_volumes, size):
+            return requested_volumes
+
+        self.stubs.Set(quota, 'allowed_volumes', fake_allowed_volumes)
+
+        volume_api = cinder.volume.api.API()
+
+        volume = volume_api.create(self.context,
+                                   size,
+                                   'name',
+                                   'description')
+        self.assertEquals(volume['size'], int(size))
+
+    def test_create_volume_int_size(self):
+        """Test volume creation with int size."""
+        self._do_test_create_volume_with_size(2)
+
+    def test_create_volume_string_size(self):
+        """Test volume creation with string size."""
+        self._do_test_create_volume_with_size('2')
+
+    def test_create_volume_with_bad_size(self):
+        def fake_allowed_volumes(context, requested_volumes, size):
+            return requested_volumes
+
+        self.stubs.Set(quota, 'allowed_volumes', fake_allowed_volumes)
+
+        volume_api = cinder.volume.api.API()
+
+        self.assertRaises(exception.InvalidInput,
+                          volume_api.create,
+                          self.context,
+                          '2Gb',
+                          'name',
+                          'description')
+
 
 class DriverTestCase(test.TestCase):
     """Base Test class for Drivers."""
index 003b3cf2b07ff5fa78803d0a9c19a7f844ca840b..a562c95ec1c54bf19c40d1d4c80ab8b7c45c8ee2 100644 (file)
@@ -93,8 +93,19 @@ class API(base.Base):
             snapshot_id = snapshot['id']
         else:
             snapshot_id = None
+
+        def as_int(s):
+            try:
+                return int(s)
+            except ValueError:
+                return s
+
+        # tolerate size as stringified int
+        size = as_int(size)
+
         if not isinstance(size, int) or size <= 0:
-            msg = _('Volume size must be an integer and greater than 0')
+            msg = (_("Volume size '%s' must be an integer and greater than 0")
+                   % size)
             raise exception.InvalidInput(reason=msg)
         if quota.allowed_volumes(context, 1, size) < 1:
             pid = context.project_id