Change in vdsm[master]: qemu-img: Handle image end offset on different lines of output

alitke at redhat.com alitke at redhat.com
Tue Mar 3 19:44:52 UTC 2015


Adam Litke has uploaded a new change for review.

Change subject: qemu-img: Handle image end offset on different lines of output
......................................................................

qemu-img: Handle image end offset on different lines of output

Current code assumes that qemu-img check output will place the image end
offset in the second line of output but it can appear later:

$ sudo qemu-img check
/rhev/data-center/mnt/blockSD/7c166135-5a53-4e9a-a77d-119bdd48ea99/images/b5af6494-3f09-4c9e-9500-33a56450091b/41b2b944-77f0-40d5-a681-c63e0dbb7482
No errors were found on the image.
70088/98304 = 71.30% allocated, 0.17% fragmented, 0.00% compressed clusters
Image end offset: 4594466816

Update the scanner so it can find it on any line from the second until
the end of output.

Change-Id: Id6610baf1702c9075d8512bdb1da0e05527a0060
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1196072
Signed-off-by: Adam Litke <alitke at redhat.com>
---
M lib/vdsm/qemuimg.py
M tests/qemuimgTests.py
2 files changed, 50 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/55/38355/1

diff --git a/lib/vdsm/qemuimg.py b/lib/vdsm/qemuimg.py
index 78d175f..d0071c9 100644
--- a/lib/vdsm/qemuimg.py
+++ b/lib/vdsm/qemuimg.py
@@ -51,7 +51,7 @@
 }
 
 INFO_OPTFIELDS_STARTIDX = 4  # qemu-img info optional fields start in this line
-
+CHECK_OPTFIELDS_STARTIDX = 1  # qemu-img check 'offset' may appear on this line
 
 class _RegexSearchError(Exception):
     pass
@@ -149,15 +149,18 @@
     # FIXME: handle different error codes and raise errors accordingly
     if rc != 0:
         raise QImgError(rc, out, err)
-    try:
-        check = {
-            'offset': int(__iregexSearch("offset", out[1]))
-        }
-    # TODO: Add requires for qemu supporting offset and print exc_info
-    except:
-        raise QImgError(rc, out, err, "unable to parse qemu-img check output")
-
-    return check
+    # Scan for 'offset' in the output
+    for row in xrange(CHECK_OPTFIELDS_STARTIDX, len(out)):
+        try:
+            check = {
+                'offset': int(__iregexSearch("offset", out[row]))
+            }
+            return check
+        except _RegexSearchError:
+            pass
+        except:
+            raise QImgError(rc, out, err,
+                            "unable to parse qemu-img check output")
 
 
 def convert(srcImage, dstImage, stop, srcFormat=None, dstFormat=None,
diff --git a/tests/qemuimgTests.py b/tests/qemuimgTests.py
index 4432d1b..08e04ef 100644
--- a/tests/qemuimgTests.py
+++ b/tests/qemuimgTests.py
@@ -336,3 +336,40 @@
     @MonkeyPatch(utils, 'execCmd', convert_src_cache_unsupported)
     def test_convert_unsupported(self, **kw):
         self.assertFalse(qemuimg._supports_src_cache('convert'))
+
+
+outputQemu2Check = """
+No errors were found on the image.
+65157/98304 = 66.28% allocated, 0.00% fragmented, 0.00% compressed clusters
+Image end offset: 4271243264
+"""
+
+outputQemuCheck = """
+No errors were found on the image.
+Image end offset: 4271243264
+"""
+
+
+class CheckTests(TestCaseBase):
+
+    def test_offset_with_stats(self):
+        def call(cmd, **kw):
+            out = ["No errors were found on the image.",
+                   "65157/98304 = 66.28% allocated, 0.00% fragmented, 0.00% "
+                   "compressed clusters",
+                   "Image end offset: 4271243264"]
+            return 0, out, []
+
+        with MonkeyPatchScope([(utils, "execCmd", call)]):
+            check = qemuimg.check('unused')
+            self.assertEquals(4271243264, check['offset'])
+
+    def test_offset_without_stats(self):
+        def call(cmd, **kw):
+            out = ["No errors were found on the image.",
+                   "Image end offset: 4271243264"]
+            return 0, out, []
+
+        with MonkeyPatchScope([(utils, "execCmd", call)]):
+            check = qemuimg.check('unused')
+            self.assertEquals(4271243264, check['offset'])


-- 
To view, visit https://gerrit.ovirt.org/38355
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id6610baf1702c9075d8512bdb1da0e05527a0060
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Adam Litke <alitke at redhat.com>


More information about the vdsm-patches mailing list