BF: Tweak python action tests and fix Deprecation Warning

pull/592/head
Steven Hiscocks 2014-01-20 23:09:00 +00:00
parent 8221c7ca71
commit 0fb7921fb1
2 changed files with 14 additions and 10 deletions

View File

@ -22,7 +22,11 @@ import smtpd
import asyncore
import threading
import unittest
import imp
import sys
if sys.version_info >= (3, 3):
import importlib
else:
import imp
from ..dummyjail import DummyJail
@ -46,7 +50,12 @@ class SMTPActionTest(unittest.TestCase):
self.jail = DummyJail()
pythonModule = os.path.join(CONFIG_DIR, "action.d", "smtp.py")
pythonModuleName = os.path.basename(pythonModule.rstrip(".py"))
customActionModule = imp.load_source(pythonModuleName, pythonModule)
if sys.version_info >= (3, 3):
customActionModule = importlib.machinery.SourceFileLoader(
pythonModuleName, pythonModule).load_module()
else:
customActionModule = imp.load_source(
pythonModuleName, pythonModule)
self.smtpd = TestSMTPServer(("localhost", 0), None)
port = self.smtpd.socket.getsockname()[1]
@ -94,23 +103,19 @@ class SMTPActionTest(unittest.TestCase):
self.assertTrue(
"Subject: [Fail2Ban] %s: banned %s" %
(self.jail.getName(), aInfo['ip']) in self.smtpd.data)
self.assertTrue(
"%i attempts" % aInfo['failures'] in self.smtpd.data)
self.action.matches = "matches"
self.action.ban(aInfo)
self.assertTrue(
"%i attempts" % aInfo['failures'] in self.smtpd.data)
self.assertTrue(aInfo['matches'] in self.smtpd.data)
self.action.matches = "ipjailmatches"
self.action.ban(aInfo)
self.assertTrue(
"%i attempts" % aInfo['failures'] in self.smtpd.data)
self.assertTrue(aInfo['ipjailmatches'] in self.smtpd.data)
self.action.matches = "ipmatches"
self.action.ban(aInfo)
self.assertTrue(
"%i attempts" % aInfo['failures'] in self.smtpd.data)
self.assertTrue(aInfo['ipmatches'] in self.smtpd.data)
def testOptions(self):

View File

@ -209,8 +209,7 @@ def gatherTests(regexps=None, no_network=False):
from . import action_d
for file_ in os.listdir(
os.path.abspath(os.path.dirname(action_d.__file__))):
if file_.startswith("test_") and file_.endswith(".py") and \
file_ != "__init__.py":
if file_.startswith("test_") and file_.endswith(".py"):
tests.addTest(testloader.loadTestsFromName(
"%s.%s" % (action_d.__name__, os.path.splitext(file_)[0])))