Last use removed in 53843e5ca when the job status watching logic moved from jqueue to luxid.
Signed-off-by: Brian Foley <[email protected]> --- lib/asyncnotifier.py | 66 ---------------- test/py/ganeti.asyncnotifier_unittest.py | 124 ------------------------------- 2 files changed, 190 deletions(-) diff --git a/lib/asyncnotifier.py b/lib/asyncnotifier.py index 6053bef..e588651 100644 --- a/lib/asyncnotifier.py +++ b/lib/asyncnotifier.py @@ -136,69 +136,3 @@ class FileEventHandlerBase(pyinotify.ProcessEvent): return result[handle] -class SingleFileEventHandler(FileEventHandlerBase): - """Handle modify events for a single file. - - """ - def __init__(self, watch_manager, callback, filename): - """Constructor for SingleFileEventHandler - - @type watch_manager: pyinotify.WatchManager - @param watch_manager: inotify watch manager - @type callback: function accepting a boolean - @param callback: function to call when an inotify event happens - @type filename: string - @param filename: config file to watch - - """ - FileEventHandlerBase.__init__(self, watch_manager) - - self._callback = callback - self._filename = filename - - self._watch_handle = None - - def enable(self): - """Watch the given file. - - """ - if self._watch_handle is not None: - return - - # Different Pyinotify versions have the flag constants at different places, - # hence not accessing them directly - mask = (pyinotify.EventsCodes.ALL_FLAGS["IN_MODIFY"] | - pyinotify.EventsCodes.ALL_FLAGS["IN_IGNORED"]) - - self._watch_handle = self.AddWatch(self._filename, mask) - - def disable(self): - """Stop watching the given file. - - """ - if self._watch_handle is not None and self.RemoveWatch(self._watch_handle): - self._watch_handle = None - - # pylint: disable=C0103 - # this overrides a method in pyinotify.ProcessEvent - def process_IN_IGNORED(self, event): - # Since we monitor a single file rather than the directory it resides in, - # when that file is replaced with another one (which is what happens when - # utils.WriteFile, the most normal way of updating files in ganeti, is - # called) we're going to receive an IN_IGNORED event from inotify, because - # of the file removal (which is contextual with the replacement). In such a - # case we'll need to create a watcher for the "new" file. This can be done - # by the callback by calling "enable" again on us. - logging.debug("Received 'ignored' inotify event for %s", event.path) - self._watch_handle = None - self._callback(False) - - # pylint: disable=C0103 - # this overrides a method in pyinotify.ProcessEvent - def process_IN_MODIFY(self, event): - # This gets called when the monitored file is modified. Note that this - # doesn't usually happen in Ganeti, as most of the time we're just - # replacing any file with a new one, at filesystem level, rather than - # actually changing it. (see utils.WriteFile) - logging.debug("Received 'modify' inotify event for %s", event.path) - self._callback(True) diff --git a/test/py/ganeti.asyncnotifier_unittest.py b/test/py/ganeti.asyncnotifier_unittest.py index 2b3098f..f2c01f2 100755 --- a/test/py/ganeti.asyncnotifier_unittest.py +++ b/test/py/ganeti.asyncnotifier_unittest.py @@ -60,129 +60,5 @@ class _MyErrorLoggingAsyncNotifier(asyncnotifier.ErrorLoggingAsyncNotifier): raise -class TestSingleFileEventHandler(testutils.GanetiTestCase): - """Test daemon.Mainloop""" - - NOTIFIERS = [NOTIFIER_TERM, NOTIFIER_NORM, NOTIFIER_ERR] = range(3) - - def setUp(self): - testutils.GanetiTestCase.setUp(self) - self.mainloop = daemon.Mainloop() - self.chk_files = [self._CreateTempFile() for i in self.NOTIFIERS] - self.notified = [False for i in self.NOTIFIERS] - # We need one watch manager per notifier, as those contain the file - # descriptor which is monitored by asyncore - self.wms = [pyinotify.WatchManager() for i in self.NOTIFIERS] - self.cbk = [self.OnInotifyCallback(self, i) for i in self.NOTIFIERS] - self.ihandler = [asyncnotifier.SingleFileEventHandler(wm, cb, cf) - for (wm, cb, cf) in - zip(self.wms, self.cbk, self.chk_files)] - self.notifiers = [_MyErrorLoggingAsyncNotifier(wm, ih) - for (wm, ih) in zip(self.wms, self.ihandler)] - # TERM notifier is enabled by default, as we use it to get out of the loop - self.ihandler[self.NOTIFIER_TERM].enable() - - def tearDown(self): - # disable the inotifiers, before removing the files - for i in self.ihandler: - i.disable() - testutils.GanetiTestCase.tearDown(self) - # and unregister the fd's being polled - for n in self.notifiers: - n.del_channel() - - class OnInotifyCallback: - def __init__(self, testobj, i): - self.testobj = testobj - self.notified = testobj.notified - self.i = i - - def __call__(self, enabled): - self.notified[self.i] = True - if self.i == self.testobj.NOTIFIER_TERM: - os.kill(os.getpid(), signal.SIGTERM) - elif self.i == self.testobj.NOTIFIER_ERR: - raise errors.GenericError("an error") - - def testReplace(self): - utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") - self.mainloop.Run() - self.assert_(self.notified[self.NOTIFIER_TERM]) - self.assertFalse(self.notified[self.NOTIFIER_NORM]) - self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0) - self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0) - - def testEnableDisable(self): - self.ihandler[self.NOTIFIER_TERM].enable() - self.ihandler[self.NOTIFIER_TERM].disable() - self.ihandler[self.NOTIFIER_TERM].disable() - self.ihandler[self.NOTIFIER_TERM].enable() - self.ihandler[self.NOTIFIER_TERM].disable() - self.ihandler[self.NOTIFIER_TERM].enable() - utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") - self.mainloop.Run() - self.assert_(self.notified[self.NOTIFIER_TERM]) - self.assertFalse(self.notified[self.NOTIFIER_NORM]) - self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0) - self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0) - - def testDoubleEnable(self): - self.ihandler[self.NOTIFIER_TERM].enable() - self.ihandler[self.NOTIFIER_TERM].enable() - utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") - self.mainloop.Run() - self.assert_(self.notified[self.NOTIFIER_TERM]) - self.assertFalse(self.notified[self.NOTIFIER_NORM]) - self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0) - self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0) - - def testDefaultDisabled(self): - utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy") - utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") - self.mainloop.Run() - self.assert_(self.notified[self.NOTIFIER_TERM]) - # NORM notifier is disabled by default - self.assertFalse(self.notified[self.NOTIFIER_NORM]) - self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0) - self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0) - - def testBothEnabled(self): - self.ihandler[self.NOTIFIER_NORM].enable() - utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy") - utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") - self.mainloop.Run() - self.assert_(self.notified[self.NOTIFIER_TERM]) - self.assert_(self.notified[self.NOTIFIER_NORM]) - self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0) - self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0) - - def testError(self): - self.ihandler[self.NOTIFIER_ERR].enable() - utils.WriteFile(self.chk_files[self.NOTIFIER_ERR], data="dummy") - self.assertRaises(errors.GenericError, self.mainloop.Run) - self.assert_(self.notified[self.NOTIFIER_ERR]) - self.assertEquals(self.notifiers[self.NOTIFIER_ERR].error_count, 1) - self.assertEquals(self.notifiers[self.NOTIFIER_NORM].error_count, 0) - self.assertEquals(self.notifiers[self.NOTIFIER_TERM].error_count, 0) - - -class TestSingleFileEventHandlerError(unittest.TestCase): - def setUp(self): - self.tmpdir = tempfile.mkdtemp() - - def tearDown(self): - shutil.rmtree(self.tmpdir) - - def test(self): - wm = pyinotify.WatchManager() - handler = asyncnotifier.SingleFileEventHandler(wm, None, - utils.PathJoin(self.tmpdir, - "nonexist")) - self.assertRaises(errors.InotifyError, handler.enable) - self.assertRaises(errors.InotifyError, handler.enable) - handler.disable() - self.assertRaises(errors.InotifyError, handler.enable) - - if __name__ == "__main__": testutils.GanetiTestProgram() -- 2.8.0.rc3.226.g39d4020
