Skip to content

Commit

Permalink
Merge pull request #3297 from candlepin/ptoscano/tests-without-pyinotify
Browse files Browse the repository at this point in the history
tests: fix test_file_monitor without pyinotify
  • Loading branch information
jirihnidek authored Jul 12, 2023
2 parents 08bfc6f + 38a98bf commit eb52690
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions test/rhsmlib/test_file_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit eb52690

Please sign in to comment.