From 130a56b71e994ebf1fb17e956aa6e2f37f907774 Mon Sep 17 00:00:00 2001 From: Michal Hecko Date: Sun, 3 Nov 2024 16:51:17 +0100 Subject: [PATCH] pes_events_scanner: ensure user's transaction cfg is reflected Previously, pes_events_scanner used transaction configuration to only modify the way it initializes event application. As a consequence, if a user specified to_remove=['pkg'], then the information would not make it to pes_events_scanner's output. This patch adds a post-processing to explicitly add transaction configuration to the result of applying PES events. --- .../libraries/pes_events_scanner.py | 41 +++++++++++++++---- .../tests/test_pes_event_scanner.py | 29 +++++++------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/repos/system_upgrade/common/actors/peseventsscanner/libraries/pes_events_scanner.py b/repos/system_upgrade/common/actors/peseventsscanner/libraries/pes_events_scanner.py index a798017fcb..56991ce9f7 100644 --- a/repos/system_upgrade/common/actors/peseventsscanner/libraries/pes_events_scanner.py +++ b/repos/system_upgrade/common/actors/peseventsscanner/libraries/pes_events_scanner.py @@ -470,9 +470,8 @@ def replace_pesids_with_repoids_in_packages(packages, source_pkgs_repoids): return packages_with_repoid.union(packages_without_pesid) -def apply_transaction_configuration(source_pkgs): +def apply_transaction_configuration(source_pkgs, transaction_configuration): source_pkgs_with_conf_applied = set(source_pkgs) - transaction_configuration = get_transaction_configuration() source_pkgs_with_conf_applied = source_pkgs.union(transaction_configuration.to_install) @@ -504,6 +503,28 @@ def remove_leapp_related_events(events): return res +def include_to_remove_from_transaction_configuration(rpm_tasks, transaction_configuration, installed_pkgs): + """ Extend rpm_tasks.to_remove with packages from transaction_configuration.to_remove """ + # We don't want to try removing packages that are not installed + installed_pkgs_requested_to_be_removed = transaction_configuration.to_remove.intersection(installed_pkgs) + pkgs_names_to_extend_to_remove_with = [pkg.name for pkg in installed_pkgs_requested_to_be_removed] + + if not installed_pkgs_requested_to_be_removed: + return rpm_tasks + + if not rpm_tasks: + return PESRpmTransactionTasks(to_install=[], + to_remove=sorted(pkgs_names_to_extend_to_remove_with), + modules_to_enable=[], + modules_to_reset=[]) + + new_to_remove_list = sorted(set(rpm_tasks.to_remove).union(pkgs_names_to_extend_to_remove_with)) + return PESRpmTransactionTasks(to_install=rpm_tasks.to_install, + to_remove=new_to_remove_list, + modules_to_enable=rpm_tasks.modules_to_enable, + modules_to_reset=rpm_tasks.modules_to_reset) + + def process(): # Retrieve data - installed_pkgs, transaction configuration, pes events events = get_pes_events('/etc/leapp/files', 'pes-events.json') @@ -511,24 +532,27 @@ def process(): return releases = get_relevant_releases(events) - source_pkgs = get_installed_pkgs() - source_pkgs = apply_transaction_configuration(source_pkgs) + installed_pkgs = get_installed_pkgs() + transaction_configuration = get_transaction_configuration() + pkgs_to_begin_computation_with = apply_transaction_configuration(installed_pkgs, transaction_configuration) # Keep track of what repoids have the source packages to be able to determine what are the PESIDs of the computed # packages of the target system, so we can distinguish what needs to be repomapped - repoids_of_source_pkgs = {pkg.repository for pkg in source_pkgs} + repoids_of_source_pkgs = {pkg.repository for pkg in pkgs_to_begin_computation_with} events = remove_leapp_related_events(events) events = remove_undesired_events(events, releases) # Apply events - compute what packages should the target system have - target_pkgs, pkgs_to_demodularize = compute_packages_on_target_system(source_pkgs, events, releases) + target_pkgs, pkgs_to_demodularize = compute_packages_on_target_system(pkgs_to_begin_computation_with, + events, releases) # Packages coming out of the events have PESID as their repository, however, we need real repoid target_pkgs = replace_pesids_with_repoids_in_packages(target_pkgs, repoids_of_source_pkgs) # Apply the desired repository blacklisting - blacklisted_repoids, target_pkgs = remove_new_packages_from_blacklisted_repos(source_pkgs, target_pkgs) + blacklisted_repoids, target_pkgs = remove_new_packages_from_blacklisted_repos(pkgs_to_begin_computation_with, + target_pkgs) # Look at the target packages and determine what repositories to enable target_repoids = sorted(set(p.repository for p in target_pkgs) - blacklisted_repoids - repoids_of_source_pkgs) @@ -536,6 +560,7 @@ def process(): api.produce(repos_to_enable) # Compare the packages on source system and the computed packages on target system and determine what to install - rpm_tasks = compute_rpm_tasks_from_pkg_set_diff(source_pkgs, target_pkgs, pkgs_to_demodularize) + rpm_tasks = compute_rpm_tasks_from_pkg_set_diff(pkgs_to_begin_computation_with, target_pkgs, pkgs_to_demodularize) + rpm_tasks = include_to_remove_from_transaction_configuration(rpm_tasks, transaction_configuration, installed_pkgs) if rpm_tasks: api.produce(rpm_tasks) diff --git a/repos/system_upgrade/common/actors/peseventsscanner/tests/test_pes_event_scanner.py b/repos/system_upgrade/common/actors/peseventsscanner/tests/test_pes_event_scanner.py index b725e5cf8b..9f631824b6 100644 --- a/repos/system_upgrade/common/actors/peseventsscanner/tests/test_pes_event_scanner.py +++ b/repos/system_upgrade/common/actors/peseventsscanner/tests/test_pes_event_scanner.py @@ -284,17 +284,14 @@ def test_actor_performs(monkeypatch): def test_transaction_configuration_has_effect(monkeypatch): _Pkg = partial(Package, repository=None, modulestream=None) - def mocked_transaction_conf(): - return TransactionConfiguration( - to_install=[_Pkg('pkg-a'), _Pkg('pkg-b')], - to_remove=[_Pkg('pkg-c'), _Pkg('pkg-d')], - to_keep=[] - ) - - monkeypatch.setattr(pes_events_scanner, 'get_transaction_configuration', mocked_transaction_conf) + transaction_cfg = TransactionConfiguration( + to_install=[_Pkg('pkg-a'), _Pkg('pkg-b')], + to_remove=[_Pkg('pkg-c'), _Pkg('pkg-d')], + to_keep=[] + ) packages = {_Pkg('pkg-a'), _Pkg('pkg-c')} - _result = pes_events_scanner.apply_transaction_configuration(packages) + _result = pes_events_scanner.apply_transaction_configuration(packages, transaction_cfg) result = {(p.name, p.repository, p.modulestream) for p in _result} expected = {('pkg-a', None, None), ('pkg-b', None, None)} @@ -338,7 +335,7 @@ def test_blacklisted_repoid_is_not_produced(monkeypatch): monkeypatch.setattr(pes_events_scanner, 'get_installed_pkgs', lambda: installed_pkgs) monkeypatch.setattr(pes_events_scanner, 'get_pes_events', lambda folder, filename: events) - monkeypatch.setattr(pes_events_scanner, 'apply_transaction_configuration', lambda pkgs: pkgs) + monkeypatch.setattr(pes_events_scanner, 'apply_transaction_configuration', lambda pkgs, transaction_cfg: pkgs) monkeypatch.setattr(pes_events_scanner, 'get_blacklisted_repoids', lambda: {'blacklisted-rhel8'}) monkeypatch.setattr(pes_events_scanner, 'replace_pesids_with_repoids_in_packages', lambda pkgs, src_pkgs_repoids: pkgs) @@ -479,6 +476,7 @@ def test_transaction_configuration_is_applied(monkeypatch): installed_pkgs = { Package(name='moved-in', repository='rhel7-base', modulestream=None), Package(name='split-in', repository='rhel7-base', modulestream=None), + Package(name='pkg-not-in-events', repository='rhel7-base', modulestream=None), } monkeypatch.setattr(pes_events_scanner, 'get_installed_pkgs', lambda *args, **kwags: installed_pkgs) @@ -503,8 +501,10 @@ def test_transaction_configuration_is_applied(monkeypatch): lambda source_pkgs, target_pkgs: (set(), target_pkgs)) msgs = [ - RpmTransactionTasks(to_remove=['split-in', 'split-in']), - RpmTransactionTasks(to_remove=['split-in']) + RpmTransactionTasks(to_remove=['pkg-not-in-events']), + RpmTransactionTasks(to_remove=['pkg-not-in-events', 'pkg-not-in-events']), + RpmTransactionTasks(to_install=['pkg-to-install']), + RpmTransactionTasks(to_keep=['keep-me']), ] mocked_actor = CurrentActorMocked(arch='x86_64', src_ver='7.9', dst_ver='8.8', msgs=msgs) monkeypatch.setattr(api, 'current_actor', mocked_actor) @@ -521,4 +521,7 @@ def test_transaction_configuration_is_applied(monkeypatch): assert len(produced_rpm_transaction_tasks) == 1 rpm_transaction_tasks = produced_rpm_transaction_tasks[0] - assert sorted(rpm_transaction_tasks.to_remove) == ['moved-in', 'split-in'] + # It is important to see 'pkg-not-in-events' in the list - if the user says remove pkg A, we really remove it + assert sorted(rpm_transaction_tasks.to_remove) == ['moved-in', 'pkg-not-in-events', 'split-in'] + assert sorted(rpm_transaction_tasks.to_install) == ['moved-out', 'pkg-to-install', 'split-out0', 'split-out1'] + assert sorted(rpm_transaction_tasks.to_keep) == ['keep-me']