diff --git a/.pylintrc b/.pylintrc index a0ac67d..ad66e15 100644 --- a/.pylintrc +++ b/.pylintrc @@ -21,6 +21,7 @@ disable= line-too-long, too-many-arguments, unnecessary-lambda, + duplicate-code, [REPORTS] diff --git a/playbooks/leapp_preupgrade_ansible.yml b/playbooks/leapp_preupgrade_ansible.yml index 93770dc..51f3e8b 100644 --- a/playbooks/leapp_preupgrade_ansible.yml +++ b/playbooks/leapp_preupgrade_ansible.yml @@ -35,7 +35,7 @@ leapp_pkg: leapp-rhui-google - src_pkg: google-rhui-client-rhel8-sap leapp_pkg: leapp-rhui-google-sap - preupgrade_command: '/usr/bin/leapp preupgrade --report-schema=1.1.0' + preupgrade_command: '/usr/bin/leapp preupgrade --report-schema=1.2.0' no_rhsm: false insights_signature_exclude: /hosts,/vars/insights_signature insights_signature: !!binary | @@ -176,15 +176,19 @@ - name: Set inhibitor count ansible.builtin.set_fact: - inhibitor_count: "{{ report_content.entries | selectattr('flags', 'defined') | selectattr('flags', 'contains', 'inhibitor') | list | length }}" + inhibitor_count: "{{ report_content.entries | selectattr('groups', 'defined') | selectattr('groups', 'contains', 'inhibitor') | list | length }}" + + - name: Set errors count + ansible.builtin.set_fact: + error_count: "{{ report_content.entries | selectattr('groups', 'defined') | selectattr('groups', 'contains', 'error') | list | length }}" - name: Set result ansible.builtin.set_fact: task_results: report_json: "{{ report_content }}" report: "{{ report_content_txt_raw.content | b64decode }}" - message: "Your system has {{ inhibitor_count }} inhibitors out of {{report_content.entries | length}} potential problems." - alert: "{{ inhibitor_count | int > 0 }}" + message: "Your system has {{ error_count }} errors and {{ inhibitor_count }} inhibitors out of {{report_content.entries | length}} potential problems." + alert: "{{ (inhibitor_count | int > 0) or (error_count | int > 0) }}" - name: Start insights-client for immediate data collection of leapp-report ansible.builtin.shell: insights-client >/dev/null 2>&1 & diff --git a/playbooks/leapp_upgrade_ansible.yml b/playbooks/leapp_upgrade_ansible.yml index c7b2eb9..2475715 100644 --- a/playbooks/leapp_upgrade_ansible.yml +++ b/playbooks/leapp_upgrade_ansible.yml @@ -35,7 +35,7 @@ leapp_pkg: leapp-rhui-google - src_pkg: google-rhui-client-rhel8-sap leapp_pkg: leapp-rhui-google-sap - upgrade_command: '/usr/bin/leapp upgrade --report-schema=1.1.0' + upgrade_command: '/usr/bin/leapp upgrade --report-schema=1.2.0' no_rhsm: false is_leapp_upgrade_successful: false insights_signature_exclude: /hosts,/vars/insights_signature diff --git a/scripts/leapp_preupgrade.py b/scripts/leapp_preupgrade.py index 9efa908..f498bd0 100644 --- a/scripts/leapp_preupgrade.py +++ b/scripts/leapp_preupgrade.py @@ -321,16 +321,25 @@ def parse_results(output): with open(JSON_REPORT_PATH, mode="r") as handler: report_json = json.load(handler) - # NOTE: with newer schema we will need to parse groups instead of flags report_entries = report_json.get("entries", []) + error_count = len( + [entry for entry in report_entries if "error" in entry.get("groups")] + ) inhibitor_count = len( - [entry for entry in report_entries if "inhibitor" in entry.get("flags")] + [entry for entry in report_entries if "inhibitor" in entry.get("groups")] ) - message = "Your system has %s inhibitors out of %s potential problems." % ( - inhibitor_count, - len(report_entries), + message = ( + "Your system has %s error%s and %s inhibitor%s out of %s potential problem%s." + % ( + error_count, + "" if error_count == 1 else "s", + inhibitor_count, + "" if inhibitor_count == 1 else "s", + len(report_entries), + "" if len(report_entries) == 1 else "s", + ) ) - alert = inhibitor_count > 0 + alert = inhibitor_count > 0 or error_count > 0 status = ( _find_highest_report_level(report_entries) if len(report_entries) > 0 @@ -378,7 +387,7 @@ def main(): ) output = OutputCollector() - preupgrade_command = ["/usr/bin/leapp", "preupgrade", "--report-schema=1.1.0"] + preupgrade_command = ["/usr/bin/leapp", "preupgrade", "--report-schema=1.2.0"] rhui_pkgs = setup_leapp(version) # Check for RHUI PKGs diff --git a/scripts/leapp_upgrade.py b/scripts/leapp_upgrade.py index 26da83f..b9e80f7 100644 --- a/scripts/leapp_upgrade.py +++ b/scripts/leapp_upgrade.py @@ -326,18 +326,28 @@ def parse_results(output, reboot_required=False): with open(JSON_REPORT_PATH, mode="r") as handler: report_json = json.load(handler) - # NOTE: with newer schema we will need to parse groups instead of flags report_entries = report_json.get("entries", []) + + error_count = len( + [entry for entry in report_entries if "error" in entry.get("groups")] + ) inhibitor_count = len( - [entry for entry in report_entries if "inhibitor" in entry.get("flags")] + [entry for entry in report_entries if "inhibitor" in entry.get("groups")] ) - message = "Your system has %s inhibitors out of %s potential problems." % ( - inhibitor_count, - len(report_entries), + message = ( + "Your system has %s error%s and %s inhibitor%s out of %s potential problem%s." + % ( + error_count, + "" if error_count == 1 else "s", + inhibitor_count, + "" if inhibitor_count == 1 else "s", + len(report_entries), + "" if len(report_entries) == 1 else "s", + ) ) if reboot_required: message += " System is ready to be upgraded. Rebooting system in 1 minute." - alert = inhibitor_count > 0 + alert = inhibitor_count > 0 or error_count > 0 status = ( _find_highest_report_level(report_entries) if len(report_entries) > 0 @@ -390,7 +400,7 @@ def main(): ) output = OutputCollector() - upgrade_command = ["/usr/bin/leapp", "upgrade", "--report-schema=1.1.0"] + upgrade_command = ["/usr/bin/leapp", "upgrade", "--report-schema=1.2.0"] rhui_pkgs = setup_leapp(version) # Check for RHUI PKGs diff --git a/tests/preupgrade/test_parse_results.py b/tests/preupgrade/test_parse_results.py index a44be97..d21173c 100644 --- a/tests/preupgrade/test_parse_results.py +++ b/tests/preupgrade/test_parse_results.py @@ -10,7 +10,7 @@ @patch("scripts.leapp_preupgrade._find_highest_report_level", return_value="ERROR") def test_gather_report_files_exist(mock_find_level, mock_exists): test_txt_content = "Test data" - test_json_content = '{"test": "hi"}' + test_json_content = '{"entries": [{"groups": ["error"]}]}' output = OutputCollector() with patch("__builtin__.open") as mock_open_reports: return_values = [test_json_content, test_txt_content] @@ -19,13 +19,15 @@ def test_gather_report_files_exist(mock_find_level, mock_exists): )(file, mode) parse_results(output) - assert mock_find_level.call_count == 0 # entries do not exists -> [] - assert output.status == "SUCCESS" + assert mock_find_level.call_count == 1 # entries do not exists -> [] + assert output.status == "ERROR" assert mock_exists.call_count == 2 assert output.report == test_txt_content - assert output.report_json.get("test") == "hi" - # NOTE: is this right? - assert output.message == "Your system has 0 inhibitors out of 0 potential problems." + assert output.report_json.get("entries") is not None + assert ( + output.message + == "Your system has 1 error and 0 inhibitors out of 1 potential problem." + ) @patch("os.path.exists", return_value=False) diff --git a/tests/upgrade/test_parse_results.py b/tests/upgrade/test_parse_results.py index 07d2d3b..d9c790a 100644 --- a/tests/upgrade/test_parse_results.py +++ b/tests/upgrade/test_parse_results.py @@ -10,7 +10,7 @@ @patch("scripts.leapp_upgrade._find_highest_report_level", return_value="ERROR") def test_gather_report_files_exist(mock_find_level, mock_exists): test_txt_content = "Test data" - test_json_content = '{"test": "hi"}' + test_json_content = '{"entries": [{"groups": ["error"]}]}' output = OutputCollector() with patch("__builtin__.open") as mock_open_reports: return_values = [test_json_content, test_txt_content] @@ -19,13 +19,15 @@ def test_gather_report_files_exist(mock_find_level, mock_exists): )(file, mode) parse_results(output) - assert mock_find_level.call_count == 0 # entries do not exists -> [] - assert output.status == "SUCCESS" + assert mock_find_level.call_count == 1 # entries do not exists -> [] + assert output.status == "ERROR" assert mock_exists.call_count == 2 assert output.report == test_txt_content - assert output.report_json.get("test") == "hi" - # NOTE: is this right? - assert output.message == "Your system has 0 inhibitors out of 0 potential problems." + assert output.report_json.get("entries") is not None + assert ( + output.message + == "Your system has 1 error and 0 inhibitors out of 1 potential problem." + ) @patch("os.path.exists", return_value=True) @@ -49,7 +51,7 @@ def test_gather_report_files_exist_with_reboot(mock_find_level, mock_exists): assert output.report_json.get("test") == "hi" assert ( output.message - == "Your system has 0 inhibitors out of 0 potential problems. System is ready to be upgraded. Rebooting system in 1 minute." + == "Your system has 0 errors and 0 inhibitors out of 0 potential problems. System is ready to be upgraded. Rebooting system in 1 minute." )