code review (e. g. remove code duplication) and coverage

pull/1577/head
sebres 2016-10-15 00:39:25 +02:00
parent 5b40309052
commit 7f8c48d59e
3 changed files with 25 additions and 24 deletions

View File

@ -310,7 +310,7 @@ class Fail2banClient(Fail2banCmdLine, Thread):
# stop options - jail name or --all # stop options - jail name or --all
break break
if self.__ping(timeout=-1): if self.__ping(timeout=-1):
if len(cmd) == 1: if len(cmd) == 1 or cmd[1] == '--all':
jail = '--all' jail = '--all'
ret, stream = self.readConfig() ret, stream = self.readConfig()
else: else:

View File

@ -81,12 +81,12 @@ class Server:
} }
self.__prev_signals = {} self.__prev_signals = {}
def __sigTERMhandler(self, signum, frame): def __sigTERMhandler(self, signum, frame): # pragma: no cover - indirect tested
logSys.debug("Caught signal %d. Exiting" % signum) logSys.debug("Caught signal %d. Exiting", signum)
self.quit() self.quit()
def __sigUSR1handler(self, signum, fname): def __sigUSR1handler(self, signum, fname): # pragma: no cover - indirect tested
logSys.debug("Caught signal %d. Flushing logs" % signum) logSys.debug("Caught signal %d. Flushing logs", signum)
self.flushLogs() self.flushLogs()
def _rebindSignal(self, s, new): def _rebindSignal(self, s, new):
@ -137,12 +137,12 @@ class Server:
# Creates a PID file. # Creates a PID file.
try: try:
logSys.debug("Creating PID file %s" % pidfile) logSys.debug("Creating PID file %s", pidfile)
pidFile = open(pidfile, 'w') pidFile = open(pidfile, 'w')
pidFile.write("%s\n" % os.getpid()) pidFile.write("%s\n" % os.getpid())
pidFile.close() pidFile.close()
except IOError as e: except (OSError, IOError) as e: # pragma: no cover
logSys.error("Unable to create PID file: %s" % e) logSys.error("Unable to create PID file: %s", e)
# Start the communication # Start the communication
logSys.debug("Starting communication") logSys.debug("Starting communication")
@ -154,10 +154,10 @@ class Server:
logSys.error("Could not start server: %s", e) logSys.error("Could not start server: %s", e)
# Removes the PID file. # Removes the PID file.
try: try:
logSys.debug("Remove PID file %s" % pidfile) logSys.debug("Remove PID file %s", pidfile)
os.remove(pidfile) os.remove(pidfile)
except OSError as e: except (OSError, IOError) as e: # pragma: no cover
logSys.error("Unable to remove PID file: %s" % e) logSys.error("Unable to remove PID file: %s", e)
logSys.info("Exiting Fail2ban") logSys.info("Exiting Fail2ban")
def quit(self): def quit(self):
@ -250,7 +250,7 @@ class Server:
def reloadJails(self, name, opts, begin): def reloadJails(self, name, opts, begin):
if begin: if begin:
# begin reload: # begin reload:
if self.__reload_state and (name == '--all' or self.__reload_state.get(name)): if self.__reload_state and (name == '--all' or self.__reload_state.get(name)): # pragma: no cover
raise ValueError('Reload already in progress') raise ValueError('Reload already in progress')
logSys.info("Reload " + (("jail %s" % name) if name != '--all' else "all jails")) logSys.info("Reload " + (("jail %s" % name) if name != '--all' else "all jails"))
with self.__lock: with self.__lock:
@ -381,11 +381,8 @@ class Server:
def addFailRegex(self, name, value, multiple=False): def addFailRegex(self, name, value, multiple=False):
flt = self.__jails[name].filter flt = self.__jails[name].filter
if multiple: if not multiple: value = (value,)
for value in value: for value in value:
logSys.debug(" failregex: %r", value)
flt.addFailRegex(value)
else:
logSys.debug(" failregex: %r", value) logSys.debug(" failregex: %r", value)
flt.addFailRegex(value) flt.addFailRegex(value)
@ -397,11 +394,8 @@ class Server:
def addIgnoreRegex(self, name, value, multiple=False): def addIgnoreRegex(self, name, value, multiple=False):
flt = self.__jails[name].filter flt = self.__jails[name].filter
if multiple: if not multiple: value = (value,)
for value in value: for value in value:
logSys.debug(" ignoreregex: %r", value)
flt.addIgnoreRegex(value)
else:
logSys.debug(" ignoreregex: %r", value) logSys.debug(" ignoreregex: %r", value)
flt.addIgnoreRegex(value) flt.addIgnoreRegex(value)
@ -671,7 +665,7 @@ class Server:
if Fail2BanDb is not None: if Fail2BanDb is not None:
self.__db = Fail2BanDb(filename) self.__db = Fail2BanDb(filename)
self.__db.delAllJails() self.__db.delAllJails()
else: else: # pragma: no cover
logSys.error( logSys.error(
"Unable to import fail2ban database module as sqlite " "Unable to import fail2ban database module as sqlite "
"is not available.") "is not available.")

View File

@ -1026,4 +1026,11 @@ class Fail2banServerTest(Fail2banClientServerBase):
self.assertNotLogged( self.assertNotLogged(
"Creating new jail 'test-jail2'", "Creating new jail 'test-jail2'",
"Jail 'test-jail2' started", all=True) "Jail 'test-jail2' started", all=True)
self.pruneLog()
# restart all jails (without restart server):
self.pruneLog("[test-phase end-2]")
self.execSuccess(startparams,
"--async", "reload", "--restart", "--all")
self.assertLogged(
"Jail 'test-jail1' stopped",
"Jail 'test-jail1' started", all=True)