mirror of https://github.com/fail2ban/fail2ban
BF: Tweak python action tests and fix Deprecation Warning
parent
8221c7ca71
commit
0fb7921fb1
|
@ -22,7 +22,11 @@ import smtpd
|
||||||
import asyncore
|
import asyncore
|
||||||
import threading
|
import threading
|
||||||
import unittest
|
import unittest
|
||||||
import imp
|
import sys
|
||||||
|
if sys.version_info >= (3, 3):
|
||||||
|
import importlib
|
||||||
|
else:
|
||||||
|
import imp
|
||||||
|
|
||||||
from ..dummyjail import DummyJail
|
from ..dummyjail import DummyJail
|
||||||
|
|
||||||
|
@ -46,7 +50,12 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
self.jail = DummyJail()
|
self.jail = DummyJail()
|
||||||
pythonModule = os.path.join(CONFIG_DIR, "action.d", "smtp.py")
|
pythonModule = os.path.join(CONFIG_DIR, "action.d", "smtp.py")
|
||||||
pythonModuleName = os.path.basename(pythonModule.rstrip(".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)
|
self.smtpd = TestSMTPServer(("localhost", 0), None)
|
||||||
port = self.smtpd.socket.getsockname()[1]
|
port = self.smtpd.socket.getsockname()[1]
|
||||||
|
@ -94,23 +103,19 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
"Subject: [Fail2Ban] %s: banned %s" %
|
"Subject: [Fail2Ban] %s: banned %s" %
|
||||||
(self.jail.getName(), aInfo['ip']) in self.smtpd.data)
|
(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.matches = "matches"
|
||||||
self.action.ban(aInfo)
|
self.action.ban(aInfo)
|
||||||
self.assertTrue(
|
|
||||||
"%i attempts" % aInfo['failures'] in self.smtpd.data)
|
|
||||||
self.assertTrue(aInfo['matches'] in self.smtpd.data)
|
self.assertTrue(aInfo['matches'] in self.smtpd.data)
|
||||||
|
|
||||||
self.action.matches = "ipjailmatches"
|
self.action.matches = "ipjailmatches"
|
||||||
self.action.ban(aInfo)
|
self.action.ban(aInfo)
|
||||||
self.assertTrue(
|
|
||||||
"%i attempts" % aInfo['failures'] in self.smtpd.data)
|
|
||||||
self.assertTrue(aInfo['ipjailmatches'] in self.smtpd.data)
|
self.assertTrue(aInfo['ipjailmatches'] in self.smtpd.data)
|
||||||
|
|
||||||
self.action.matches = "ipmatches"
|
self.action.matches = "ipmatches"
|
||||||
self.action.ban(aInfo)
|
self.action.ban(aInfo)
|
||||||
self.assertTrue(
|
|
||||||
"%i attempts" % aInfo['failures'] in self.smtpd.data)
|
|
||||||
self.assertTrue(aInfo['ipmatches'] in self.smtpd.data)
|
self.assertTrue(aInfo['ipmatches'] in self.smtpd.data)
|
||||||
|
|
||||||
def testOptions(self):
|
def testOptions(self):
|
||||||
|
|
|
@ -209,8 +209,7 @@ def gatherTests(regexps=None, no_network=False):
|
||||||
from . import action_d
|
from . import action_d
|
||||||
for file_ in os.listdir(
|
for file_ in os.listdir(
|
||||||
os.path.abspath(os.path.dirname(action_d.__file__))):
|
os.path.abspath(os.path.dirname(action_d.__file__))):
|
||||||
if file_.startswith("test_") and file_.endswith(".py") and \
|
if file_.startswith("test_") and file_.endswith(".py"):
|
||||||
file_ != "__init__.py":
|
|
||||||
tests.addTest(testloader.loadTestsFromName(
|
tests.addTest(testloader.loadTestsFromName(
|
||||||
"%s.%s" % (action_d.__name__, os.path.splitext(file_)[0])))
|
"%s.%s" % (action_d.__name__, os.path.splitext(file_)[0])))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue