From f709c9d3db7578afd2c05f723f9bd4160e1c80d8 Mon Sep 17 00:00:00 2001 From: Chang Bo Guo Date: Tue, 29 Oct 2013 02:03:50 -0700 Subject: [PATCH] clean up numeric expressions in test Replace numeric expressions with constants in tests to make code more readable. Change-Id: I42469cdbe3cafd36f8d9855c8284f885ffa7f465 --- cinder/backup/drivers/swift.py | 3 ++- cinder/tests/test_gpfs.py | 7 ++++--- cinder/tests/test_image_utils.py | 3 ++- cinder/tests/test_nfs.py | 2 +- cinder/tests/test_rbd.py | 2 +- cinder/tests/test_scality.py | 3 ++- cinder/tests/test_sheepdog.py | 4 ++-- cinder/tests/test_solidfire.py | 3 ++- cinder/tests/test_storwize_svc.py | 2 +- cinder/tests/test_vmware_vmdk.py | 2 +- cinder/volume/drivers/rbd.py | 4 ++-- cinder/volume/drivers/sheepdog.py | 4 ++-- cinder/volume/drivers/storwize_svc.py | 5 +++-- 13 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cinder/backup/drivers/swift.py b/cinder/backup/drivers/swift.py index b243d32b750..655bc65260a 100644 --- a/cinder/backup/drivers/swift.py +++ b/cinder/backup/drivers/swift.py @@ -44,6 +44,7 @@ from cinder.backup.driver import BackupDriver from cinder import exception from cinder.openstack.common import log as logging from cinder.openstack.common import timeutils +from cinder import units from swiftclient import client as swift @@ -242,7 +243,7 @@ class SwiftBackupDriver(BackupDriver): backup['service_metadata'] = object_prefix self.db.backup_update(self.context, backup_id, {'service_metadata': object_prefix}) - volume_size_bytes = volume['size'] * 1024 * 1024 * 1024 + volume_size_bytes = volume['size'] * units.GiB availability_zone = self.az LOG.debug(_('starting backup of volume: %(volume_id)s to swift,' ' volume size: %(volume_size_bytes)d, swift object names' diff --git a/cinder/tests/test_gpfs.py b/cinder/tests/test_gpfs.py index 316e182834f..f6909a17ef2 100644 --- a/cinder/tests/test_gpfs.py +++ b/cinder/tests/test_gpfs.py @@ -28,6 +28,7 @@ from cinder.openstack.common import log as logging from cinder.openstack.common import processutils from cinder import test from cinder.tests import utils as test_utils +from cinder import units from cinder import utils from cinder.volume import configuration as conf from cinder.volume.drivers.gpfs import GPFSDriver @@ -434,7 +435,7 @@ class GPFSDriverTestCase(test.TestCase): return False def _fake_get_available_capacity(self, path): - fake_avail = 80 * 1024 * 1024 * 1024 + fake_avail = 80 * units.GiB fake_size = 2 * fake_avail return fake_avail, fake_size @@ -485,14 +486,14 @@ class GPFSDriverTestCase(test.TestCase): data = FakeQemuImgInfo() data.file_format = 'qcow2' data.backing_file = None - data.virtual_size = 1024 * 1024 * 1024 + data.virtual_size = 1 * units.GiB return data def _fake_qemu_raw_image_info(self, path): data = FakeQemuImgInfo() data.file_format = 'raw' data.backing_file = None - data.virtual_size = 1024 * 1024 * 1024 + data.virtual_size = 1 * units.GiB return data def _fake_qemu_image_resize(self, path, size): diff --git a/cinder/tests/test_image_utils.py b/cinder/tests/test_image_utils.py index 967aa381820..174c263ab3d 100644 --- a/cinder/tests/test_image_utils.py +++ b/cinder/tests/test_image_utils.py @@ -25,6 +25,7 @@ from cinder import context from cinder import exception from cinder.image import image_utils from cinder import test +from cinder import units from cinder import utils @@ -37,7 +38,7 @@ class FakeImageService: data.write(self._imagedata.get(image_id, '')) def show(self, context, image_id): - return {'size': 2 * 1024 * 1024 * 1024, + return {'size': 2 * units.GiB, 'disk_format': 'qcow2', 'container_format': 'bare'} diff --git a/cinder/tests/test_nfs.py b/cinder/tests/test_nfs.py index c221019021f..993f35d17b0 100644 --- a/cinder/tests/test_nfs.py +++ b/cinder/tests/test_nfs.py @@ -182,7 +182,7 @@ class NfsDriverTestCase(test.TestCase): mox.StubOutWithMock(image_utils, 'qemu_img_info') data = mox_lib.MockAnything() - data.virtual_size = 1024 ** 3 + data.virtual_size = 1 * units.GiB image_utils.qemu_img_info(TEST_IMG_SOURCE).AndReturn(data) mox.ReplayAll() diff --git a/cinder/tests/test_rbd.py b/cinder/tests/test_rbd.py index 72e176ec8cb..33fc0630e9a 100644 --- a/cinder/tests/test_rbd.py +++ b/cinder/tests/test_rbd.py @@ -116,7 +116,7 @@ class RBDTestCase(test.TestCase): self.rbd.RBD_FEATURE_LAYERING = 1 _mock_rbd = self.mox.CreateMockAnything() self.rbd.RBD().AndReturn(_mock_rbd) - _mock_rbd.create(mox.IgnoreArg(), str(name), size * 1024 ** 3, + _mock_rbd.create(mox.IgnoreArg(), str(name), size * units.GiB, old_format=False, features=self.rbd.RBD_FEATURE_LAYERING) mock_client.__exit__(None, None, None).AndReturn(None) diff --git a/cinder/tests/test_scality.py b/cinder/tests/test_scality.py index 97966c5ba00..5c4c62d37d9 100644 --- a/cinder/tests/test_scality.py +++ b/cinder/tests/test_scality.py @@ -27,6 +27,7 @@ from cinder import context from cinder import exception from cinder.image import image_utils from cinder import test +from cinder import units from cinder import utils from cinder.volume.drivers import scality @@ -181,7 +182,7 @@ class ScalityDriverTestCase(test.TestCase): self.TEST_VOLNAME)) self.assertTrue(os.path.isfile(self.TEST_VOLPATH)) self.assertEqual(os.stat(self.TEST_VOLPATH).st_size, - 100 * 1024 * 1024) + 100 * units.MiB) def test_delete_volume(self): """Expected behaviour for delete_volume.""" diff --git a/cinder/tests/test_sheepdog.py b/cinder/tests/test_sheepdog.py index 51428399d7e..793579884ad 100644 --- a/cinder/tests/test_sheepdog.py +++ b/cinder/tests/test_sheepdog.py @@ -71,8 +71,8 @@ class SheepdogTestCase(test.TestCase): vendor_name='Open Source', dirver_version=self.driver.VERSION, storage_protocol='sheepdog', - total_capacity_gb=float(107287605248) / (1024 ** 3), - free_capacity_gb=float(107287605248 - 3623897354) / (1024 ** 3), + total_capacity_gb=float(107287605248) / units.GiB, + free_capacity_gb=float(107287605248 - 3623897354) / units.GiB, reserved_percentage=0, QoS_support=False) actual = self.driver.get_volume_stats(True) diff --git a/cinder/tests/test_solidfire.py b/cinder/tests/test_solidfire.py index 255a674bc40..73e11793f61 100644 --- a/cinder/tests/test_solidfire.py +++ b/cinder/tests/test_solidfire.py @@ -21,6 +21,7 @@ from cinder import exception from cinder.openstack.common import log as logging from cinder.openstack.common import timeutils from cinder import test +from cinder import units from cinder.volume import configuration as conf from cinder.volume.drivers.solidfire import SolidFireDriver @@ -112,7 +113,7 @@ class SolidFireVolumeTestCase(test.TestCase): 'name': test_name, 'accountID': 25, 'sliceCount': 1, - 'totalSize': 1048576 * 1024, + 'totalSize': 1 * units.GiB, 'enable512e': True, 'access': "readWrite", 'status': "active", diff --git a/cinder/tests/test_storwize_svc.py b/cinder/tests/test_storwize_svc.py index 8cc69155345..24a219d2115 100644 --- a/cinder/tests/test_storwize_svc.py +++ b/cinder/tests/test_storwize_svc.py @@ -1739,7 +1739,7 @@ class StorwizeSVCDriverTestCase(test.TestCase): # Make sure volume attributes are as they should be attributes = self.driver._get_vdisk_attributes(volume['name']) - attr_size = float(attributes['capacity']) / (1024 ** 3) # bytes to GB + attr_size = float(attributes['capacity']) / units.GiB # bytes to GB self.assertEqual(attr_size, float(volume['size'])) pool = self.driver.configuration.local_conf.storwize_svc_volpool_name self.assertEqual(attributes['mdisk_grp_name'], pool) diff --git a/cinder/tests/test_vmware_vmdk.py b/cinder/tests/test_vmware_vmdk.py index ca4e5fd0fc2..5846de20918 100644 --- a/cinder/tests/test_vmware_vmdk.py +++ b/cinder/tests/test_vmware_vmdk.py @@ -1320,7 +1320,7 @@ class VMwareEsxVmdkDriverTestCase(test.TestCase): image_id = 'image-id' image_meta = FakeObject() image_meta['disk_format'] = 'vmdk' - image_meta['size'] = 1024 * 1024 + image_meta['size'] = 1 * units.MiB image_service = m.CreateMock(glance.GlanceImageService) image_service.show(mox.IgnoreArg(), image_id).AndReturn(image_meta) volume = FakeObject() diff --git a/cinder/volume/drivers/rbd.py b/cinder/volume/drivers/rbd.py index ddcf16dcde0..0ab857c3ac0 100644 --- a/cinder/volume/drivers/rbd.py +++ b/cinder/volume/drivers/rbd.py @@ -339,8 +339,8 @@ class RBDDriver(driver.VolumeDriver): try: with RADOSClient(self) as client: new_stats = client.cluster.get_cluster_stats() - stats['total_capacity_gb'] = new_stats['kb'] / 1024 ** 2 - stats['free_capacity_gb'] = new_stats['kb_avail'] / 1024 ** 2 + stats['total_capacity_gb'] = new_stats['kb'] / units.MiB + stats['free_capacity_gb'] = new_stats['kb_avail'] / units.MiB except self.rados.Error: # just log and return unknown capacities LOG.exception(_('error refreshing volume stats')) diff --git a/cinder/volume/drivers/sheepdog.py b/cinder/volume/drivers/sheepdog.py index 57a8bfde355..1168a6086ca 100644 --- a/cinder/volume/drivers/sheepdog.py +++ b/cinder/volume/drivers/sheepdog.py @@ -172,8 +172,8 @@ class SheepdogDriver(driver.VolumeDriver): m = self.stats_pattern.match(stdout) total = float(m.group(1)) used = float(m.group(2)) - stats['total_capacity_gb'] = total / (1024 ** 3) - stats['free_capacity_gb'] = (total - used) / (1024 ** 3) + stats['total_capacity_gb'] = total / units.GiB + stats['free_capacity_gb'] = (total - used) / units.GiB except processutils.ProcessExecutionError: LOG.exception(_('error refreshing volume stats')) diff --git a/cinder/volume/drivers/storwize_svc.py b/cinder/volume/drivers/storwize_svc.py index 3026504d4f7..d03da19c576 100644 --- a/cinder/volume/drivers/storwize_svc.py +++ b/cinder/volume/drivers/storwize_svc.py @@ -54,6 +54,7 @@ from cinder.openstack.common import log as logging from cinder.openstack.common import loopingcall from cinder.openstack.common import processutils from cinder.openstack.common import strutils +from cinder import units from cinder import utils from cinder.volume.drivers.san import san from cinder.volume import volume_types @@ -1562,9 +1563,9 @@ class StorwizeSVCDriver(san.SanDriver): raise exception.VolumeBackendAPIException(data=exception_message) data['total_capacity_gb'] = (float(attributes['capacity']) / - (1024 ** 3)) + units.GiB) data['free_capacity_gb'] = (float(attributes['free_capacity']) / - (1024 ** 3)) + units.GiB) data['easytier_support'] = attributes['easy_tier'] in ['on', 'auto'] data['compression_support'] = self._compression_enabled data['extent_size'] = self._extent_size