From 3ea116b6ca6ccde6c404e3c4eb1303faa13455a8 Mon Sep 17 00:00:00 2001 From: john feng Date: Fri, 2 May 2025 11:13:14 -0700 Subject: [PATCH 1/3] add ut for get_other_updates --- src/core/tests/Test_YumPackageManager.py | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/core/tests/Test_YumPackageManager.py b/src/core/tests/Test_YumPackageManager.py index 08d40414..805557d6 100644 --- a/src/core/tests/Test_YumPackageManager.py +++ b/src/core/tests/Test_YumPackageManager.py @@ -42,6 +42,15 @@ def mock_linux7_distribution_to_return_redhat(self): def mock_linux8_distribution_to_return_redhat(self): return ['Red Hat Enterprise Linux Server', '8', 'Ootpa'] + + def mock_centos_linux_distribution(self): + return ['CentOS Linux', '7.9.2009', 'Core'] + + def mock_get_all_updates(self, cached): + return [], [] + + def mock_get_security_updates(self): + return [], [] #endregion Mocks def mock_do_processes_require_restart_raise_exception(self): @@ -757,5 +766,22 @@ def test_get_dependent_list_yum_version_4_update_in_two_lines_with_unexpected_ou dependent_list = package_manager.get_dependent_list(["polkit.x86_64"]) self.assertEqual(len(dependent_list), 0) + def test_get_other_updates_exception(self): + """ test get_other_updates throw exception path. """ + # Set up + package_manager = self.runtime.container.get('package_manager') + package_manager.get_all_updates = self.mock_get_all_updates + package_manager.get_security_updates = self.mock_get_security_updates + self.runtime.env_layer.platform.linux_distribution = self.mock_centos_linux_distribution + + # Act + with self.assertRaises(Exception) as context: + package_manager.get_other_updates() + + # Assert + print(str(context.exception)) + self.assertTrue("Classification-based patching is only supported on YUM if the computer is independently configured to receive classification information." in str(context.exception)) + + if __name__ == '__main__': unittest.main() From acdaf5d946ab878346cbc2831502c6e9a9b1987e Mon Sep 17 00:00:00 2001 From: john feng Date: Mon, 5 May 2025 23:07:26 -0700 Subject: [PATCH 2/3] add uts --- .../src/package_managers/YumPackageManager.py | 1 - src/core/tests/Test_YumPackageManager.py | 29 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/src/package_managers/YumPackageManager.py b/src/core/src/package_managers/YumPackageManager.py index 1773fb75..e63f8bd5 100644 --- a/src/core/src/package_managers/YumPackageManager.py +++ b/src/core/src/package_managers/YumPackageManager.py @@ -259,7 +259,6 @@ def install_updates_fail_safe(self, excluded_packages): for excluded_package in excluded_packages: excluded_string += excluded_package + ' ' cmd = self.all_but_excluded_upgrade_cmd + excluded_string - self.composite_logger.log_debug("[YPM][FAIL SAFE MODE] UPDATING PACKAGES USING COMMAND: " + cmd) self.invoke_package_manager(cmd) diff --git a/src/core/tests/Test_YumPackageManager.py b/src/core/tests/Test_YumPackageManager.py index 805557d6..ffd260af 100644 --- a/src/core/tests/Test_YumPackageManager.py +++ b/src/core/tests/Test_YumPackageManager.py @@ -782,6 +782,35 @@ def test_get_other_updates_exception(self): print(str(context.exception)) self.assertTrue("Classification-based patching is only supported on YUM if the computer is independently configured to receive classification information." in str(context.exception)) + def test_install_updates_fail_safe(self): + """Test package manager's install_updates_fail_safe method""" + # Set up + test_excluded_pkgs = ["kernel.x86_64", "kernel.i686", "tzdata.noarch"] + package_manager = self.container.get('package_manager') + self.assertIsNotNone(package_manager) + + # Act + result = package_manager.install_updates_fail_safe(test_excluded_pkgs) + + # Verify + self.assertIsNone(result) + + self.runtime.stop() + + def test_get_product_arch(self): + # Set up + test_pkg_name = "selinux-policy.noarch" + package_manager = self.container.get('package_manager') + self.assertIsNotNone(package_manager) + + # Act + result = package_manager.get_product_arch(test_pkg_name) + # verify + self.assertEqual(".noarch", result) + + self.runtime.stop() + + if __name__ == '__main__': unittest.main() From ed9877750bab609a2c9f92e0c1bcbee6c5f90039 Mon Sep 17 00:00:00 2001 From: john feng Date: Tue, 6 May 2025 08:28:43 -0700 Subject: [PATCH 3/3] add ut for disable_auto_update_on_reboot --- src/core/tests/Test_YumPackageManager.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/core/tests/Test_YumPackageManager.py b/src/core/tests/Test_YumPackageManager.py index ffd260af..4c873ff9 100644 --- a/src/core/tests/Test_YumPackageManager.py +++ b/src/core/tests/Test_YumPackageManager.py @@ -51,6 +51,9 @@ def mock_get_all_updates(self, cached): def mock_get_security_updates(self): return [], [] + + def mock_bad_run_command_output(self, cmd, no_output=False, chk_err=False): + return 1, "bad cmd" #endregion Mocks def mock_do_processes_require_restart_raise_exception(self): @@ -783,7 +786,7 @@ def test_get_other_updates_exception(self): self.assertTrue("Classification-based patching is only supported on YUM if the computer is independently configured to receive classification information." in str(context.exception)) def test_install_updates_fail_safe(self): - """Test package manager's install_updates_fail_safe method""" + """Test install_updates_fail_safe """ # Set up test_excluded_pkgs = ["kernel.x86_64", "kernel.i686", "tzdata.noarch"] package_manager = self.container.get('package_manager') @@ -798,6 +801,7 @@ def test_install_updates_fail_safe(self): self.runtime.stop() def test_get_product_arch(self): + """ Test get_product_arch method to return pkg arch type.""" # Set up test_pkg_name = "selinux-policy.noarch" package_manager = self.container.get('package_manager') @@ -810,7 +814,23 @@ def test_get_product_arch(self): self.assertEqual(".noarch", result) self.runtime.stop() + + def test_disable_auto_update_on_reboot_exception(self): + """ test disable_auto_update_on_reboot throw exception path. """ + # Set up + bad_disable_cmd = "systemctl disable yum-cron123" + package_manager = self.runtime.container.get('package_manager') + package_manager.env_layer.run_command_output = self.mock_bad_run_command_output + + # Act + with self.assertRaises(Exception) as context: + package_manager.disable_auto_update_on_reboot(bad_disable_cmd) + + # Assert + print(str(context.exception)) + self.assertTrue("Unexpected return code (1) on command: systemctl disable yum-cron123" in str(context.exception)) if __name__ == '__main__': unittest.main() +