[blivet:f21-branch 04/14] Extend mdadm() to capture output

mulhern amulhern at redhat.com
Mon Sep 29 17:22:51 UTC 2014


Use it in mdexamine()

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/devicelibs/mdraid.py          | 25 ++++++++++++++++++-------
 tests/devicelibs_test/mdraid_test.py | 12 ++++++------
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/blivet/devicelibs/mdraid.py b/blivet/devicelibs/mdraid.py
index aaf4567..d269c83 100644
--- a/blivet/devicelibs/mdraid.py
+++ b/blivet/devicelibs/mdraid.py
@@ -79,10 +79,20 @@ def get_raid_superblock_size(size, version=None):
     log.info("Using %s superBlockSize", headroom)
     return headroom
 
-def mdadm(args):
-    ret = util.run_program(["mdadm"] + args)
+def mdadm(args, capture=False):
+    """ Run mdadm with specified arguments.
+
+        :param bool capture: if True, return the output of the command
+        :returns: the output of the command or None
+        :rtype: str or NoneType
+        :raises: MDRaidError if command fails
+    """
+    argv = ["mdadm"] + args
+    (ret, out) = util.run_program_and_capture_output(argv)
     if ret:
-        raise MDRaidError("running mdadm " + " ".join(args) + " failed")
+        raise MDRaidError(ret)
+    if capture:
+        return out
 
 def mdcreate(device, level, disks, spares=0, metadataVer=None, bitmap=False):
     """ Create an mdarray from a list of devices.
@@ -263,10 +273,11 @@ def mdexamine(device):
         :rtype: a dict of strings
         :returns: a dict containing labels and values extracted from output
     """
-    _vars = util.capture_output(["mdadm",
-                                 "--examine", "--export", device]).split()
-    _bvars = util.capture_output(["mdadm",
-                                 "--examine", "--brief", device]).split()
+    try:
+        _vars = mdadm(["--examine", "--export", device], capture=True).split()
+        _bvars = mdadm(["--examine", "--brief", device], capture=True).split()
+    except MDRaidError as e:
+        raise MDRaidError("mdexamine failed for %s: %s" % (device, e))
 
     info = {}
     if len(_bvars) > 1 and _bvars[1].startswith("/dev/md"):
diff --git a/tests/devicelibs_test/mdraid_test.py b/tests/devicelibs_test/mdraid_test.py
index 107423e..d6ae644 100755
--- a/tests/devicelibs_test/mdraid_test.py
+++ b/tests/devicelibs_test/mdraid_test.py
@@ -101,14 +101,14 @@ class MDRaidAsRootTestCase(loopbackedtestcase.LoopBackedTestCase):
         # wait for raid to settle
         time.sleep(2)
 
-        # examining the array itself yield no data
-        info = mdraid.mdexamine(self._dev_name)
-        self.assertEqual(info, {})
+        # invoking mdexamine on the array itself raises an error
+        with self.assertRaisesRegexp(MDRaidError, "mdexamine failed"):
+            mdraid.mdexamine(self._dev_name)
 
     def testMDExamineNonMDRaid(self):
-        # invoking mdexamine on a device that is not an array member yields {}
-        info = mdraid.mdexamine(self.loopDevices[0])
-        self.assertEqual(info, {})
+        # invoking mdexamine on any non-array member raises an error
+        with self.assertRaisesRegexp(MDRaidError, "mdexamine failed"):
+            mdraid.mdexamine(self.loopDevices[0])
 
     def _testMDExamine(self, names, metadataVersion=None, level=None):
         """ Test mdexamine for a specified metadataVersion.
-- 
1.9.3



More information about the anaconda-patches mailing list