Skip to content

Commit

Permalink
Add remaining fields to the conversion schema
Browse files Browse the repository at this point in the history
Signed-off-by: Rodolfo Olivieri <[email protected]>
  • Loading branch information
r0x0d committed Nov 13, 2023
1 parent ddf8a15 commit 53b0e76
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
7 changes: 7 additions & 0 deletions schemas/conversion_schema_1.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"status": {
"$ref": "#/$defs/status_codes"
},
"alert": {
"type": "boolean"
},
"error": {
"type": "boolean"
},
"report": {
"type": "string"
},
Expand Down Expand Up @@ -41,6 +47,7 @@
},
"required": [
"status",
"alert",
"message",
"report",
"report_json"
Expand Down
22 changes: 19 additions & 3 deletions scripts/conversion_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ def __init__(self, message, report):
class OutputCollector(object):
"""Wrapper class for script expected stdout"""

def __init__(self, status="", message="", report="", entries=None):
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-arguments
# Eight and five is reasonable in this case.

def __init__(
self, status="", message="", report="", entries=None, alert=False, error=False
):
self.status = status
self.alert = alert # true if error true or if conversion inhibited
self.error = error # true if the script wasn't able to finish, otherwise false
self.message = message
self.report = report
self.tasks_format_version = "1.0"
Expand All @@ -67,6 +75,8 @@ def to_dict(self):

return {
"status": self.status,
"alert": self.alert,
"error": self.error,
"message": self.message,
"report": self.report,
"report_json": self.report_json,
Expand Down Expand Up @@ -143,14 +153,16 @@ def gather_textual_report():
def generate_report_message(highest_status):
"""Generate a report message based on the status severity."""
message = ""
alert = False

if STATUS_CODE[highest_status] <= STATUS_CODE["WARNING"]:
message = "No problems found. The system was converted successfully."

if STATUS_CODE[highest_status] > STATUS_CODE["WARNING"]:
message = "The conversion cannot proceed. You must resolve existing issues to perform the conversion."
alert = True

return message
return message, alert


def setup_convert2rhel(required_files):
Expand Down Expand Up @@ -445,21 +457,25 @@ def main():

# Generate report message and transform the raw data into entries for
# Insights.
output.message = generate_report_message(highest_level)
output.message, output.alert = generate_report_message(highest_level)
output.entries = transform_raw_data(data)
update_insights_inventory()
print("Conversion script finish successfully!")
except ProcessError as exception:
print(exception.report)
output = OutputCollector(
status="ERROR",
alert=True,
error=True,
message=exception.message,
report=exception.report,
)
except Exception as exception:
print(str(exception))
output = OutputCollector(
status="ERROR",
alert=True,
error=True,
message="An unexpected error occurred. Expand the row for more details.",
report=str(exception),
)
Expand Down
13 changes: 7 additions & 6 deletions tests/conversion_script/test_generate_report_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@


@pytest.mark.parametrize(
("highest_status", "expected_message"),
("highest_status", "expected_message", "has_alert"),
[
("SUCCESS", "No problems found. The system was converted successfully."),
("INFO", "No problems found. The system was converted successfully."),
("WARNING", "No problems found. The system was converted successfully."),
("SUCCESS", "No problems found. The system was converted successfully.", False),
("INFO", "No problems found. The system was converted successfully.", False),
("WARNING", "No problems found. The system was converted successfully.", False),
(
"ERROR",
"The conversion cannot proceed. You must resolve existing issues to perform the conversion.",
True,
),
],
)
def test_generate_report_message(highest_status, expected_message):
assert generate_report_message(highest_status) == expected_message
def test_generate_report_message(highest_status, expected_message, has_alert):
assert generate_report_message(highest_status) == (expected_message, has_alert)
6 changes: 3 additions & 3 deletions tests/conversion_script/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@patch("scripts.conversion_script.run_convert2rhel", side_effect=Mock())
@patch("scripts.conversion_script.find_highest_report_level", side_effect=Mock(return_value=["SUCCESS"]))
@patch("scripts.conversion_script.gather_textual_report", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.generate_report_message", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.generate_report_message", side_effect=Mock(return_value=("", False)))
@patch("scripts.conversion_script.transform_raw_data", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.cleanup", side_effect=Mock())
# fmt: on
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_main_success(
@patch("scripts.conversion_script.run_convert2rhel", side_effect=ProcessError("test", "Process error"))
@patch("scripts.conversion_script.find_highest_report_level", side_effect=Mock(return_value=["SUCCESS"]))
@patch("scripts.conversion_script.gather_textual_report", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.generate_report_message", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.generate_report_message", side_effect=Mock(return_value=("", False)))
@patch("scripts.conversion_script.cleanup", side_effect=Mock())
# fmt: on
def test_main_process_error(
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_main_process_error(
@patch("scripts.conversion_script.run_convert2rhel", side_effect=Mock())
@patch("scripts.conversion_script.find_highest_report_level", side_effect=Mock(return_value=["SUCCESS"]))
@patch("scripts.conversion_script.gather_textual_report", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.generate_report_message", side_effect=Mock(return_value=""))
@patch("scripts.conversion_script.generate_report_message", side_effect=Mock(return_value=("", False)))
@patch("scripts.conversion_script.cleanup", side_effect=Mock())
# fmt: on
def test_main_general_exception(
Expand Down
4 changes: 2 additions & 2 deletions tests/conversion_script/test_output_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_output_schema():
output_collector = OutputCollector(status=status)
empty_output = output_collector.to_dict()

with open("schemas/preconversion_assessment_schema_1.0.json", "r") as schema:
with open("schemas/conversion_schema_1.0.json", "r") as schema:
schema_json = json.load(schema)
# If some difference between generated json and its schema invoke exception
jsonschema.validate(instance=empty_output, schema=schema_json)
Expand All @@ -26,7 +26,7 @@ def test_output_schema_entries_report():
output_collector.entries = {"hi": "world"}
full_output = output_collector.to_dict()

with open("schemas/preconversion_assessment_schema_1.0.json", "r") as schema:
with open("schemas/conversion_schema_1.0.json", "r") as schema:
schema_json = json.load(schema)
# If some difference between generated json and its schema invoke exception
jsonschema.validate(instance=full_output, schema=schema_json)

0 comments on commit 53b0e76

Please sign in to comment.