summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Mielke <mark.mielke@gmail.com>2019-09-15 07:32:20 -0400
committerafazekas <afazekas@redhat.com>2019-10-03 19:27:06 +0000
commit89bccdee95f81ddb54b427d6af172bb987fd7545 (patch)
treee18a4c859095293a3415d13db38e506b570cfa55
parentc83cf61408d64dcbc8df3de96cc2bdf02b1fde00 (diff)
downloadoslo-utils-89bccdee95f81ddb54b427d6af172bb987fd7545.tar.gz
Support "qemu-img info" virtual size in QEMU 4.1 and later3.41.2
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 5204dfcd6c5a8f75bb1fe3dc348cec82a6284eff)
-rw-r--r--oslo_utils/imageutils.py7
-rw-r--r--oslo_utils/tests/test_imageutils.py12
2 files changed, 17 insertions, 2 deletions
diff --git a/oslo_utils/imageutils.py b/oslo_utils/imageutils.py
index a9bb3bb..14d7b5c 100644
--- a/oslo_utils/imageutils.py
+++ b/oslo_utils/imageutils.py
@@ -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):
diff --git a/oslo_utils/tests/test_imageutils.py b/oslo_utils/tests/test_imageutils.py
index 532c455..de262e0 100644
--- a/oslo_utils/tests/test_imageutils.py
+++ b/oslo_utils/tests/test_imageutils.py
@@ -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)),
]