From 656e79da1854bee7db063644d0b2c0ce0e8955fd Mon Sep 17 00:00:00 2001 From: Edward Hope-Morley Date: Wed, 13 Sep 2017 16:00:09 -0600 Subject: [PATCH] Catch blkid error when device is not yet formatted When a new device is added to the ring we first try to identify whether the device is already in the ring by polling for an fs uuid. If the device has never been used this is expected to fail so lets catch the error. Also fixes log message. Change-Id: I20354dedfa27a6b8dec92828cabb50a20d0d8838 Closes-Bug: 1567198 --- lib/swift_storage_utils.py | 18 ++++++++++++++++-- unit_tests/test_swift_storage_utils.py | 6 ++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/swift_storage_utils.py b/lib/swift_storage_utils.py index 9647267..0a5df32 100644 --- a/lib/swift_storage_utils.py +++ b/lib/swift_storage_utils.py @@ -364,7 +364,21 @@ def is_device_in_ring(dev, skip_rel_check=False, ignore_deactivated=True): def get_device_blkid(dev): - blk_uuid = subprocess.check_output(['blkid', '-s', 'UUID', dev]) + """Try to get the fs uuid of the provided device. + + If this is called for a new unformatted device we expect blkid to fail + hence return None to indicate the device is not in use. + + :param dev: block device path + :returns: UUID of device if found else None + """ + try: + blk_uuid = subprocess.check_output(['blkid', '-s', 'UUID', dev]) + except CalledProcessError: + # If the device has not be used or formatted yet we expect this to + # fail. + return None + blk_uuid = re.match(r'^%s:\s+UUID="(.+)"$' % (dev), blk_uuid.strip()) if blk_uuid: return blk_uuid.group(1) @@ -402,7 +416,7 @@ def remember_devices(devs): level=WARNING) else: log("Adding device '%s' with blkid='%s' to devstore" % - (blk_uuid, dev), + (dev, blk_uuid), level=DEBUG) devstore[key] = {'blkid': blk_uuid, 'status': 'active'} diff --git a/unit_tests/test_swift_storage_utils.py b/unit_tests/test_swift_storage_utils.py index f9301f8..58eb26e 100644 --- a/unit_tests/test_swift_storage_utils.py +++ b/unit_tests/test_swift_storage_utils.py @@ -447,3 +447,9 @@ class SwiftStorageUtilsTests(CharmTestCase): uuid = swift_utils.get_device_blkid(dev) self.assertEqual(uuid, "808bc298-0609-4619-aaef-ed7a5ab0ebb7") mock_check_output.assert_called_with(cmd) + + def fake_check_output(*args, **kwargs): + raise swift_utils.CalledProcessError('a', 'b', 'c') + + mock_check_output.side_effect = fake_check_output + self.assertIsNone(swift_utils.get_device_blkid(dev))