From 0b7876106a1d4559c76691e5e3e3f6aa180c9d5e Mon Sep 17 00:00:00 2001 From: Matyas Horky Date: Tue, 3 Jan 2023 16:14:05 +0100 Subject: [PATCH 1/2] Alter import of rhsm.config functions Because 'get_config_parser()' and 'in_container()' functions were being imported directly, instead of as reference, the mocks that tests perform were not applying to them, causing test suite to fail when run from inside a container. Future-wise it is better to manage mocks on one place, instead of patching the functions whenever they are being imported. --- src/subscription_manager/repolib.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/subscription_manager/repolib.py b/src/subscription_manager/repolib.py index dc649ea65..ee8c8adb9 100644 --- a/src/subscription_manager/repolib.py +++ b/src/subscription_manager/repolib.py @@ -26,7 +26,7 @@ from subscription_manager.repofile import YumRepoFile from subscription_manager.utils import get_supported_resources -from rhsm.config import get_config_parser, in_container +import rhsm.config import configparser from rhsmlib.facts.hwprobe import HardwareCollector @@ -39,7 +39,7 @@ log = logging.getLogger(__name__) -conf = config.Config(get_config_parser()) +conf = config.Config(rhsm.config.get_config_parser()) ALLOWED_CONTENT_TYPES = ["yum", "deb"] @@ -303,7 +303,7 @@ def get_expansion(self): # same consumer as the host they run on. They only have the same # access to content as the host they run on.) result = None - if not in_container(): + if not rhsm.config.in_container(): uep = self.cp_provider.get_consumer_auth_cp() result = self.release_status_cache.read_status(uep, self.identity.uuid) From 68acefc165c4dfd231fdafaa6695e3fca1e52e26 Mon Sep 17 00:00:00 2001 From: Matyas Horky Date: Thu, 1 Sep 2022 15:23:10 +0200 Subject: [PATCH 2/2] ENT-5536: Fix FileMonitor tests * Card ID: ENT-5536 Subprocess calls to 'touch' were sometimes failing to update the modification date (race condition, probably?), making the tests fail. When 'os.utime' is used, this is no longer the case and the files are always reported as modified. --- test/rhsmlib/test_file_monitor.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/test/rhsmlib/test_file_monitor.py b/test/rhsmlib/test_file_monitor.py index b3950ea6f..5b1cfeac4 100644 --- a/test/rhsmlib/test_file_monitor.py +++ b/test/rhsmlib/test_file_monitor.py @@ -10,14 +10,18 @@ # Red Hat trademarks are not licensed under GPLv2. No permission is # granted to use or replicate Red Hat trademarks that are incorporated # in this software or its documentation. +import subprocess +import tempfile +import time +import os +from threading import Thread -from rhsmlib import file_monitor import configparser +from rhsmlib import file_monitor + from unittest.mock import Mock, patch + from test import fixture -from threading import Thread -import subprocess -import tempfile class TestFilesystemWatcher(fixture.SubManFixture): @@ -35,6 +39,9 @@ def setUp(self): self.fsw1 = file_monitor.FilesystemWatcher(self.dir_list) self.fsw2 = file_monitor.InotifyFilesystemWatcher(self.dir_list) + now: float = time.time() + self.future = now + 120.0 + def tearDown(self): super(TestFilesystemWatcher, self).tearDown() @@ -99,18 +106,18 @@ def test_polling_stop_value_change(self): def test_polling_changed_dw_set_2_modified(self): self.fsw1.changed_dw_set() - subprocess.call("touch %s -m" % self.testpath1, shell=True) - subprocess.call("touch %s -m" % self.testpath2, shell=True) + os.utime(self.testpath1, (self.future, self.future)) + os.utime(self.testpath2, (self.future, self.future)) self.assertEqual(self.fsw1.changed_dw_set(), {self.dw1, self.dw2, self.dw3}) def test_polling_changed_dw_set_first_modified(self): self.fsw1.changed_dw_set() - subprocess.call("touch %s -m" % self.testpath1, shell=True) + os.utime(self.testpath1, (self.future, self.future)) self.assertEqual(self.fsw1.changed_dw_set(), {self.dw1}) def test_polling_changed_dw_set_second_modified(self): self.fsw1.changed_dw_set() - subprocess.call("touch %s -m" % self.testpath2, shell=True) + os.utime(self.testpath2, (self.future, self.future)) self.assertEqual(self.fsw1.changed_dw_set(), {self.dw2, self.dw3}) def test_polling_changed_dw_set_0_modified(self): @@ -119,18 +126,18 @@ def test_polling_changed_dw_set_0_modified(self): def test_polling_update_2_modified(self): self.fsw1.changed_dw_set() - subprocess.call("touch %s -m" % self.testpath1, shell=True) - subprocess.call("touch %s -m" % self.testpath2, shell=True) + os.utime(self.testpath1, (self.future, self.future)) + os.utime(self.testpath2, (self.future, self.future)) self.assertEqual(self.fsw1.update(), {self.dw1, self.dw2, self.dw3}) def test_polling_update_first_modified(self): self.fsw1.changed_dw_set() - subprocess.call("touch %s -m" % self.testpath1, shell=True) + os.utime(self.testpath1, (self.future, self.future)) self.assertEqual(self.fsw1.update(), {self.dw1}) def test_polling_update_second_modified(self): self.fsw1.changed_dw_set() - subprocess.call("touch %s -m" % self.testpath2, shell=True) + os.utime(self.testpath2, (self.future, self.future)) self.assertEqual(self.fsw1.update(), {self.dw2, self.dw3}) def test_polling_update_0_modified(self):