From 9b83a3128d16fb545798283850a2760036915234 Mon Sep 17 00:00:00 2001 From: sebres Date: Wed, 17 May 2017 12:23:28 +0200 Subject: [PATCH] code review, try to increase coverage --- fail2ban/server/filterpyinotify.py | 7 ++----- fail2ban/tests/filtertestcase.py | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/fail2ban/server/filterpyinotify.py b/fail2ban/server/filterpyinotify.py index 71540f3c..d945fc2d 100644 --- a/fail2ban/server/filterpyinotify.py +++ b/fail2ban/server/filterpyinotify.py @@ -115,7 +115,7 @@ class FilterPyinotify(FileFilter): # watch was removed for some reasons (log-rotate?): if isWD and (assumeNoDir or not os.path.isdir(path)): self._addPending(path, event, isDir=True) - elif not isWF: + elif not isWF: # pragma: no cover (assume too sporadic) for logpath in self.__watchDirs: if logpath.startswith(path + pathsep) and (assumeNoDir or not os.path.isdir(logpath)): self._addPending(logpath, event, isDir=True) @@ -182,14 +182,11 @@ class FilterPyinotify(FileFilter): logSys.log(logging.MSG, "Log presence detected for %s %s", "directory" if isDir else "file", path) found[path] = isDir - for path in found: - try: - del self.__pending[path] - except KeyError: pass self.__pendingChkTime = time.time() self.__pendingNextTime = self.__pendingChkTime + minTime # process now because we've missed it in monitoring: for path, isDir in found.iteritems(): + self._delPending(path) # refresh monitoring of this: self._refreshWatcher(path, isDir=isDir) if isDir: diff --git a/fail2ban/tests/filtertestcase.py b/fail2ban/tests/filtertestcase.py index ec2dea89..2bb64132 100644 --- a/fail2ban/tests/filtertestcase.py +++ b/fail2ban/tests/filtertestcase.py @@ -973,6 +973,7 @@ def get_monitor_failures_testcase(Filter_): @with_tmpdir def test_move_dir(self, tmp): self.file.close() + self.filter.setMaxRetry(10) self.filter.delLogPath(self.name) # if we rename parent dir into a new location (simulate directory-base log rotation) tmpsub1 = os.path.join(tmp, "1") @@ -987,13 +988,25 @@ def get_monitor_failures_testcase(Filter_): self.file.close() self._wait4failures(1) - # rotate whole directory: rename directory 1 as 2: - os.rename(tmpsub1, tmpsub2) + # rotate whole directory: rename directory 1 as 2a: + os.rename(tmpsub1, tmpsub2 + 'a') os.mkdir(tmpsub1) self.file = _copy_lines_between_files(GetFailures.FILENAME_01, self.name, skip=12, n=1, mode='w') self.file.close() self._wait4failures(2) + + # rotate whole directory: rename directory 1 as 2b: + os.rename(tmpsub1, tmpsub2 + 'b') + # wait a bit in-between (try to increase coverage, should find pending file for pending dir): + self.waitForTicks(2) + os.mkdir(tmpsub1) + self.waitForTicks(2) + self.file = _copy_lines_between_files(GetFailures.FILENAME_01, self.name, + skip=12, n=1, mode='w') + self.file.close() + self._wait4failures(3) + # stop before tmpdir deleted (just prevents many monitor events) self.filter.stop()