Skip to content

Commit

Permalink
handle mdadm command not available
Browse files Browse the repository at this point in the history
  • Loading branch information
matejmatuska committed Jul 7, 2023
1 parent 8c88912 commit 026ead8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
21 changes: 14 additions & 7 deletions repos/system_upgrade/common/libraries/mdraid.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
from leapp.exceptions import StopActorExecution
from leapp.libraries.stdlib import api, CalledProcessError, run


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']


Expand All @@ -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)
)
Expand Down
18 changes: 17 additions & 1 deletion repos/system_upgrade/common/libraries/tests/test_mdraid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 026ead8

Please sign in to comment.