Skip to content

Commit

Permalink
Merge pull request #9 from oamg/feat/use-1.2.0-report-schema
Browse files Browse the repository at this point in the history
Use --report-schema=1.2.0 in all tasks
  • Loading branch information
andywaltlova authored Jan 22, 2024
2 parents 88d5e68 + 90a4024 commit a9259ac
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 32 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ disable=
line-too-long,
too-many-arguments,
unnecessary-lambda,
duplicate-code,

[REPORTS]

Expand Down
12 changes: 8 additions & 4 deletions playbooks/leapp_preupgrade_ansible.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 &
Expand Down
2 changes: 1 addition & 1 deletion playbooks/leapp_upgrade_ansible.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions scripts/leapp_preupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 17 additions & 7 deletions scripts/leapp_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions tests/preupgrade/test_parse_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions tests/upgrade/test_parse_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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."
)


Expand Down

0 comments on commit a9259ac

Please sign in to comment.