From 1dc9922f71f2dd0ee4f33a334aed055e199d9421 Mon Sep 17 00:00:00 2001 From: Inessa Vasilevskaya Date: Mon, 4 Dec 2023 11:21:37 +0100 Subject: [PATCH] POC of check custom modifications This commit introduces an actor that processes the CustomModification messages and produces report entries. Some basic unit tests added as well. RHEL-1774 --- .../actors/checkcustommodifications/actor.py | 19 ++++++++++++ .../libraries/checkcustommodifications.py | 31 +++++++++++++++++++ .../tests/test_checkcustommodifications.py | 26 ++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 repos/system_upgrade/common/actors/checkcustommodifications/actor.py create mode 100644 repos/system_upgrade/common/actors/checkcustommodifications/libraries/checkcustommodifications.py create mode 100644 repos/system_upgrade/common/actors/checkcustommodifications/tests/test_checkcustommodifications.py diff --git a/repos/system_upgrade/common/actors/checkcustommodifications/actor.py b/repos/system_upgrade/common/actors/checkcustommodifications/actor.py new file mode 100644 index 0000000000..a1a50badc5 --- /dev/null +++ b/repos/system_upgrade/common/actors/checkcustommodifications/actor.py @@ -0,0 +1,19 @@ +from leapp.actors import Actor +from leapp.libraries.actor import checkcustommodifications +from leapp.models import CustomModifications, Report +from leapp.tags import ChecksPhaseTag, IPUWorkflowTag + + +class CheckCustomModificationsActor(Actor): + """ + Checks CustomModifications messages and produces a report about files in leapp directories that have been + modified or newly added. + """ + + name = 'check_custom_modifications_actor' + consumes = (CustomModifications,) + produces = (Report,) + tags = (IPUWorkflowTag, ChecksPhaseTag) + + def process(self): + checkcustommodifications.report_any_modifications() diff --git a/repos/system_upgrade/common/actors/checkcustommodifications/libraries/checkcustommodifications.py b/repos/system_upgrade/common/actors/checkcustommodifications/libraries/checkcustommodifications.py new file mode 100644 index 0000000000..d6950bef3f --- /dev/null +++ b/repos/system_upgrade/common/actors/checkcustommodifications/libraries/checkcustommodifications.py @@ -0,0 +1,31 @@ +from leapp import reporting +from leapp.libraries.stdlib import api +from leapp.models import CustomModifications + + +def _create_report(msgs): + if not msgs: + # Nothing to report + return + report_type = msgs[0].type + discovered_files = '\n'.join( + ['- {filename}{actor}'.format(filename=m.filename, + actor=' ({} Actor)'.format(m.actor_name) if m.actor_name else '') + for m in msgs]) + title = '{report_type} files were discovered on your system in leapp directories' + summary = ('Apparently some {report_type} files have been found in leapp installation directories.\n' + 'Please consult the list of discovered files for more information:\n' + '{discovered_files}') + report_parts = [ + reporting.Title(title.format(report_type=report_type.capitalize())), + reporting.Summary(summary.format(report_type=report_type, discovered_files=discovered_files)), + reporting.Severity(reporting.Severity.HIGH), + reporting.Groups([reporting.Groups.REPOSITORY]), + ] + reporting.create_report(report_parts) + + +def report_any_modifications(): + modifications = list(api.consume(CustomModifications)) + _create_report([m for m in modifications if m.type == 'custom']) + _create_report([m for m in modifications if m.type == 'modified']) diff --git a/repos/system_upgrade/common/actors/checkcustommodifications/tests/test_checkcustommodifications.py b/repos/system_upgrade/common/actors/checkcustommodifications/tests/test_checkcustommodifications.py new file mode 100644 index 0000000000..70c2ffe554 --- /dev/null +++ b/repos/system_upgrade/common/actors/checkcustommodifications/tests/test_checkcustommodifications.py @@ -0,0 +1,26 @@ +from leapp.libraries.actor import checkcustommodifications +from leapp.models import CustomModifications, Report + + +def test_report_any_modifications(current_actor_context): + discovered_msgs = [CustomModifications(filename='some/changed/leapp/actor/file', + type='modified', + actor_name='an_actor'), + CustomModifications(filename='some/new/actor/in/leapp/dir', + type='custom', + actor_name='a_new_actor'), + CustomModifications(filename='some/new/file/unrelated/to/any/actor', type='custom', + actor_name='')] + for msg in discovered_msgs: + current_actor_context.feed(msg) + current_actor_context.run() + reports = current_actor_context.consume(Report) + custom_files_report = next((r.report for r in reports if 'Custom' in r.report['title']), None) + modified_files_report = next((r.report for r in reports if 'Modified' in r.report['title']), None) + assert (custom_files_report['title'] == + 'Custom files were discovered on your system in leapp directories') + assert 'some/new/actor/in/leapp/dir (a_new_actor Actor)' in custom_files_report['summary'] + assert 'some/new/file/unrelated/to/any/actor' in custom_files_report['summary'] + assert (modified_files_report['title'] == + 'Modified files were discovered on your system in leapp directories') + assert 'some/changed/leapp/actor/file (an_actor Actor)' in modified_files_report['summary']