Merge "Support "qemu-img info" virtual size in QEMU 4.1 and later"

This commit is contained in:
Zuul 2019-09-30 18:19:38 +00:00 committed by Gerrit Code Review
commit 962c12a407
2 changed files with 17 additions and 2 deletions

View File

@ -44,7 +44,7 @@ class QemuImgInfo(object):
BACKING_FILE_RE = re.compile((r"^(.*?)\s*\(actual\s+path\s*:"
r"\s+(.*?)\)\s*$"), re.I)
TOP_LEVEL_RE = re.compile(r"^([\w\d\s\_\-]+):(.*)$")
SIZE_RE = re.compile(r"(\d*\.?\d+)(\w+)?(\s*\(\s*(\d+)\s+bytes\s*\))?",
SIZE_RE = re.compile(r"(\d*\.?\d+)\s*(\w+)?(\s*\(\s*(\d+)\s+bytes\s*\))?",
re.I)
def __init__(self, cmd_output=None, format='human'):
@ -106,7 +106,10 @@ class QemuImgInfo(object):
return int(real_size.group(4))
elif not unit_of_measure:
return int(magnitude)
return strutils.string_to_bytes('%s%sB' % (magnitude, unit_of_measure),
# Allow abbreviated unit such as K to mean KB for compatibility.
if len(unit_of_measure) == 1 and unit_of_measure != 'B':
unit_of_measure += 'B'
return strutils.string_to_bytes('%s%s' % (magnitude, unit_of_measure),
return_int=True)
def _extract_details(self, root_cmd, root_details, lines_after):

View File

@ -38,23 +38,35 @@ class ImageUtilsRawTestCase(test_base.BaseTestCase):
exp_virtual_size=67108844)),
('64M_byte', dict(virtual_size='67108844',
exp_virtual_size=67108844)),
('64_MiB_with_byte_hint', dict(virtual_size='64 MiB (67108844 bytes)',
exp_virtual_size=67108844)),
('4.4M', dict(virtual_size='4.4M',
exp_virtual_size=4613735)),
('4.4M_with_byte_hint', dict(virtual_size='4.4M (4592640 bytes)',
exp_virtual_size=4592640)),
('4.4_MiB_with_byte_hint', dict(virtual_size='4.4 MiB (4592640 bytes)',
exp_virtual_size=4592640)),
('2K', dict(virtual_size='2K',
exp_virtual_size=2048)),
('2K_with_byte_hint', dict(virtual_size='2K (2048 bytes)',
exp_virtual_size=2048)),
('2_KiB_with_byte_hint', dict(virtual_size='2 KiB (2048 bytes)',
exp_virtual_size=2048)),
]
_disk_size = [
('96K', dict(disk_size='96K',
exp_disk_size=98304)),
('96_KiB', dict(disk_size='96 KiB',
exp_disk_size=98304)),
('96K_byte', dict(disk_size='98304',
exp_disk_size=98304)),
('98304_B', dict(disk_size='98304 B',
exp_disk_size=98304)),
('3.1G', dict(disk_size='3.1G',
exp_disk_size=3328599655)),
('3.1_GiB', dict(disk_size='3.1 GiB',
exp_disk_size=3328599655)),
('unavailable', dict(disk_size='unavailable',
exp_disk_size=0)),
]