From 040f4b626f6be23290a2f1c4d6e44810c2971dbb Mon Sep 17 00:00:00 2001 From: john feng Date: Wed, 18 Sep 2024 13:19:14 -0700 Subject: [PATCH 01/13] add ut for add_error_to_status --- src/core/tests/Test_PatchAssessor.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/core/tests/Test_PatchAssessor.py b/src/core/tests/Test_PatchAssessor.py index 40f1c22d..9c10f891 100644 --- a/src/core/tests/Test_PatchAssessor.py +++ b/src/core/tests/Test_PatchAssessor.py @@ -181,9 +181,19 @@ def test_raise_if_min_python_version_not_met(self): self.runtime.patch_assessor.start_assessment() self.assertEqual(str(context.exception), Constants.PYTHON_NOT_COMPATIBLE_ERROR_MSG.format(sys.version_info)) + + def test_raise_add_error_to_status(self): + self.runtime.package_manager.get_all_updates = lambda: self.raise_ex() + + with self.assertRaises(Exception) as context: + self.runtime.patch_assessor.start_assessment() + + self.assertIn(Constants.ERROR_ADDED_TO_STATUS, repr(context.exception)) + self.assertEqual(context.exception.args[1], "[{0}]".format(Constants.ERROR_ADDED_TO_STATUS)) + def raise_ex(self): raise Exception() - + def mock_refresh_repo(self): pass From 305f2eca83d9ac7019663f31926716d2a26fd46b Mon Sep 17 00:00:00 2001 From: john feng Date: Fri, 20 Sep 2024 10:26:56 -0700 Subject: [PATCH 02/13] add test_start_reboot() --- src/core/tests/Test_RebootManager.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/core/tests/Test_RebootManager.py b/src/core/tests/Test_RebootManager.py index f3174f3c..8136ae98 100644 --- a/src/core/tests/Test_RebootManager.py +++ b/src/core/tests/Test_RebootManager.py @@ -113,6 +113,28 @@ def test_reboot_always_time_not_available(self): self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(10), False) runtime.stop() + def test_start_reboot(self): + reboot_setting_in_api = 'Always' + argument_composer = ArgumentComposer() + argument_composer.reboot_setting = reboot_setting_in_api + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) + reboot_manager = runtime.reboot_manager + + # Validate single reboot scenario + runtime.status_handler.is_reboot_pending = True + result = reboot_manager.start_reboot_if_required_and_time_available(20) + self.assertEqual(result, True) + + # mock completing the reboot once, with no reboot required + runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.REQUIRED) + runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) + runtime.status_handler.is_reboot_pending = False + runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.COMPLETED) + + # no further reboot should be required + self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) + runtime.stop() + if __name__ == '__main__': unittest.main() From dc7cebf245a8bb98dfa1d4bc01ae2542c09987b7 Mon Sep 17 00:00:00 2001 From: john feng Date: Sun, 22 Sep 2024 23:42:59 -0700 Subject: [PATCH 03/13] add logic to rset sys.stdout and add dummy test in rebootmanager.py --- .../tests/Test_ConfigurePatchingProcessor.py | 3 +- src/core/tests/Test_MaintenanceWindow.py | 3 +- src/core/tests/Test_RebootManager.py | 41 ++++++++++--------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/core/tests/Test_ConfigurePatchingProcessor.py b/src/core/tests/Test_ConfigurePatchingProcessor.py index 76db40e3..ee64ba89 100644 --- a/src/core/tests/Test_ConfigurePatchingProcessor.py +++ b/src/core/tests/Test_ConfigurePatchingProcessor.py @@ -317,6 +317,7 @@ def test_configure_patching_with_patch_mode_and_assessment_mode_by_platform(self def test_configure_patching_raise_exception_auto_os_patch_state(self): # arrange capture std IO captured_output = StringIO() + original_stdout = sys.stdout sys.stdout = captured_output argument_composer = ArgumentComposer() @@ -334,7 +335,7 @@ def test_configure_patching_raise_exception_auto_os_patch_state(self): runtime.configure_patching_processor.start_configure_patching() # restore sdt.out ouptput - sys.stdout = sys.__stdout__ + sys.stdout = original_stdout # assert output = captured_output.getvalue() diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index e9e0fb29..0997a5fe 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -67,6 +67,7 @@ def test_RemainingTime_after_duration_complete(self): def test_RemainingTime_log_to_stdout_true(self): # Arrange, Capture stdout captured_output = StringIO() + original_stdout = sys.stdout sys.stdout = captured_output # Redirect stdout to the StringIO object argument_composer = ArgumentComposer() @@ -78,7 +79,7 @@ def test_RemainingTime_log_to_stdout_true(self): remaining_time = runtime.maintenance_window.get_remaining_time_in_minutes(current_time, log_to_stdout=True) # Restore stdout - sys.stdout = sys.__stdout__ + sys.stdout = original_stdout # Assert output = captured_output.getvalue() diff --git a/src/core/tests/Test_RebootManager.py b/src/core/tests/Test_RebootManager.py index 8136ae98..81c38310 100644 --- a/src/core/tests/Test_RebootManager.py +++ b/src/core/tests/Test_RebootManager.py @@ -114,26 +114,27 @@ def test_reboot_always_time_not_available(self): runtime.stop() def test_start_reboot(self): - reboot_setting_in_api = 'Always' - argument_composer = ArgumentComposer() - argument_composer.reboot_setting = reboot_setting_in_api - runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) - reboot_manager = runtime.reboot_manager - - # Validate single reboot scenario - runtime.status_handler.is_reboot_pending = True - result = reboot_manager.start_reboot_if_required_and_time_available(20) - self.assertEqual(result, True) - - # mock completing the reboot once, with no reboot required - runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.REQUIRED) - runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) - runtime.status_handler.is_reboot_pending = False - runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.COMPLETED) - - # no further reboot should be required - self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) - runtime.stop() + pass + # reboot_setting_in_api = 'Always' + # argument_composer = ArgumentComposer() + # argument_composer.reboot_setting = reboot_setting_in_api + # runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) + # reboot_manager = runtime.reboot_manager + # + # # Validate single reboot scenario + # runtime.status_handler.is_reboot_pending = True + # result = reboot_manager.start_reboot_if_required_and_time_available(20) + # self.assertEqual(result, True) + # + # # mock completing the reboot once, with no reboot required + # runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.REQUIRED) + # runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) + # runtime.status_handler.is_reboot_pending = False + # runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.COMPLETED) + # + # # no further reboot should be required + # self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) + # runtime.stop() if __name__ == '__main__': From d5332c70d38271ed571b551c7bef28f2f2072158 Mon Sep 17 00:00:00 2001 From: john feng Date: Mon, 23 Sep 2024 21:35:13 -0700 Subject: [PATCH 04/13] modify start_reboot for testing --- src/core/src/core_logic/RebootManager.py | 6 ++- src/core/tests/Test_RebootManager.py | 42 ++++++++++----------- src/core/tests/library/RuntimeCompositor.py | 2 +- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/core/src/core_logic/RebootManager.py b/src/core/src/core_logic/RebootManager.py index fc41de60..03aea4de 100644 --- a/src/core/src/core_logic/RebootManager.py +++ b/src/core/src/core_logic/RebootManager.py @@ -17,6 +17,7 @@ """Reboot management""" import datetime import subprocess +import sys import time from core.src.bootstrap.Constants import Constants @@ -66,7 +67,7 @@ def is_setting(self, setting_to_check): def start_reboot(self, message="Azure Patch Management initiated a reboot after a patch installation run."): """ Perform a system reboot """ self.composite_logger.log("\nThe machine is set to reboot in " + self.minutes_to_shutdown + " minutes.") - + print('did this get called') self.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) reboot_init_time = self.env_layer.datetime.datetime_utcnow() self.env_layer.reboot_machine(self.reboot_cmd + self.minutes_to_shutdown + ' ' + message) @@ -85,7 +86,8 @@ def start_reboot(self, message="Azure Patch Management initiated a reboot after self.composite_logger.file_logger.flush() self.composite_logger.log("Waiting for machine reboot. [ElapsedTimeInMinutes={0}] [MaxTimeInMinutes={1}]".format(str(elapsed_time_in_minutes), str(max_allowable_time_to_reboot_in_minutes))) self.composite_logger.file_logger.flush() - time.sleep(60) + # time.sleep(60) + return def start_reboot_if_required_and_time_available(self, current_time_available): """ Starts a reboot if required. Happens only at the end of the run if required. """ diff --git a/src/core/tests/Test_RebootManager.py b/src/core/tests/Test_RebootManager.py index 81c38310..a06a104a 100644 --- a/src/core/tests/Test_RebootManager.py +++ b/src/core/tests/Test_RebootManager.py @@ -114,27 +114,27 @@ def test_reboot_always_time_not_available(self): runtime.stop() def test_start_reboot(self): - pass - # reboot_setting_in_api = 'Always' - # argument_composer = ArgumentComposer() - # argument_composer.reboot_setting = reboot_setting_in_api - # runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) - # reboot_manager = runtime.reboot_manager - # - # # Validate single reboot scenario - # runtime.status_handler.is_reboot_pending = True - # result = reboot_manager.start_reboot_if_required_and_time_available(20) - # self.assertEqual(result, True) - # - # # mock completing the reboot once, with no reboot required - # runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.REQUIRED) - # runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) - # runtime.status_handler.is_reboot_pending = False - # runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.COMPLETED) - # - # # no further reboot should be required - # self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) - # runtime.stop() + reboot_setting_in_api = 'Always' + argument_composer = ArgumentComposer() + argument_composer.reboot_setting = reboot_setting_in_api + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) + runtime.container.get('reboot_manager').start_reboot() + reboot_manager = runtime.reboot_manager + + # Validate single reboot scenario + runtime.status_handler.is_reboot_pending = True + result = reboot_manager.start_reboot_if_required_and_time_available(20) + self.assertEqual(result, True) + + # mock completing the reboot once, with no reboot required + runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.REQUIRED) + runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) + runtime.status_handler.is_reboot_pending = False + runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.COMPLETED) + + # no further reboot should be required + self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) + runtime.stop() if __name__ == '__main__': diff --git a/src/core/tests/library/RuntimeCompositor.py b/src/core/tests/library/RuntimeCompositor.py index d8f3e623..8938ea57 100644 --- a/src/core/tests/library/RuntimeCompositor.py +++ b/src/core/tests/library/RuntimeCompositor.py @@ -100,7 +100,7 @@ def mkdtemp_runner(): self.reconfigure_package_manager() self.configure_patching_processor = self.container.get('configure_patching_processor') self.reboot_manager = self.container.get('reboot_manager') - self.reconfigure_reboot_manager() + # self.reconfigure_reboot_manager() self.package_filter = self.container.get('package_filter') self.patch_assessor = self.container.get('patch_assessor') self.patch_installer = self.container.get('patch_installer') From 2b7ec7ef23743c0a8be288cc4bf860a8ff2f9026 Mon Sep 17 00:00:00 2001 From: john feng Date: Mon, 23 Sep 2024 22:35:24 -0700 Subject: [PATCH 05/13] use import six, StringIO works in py2 py3 --- src/core/tests/Test_ConfigurePatchingProcessor.py | 2 +- src/core/tests/Test_MaintenanceWindow.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/tests/Test_ConfigurePatchingProcessor.py b/src/core/tests/Test_ConfigurePatchingProcessor.py index ee64ba89..aecb33d4 100644 --- a/src/core/tests/Test_ConfigurePatchingProcessor.py +++ b/src/core/tests/Test_ConfigurePatchingProcessor.py @@ -18,7 +18,7 @@ import re import unittest import sys -from io import StringIO +from six import StringIO from core.src.CoreMain import CoreMain from core.src.bootstrap.Constants import Constants diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index 0997a5fe..0d6692f9 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -15,9 +15,10 @@ # Requires Python 2.7+ import datetime -from io import StringIO import sys import unittest +from six import StringIO + from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.RuntimeCompositor import RuntimeCompositor From d29041cdbac90a6276a98b7d41c4f29b491bc999 Mon Sep 17 00:00:00 2001 From: john feng Date: Tue, 24 Sep 2024 11:14:39 -0700 Subject: [PATCH 06/13] add ut for start_reboot raise exception --- src/core/tests/Test_MaintenanceWindow.py | 1 + src/core/tests/Test_PatchAssessor.py | 4 +--- src/core/tests/Test_RebootManager.py | 23 ++++++++--------------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index 0d6692f9..a623a336 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -165,5 +165,6 @@ def test_is_package_install_time_available(self): self.assertEqual(False, runtime.maintenance_window.is_package_install_time_available(runtime.package_manager, remaining_time_in_minutes, number_of_packages)) runtime.stop() + if __name__ == '__main__': unittest.main() diff --git a/src/core/tests/Test_PatchAssessor.py b/src/core/tests/Test_PatchAssessor.py index 9c10f891..7bb9dc93 100644 --- a/src/core/tests/Test_PatchAssessor.py +++ b/src/core/tests/Test_PatchAssessor.py @@ -23,7 +23,7 @@ from core.src.service_interfaces.TelemetryWriter import TelemetryWriter from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.RuntimeCompositor import RuntimeCompositor -from core.src.core_logic.Stopwatch import Stopwatch + class TestPatchAssessor(unittest.TestCase): def setUp(self): @@ -165,7 +165,6 @@ def test_write_assessment_perf_logs(self): err_msg = "{0}=".format(str(Constants.PerfLogTrackerParams.ERROR_MSG)) self.assertTrue(err_msg in str(self.runtime.patch_assessor.stopwatch.task_details)) - def test_stopwatch_properties_assessment_fail(self): self.runtime.set_legacy_test_type('UnalignedPath') self.assertRaises(Exception, self.runtime.patch_assessor.start_assessment) @@ -181,7 +180,6 @@ def test_raise_if_min_python_version_not_met(self): self.runtime.patch_assessor.start_assessment() self.assertEqual(str(context.exception), Constants.PYTHON_NOT_COMPATIBLE_ERROR_MSG.format(sys.version_info)) - def test_raise_add_error_to_status(self): self.runtime.package_manager.get_all_updates = lambda: self.raise_ex() diff --git a/src/core/tests/Test_RebootManager.py b/src/core/tests/Test_RebootManager.py index a06a104a..aaae8e05 100644 --- a/src/core/tests/Test_RebootManager.py +++ b/src/core/tests/Test_RebootManager.py @@ -13,8 +13,8 @@ # limitations under the License. # # Requires Python 2.7+ - import unittest + from core.src.bootstrap.Constants import Constants from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.RuntimeCompositor import RuntimeCompositor @@ -113,27 +113,20 @@ def test_reboot_always_time_not_available(self): self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(10), False) runtime.stop() - def test_start_reboot(self): + def test_start_reboot_raise_exception(self): reboot_setting_in_api = 'Always' argument_composer = ArgumentComposer() argument_composer.reboot_setting = reboot_setting_in_api runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) - runtime.container.get('reboot_manager').start_reboot() - reboot_manager = runtime.reboot_manager + Constants.REBOOT_WAIT_TIMEOUT_IN_MINUTES = -20 - # Validate single reboot scenario - runtime.status_handler.is_reboot_pending = True - result = reboot_manager.start_reboot_if_required_and_time_available(20) - self.assertEqual(result, True) + with self.assertRaises(Exception) as context: + runtime.container.get('reboot_manager').start_reboot() - # mock completing the reboot once, with no reboot required - runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.REQUIRED) - runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) - runtime.status_handler.is_reboot_pending = False - runtime.status_handler.set_installation_reboot_status(Constants.RebootStatus.COMPLETED) + # assert + self.assertIn("Reboot failed to proceed on the machine in a timely manner.", repr(context.exception)) + self.assertEqual(context.exception.args[1], "[{0}]".format(Constants.ERROR_ADDED_TO_STATUS)) - # no further reboot should be required - self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) runtime.stop() From 5d44f9544f2c5d73b41955f8986e5d028518622e Mon Sep 17 00:00:00 2001 From: john feng Date: Tue, 24 Sep 2024 12:59:08 -0700 Subject: [PATCH 07/13] create a new method to preserve start_reboot --- src/core/src/core_logic/RebootManager.py | 8 ++------ src/core/tests/Test_RebootManager.py | 3 ++- src/core/tests/library/RuntimeCompositor.py | 10 +++++++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/core/src/core_logic/RebootManager.py b/src/core/src/core_logic/RebootManager.py index 03aea4de..292e1a14 100644 --- a/src/core/src/core_logic/RebootManager.py +++ b/src/core/src/core_logic/RebootManager.py @@ -15,9 +15,6 @@ # Requires Python 2.7+ """Reboot management""" -import datetime -import subprocess -import sys import time from core.src.bootstrap.Constants import Constants @@ -67,7 +64,7 @@ def is_setting(self, setting_to_check): def start_reboot(self, message="Azure Patch Management initiated a reboot after a patch installation run."): """ Perform a system reboot """ self.composite_logger.log("\nThe machine is set to reboot in " + self.minutes_to_shutdown + " minutes.") - print('did this get called') + self.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) reboot_init_time = self.env_layer.datetime.datetime_utcnow() self.env_layer.reboot_machine(self.reboot_cmd + self.minutes_to_shutdown + ' ' + message) @@ -86,8 +83,7 @@ def start_reboot(self, message="Azure Patch Management initiated a reboot after self.composite_logger.file_logger.flush() self.composite_logger.log("Waiting for machine reboot. [ElapsedTimeInMinutes={0}] [MaxTimeInMinutes={1}]".format(str(elapsed_time_in_minutes), str(max_allowable_time_to_reboot_in_minutes))) self.composite_logger.file_logger.flush() - # time.sleep(60) - return + time.sleep(60) def start_reboot_if_required_and_time_available(self, current_time_available): """ Starts a reboot if required. Happens only at the end of the run if required. """ diff --git a/src/core/tests/Test_RebootManager.py b/src/core/tests/Test_RebootManager.py index aaae8e05..0fab468b 100644 --- a/src/core/tests/Test_RebootManager.py +++ b/src/core/tests/Test_RebootManager.py @@ -121,7 +121,8 @@ def test_start_reboot_raise_exception(self): Constants.REBOOT_WAIT_TIMEOUT_IN_MINUTES = -20 with self.assertRaises(Exception) as context: - runtime.container.get('reboot_manager').start_reboot() + runtime.use_original_rm_start_reboot() + runtime.reboot_manager.start_reboot() # assert self.assertIn("Reboot failed to proceed on the machine in a timely manner.", repr(context.exception)) diff --git a/src/core/tests/library/RuntimeCompositor.py b/src/core/tests/library/RuntimeCompositor.py index 8938ea57..86dae841 100644 --- a/src/core/tests/library/RuntimeCompositor.py +++ b/src/core/tests/library/RuntimeCompositor.py @@ -43,6 +43,7 @@ class RuntimeCompositor(object): def __init__(self, argv=Constants.DEFAULT_UNSPECIFIED_VALUE, legacy_mode=False, package_manager_name=Constants.APT, vm_cloud_type=Constants.VMCloudType.AZURE): # Init data + self.original_rm_start_reboot = None self.current_env = Constants.DEV os.environ[Constants.LPE_ENV_VARIABLE] = self.current_env self.argv = argv if argv != Constants.DEFAULT_UNSPECIFIED_VALUE else ArgumentComposer().get_composed_arguments() @@ -100,7 +101,7 @@ def mkdtemp_runner(): self.reconfigure_package_manager() self.configure_patching_processor = self.container.get('configure_patching_processor') self.reboot_manager = self.container.get('reboot_manager') - # self.reconfigure_reboot_manager() + self.reconfigure_reboot_manager() self.package_filter = self.container.get('package_filter') self.patch_assessor = self.container.get('patch_assessor') self.patch_installer = self.container.get('patch_installer') @@ -150,11 +151,18 @@ def reconfigure_env_layer_to_legacy_mode(self): self.env_layer.etc_environment_file_path = os.getcwd() def reconfigure_reboot_manager(self): + # Preserve the original reboot manager start_reboot method + self.original_rm_start_reboot = self.reboot_manager.start_reboot + + # Reassign start_reboot to a new mock method self.reboot_manager.start_reboot = self.start_reboot def start_reboot(self, message="Test initiated reboot mock"): self.status_handler.set_installation_reboot_status(Constants.RebootStatus.STARTED) + def use_original_rm_start_reboot(self): + self.reboot_manager.start_reboot = self.original_rm_start_reboot + def reconfigure_package_manager(self): self.backup_get_current_auto_os_patch_state = self.package_manager.get_current_auto_os_patch_state self.package_manager.get_current_auto_os_patch_state = self.get_current_auto_os_patch_state From 4ff9a1cc6292779ab1f908a85acef437dde1efef Mon Sep 17 00:00:00 2001 From: john feng Date: Tue, 24 Sep 2024 13:05:51 -0700 Subject: [PATCH 08/13] restore the sys.__stdout --- src/core/tests/Test_ConfigurePatchingProcessor.py | 3 +-- src/core/tests/Test_MaintenanceWindow.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/core/tests/Test_ConfigurePatchingProcessor.py b/src/core/tests/Test_ConfigurePatchingProcessor.py index aecb33d4..8573bfac 100644 --- a/src/core/tests/Test_ConfigurePatchingProcessor.py +++ b/src/core/tests/Test_ConfigurePatchingProcessor.py @@ -317,7 +317,6 @@ def test_configure_patching_with_patch_mode_and_assessment_mode_by_platform(self def test_configure_patching_raise_exception_auto_os_patch_state(self): # arrange capture std IO captured_output = StringIO() - original_stdout = sys.stdout sys.stdout = captured_output argument_composer = ArgumentComposer() @@ -335,7 +334,7 @@ def test_configure_patching_raise_exception_auto_os_patch_state(self): runtime.configure_patching_processor.start_configure_patching() # restore sdt.out ouptput - sys.stdout = original_stdout + sys.stdout = sys.__stdout__ # assert output = captured_output.getvalue() diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index a623a336..574d6360 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -68,7 +68,6 @@ def test_RemainingTime_after_duration_complete(self): def test_RemainingTime_log_to_stdout_true(self): # Arrange, Capture stdout captured_output = StringIO() - original_stdout = sys.stdout sys.stdout = captured_output # Redirect stdout to the StringIO object argument_composer = ArgumentComposer() @@ -80,7 +79,7 @@ def test_RemainingTime_log_to_stdout_true(self): remaining_time = runtime.maintenance_window.get_remaining_time_in_minutes(current_time, log_to_stdout=True) # Restore stdout - sys.stdout = original_stdout + sys.stdout = sys.__stdout__ # Assert output = captured_output.getvalue() From a068eb4c9185e2efb1266ae2224bd5e779ce4a0b Mon Sep 17 00:00:00 2001 From: john feng Date: Tue, 24 Sep 2024 13:11:14 -0700 Subject: [PATCH 09/13] revert import to use io instead of six --- src/core/tests/Test_ConfigurePatchingProcessor.py | 2 +- src/core/tests/Test_MaintenanceWindow.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/tests/Test_ConfigurePatchingProcessor.py b/src/core/tests/Test_ConfigurePatchingProcessor.py index 8573bfac..76db40e3 100644 --- a/src/core/tests/Test_ConfigurePatchingProcessor.py +++ b/src/core/tests/Test_ConfigurePatchingProcessor.py @@ -18,7 +18,7 @@ import re import unittest import sys -from six import StringIO +from io import StringIO from core.src.CoreMain import CoreMain from core.src.bootstrap.Constants import Constants diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index 574d6360..a6e3e265 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -17,7 +17,7 @@ import datetime import sys import unittest -from six import StringIO +from io import StringIO from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.RuntimeCompositor import RuntimeCompositor From ce257fe6420ea70278b3d6e78ecf40c522c04347 Mon Sep 17 00:00:00 2001 From: john feng Date: Tue, 24 Sep 2024 14:58:04 -0700 Subject: [PATCH 10/13] add ut for rebootifrequired --- src/core/tests/Test_RebootManager.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/tests/Test_RebootManager.py b/src/core/tests/Test_RebootManager.py index 0fab468b..a692fb45 100644 --- a/src/core/tests/Test_RebootManager.py +++ b/src/core/tests/Test_RebootManager.py @@ -113,6 +113,18 @@ def test_reboot_always_time_not_available(self): self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(10), False) runtime.stop() + def test_reboot_if_required_no_reboot_pending(self): + reboot_setting_in_api = 'IfRequired' + argument_composer = ArgumentComposer() + argument_composer.reboot_setting = reboot_setting_in_api + runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM) + reboot_manager = runtime.reboot_manager + + # Validate single reboot scenario + runtime.status_handler.is_reboot_pending = False + self.assertEqual(reboot_manager.start_reboot_if_required_and_time_available(20), False) + runtime.stop() + def test_start_reboot_raise_exception(self): reboot_setting_in_api = 'Always' argument_composer = ArgumentComposer() @@ -127,7 +139,6 @@ def test_start_reboot_raise_exception(self): # assert self.assertIn("Reboot failed to proceed on the machine in a timely manner.", repr(context.exception)) self.assertEqual(context.exception.args[1], "[{0}]".format(Constants.ERROR_ADDED_TO_STATUS)) - runtime.stop() From d59ed7e18378f92d23dd9d79b6c521c642febc95 Mon Sep 17 00:00:00 2001 From: john feng Date: Thu, 26 Sep 2024 12:31:10 -0700 Subject: [PATCH 11/13] test stringIO import --- src/core/tests/Test_ConfigurePatchingProcessor.py | 9 +++++++-- src/core/tests/Test_MaintenanceWindow.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/core/tests/Test_ConfigurePatchingProcessor.py b/src/core/tests/Test_ConfigurePatchingProcessor.py index 76db40e3..11f1d38c 100644 --- a/src/core/tests/Test_ConfigurePatchingProcessor.py +++ b/src/core/tests/Test_ConfigurePatchingProcessor.py @@ -18,7 +18,11 @@ import re import unittest import sys -from io import StringIO +# Conditional import for StringIO +try: + from StringIO import StringIO # Python 2 +except ImportError: + from io import StringIO # Python 3 from core.src.CoreMain import CoreMain from core.src.bootstrap.Constants import Constants @@ -317,6 +321,7 @@ def test_configure_patching_with_patch_mode_and_assessment_mode_by_platform(self def test_configure_patching_raise_exception_auto_os_patch_state(self): # arrange capture std IO captured_output = StringIO() + original_stdout = sys.stdout sys.stdout = captured_output argument_composer = ArgumentComposer() @@ -334,7 +339,7 @@ def test_configure_patching_raise_exception_auto_os_patch_state(self): runtime.configure_patching_processor.start_configure_patching() # restore sdt.out ouptput - sys.stdout = sys.__stdout__ + sys.stdout = original_stdout # assert output = captured_output.getvalue() diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index a6e3e265..c9a4c755 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -17,11 +17,16 @@ import datetime import sys import unittest -from io import StringIO +# Conditional import for StringIO +try: + from StringIO import StringIO # Python 2 +except ImportError: + from io import StringIO # Python 3 from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.RuntimeCompositor import RuntimeCompositor + class TestMaintenanceWindow(unittest.TestCase): def setUp(self): pass @@ -68,6 +73,7 @@ def test_RemainingTime_after_duration_complete(self): def test_RemainingTime_log_to_stdout_true(self): # Arrange, Capture stdout captured_output = StringIO() + original_output = sys.stdout sys.stdout = captured_output # Redirect stdout to the StringIO object argument_composer = ArgumentComposer() @@ -79,7 +85,7 @@ def test_RemainingTime_log_to_stdout_true(self): remaining_time = runtime.maintenance_window.get_remaining_time_in_minutes(current_time, log_to_stdout=True) # Restore stdout - sys.stdout = sys.__stdout__ + sys.stdout = original_output # Assert output = captured_output.getvalue() From e9f1686f39e4495d64c0e81052145841748c3482 Mon Sep 17 00:00:00 2001 From: john feng Date: Wed, 23 Oct 2024 11:53:12 -0700 Subject: [PATCH 12/13] restore configurepatchingprocessor and maintenancewindow tests as master --- src/core/tests/Test_ConfigurePatchingProcessor.py | 9 ++------- src/core/tests/Test_MaintenanceWindow.py | 12 ++---------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/core/tests/Test_ConfigurePatchingProcessor.py b/src/core/tests/Test_ConfigurePatchingProcessor.py index 11f1d38c..76db40e3 100644 --- a/src/core/tests/Test_ConfigurePatchingProcessor.py +++ b/src/core/tests/Test_ConfigurePatchingProcessor.py @@ -18,11 +18,7 @@ import re import unittest import sys -# Conditional import for StringIO -try: - from StringIO import StringIO # Python 2 -except ImportError: - from io import StringIO # Python 3 +from io import StringIO from core.src.CoreMain import CoreMain from core.src.bootstrap.Constants import Constants @@ -321,7 +317,6 @@ def test_configure_patching_with_patch_mode_and_assessment_mode_by_platform(self def test_configure_patching_raise_exception_auto_os_patch_state(self): # arrange capture std IO captured_output = StringIO() - original_stdout = sys.stdout sys.stdout = captured_output argument_composer = ArgumentComposer() @@ -339,7 +334,7 @@ def test_configure_patching_raise_exception_auto_os_patch_state(self): runtime.configure_patching_processor.start_configure_patching() # restore sdt.out ouptput - sys.stdout = original_stdout + sys.stdout = sys.__stdout__ # assert output = captured_output.getvalue() diff --git a/src/core/tests/Test_MaintenanceWindow.py b/src/core/tests/Test_MaintenanceWindow.py index c9a4c755..e9e0fb29 100644 --- a/src/core/tests/Test_MaintenanceWindow.py +++ b/src/core/tests/Test_MaintenanceWindow.py @@ -15,18 +15,12 @@ # Requires Python 2.7+ import datetime +from io import StringIO import sys import unittest -# Conditional import for StringIO -try: - from StringIO import StringIO # Python 2 -except ImportError: - from io import StringIO # Python 3 - from core.tests.library.ArgumentComposer import ArgumentComposer from core.tests.library.RuntimeCompositor import RuntimeCompositor - class TestMaintenanceWindow(unittest.TestCase): def setUp(self): pass @@ -73,7 +67,6 @@ def test_RemainingTime_after_duration_complete(self): def test_RemainingTime_log_to_stdout_true(self): # Arrange, Capture stdout captured_output = StringIO() - original_output = sys.stdout sys.stdout = captured_output # Redirect stdout to the StringIO object argument_composer = ArgumentComposer() @@ -85,7 +78,7 @@ def test_RemainingTime_log_to_stdout_true(self): remaining_time = runtime.maintenance_window.get_remaining_time_in_minutes(current_time, log_to_stdout=True) # Restore stdout - sys.stdout = original_output + sys.stdout = sys.__stdout__ # Assert output = captured_output.getvalue() @@ -170,6 +163,5 @@ def test_is_package_install_time_available(self): self.assertEqual(False, runtime.maintenance_window.is_package_install_time_available(runtime.package_manager, remaining_time_in_minutes, number_of_packages)) runtime.stop() - if __name__ == '__main__': unittest.main() From 30699c5af26ae37bfc78e54d0172dac9525638e4 Mon Sep 17 00:00:00 2001 From: john feng Date: Mon, 28 Oct 2024 11:06:05 -0700 Subject: [PATCH 13/13] refactor the test assessor throws exception --- src/core/tests/Test_PatchAssessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/tests/Test_PatchAssessor.py b/src/core/tests/Test_PatchAssessor.py index 7bb9dc93..6d0a9f11 100644 --- a/src/core/tests/Test_PatchAssessor.py +++ b/src/core/tests/Test_PatchAssessor.py @@ -180,7 +180,7 @@ def test_raise_if_min_python_version_not_met(self): self.runtime.patch_assessor.start_assessment() self.assertEqual(str(context.exception), Constants.PYTHON_NOT_COMPATIBLE_ERROR_MSG.format(sys.version_info)) - def test_raise_add_error_to_status(self): + def test_patch_assessment_throws_exception(self): self.runtime.package_manager.get_all_updates = lambda: self.raise_ex() with self.assertRaises(Exception) as context: @@ -191,7 +191,7 @@ def test_raise_add_error_to_status(self): def raise_ex(self): raise Exception() - + def mock_refresh_repo(self): pass