Skip to content

Commit

Permalink
[RHELC-1736] Fix different exit codes
Browse files Browse the repository at this point in the history
The convert2rhel finished with different exit codes when inhibitor was
found and different subcommands were used.

In the code was missing part for handling the inhibitors during analysis
exit.
  • Loading branch information
hosekadam committed Oct 14, 2024
1 parent ad44985 commit 58a8f3c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion convert2rhel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def main_locked():
if backup.backup_control.rollback_failed:
return ConversionExitCodes.FAILURE

if _get_failed_actions(pre_conversion_results):
return ConversionExitCodes.INHIBITORS_FOUND

return ConversionExitCodes.SUCCESSFUL
except _InhibitorsFound as err:
loggerinst.critical_no_exit(str(err))
Expand Down Expand Up @@ -229,6 +232,10 @@ def main_locked():
return ConversionExitCodes.SUCCESSFUL


def _get_failed_actions(results):
return actions.find_actions_of_severity(results, "SKIP", level_for_raw_action_data)


def _raise_for_skipped_failures(results):
"""Analyze the action results for failures
Expand All @@ -237,7 +244,7 @@ def _raise_for_skipped_failures(results):
:raises SystemExit: In case we detect any actions that has level of `SKIP`
or above.
"""
failures = actions.find_actions_of_severity(results, "SKIP", level_for_raw_action_data)
failures = _get_failed_actions(results)
if failures:
# The report will be handled in the error handler, after rollback.
message = (
Expand Down
44 changes: 44 additions & 0 deletions convert2rhel/unit_tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def test_main_rollback_analyze_exit_phase_without_subman(self, global_tool_opts,
(main, "rollback_changes", mock.Mock()),
(report, "summary_as_json", mock.Mock()),
(report, "summary_as_txt", mock.Mock()),
(actions, "find_actions_of_severity", mock.Mock(return_value=[])),
)
global_tool_opts.activity = "analysis"
for module, function, value in mocks:
Expand Down Expand Up @@ -555,6 +556,7 @@ def test_main_rollback_analyze_exit_phase(self, global_tool_opts, monkeypatch, t
clear_versionlock_mock = mock.Mock()
summary_as_json_mock = mock.Mock()
summary_as_txt_mock = mock.Mock()
find_actions_of_severity = mock.Mock(return_value=[])

# Mock the rollback calls
finish_collection_mock = mock.Mock()
Expand All @@ -581,6 +583,7 @@ def test_main_rollback_analyze_exit_phase(self, global_tool_opts, monkeypatch, t
monkeypatch.setattr(main, "rollback_changes", rollback_changes_mock)
monkeypatch.setattr(report, "summary_as_json", summary_as_json_mock)
monkeypatch.setattr(report, "summary_as_txt", summary_as_txt_mock)
monkeypatch.setattr(actions, "find_actions_of_severity", find_actions_of_severity)
global_tool_opts.activity = "analysis"

assert main.main() == 0
Expand Down Expand Up @@ -668,6 +671,47 @@ def test_main_rollback_post_ponr_changes_phase(self, monkeypatch, caplog, tmp_pa
assert "The system is left in an undetermined state that Convert2RHEL cannot fix." in caplog.records[-3].message
assert update_rhsm_custom_facts_mock.call_count == 1

@pytest.mark.parametrize(
("activity", "inhibitor", "rc"),
(
("analysis", True, 2),
("analysis", False, 0),
("convert", True, 2),
("convert", False, 0),
),
)
def test_main_inhibitor_return_code(self, monkeypatch, activity, inhibitor, rc, tmp_path, global_tool_opts):
mocks = (
(applock, "_DEFAULT_LOCK_DIR", str(tmp_path)),
(utils, "require_root", mock.Mock()),
(main, "initialize_file_logging", mock.Mock()),
(cli, "CLI", mock.Mock()),
(main, "show_eula", mock.Mock()),
(breadcrumbs, "print_data_collection", mock.Mock()),
(system_info, "resolve_system_info", mock.Mock()),
(system_info, "print_system_information", mock.Mock()),
(breadcrumbs, "collect_early_data", mock.Mock()),
(pkghandler, "clear_versionlock", mock.Mock()),
(pkgmanager, "clean_yum_metadata", mock.Mock()),
(actions, "run_pre_actions", mock.Mock()),
(actions, "find_actions_of_severity", mock.Mock(return_value=inhibitor)),
(report, "_summary", mock.Mock()),
(breadcrumbs, "finish_collection", mock.Mock()),
(subscription, "should_subscribe", mock.Mock(side_effect=lambda: True)),
(subscription, "update_rhsm_custom_facts", mock.Mock()),
(main, "rollback_changes", mock.Mock()),
(report, "summary_as_json", mock.Mock()),
(report, "summary_as_txt", mock.Mock()),
(utils, "ask_to_continue", mock.Mock()),
(actions, "run_post_actions", mock.Mock()),
(utils, "restart_system", mock.Mock()),
)
global_tool_opts.activity = activity
for module, function, value in mocks:
monkeypatch.setattr(module, function, value)

assert main.main() == rc


@pytest.mark.parametrize(
("data", "exception", "match", "activity"),
Expand Down

0 comments on commit 58a8f3c

Please sign in to comment.