From e72ab53ca927a1a92dddde9a173d830ceba2a56b Mon Sep 17 00:00:00 2001 From: Matej Matuska Date: Thu, 8 Aug 2024 20:25:44 +0200 Subject: [PATCH 1/2] Fix incorrect mock in updategrubcore tests The run function was mocked instead of `leapp.libraries.common.grub.get_grub_devices`, making test fail when ran directly on the host, not in containers, because gru2-probe failed there as expected. Left a couple of TODOs to improve error handling in the grub library. Jira: OAMG-11647 --- .../updategrubcore/tests/test_updategrubcore.py | 14 ++++++-------- repos/system_upgrade/common/libraries/grub.py | 7 ++++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/repos/system_upgrade/common/actors/updategrubcore/tests/test_updategrubcore.py b/repos/system_upgrade/common/actors/updategrubcore/tests/test_updategrubcore.py index 7e658a70f3..27a4a24562 100644 --- a/repos/system_upgrade/common/actors/updategrubcore/tests/test_updategrubcore.py +++ b/repos/system_upgrade/common/actors/updategrubcore/tests/test_updategrubcore.py @@ -3,11 +3,10 @@ from leapp import reporting from leapp.exceptions import StopActorExecution from leapp.libraries.actor import updategrubcore -from leapp.libraries.common import testutils +from leapp.libraries.common import grub, testutils from leapp.libraries.common.config import architecture from leapp.libraries.stdlib import CalledProcessError from leapp.models import FirmwareFacts -from leapp.reporting import Report UPDATE_OK_TITLE = 'GRUB core successfully updated' UPDATE_FAILED_TITLE = 'GRUB core update failed' @@ -107,14 +106,13 @@ def test_update_grub_nogrub_system_ibmz(monkeypatch): def test_update_grub_nogrub_system(monkeypatch): - def raise_call_oserror(dummy): - # Note: grub2-probe is enough right now. If the implementation is changed, - # the test will most likely start to fail and better mocking will be needed. - raise OSError('File not found: grub2-probe') + def get_grub_devices_mocked(): + # this is not very well documented, but the grub.get_grub_devices function raises a StopActorExecution on error + # (whether that's caused by determining root partition or determining the block device a given partition is on + raise StopActorExecution() + monkeypatch.setattr(grub, 'get_grub_devices', get_grub_devices_mocked) monkeypatch.setattr(reporting, 'create_report', testutils.create_report_mocked()) - monkeypatch.setattr(updategrubcore, 'run', run_mocked(raise_err=True, raise_callback=raise_call_oserror)) - msgs = [FirmwareFacts(firmware='bios')] curr_actor_mocked = testutils.CurrentActorMocked(arch=architecture.ARCH_X86_64, msgs=msgs) monkeypatch.setattr(updategrubcore.api, 'current_actor', curr_actor_mocked) diff --git a/repos/system_upgrade/common/libraries/grub.py b/repos/system_upgrade/common/libraries/grub.py index 957d51d651..3c80556e1d 100644 --- a/repos/system_upgrade/common/libraries/grub.py +++ b/repos/system_upgrade/common/libraries/grub.py @@ -36,7 +36,7 @@ def blk_dev_from_partition(partition): api.current_logger().warning( 'Could not get parent device of {} partition'.format(partition) ) - raise StopActorExecution() + raise StopActorExecution() # TODO: return some meaningful value/error # lsblk "-s" option prints dependencies in inverse order, so the parent device will always # be the last or the only device. # Command result example: @@ -55,14 +55,14 @@ def get_boot_partition(): api.current_logger().warning( 'Could not get name of underlying /boot partition' ) - raise StopActorExecution() + raise StopActorExecution() # TODO: return some meaningful value/error except OSError: api.current_logger().warning( 'Could not get name of underlying /boot partition:' ' grub2-probe is missing.' ' Possibly called on system that does not use GRUB2?' ) - raise StopActorExecution() + raise StopActorExecution() # TODO: return some meaningful value/error boot_partition = result['stdout'].strip() api.current_logger().info('/boot is on {}'.format(boot_partition)) return boot_partition @@ -77,6 +77,7 @@ def get_grub_devices(): :return: Devices where GRUB is located :rtype: list """ + # TODO: catch errors and return meaningful value/error instead of StopActorExecution boot_device = get_boot_partition() devices = [] if mdraid.is_mdraid_dev(boot_device): From 0664b100f23fc47dd6d1047bb760b150a39ce335 Mon Sep 17 00:00:00 2001 From: Matej Matuska Date: Fri, 9 Aug 2024 11:54:50 +0200 Subject: [PATCH 2/2] userspacegen: Mock setup_target_rhui_access_if_needed as no-op The setup_target_rhui_access_if_needed() function is not covered by tests and makes tests fail outside of containers because it calls commands (api.run) which are not mocked. Jira: OAMG-11647 --- .../actors/targetuserspacecreator/libraries/userspacegen.py | 1 + .../tests/unit_test_targetuserspacecreator.py | 1 + 2 files changed, 2 insertions(+) diff --git a/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py b/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py index 7d9c4d6407..12753c72ee 100644 --- a/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py +++ b/repos/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py @@ -1266,6 +1266,7 @@ def perform(): target_iso = next(api.consume(TargetOSInstallationImage), None) with mounting.mount_upgrade_iso_to_root_dir(overlay.target, target_iso): + # TODO: this is out of tests completely setup_target_rhui_access_if_needed(context, indata) target_repoids = _gather_target_repositories(context, indata, prod_cert_path) diff --git a/repos/system_upgrade/common/actors/targetuserspacecreator/tests/unit_test_targetuserspacecreator.py b/repos/system_upgrade/common/actors/targetuserspacecreator/tests/unit_test_targetuserspacecreator.py index 826ae06d62..69ed704042 100644 --- a/repos/system_upgrade/common/actors/targetuserspacecreator/tests/unit_test_targetuserspacecreator.py +++ b/repos/system_upgrade/common/actors/targetuserspacecreator/tests/unit_test_targetuserspacecreator.py @@ -1213,6 +1213,7 @@ def test_perform_ok(monkeypatch): monkeypatch.setattr(overlaygen, 'create_source_overlay', MockedMountingBase) monkeypatch.setattr(userspacegen, '_gather_target_repositories', lambda *x: repoids) monkeypatch.setattr(userspacegen, '_create_target_userspace', lambda *x: None) + monkeypatch.setattr(userspacegen, 'setup_target_rhui_access_if_needed', lambda *x: None) monkeypatch.setattr(userspacegen.api, 'current_actor', CurrentActorMocked()) monkeypatch.setattr(userspacegen.api, 'produce', produce_mocked()) monkeypatch.setattr(repofileutils, 'get_repodirs', lambda: ['/etc/yum.repos.d'])