Merge "replicator: size: Display human-readable size"
This commit is contained in:
commit
fefc3379bd
@ -313,6 +313,14 @@ def get_image_service():
|
||||
return ImageService
|
||||
|
||||
|
||||
def _human_readable_size(num, suffix='B'):
|
||||
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
|
||||
if abs(num) < 1024.0:
|
||||
return "%3.1f %s%s" % (num, unit, suffix)
|
||||
num /= 1024.0
|
||||
return "%.1f %s%s" % (num, 'Yi', suffix)
|
||||
|
||||
|
||||
def replication_size(options, args):
|
||||
"""%(prog)s size <server:port>
|
||||
|
||||
@ -339,8 +347,10 @@ def replication_size(options, args):
|
||||
total_size += int(image['size'])
|
||||
count += 1
|
||||
|
||||
print(_('Total size is %(size)d bytes across %(img_count)d images') %
|
||||
print(_('Total size is %(size)d bytes (%(human_size)s) across '
|
||||
'%(img_count)d images') %
|
||||
{'size': total_size,
|
||||
'human_size': _human_readable_size(total_size),
|
||||
'img_count': count})
|
||||
|
||||
|
||||
|
@ -364,7 +364,10 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
|
||||
glance_replicator.get_image_service = orig_img_service
|
||||
|
||||
output = output.rstrip()
|
||||
self.assertEqual('Total size is 400 bytes across 2 images', output)
|
||||
self.assertEqual(
|
||||
'Total size is 400 bytes (400.0 B) across 2 images',
|
||||
output
|
||||
)
|
||||
|
||||
def test_replication_size_with_no_args(self):
|
||||
args = []
|
||||
@ -376,6 +379,20 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
|
||||
command = glance_replicator.replication_size
|
||||
self.assertTrue(check_bad_args(command, args))
|
||||
|
||||
def test_human_readable_size(self):
|
||||
_human_readable_size = glance_replicator._human_readable_size
|
||||
|
||||
self.assertEqual('0.0 B', _human_readable_size(0))
|
||||
self.assertEqual('1.0 B', _human_readable_size(1))
|
||||
self.assertEqual('512.0 B', _human_readable_size(512))
|
||||
self.assertEqual('1.0 KiB', _human_readable_size(1024))
|
||||
self.assertEqual('2.0 KiB', _human_readable_size(2048))
|
||||
self.assertEqual('8.0 KiB', _human_readable_size(8192))
|
||||
self.assertEqual('64.0 KiB', _human_readable_size(65536))
|
||||
self.assertEqual('93.3 KiB', _human_readable_size(95536))
|
||||
self.assertEqual('117.7 MiB', _human_readable_size(123456789))
|
||||
self.assertEqual('36.3 GiB', _human_readable_size(39022543360))
|
||||
|
||||
def test_replication_dump(self):
|
||||
tempdir = self.useFixture(fixtures.TempDir()).path
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user