From 38a98bf4dd890b91f0323a96d913b52779fa2bea Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 12 Jul 2023 11:24:32 +0200 Subject: [PATCH] tests: fix test_file_monitor without pyinotify The 'file_monitor' module already handled the lack of the 'pyinotify' module, falling back to a polling-based file watcher. Unfortunately, the tests for 'file_monitor' assumed that 'pyinotify' was always present. Hence, make it possible to run test_file_monitor successfully in case 'pyinotify' is not available: - create a inotify-based filesystem watcher only when 'pyinotify' is available - mark the tests that assume the presence of 'pyinotify' directly or indirectly - adjust 'test_inotify_avail' to the actual availability of 'pyinotify' - add a new 'test_create_fsw_real' to check the actual filesystem watcher depending on what is available --- test/rhsmlib/test_file_monitor.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test/rhsmlib/test_file_monitor.py b/test/rhsmlib/test_file_monitor.py index 5b1cfeac41..18dffdeaf7 100644 --- a/test/rhsmlib/test_file_monitor.py +++ b/test/rhsmlib/test_file_monitor.py @@ -19,11 +19,17 @@ import configparser from rhsmlib import file_monitor +import unittest from unittest.mock import Mock, patch from test import fixture +# we could probably try to import 'pyinotify' ourselves here to check +# that it is present; shamelessly rely on the import logic in file_monitor +HAS_PYINOTIFY = file_monitor.pyinotify is not None + + class TestFilesystemWatcher(fixture.SubManFixture): def setUp(self): super(TestFilesystemWatcher, self).setUp() @@ -37,7 +43,8 @@ def setUp(self): self.dw3 = file_monitor.DirectoryWatch(self.testpath2, [self.mock_cb1, self.mock_cb2], is_glob=False) self.dir_list = {"DW1": self.dw1, "DW2": self.dw2, "DW3": self.dw3} self.fsw1 = file_monitor.FilesystemWatcher(self.dir_list) - self.fsw2 = file_monitor.InotifyFilesystemWatcher(self.dir_list) + if HAS_PYINOTIFY: + self.fsw2 = file_monitor.InotifyFilesystemWatcher(self.dir_list) now: float = time.time() self.future = now + 120.0 @@ -74,12 +81,20 @@ def test_create_fsw(self, mock_config, mock_avail): fsw = file_monitor.create_filesystem_watcher(self.dir_list) self.assertIsInstance(fsw, file_monitor.FilesystemWatcher) + @patch("rhsmlib.file_monitor.is_inotify_config") + def test_create_fsw_real(self, mock_config): + mock_config.return_value = True + fsw = file_monitor.create_filesystem_watcher(self.dir_list) + self.assertIsInstance( + fsw, file_monitor.InotifyFilesystemWatcher if HAS_PYINOTIFY else file_monitor.FilesystemWatcher + ) + @patch("rhsmlib.file_monitor.pyinotify", new=None) def test_inotify_None(self): self.assertFalse(file_monitor.is_inotify_available()) def test_inotify_avail(self): - self.assertTrue(file_monitor.is_inotify_available(), "expected: inotify is available") + self.assertEqual(file_monitor.is_inotify_available(), HAS_PYINOTIFY, "expected: inotify is available") @patch("rhsmlib.file_monitor.conf") def test_inotify_config(self, mock_config): @@ -157,15 +172,18 @@ def test_polling_loop_stops(self): loop_thread.join(5.0) self.assertTrue(test_loop.stopped) + @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available") def test_inotify_stop_value_change(self): self.assertFalse(self.fsw2.should_stop) self.fsw2.stop() self.assertTrue(self.fsw2.should_stop) + @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available") def test_inotify_no_loop_when_stopped(self): self.fsw2.should_stop = True self.fsw2.loop() + @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available") @patch("rhsmlib.file_monitor.DirectoryWatch.notify") @patch("pyinotify.Event") def test_handle_event(self, mock_event, mock_notify): @@ -226,6 +244,7 @@ def test_notify(self): self.mock_cb1.assert_called_once() self.mock_cb2.assert_called_once() + @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available") @patch("pyinotify.Event") def test_paths_match(self, mock_event): mock_event.path = self.testpath2 @@ -239,6 +258,7 @@ def test_paths_match(self, mock_event): mock_event.pathname = self.testpath1 self.assertFalse(self.dw3.paths_match(mock_event.path, mock_event.pathname)) + @unittest.skipIf(not HAS_PYINOTIFY, "'pyinotify' is not available") @patch("pyinotify.Event") def test_file_modified(self, mock_event): mock_event.mask = 1