diff --git a/convert2rhel/unit_tests/main_test.py b/convert2rhel/unit_tests/main_test.py index 1bb45f215a..5bd6973606 100644 --- a/convert2rhel/unit_tests/main_test.py +++ b/convert2rhel/unit_tests/main_test.py @@ -392,3 +392,142 @@ def test_main(monkeypatch): assert restart_system_mock.call_count == 1 assert finish_collection_mock.call_count == 1 assert update_rhsm_custom_facts_mock.call_count == 1 + + +def test_main_rollback_post_cli_phase(monkeypatch, caplog): + require_root_mock = mock.Mock() + initialize_logger_mock = mock.Mock() + toolopts_cli_mock = mock.Mock() + show_eula_mock = mock.Mock(side_effect=Exception) + + # Mock the rollback calls + finish_collection_mock = mock.Mock() + + monkeypatch.setattr(utils, "require_root", require_root_mock) + monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock) + monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock) + monkeypatch.setattr(main, "show_eula", show_eula_mock) + monkeypatch.setattr(breadcrumbs, "finish_collection", finish_collection_mock) + + assert main.main() == 1 + assert require_root_mock.call_count == 1 + assert initialize_logger_mock.call_count == 1 + assert toolopts_cli_mock.call_count == 1 + assert show_eula_mock.call_count == 1 + assert finish_collection_mock.call_count == 1 + assert "No changes were made to the system." in caplog.records[-1].message + + +def test_main_rollback_pre_ponr_changes_phase(monkeypatch): + require_root_mock = mock.Mock() + initialize_logger_mock = mock.Mock() + toolopts_cli_mock = mock.Mock() + show_eula_mock = mock.Mock() + resolve_system_info_mock = mock.Mock() + collect_early_data_mock = mock.Mock() + clean_yum_metadata_mock = mock.Mock() + perform_pre_checks_mock = mock.Mock() + system_release_file_mock = mock.Mock() + os_release_file_mock = mock.Mock() + backup_yum_repos_mock = mock.Mock() + clear_versionlock_mock = mock.Mock() + pre_ponr_conversion_mock = mock.Mock(side_effect=Exception) + + # Mock the rollback calls + finish_collection_mock = mock.Mock() + rollback_changes_mock = mock.Mock() + + monkeypatch.setattr(utils, "require_root", require_root_mock) + monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock) + monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock) + monkeypatch.setattr(main, "show_eula", show_eula_mock) + monkeypatch.setattr(system_info, "resolve_system_info", resolve_system_info_mock) + monkeypatch.setattr(breadcrumbs, "collect_early_data", collect_early_data_mock) + monkeypatch.setattr(pkghandler, "clear_versionlock", clear_versionlock_mock) + monkeypatch.setattr(pkghandler, "clean_yum_metadata", clean_yum_metadata_mock) + monkeypatch.setattr(checks, "perform_pre_checks", perform_pre_checks_mock) + monkeypatch.setattr(system_release_file, "backup", system_release_file_mock) + monkeypatch.setattr(os_release_file, "backup", os_release_file_mock) + monkeypatch.setattr(repo, "backup_yum_repos", backup_yum_repos_mock) + monkeypatch.setattr(main, "pre_ponr_conversion", pre_ponr_conversion_mock) + monkeypatch.setattr(breadcrumbs, "finish_collection", finish_collection_mock) + monkeypatch.setattr(main, "rollback_changes", rollback_changes_mock) + + assert main.main() == 1 + assert require_root_mock.call_count == 1 + assert initialize_logger_mock.call_count == 1 + assert toolopts_cli_mock.call_count == 1 + assert show_eula_mock.call_count == 1 + assert resolve_system_info_mock.call_count == 1 + assert collect_early_data_mock.call_count == 1 + assert clean_yum_metadata_mock.call_count == 1 + assert perform_pre_checks_mock.call_count == 1 + assert system_release_file_mock.call_count == 1 + assert os_release_file_mock.call_count == 1 + assert backup_yum_repos_mock.call_count == 1 + assert clear_versionlock_mock.call_count == 1 + assert pre_ponr_conversion_mock.call_count == 1 + assert finish_collection_mock.call_count == 1 + assert rollback_changes_mock.call_count == 1 + + +def test_main_rollback_post_ponr_changes_phase(monkeypatch, caplog): + require_root_mock = mock.Mock() + initialize_logger_mock = mock.Mock() + toolopts_cli_mock = mock.Mock() + show_eula_mock = mock.Mock() + resolve_system_info_mock = mock.Mock() + collect_early_data_mock = mock.Mock() + clean_yum_metadata_mock = mock.Mock() + perform_pre_checks_mock = mock.Mock() + system_release_file_mock = mock.Mock() + os_release_file_mock = mock.Mock() + backup_yum_repos_mock = mock.Mock() + clear_versionlock_mock = mock.Mock() + pre_ponr_conversion_mock = mock.Mock() + ask_to_continue_mock = mock.Mock() + post_ponr_conversion_mock = mock.Mock(side_effect=Exception) + + # Mock the rollback calls + finish_collection_mock = mock.Mock() + update_rhsm_custom_facts_mock = mock.Mock() + + monkeypatch.setattr(utils, "require_root", require_root_mock) + monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock) + monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock) + monkeypatch.setattr(main, "show_eula", show_eula_mock) + monkeypatch.setattr(system_info, "resolve_system_info", resolve_system_info_mock) + monkeypatch.setattr(breadcrumbs, "collect_early_data", collect_early_data_mock) + monkeypatch.setattr(pkghandler, "clear_versionlock", clear_versionlock_mock) + monkeypatch.setattr(pkghandler, "clean_yum_metadata", clean_yum_metadata_mock) + monkeypatch.setattr(checks, "perform_pre_checks", perform_pre_checks_mock) + monkeypatch.setattr(system_release_file, "backup", system_release_file_mock) + monkeypatch.setattr(os_release_file, "backup", os_release_file_mock) + monkeypatch.setattr(repo, "backup_yum_repos", backup_yum_repos_mock) + monkeypatch.setattr(main, "pre_ponr_conversion", pre_ponr_conversion_mock) + monkeypatch.setattr(utils, "ask_to_continue", ask_to_continue_mock) + monkeypatch.setattr(main, "post_ponr_conversion", post_ponr_conversion_mock) + monkeypatch.setattr(breadcrumbs, "finish_collection", finish_collection_mock) + monkeypatch.setattr(subscription, "update_rhsm_custom_facts", update_rhsm_custom_facts_mock) + + assert main.main() == 1 + assert require_root_mock.call_count == 1 + assert initialize_logger_mock.call_count == 1 + assert toolopts_cli_mock.call_count == 1 + assert show_eula_mock.call_count == 1 + assert resolve_system_info_mock.call_count == 1 + assert collect_early_data_mock.call_count == 1 + assert clean_yum_metadata_mock.call_count == 1 + assert perform_pre_checks_mock.call_count == 1 + assert system_release_file_mock.call_count == 1 + assert os_release_file_mock.call_count == 1 + assert backup_yum_repos_mock.call_count == 1 + assert clear_versionlock_mock.call_count == 1 + assert pre_ponr_conversion_mock.call_count == 1 + assert ask_to_continue_mock.call_count == 1 + assert post_ponr_conversion_mock.call_count == 1 + assert finish_collection_mock.call_count == 1 + assert ( + "Conversion process interrupted and manual user intervention will be necessary." in caplog.records[-1].message + ) + assert update_rhsm_custom_facts_mock.call_count == 1