]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix bug where image size is incorrectly rejected.
authorDermot Tynan <tynan@hp.com>
Thu, 30 Aug 2012 17:38:22 +0000 (17:38 +0000)
committerDermot Tynan <tynan@hp.com>
Tue, 4 Sep 2012 15:05:11 +0000 (15:05 +0000)
Fixes bug 1043952.

Added GB-1 (one byte less than a gig) to the numerator to
correct round-off errors.

Change-Id: I6d97f3c53ab5a8ff5a0752400fd189f23223958f

Fixed PEP8 complaints.

Change-Id: Iacecddd4e65c80964f4860d6580872d897ec2356

cinder/tests/test_volume.py
cinder/volume/api.py

index 896a4e0252d921a74d4ef9b02fde973dade01768..11b53e57149bd3061430408e1b766f3c8f445674 100644 (file)
@@ -554,6 +554,49 @@ class VolumeTestCase(test.TestCase):
             db.volume_destroy(self.context, volume_id)
             os.unlink(dst_path)
 
+    def test_create_volume_from_exact_sized_image(self):
+        """Verify that an image which is exactly the same size as the
+        volume, will work correctly."""
+        class _FakeImageService:
+            def __init__(self, db_driver=None, image_service=None):
+                pass
+
+            def show(self, context, image_id):
+                return {'size': 2 * 1024 * 1024 * 1024}
+
+        image_id = '70a599e0-31e7-49b7-b260-868f441e862b'
+
+        try:
+            volume_id = None
+            volume_api = cinder.volume.api.API(
+                                            image_service=_FakeImageService())
+            volume = volume_api.create(self.context, 2, 'name', 'description',
+                                       image_id=1)
+            volume_id = volume['id']
+            self.assertEqual(volume['status'], 'creating')
+
+        finally:
+            # cleanup
+            db.volume_destroy(self.context, volume_id)
+
+    def test_create_volume_from_oversized_image(self):
+        """Verify that an image which is too big will fail correctly."""
+        class _FakeImageService:
+            def __init__(self, db_driver=None, image_service=None):
+                pass
+
+            def show(self, context, image_id):
+                return {'size': 2 * 1024 * 1024 * 1024 + 1}
+
+        image_id = '70a599e0-31e7-49b7-b260-868f441e862b'
+
+        volume_api = cinder.volume.api.API(image_service=_FakeImageService())
+
+        self.assertRaises(exception.InvalidInput,
+                          volume_api.create,
+                          self.context, 2,
+                          'name', 'description', image_id=1)
+
     def _do_test_create_volume_with_size(self, size):
         def fake_reserve(context, expire=None, **deltas):
             return ["RESERVATION"]
index a13d801de70217c26580a77906dafd1bc1ab9c0c..e6a6d6d3f3d3e886a85551f14ada14afd564002a 100644 (file)
@@ -135,7 +135,7 @@ class API(base.Base):
         if image_id:
             # check image existence
             image_meta = self.image_service.show(context, image_id)
-            image_size_in_gb = int(image_meta['size']) / GB
+            image_size_in_gb = (int(image_meta['size']) + GB - 1) / GB
             #check image size is not larger than volume size.
             if image_size_in_gb > size:
                 msg = _('Size of specified image is larger than volume size.')