From: Dermot Tynan Date: Thu, 30 Aug 2012 17:38:22 +0000 (+0000) Subject: Fix bug where image size is incorrectly rejected. X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=ffe5036fa0e63ccde2d19aa0f425ec43de338dd7;p=openstack-build%2Fcinder-build.git Fix bug where image size is incorrectly rejected. 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 --- diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 896a4e025..11b53e571 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -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"] diff --git a/cinder/volume/api.py b/cinder/volume/api.py index a13d801de..e6a6d6d3f 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -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.')