Skip to content

Commit 82f45bb

Browse files
authored
Bugfix/disable rhel8+ security plugin (#266)
* add logic to skip security plugin for rhel8+ images * add log and ut for disable security plug for rhel8+ * refactor and remove prints and extra lines * remove semicolon and reformat ut test_yumpkgmanager * modify the log output * refactor code
1 parent 89b35c0 commit 82f45bb

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/core/src/package_managers/YumPackageManager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ def get_all_updates(self, cached=False):
143143
def get_security_updates(self):
144144
"""Get missing security updates"""
145145
self.composite_logger.log("\nDiscovering 'security' packages...")
146-
self.install_yum_security_prerequisite()
146+
147+
if not self.__is_image_rhel8_or_higher():
148+
self.install_yum_security_prerequisite()
149+
147150
out = self.invoke_package_manager(self.yum_check_security)
148151
security_packages, security_package_versions = self.extract_packages_and_versions(out)
149152

@@ -176,6 +179,17 @@ def get_other_updates(self):
176179
self.composite_logger.log("Discovered " + str(len(other_packages)) + " 'other' package entries.")
177180
return other_packages, other_package_versions
178181

182+
def __is_image_rhel8_or_higher(self):
183+
""" Check if image is RHEL8+ return true else false """
184+
if self.env_layer.platform.linux_distribution() is not None:
185+
os_offer, os_version, os_code = self.env_layer.platform.linux_distribution()
186+
187+
if "Red Hat Enterprise Linux" in os_offer and int(os_version.split('.')[0]) >= 8:
188+
self.composite_logger.log_debug("Verify RHEL image version: " + str(os_version))
189+
return True
190+
191+
return False
192+
179193
def set_max_patch_publish_date(self, max_patch_publish_date=str()):
180194
pass
181195

src/core/tests/Test_YumPackageManager.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import unittest
1919
from core.src.bootstrap.Constants import Constants
2020
from core.tests.library.ArgumentComposer import ArgumentComposer
21+
from core.tests.library.LegacyEnvLayerExtensions import LegacyEnvLayerExtensions
2122
from core.tests.library.RuntimeCompositor import RuntimeCompositor
2223

2324

@@ -35,6 +36,12 @@ def mock_do_processes_require_restart(self):
3536

3637
def mock_write_with_retry_raise_exception(self, file_path_or_handle, data, mode='a+'):
3738
raise Exception
39+
40+
def mock_linux7_distribution_to_return_redhat(self):
41+
return ['Red Hat Enterprise Linux Server', '7', 'Maipo']
42+
43+
def mock_linux8_distribution_to_return_redhat(self):
44+
return ['Red Hat Enterprise Linux Server', '8', 'Ootpa']
3845
#endregion Mocks
3946

4047
def mock_do_processes_require_restart_raise_exception(self):
@@ -619,5 +626,55 @@ def test_obsolete_packages_should_not_considered_in_available_updates(self):
619626
self.assertTrue(available_updates[0] == "grub2-tools.x86_64")
620627
self.assertTrue(package_versions[0] == "1:2.02-142.el8")
621628

629+
def test_rhel7_image_with_security_plugin(self):
630+
"""Unit test for yum package manager rhel images below 8 and Classification = Security"""
631+
# mock linux_distribution
632+
backup_envlayer_platform_linux_distribution = LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution
633+
LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution = self.mock_linux7_distribution_to_return_redhat
634+
635+
self.__assert_test_rhel8_image()
636+
637+
# restore linux_distribution
638+
LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution = backup_envlayer_platform_linux_distribution
639+
640+
def test_rhel8_image_higher_no_security_plugin(self):
641+
"""Unit test for yum package manager rhel images >= 8 and Classification = Security"""
642+
# mock linux_distribution
643+
backup_envlayer_platform_linux_distribution = LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution
644+
LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution = self.mock_linux8_distribution_to_return_redhat
645+
646+
self.__assert_test_rhel8_image()
647+
648+
# restore linux_distribution
649+
LegacyEnvLayerExtensions.LegacyPlatform.linux_distribution = backup_envlayer_platform_linux_distribution
650+
651+
def __assert_test_rhel8_image(self):
652+
self.runtime.set_legacy_test_type('HappyPath')
653+
package_manager = self.container.get('package_manager')
654+
self.assertIsNotNone(package_manager)
655+
self.runtime.stop()
656+
657+
argument_composer = ArgumentComposer()
658+
argument_composer.classifications_to_include = [Constants.PackageClassification.SECURITY]
659+
argument_composer.patches_to_include = ["ssh", "tcpdump"]
660+
argument_composer.patches_to_exclude = ["ssh*", "test"]
661+
self.runtime = RuntimeCompositor(argument_composer.get_composed_arguments(), True, Constants.YUM)
662+
self.container = self.runtime.container
663+
664+
package_filter = self.container.get('package_filter')
665+
self.assertIsNotNone(package_filter)
666+
667+
available_updates, package_versions = package_manager.get_available_updates(package_filter)
668+
669+
# test for get_available_updates
670+
self.assertIsNotNone(available_updates)
671+
self.assertIsNotNone(package_versions)
672+
self.assertEqual(len(available_updates), 2)
673+
self.assertEqual(len(package_versions), 2)
674+
self.assertEqual(available_updates[0], "libgcc.i686")
675+
self.assertEqual(package_versions[0], "4.8.5-28.el7")
676+
self.assertEqual(available_updates[1], "tcpdump.x86_64")
677+
self.assertEqual(package_versions[1], "14:4.9.2-3.el7")
678+
622679
if __name__ == '__main__':
623680
unittest.main()

0 commit comments

Comments
 (0)