From 886f7f84191c40e3e2c34d6528502a7e57846fca Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Thu, 19 Feb 2015 13:18:53 +0100 Subject: [PATCH] Use oslo_utils.units where appropriate Use units from oslo_utils where appropriate instead of using magic numbers. Changes from patch Ib08b8d8843b72966e2cf87f741b1cc0eea0672e5 are also included. Co-Authored-By: Gorka Eguileor Co-Authored-By: James Page Change-Id: I082ea91cb06e49659495cae9072b263eccda76a1 --- glance_store/_drivers/cinder.py | 3 ++- glance_store/_drivers/rbd.py | 5 +++-- glance_store/_drivers/swift/store.py | 5 +++-- glance_store/driver.py | 3 ++- tests/unit/test_cinder_store.py | 4 +++- tests/unit/test_filesystem_store.py | 29 +++++++++++++--------------- tests/unit/test_rbd_store.py | 5 +++-- tests/unit/test_s3_store.py | 2 +- tests/unit/test_swift_store.py | 12 ++++++------ 9 files changed, 36 insertions(+), 32 deletions(-) diff --git a/glance_store/_drivers/cinder.py b/glance_store/_drivers/cinder.py index f91072c7..ab9e284f 100644 --- a/glance_store/_drivers/cinder.py +++ b/glance_store/_drivers/cinder.py @@ -18,6 +18,7 @@ from cinderclient import exceptions as cinder_exception from cinderclient import service_catalog from cinderclient.v2 import client as cinderclient from oslo_config import cfg +from oslo_utils import units from glance_store import capabilities from glance_store.common import utils @@ -173,7 +174,7 @@ class Store(glance_store.driver.Store): volume = get_cinderclient(self.conf, context).volumes.get(loc.volume_id) # GB unit convert to byte - return volume.size * (1024 ** 3) + return volume.size * units.Gi except cinder_exception.NotFound as e: reason = _("Failed to get image size due to " "volume can not be found: %s") % self.volume_id diff --git a/glance_store/_drivers/rbd.py b/glance_store/_drivers/rbd.py index 09d24686..8d64e34d 100644 --- a/glance_store/_drivers/rbd.py +++ b/glance_store/_drivers/rbd.py @@ -24,6 +24,7 @@ import math import urllib from oslo_config import cfg +from oslo_utils import units from glance_store import capabilities from glance_store.common import utils @@ -193,7 +194,7 @@ class Store(driver.Store): """ try: chunk = self.conf.glance_store.rbd_store_chunk_size - self.chunk_size = chunk * (1024 ** 2) + self.chunk_size = chunk * units.Mi self.READ_CHUNKSIZE = self.chunk_size self.WRITE_CHUNKSIZE = self.READ_CHUNKSIZE @@ -369,7 +370,7 @@ class Store(driver.Store): length = offset + chunk_length bytes_written += chunk_length LOG.debug(_("resizing image to %s KiB") % - (length / 1024)) + (length / units.Ki)) image.resize(length) LOG.debug(_("writing chunk at offset %s") % (offset)) diff --git a/glance_store/_drivers/swift/store.py b/glance_store/_drivers/swift/store.py index 30b69f3f..4ec42f98 100644 --- a/glance_store/_drivers/swift/store.py +++ b/glance_store/_drivers/swift/store.py @@ -22,6 +22,7 @@ import math from oslo.config import cfg from oslo.utils import excutils +from oslo_utils import units import six import six.moves.urllib.parse as urlparse import swiftclient @@ -43,9 +44,9 @@ LOG = logging.getLogger(__name__) _LI = i18n._LI DEFAULT_CONTAINER = 'glance' -DEFAULT_LARGE_OBJECT_SIZE = 5 * 1024 # 5GB +DEFAULT_LARGE_OBJECT_SIZE = 5 * units.Ki # 5GB DEFAULT_LARGE_OBJECT_CHUNK_SIZE = 200 # 200M -ONE_MB = 1000 * 1024 +ONE_MB = units.k * units.Ki # Here we used the mixed meaning of MB _SWIFT_OPTS = [ cfg.StrOpt('swift_store_auth_version', default='2', diff --git a/glance_store/driver.py b/glance_store/driver.py index c7cf3a59..cf98d1ca 100644 --- a/glance_store/driver.py +++ b/glance_store/driver.py @@ -20,6 +20,7 @@ import logging from oslo.config import cfg from oslo.utils import importutils +from oslo_utils import units from glance_store import capabilities from glance_store.common import utils @@ -33,7 +34,7 @@ LOG = logging.getLogger(__name__) class Store(capabilities.StoreCapability): OPTIONS = None - READ_CHUNKSIZE = 16 * (1024 * 1024) # 16M + READ_CHUNKSIZE = 16 * units.Mi # 16M WRITE_CHUNKSIZE = READ_CHUNKSIZE def __init__(self, conf): diff --git a/tests/unit/test_cinder_store.py b/tests/unit/test_cinder_store.py index 09726649..c5263608 100644 --- a/tests/unit/test_cinder_store.py +++ b/tests/unit/test_cinder_store.py @@ -15,6 +15,8 @@ import mock +from oslo_utils import units + import glance_store from glance_store._drivers import cinder from glance_store import exceptions @@ -70,7 +72,7 @@ class TestCinderStore(base.StoreBaseTest, loc = location.get_location_from_uri(uri, conf=self.conf) image_size = self.store.get_size(loc, context=fake_context) self.assertEqual(image_size, - fake_volumes.values()[0].size * (1024 ** 3)) + fake_volumes.values()[0].size * units.Gi) def test_cinder_delete_raise_error(self): uri = 'cinder://12345678-9012-3455-6789-012345678901' diff --git a/tests/unit/test_filesystem_store.py b/tests/unit/test_filesystem_store.py index ac72e897..27993e48 100644 --- a/tests/unit/test_filesystem_store.py +++ b/tests/unit/test_filesystem_store.py @@ -39,9 +39,6 @@ from glance_store.tests import base from tests.unit import test_store_capabilities -KB = 1024 - - class TestStore(base.StoreBaseTest, test_store_capabilities.TestStoreCapabilitiesChecking): @@ -155,9 +152,9 @@ class TestStore(base.StoreBaseTest, def test_add(self): """Test that we can add an image via the filesystem backend.""" - ChunkedFile.CHUNKSIZE = 1024 + ChunkedFile.CHUNKSIZE = units.Ki expected_image_id = str(uuid.uuid4()) - expected_file_size = 5 * KB # 5K + expected_file_size = 5 * units.Ki # 5K expected_file_contents = "*" * expected_file_size expected_checksum = hashlib.md5(expected_file_contents).hexdigest() expected_location = "file://%s/%s" % (self.test_dir, @@ -225,9 +222,9 @@ class TestStore(base.StoreBaseTest, Tests that adding an image with an existing identifier raises an appropriate exception """ - ChunkedFile.CHUNKSIZE = 1024 + ChunkedFile.CHUNKSIZE = units.Ki image_id = str(uuid.uuid4()) - file_size = 5 * KB # 5K + file_size = 5 * units.Ki # 5K file_contents = "*" * file_size image_file = StringIO.StringIO(file_contents) @@ -240,9 +237,9 @@ class TestStore(base.StoreBaseTest, image_id, image_file, 0) def _do_test_add_write_failure(self, errno, exception): - ChunkedFile.CHUNKSIZE = 1024 + ChunkedFile.CHUNKSIZE = units.Ki image_id = str(uuid.uuid4()) - file_size = 5 * KB # 5K + file_size = 5 * units.Ki # 5K file_contents = "*" * file_size path = os.path.join(self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) @@ -291,9 +288,9 @@ class TestStore(base.StoreBaseTest, Tests the partial image file is cleaned up after a read failure. """ - ChunkedFile.CHUNKSIZE = 1024 + ChunkedFile.CHUNKSIZE = units.Ki image_id = str(uuid.uuid4()) - file_size = 5 * KB # 5K + file_size = 5 * units.Ki # 5K file_contents = "*" * file_size path = os.path.join(self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) @@ -315,7 +312,7 @@ class TestStore(base.StoreBaseTest, """ # First add an image image_id = str(uuid.uuid4()) - file_size = 5 * KB # 5K + file_size = 5 * units.Ki # 5K file_contents = "*" * file_size image_file = StringIO.StringIO(file_contents) @@ -468,7 +465,7 @@ class TestStore(base.StoreBaseTest, self.store.configure() """Test that we can add an image via the filesystem backend""" - ChunkedFile.CHUNKSIZE = 1024 + ChunkedFile.CHUNKSIZE = units.Ki expected_image_id = str(uuid.uuid4()) expected_file_size = 5 * units.Ki # 5K expected_file_contents = "*" * expected_file_size @@ -520,7 +517,7 @@ class TestStore(base.StoreBaseTest, with mock.patch.object(self.store, '_get_capacity_info') as capacity: capacity.return_value = 0 - ChunkedFile.CHUNKSIZE = 1024 + ChunkedFile.CHUNKSIZE = units.Ki expected_image_id = str(uuid.uuid4()) expected_file_size = 5 * units.Ki # 5K expected_file_contents = "*" * expected_file_size @@ -573,7 +570,7 @@ class TestStore(base.StoreBaseTest, self.store.configure_add() - Store.WRITE_CHUNKSIZE = 1024 + Store.WRITE_CHUNKSIZE = units.Ki expected_image_id = str(uuid.uuid4()) expected_file_size = 5 * units.Ki # 5K expected_file_contents = "*" * expected_file_size @@ -614,7 +611,7 @@ class TestStore(base.StoreBaseTest, self.store.configure_add() - Store.WRITE_CHUNKSIZE = 1024 + Store.WRITE_CHUNKSIZE = units.Ki expected_image_id = str(uuid.uuid4()) expected_file_size = 5 * units.Ki # 5K expected_file_contents = "*" * expected_file_size diff --git a/tests/unit/test_rbd_store.py b/tests/unit/test_rbd_store.py index 917b7ff0..ede804a5 100644 --- a/tests/unit/test_rbd_store.py +++ b/tests/unit/test_rbd_store.py @@ -16,6 +16,7 @@ import StringIO import mock +from oslo_utils import units from glance_store._drivers import rbd as rbd_store from glance_store import exceptions @@ -163,12 +164,12 @@ class TestStore(base.StoreBaseTest, self.location = rbd_store.StoreLocation(self.store_specs, self.conf) # Provide enough data to get more than one chunk iteration. - self.data_len = 3 * 1024 + self.data_len = 3 * units.Ki self.data_iter = StringIO.StringIO('*' * self.data_len) def test_add_w_image_size_zero(self): """Assert that correct size is returned even though 0 was provided.""" - self.store.chunk_size = 1024 + self.store.chunk_size = units.Ki with mock.patch.object(rbd_store.rbd.Image, 'resize') as resize: with mock.patch.object(rbd_store.rbd.Image, 'write') as write: ret = self.store.add('fake_image_id', self.data_iter, 0) diff --git a/tests/unit/test_s3_store.py b/tests/unit/test_s3_store.py index 39c0ba5c..9345ac18 100644 --- a/tests/unit/test_s3_store.py +++ b/tests/unit/test_s3_store.py @@ -56,7 +56,7 @@ class FakeKey(object): self.data = None self.size = 0 self.etag = None - self.BufferSize = 1024 + self.BufferSize = units.Ki def close(self): pass diff --git a/tests/unit/test_swift_store.py b/tests/unit/test_swift_store.py index 8a541a4b..4141d84a 100644 --- a/tests/unit/test_swift_store.py +++ b/tests/unit/test_swift_store.py @@ -662,8 +662,8 @@ class SwiftTests(object): orig_max_size = self.store.large_object_size orig_temp_size = self.store.large_object_chunk_size try: - self.store.large_object_size = 1024 - self.store.large_object_chunk_size = 1024 + self.store.large_object_size = units.Ki + self.store.large_object_chunk_size = units.Ki loc, size, checksum, _ = self.store.add(expected_image_id, image_swift, expected_swift_size) @@ -719,9 +719,9 @@ class SwiftTests(object): global MAX_SWIFT_OBJECT_SIZE orig_max_swift_object_size = MAX_SWIFT_OBJECT_SIZE try: - MAX_SWIFT_OBJECT_SIZE = 1024 - self.store.large_object_size = 1024 - self.store.large_object_chunk_size = 1024 + MAX_SWIFT_OBJECT_SIZE = units.Ki + self.store.large_object_size = units.Ki + self.store.large_object_chunk_size = units.Ki loc, size, checksum, _ = self.store.add(expected_image_id, image_swift, 0) finally: @@ -1446,7 +1446,7 @@ class TestChunkReader(base.StoreBaseTest): bytes_read += len(chunk) if not chunk: break - self.assertEqual(1024, bytes_read) + self.assertEqual(units.Ki, bytes_read) data_file.close()