From c6e8c700f7f712d9b5c93c125bdcefbef670c7ad Mon Sep 17 00:00:00 2001 From: sebres Date: Tue, 22 Nov 2016 13:57:06 +0100 Subject: [PATCH] test cases fixed --- fail2ban/client/jailreader.py | 14 +++++++++----- fail2ban/tests/clientreadertestcase.py | 17 +++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/fail2ban/client/jailreader.py b/fail2ban/client/jailreader.py index df6c6664..74072481 100644 --- a/fail2ban/client/jailreader.py +++ b/fail2ban/client/jailreader.py @@ -127,9 +127,11 @@ class JailReader(ConfigReader): if self.isEnabled(): # Read filter - if self.__opts["filter"]: - filterName, filterOpt = JailReader.extractOptions( - self.__opts["filter"]) + flt = self.__opts["filter"] + if flt: + filterName, filterOpt = JailReader.extractOptions(flt) + if not filterName: + raise ValueError("Invalid filter declaration %r" % flt) self.__filter = FilterReader( filterName, self.__name, filterOpt, share_config=self.share_config, basedir=self.getBaseDir()) ret = self.__filter.read() @@ -157,6 +159,8 @@ class JailReader(ConfigReader): if not act: # skip empty actions continue actName, actOpt = JailReader.extractOptions(act) + if not actName: + raise ValueError("Invalid action declaration %r" % act) if actName.endswith(".py"): self.__actions.append([ "set", @@ -178,8 +182,8 @@ class JailReader(ConfigReader): else: raise AttributeError("Unable to read action") except Exception as e: - logSys.debug("Caught exception: %s" % (e,)) - raise ValueError("Error in action definition %r" % e) + logSys.debug("Caught exception: %s", e, exc_info=True) + raise ValueError("Error in action definition %r: %r" % (act, e)) if not len(self.__actions): logSys.warning("No actions were defined for %s" % self.__name) diff --git a/fail2ban/tests/clientreadertestcase.py b/fail2ban/tests/clientreadertestcase.py index e68523c2..20d6ced1 100644 --- a/fail2ban/tests/clientreadertestcase.py +++ b/fail2ban/tests/clientreadertestcase.py @@ -193,13 +193,9 @@ class JailReaderTest(LogCaptureTestCase): self.assertTrue(jail.read()) self.assertFalse(jail.getOptions()) self.assertTrue(jail.isEnabled()) - self.assertLogged('Error in action definition joho[foo') - # This unittest has been deactivated for some time... - # self.assertLogged( - # 'Caught exception: While reading action joho[foo we should have got 1 or 2 groups. Got: 0') - # let's test for what is actually logged and handle changes in the future + self.assertLogged("Error in action definition 'joho[foo'") self.assertLogged( - "Caught exception: 'NoneType' object has no attribute 'endswith'") + "Caught exception: Invalid action declaration 'joho[foo'") if STOCK: def testStockSSHJail(self): @@ -496,7 +492,7 @@ class JailsReaderTest(LogCaptureTestCase): def testReadTestJailConf(self): jails = JailsReader(basedir=IMPERFECT_CONFIG, share_config=IMPERFECT_CONFIG_SHARE_CFG) self.assertTrue(jails.read()) - self.assertFalse(jails.getOptions()) + self.assertTrue(jails.getOptions()) self.assertRaises(ValueError, jails.convert) comm_commands = jails.convert(allow_no_files=True) self.maxDiff = None @@ -525,7 +521,12 @@ class JailsReaderTest(LogCaptureTestCase): ['start', 'emptyaction'], ['start', 'missinglogfiles'], ['start', 'brokenaction'], - ['start', 'parse_to_end_of_jail.conf'],])) + ['start', 'parse_to_end_of_jail.conf'], + ['config-error', + 'Jail \'brokenactiondef\' skipped, because of wrong configuration: Error in action definition \'joho[foo\': ValueError("Invalid action declaration \'joho[foo\'",)'], + ['config-error', + "Jail 'missingbitsjail' skipped, because of wrong configuration: Unable to read the filter 'catchallthebadies'"], + ])) self.assertLogged("Errors in jail 'missingbitsjail'. Skipping...") self.assertLogged("No file(s) found for glob /weapons/of/mass/destruction")