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

QEMU 4.0 and earlier have output like:

    virtual size: 1.5M (1572864 bytes)

QEMU 4.1 and later have output like:

    virtual size: 1.5 MiB (1572864 bytes)

Adjust the regular expression to allow for optional whitespace
between the magnitude and the unit.

Adjust the unit parsing to support the expanded "MiB" form.

Change-Id: I1f316d6982c0def3296af4835484ad2d81a87fd4
Closes-Bug: 1844050
(cherry picked from commit 5204dfcd6c)
This commit is contained in:
Mark Mielke 2019-09-15 07:32:20 -04:00 committed by afazekas
parent c83cf61408
commit 89bccdee95
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)),
]