From 2e85af59af3429e33cba91af844d50a324512bd4 Mon Sep 17 00:00:00 2001 From: Petr Stodulka Date: Mon, 17 Jul 2023 18:41:18 +0200 Subject: [PATCH 35/42] 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. --- repos/system_upgrade/common/libraries/mdraid.py | 10 +++++++--- .../common/libraries/tests/test_mdraid.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/repos/system_upgrade/common/libraries/mdraid.py b/repos/system_upgrade/common/libraries/mdraid.py index 5eb89c56..5b59814f 100644 --- a/repos/system_upgrade/common/libraries/mdraid.py +++ b/repos/system_upgrade/common/libraries/mdraid.py @@ -1,3 +1,5 @@ +import os + from leapp.libraries.stdlib import api, CalledProcessError, run @@ -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 diff --git a/repos/system_upgrade/common/libraries/tests/test_mdraid.py b/repos/system_upgrade/common/libraries/tests/test_mdraid.py index 6a25d736..cb7c1059 100644 --- a/repos/system_upgrade/common/libraries/tests/test_mdraid.py +++ b/repos/system_upgrade/common/libraries/tests/test_mdraid.py @@ -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 @@ -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) @@ -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) -- 2.41.0