Skip to content

Commit 2e85af5

Browse files
committed
mdraid.py lib: Check if /usr/sbin/mdadm exists
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.
1 parent 2ba4407 commit 2e85af5

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

repos/system_upgrade/common/libraries/mdraid.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from leapp.libraries.stdlib import api, CalledProcessError, run
24

35

@@ -12,11 +14,13 @@ def is_mdraid_dev(dev):
1214
:raises CalledProcessError: If an error occurred
1315
"""
1416
fail_msg = 'Could not check if device "{}" is an md device: {}'
17+
if not os.path.exists('/usr/sbin/mdadm'):
18+
api.current_logger().warning(fail_msg.format(
19+
dev, '/usr/sbin/mdadm is not installed.'
20+
))
21+
return False
1522
try:
1623
result = run(['mdadm', '--query', dev])
17-
except OSError as err:
18-
api.current_logger().warning(fail_msg.format(dev, err))
19-
return False
2024
except CalledProcessError as err:
2125
err.message = fail_msg.format(dev, err)
2226
raise # let the calling actor handle the exception

repos/system_upgrade/common/libraries/tests/test_mdraid.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_is_mdraid_dev(monkeypatch, dev, expected):
5151
run_mocked = RunMocked()
5252
monkeypatch.setattr(mdraid, 'run', run_mocked)
5353
monkeypatch.setattr(api, 'current_logger', logger_mocked())
54+
monkeypatch.setattr(os.path, 'exists', lambda dummy: True)
5455

5556
result = mdraid.is_mdraid_dev(dev)
5657
assert mdraid.run.called == 1
@@ -62,6 +63,7 @@ def test_is_mdraid_dev_error(monkeypatch):
6263
run_mocked = RunMocked(raise_err=True)
6364
monkeypatch.setattr(mdraid, 'run', run_mocked)
6465
monkeypatch.setattr(api, 'current_logger', logger_mocked())
66+
monkeypatch.setattr(os.path, 'exists', lambda dummy: True)
6567

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

7375

76+
def test_is_mdraid_dev_notool(monkeypatch):
77+
run_mocked = RunMocked(raise_err=True)
78+
monkeypatch.setattr(mdraid, 'run', run_mocked)
79+
monkeypatch.setattr(api, 'current_logger', logger_mocked())
80+
monkeypatch.setattr(os.path, 'exists', lambda dummy: False)
81+
82+
result = mdraid.is_mdraid_dev(MD_DEVICE)
83+
assert not result
84+
assert not mdraid.run.called
85+
assert api.current_logger.warnmsg
86+
87+
7488
def test_get_component_devices_ok(monkeypatch):
7589
run_mocked = RunMocked()
7690
monkeypatch.setattr(mdraid, 'run', run_mocked)

0 commit comments

Comments
 (0)