From a2bac16e4e07d77430ac7e16dbf87343a109efd0 Mon Sep 17 00:00:00 2001 From: Pavel Moravec Date: Wed, 13 Nov 2024 19:57:11 +0100 Subject: [PATCH] [tests] Allow tests to restrict OS version Add a new `versions` class attribute that allows test classes to specify which version(s) are allowed/designed to execute on. If the test is attempted to be executed on a version that's not specified, the test will be skipped and a message will be printed. By default, the list of versions will be empty, which means that the test should run in all versions of the OS. This patch contains a change to restrict the execution of Teamd to RHEL 8, because it was deprecated for RHEL 9 and later. Resolves: #3782 Signed-off-by: Jose Castillo --- tests/report_tests/plugin_tests/teamd.py | 3 +++ tests/sos_tests.py | 24 +++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/report_tests/plugin_tests/teamd.py b/tests/report_tests/plugin_tests/teamd.py index 6b1b9337d5..8632d53962 100644 --- a/tests/report_tests/plugin_tests/teamd.py +++ b/tests/report_tests/plugin_tests/teamd.py @@ -25,6 +25,9 @@ class TeamdPluginTest(StageTwoReportTest): sos_cmd = '-o teamd' redhat_only = True + # teaming has been deprecated from RHEL 9 + only_os_versions = ['8'] + def pre_sos_setup(self): # restart NetworkManager to account for the new package nmout = process.run('systemctl restart NetworkManager', timeout=30) diff --git a/tests/sos_tests.py b/tests/sos_tests.py index 6ef91517e2..cd064faff6 100644 --- a/tests/sos_tests.py +++ b/tests/sos_tests.py @@ -34,6 +34,8 @@ RH_DIST = ['rhel', 'centos', 'fedora', 'centos-stream'] UBUNTU_DIST = ['Ubuntu', 'debian'] +_distro = distro.detect() + def skipIf(cond, message=None): # pylint: disable=unused-argument @@ -51,7 +53,7 @@ def wrapper(self, *args, **kwargs): # pylint: disable=unused-argument def redhat_only(tst): def wrapper(func): - if distro.detect().name not in RH_DIST: + if _distro.name not in RH_DIST: raise TestSkipError('Not running on a Red Hat distro') return wrapper @@ -59,7 +61,7 @@ def wrapper(func): # pylint: disable=unused-argument def ubuntu_only(tst): def wrapper(func): - if distro.detect().name not in UBUNTU_DIST: + if _distro.name not in UBUNTU_DIST: raise TestSkipError('Not running on a Ubuntu or Debian distro') return wrapper @@ -82,6 +84,7 @@ class BaseSoSTest(Test): ubuntu_only = False end_of_test_case = False arch = [] + only_os_versions = [] @property def klass_name(self): @@ -261,14 +264,29 @@ def check_arch_for_enablement(self): raise TestSkipError(f"Unsupported architecture {sys_arch} for test " f"(supports: {self.arch})") + def check_os_version_for_enablement(self): + """ + Check if the test case is meant only for a specific version or versions + + Takes the `versions` class attribute, a list that specifies + the versions where the test applies. If the list is empty, assume all + versions of the OS are acceptable. Otherwise, raise a TestSkipError. + """ + os_version = _distro.version + if not self.only_os_versions or os_version in self.only_os_versions: + return True + raise TestSkipError(f"Unsupported OS version {os_version} " + f"(supports: {self.only_os_versions})") + def setUp(self): """Setup the tmpdir and any needed mocking for the test, then execute the defined sos command. Ensure that we only run the sos command once for every test case, instead of once for every test_* method defined. """ - self.local_distro = distro.detect().name + self.local_distro = _distro.name self.check_distro_for_enablement() self.check_arch_for_enablement() + self.check_os_version_for_enablement() # check to prevent multiple setUp() runs if not os.path.isdir(self.tmpdir): # setup our class-shared tmpdir