Skip to content

Commit

Permalink
mdraid.py lib: Check if /usr/sbin/mdadm exists
Browse files Browse the repository at this point in the history
Praviously the check was implemented using OSError return from `run`
function. However, in this particular case it's not safe and leads
to unexpected behaviour. Check the existence of the file explicitly
instead prior the `run` function is called.

Update existing unit-tests and extend the test case when mdadm
is not installed.
  • Loading branch information
pirat89 committed Jul 17, 2023
1 parent c7b1e75 commit 50d06eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions repos/system_upgrade/common/libraries/mdraid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from leapp.libraries.stdlib import api, CalledProcessError, run


Expand All @@ -12,11 +14,13 @@ def is_mdraid_dev(dev):
:raises CalledProcessError: If an error occurred
"""
fail_msg = 'Could not check if device "{}" is an md device: {}'
if not os.path.exists('/usr/sbin/mdadm'):
api.current_logger().warning(fail_msg.format(
dev, '/usr/sbin/mdadm is not installed.'
))
return False
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:
err.message = fail_msg.format(dev, err)
raise # let the calling actor handle the exception
Expand Down
14 changes: 14 additions & 0 deletions repos/system_upgrade/common/libraries/tests/test_mdraid.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_is_mdraid_dev(monkeypatch, dev, expected):
run_mocked = RunMocked()
monkeypatch.setattr(mdraid, 'run', run_mocked)
monkeypatch.setattr(api, 'current_logger', logger_mocked())
monkeypatch.setattr(os.path, 'exists', lambda dummy: True)

result = mdraid.is_mdraid_dev(dev)
assert mdraid.run.called == 1
Expand All @@ -62,6 +63,7 @@ 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())
monkeypatch.setattr(os.path, 'exists', lambda dummy: True)

with pytest.raises(CalledProcessError) as err:
mdraid.is_mdraid_dev(MD_DEVICE)
Expand All @@ -71,6 +73,18 @@ def test_is_mdraid_dev_error(monkeypatch):
assert expect_msg in err.value.message


def test_is_mdraid_dev_notool(monkeypatch):
run_mocked = RunMocked(raise_err=True)
monkeypatch.setattr(mdraid, 'run', run_mocked)
monkeypatch.setattr(api, 'current_logger', logger_mocked())
monkeypatch.setattr(os.path, 'exists', lambda dummy: False)

result = mdraid.is_mdraid_dev(MD_DEVICE)
assert not result
assert not mdraid.run.called
assert api.current_logger.warnmsg


def test_get_component_devices_ok(monkeypatch):
run_mocked = RunMocked()
monkeypatch.setattr(mdraid, 'run', run_mocked)
Expand Down

0 comments on commit 50d06eb

Please sign in to comment.