From 27d837f3a75295f2ac35ebbda293c4b5c674c381 Mon Sep 17 00:00:00 2001 From: Matej Matuska Date: Fri, 7 Jul 2023 13:04:56 +0200 Subject: [PATCH] handle mdadm command not available --- .../system_upgrade/common/libraries/mdraid.py | 21 ++++++++++++------- .../common/libraries/tests/test_mdraid.py | 18 +++++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/repos/system_upgrade/common/libraries/mdraid.py b/repos/system_upgrade/common/libraries/mdraid.py index c4346f1da7..2c74ff8b2d 100644 --- a/repos/system_upgrade/common/libraries/mdraid.py +++ b/repos/system_upgrade/common/libraries/mdraid.py @@ -1,4 +1,3 @@ -from leapp.exceptions import StopActorExecution from leapp.libraries.stdlib import api, CalledProcessError, run @@ -6,15 +5,22 @@ def is_mdraid_dev(dev): """ Check if a given device is an md (Multiple Device) device - :raises: StopActorExecution in case of error + It is expected that the "mdadm" command is available, + if it's not it is assumed the device is not an md device. + + :return: True if the device is an md device, False otherwise + :raises CalledProcessError: If an error occurred """ + fail_msg = 'Could not check if device "{}" is an md device: {}' try: result = run(['mdadm', '--query', dev]) + except OSError as err: + api.current_logger().warning(fail_msg.format(dev, err)) + return False except CalledProcessError as err: - api.current_logger().warning( - 'Could not check if device is an md device: {}'.format(err) - ) - raise StopActorExecution() + err.message = fail_msg.format(dev, err) + raise # let the calling actor handle the exception + return '--detail' in result['stdout'] @@ -23,11 +29,12 @@ def get_component_devices(raid_dev): Get list of component devices in an md (Multiple Device) array :return: The list of component devices or None in case of error + :raises ValueError: If the device is not an mdraid device """ try: # using both --verbose and --brief for medium verbosity result = run(['mdadm', '--detail', '--verbose', '--brief', raid_dev]) - except CalledProcessError as err: + except (OSError, CalledProcessError) as err: api.current_logger().warning( 'Could not get md array component devices: {}'.format(err) ) diff --git a/repos/system_upgrade/common/libraries/tests/test_mdraid.py b/repos/system_upgrade/common/libraries/tests/test_mdraid.py index fe19d85600..6a25d73604 100644 --- a/repos/system_upgrade/common/libraries/tests/test_mdraid.py +++ b/repos/system_upgrade/common/libraries/tests/test_mdraid.py @@ -58,6 +58,19 @@ def test_is_mdraid_dev(monkeypatch, dev, expected): assert not api.current_logger.warnmsg +def test_is_mdraid_dev_error(monkeypatch): + run_mocked = RunMocked(raise_err=True) + monkeypatch.setattr(mdraid, 'run', run_mocked) + monkeypatch.setattr(api, 'current_logger', logger_mocked()) + + with pytest.raises(CalledProcessError) as err: + mdraid.is_mdraid_dev(MD_DEVICE) + + assert mdraid.run.called == 1 + expect_msg = 'Could not check if device "{}" is an md device:'.format(MD_DEVICE) + assert expect_msg in err.value.message + + def test_get_component_devices_ok(monkeypatch): run_mocked = RunMocked() monkeypatch.setattr(mdraid, 'run', run_mocked) @@ -73,6 +86,9 @@ def test_get_component_devices_not_md_device(monkeypatch): run_mocked = RunMocked() monkeypatch.setattr(mdraid, 'run', run_mocked) - with pytest.raises(ValueError): + with pytest.raises(ValueError) as err: mdraid.get_component_devices(NOT_MD_DEVICE) + assert mdraid.run.called == 1 + expect_msg = 'Expected md device, but got: {}'.format(NOT_MD_DEVICE) + assert expect_msg in str(err.value)