coverage for get ban --with-time

pull/2362/head
sebres 6 years ago
parent e44cd671b2
commit e046b09c8a

@ -41,7 +41,7 @@ from ..server.jailthread import JailThread
from ..server.ticket import BanTicket from ..server.ticket import BanTicket
from ..server.utils import Utils from ..server.utils import Utils
from .dummyjail import DummyJail from .dummyjail import DummyJail
from .utils import LogCaptureTestCase from .utils import LogCaptureTestCase, with_alt_time, MyTime
from ..helpers import getLogger, extractOptions, PREFER_ENC from ..helpers import getLogger, extractOptions, PREFER_ENC
from .. import version from .. import version
@ -374,6 +374,7 @@ class Transmitter(TransmitterBase):
self.assertLogged("Ban 192.0.2.2", wait=True) self.assertLogged("Ban 192.0.2.2", wait=True)
self.assertNotLogged("Ban 192.0.2.1") self.assertNotLogged("Ban 192.0.2.1")
@with_alt_time
def testJailBanList(self): def testJailBanList(self):
jail = "TestJailBanList" jail = "TestJailBanList"
self.server.addJail(jail, FAST_BACKEND) self.server.addJail(jail, FAST_BACKEND)
@ -381,7 +382,7 @@ class Transmitter(TransmitterBase):
# Helper to process set banip/set unbanip commands and compare the list of # Helper to process set banip/set unbanip commands and compare the list of
# banned IP addresses with outList. # banned IP addresses with outList.
def _getBanListTest(jail, banip=None, unbanip=None, outList=[]): def _getBanListTest(jail, banip=None, unbanip=None, args=(), outList=[]):
# Ban IP address # Ban IP address
if banip is not None: if banip is not None:
self.assertEqual( self.assertEqual(
@ -396,15 +397,18 @@ class Transmitter(TransmitterBase):
self.assertLogged("Unban %s" % unbanip, wait=True) # Give chance to unban self.assertLogged("Unban %s" % unbanip, wait=True) # Give chance to unban
# Compare the list of banned IP addresses with outList # Compare the list of banned IP addresses with outList
self.assertSortedEqual( self.assertSortedEqual(
self.transm.proceed(["get", jail, "banip"]), self.transm.proceed(["get", jail, "banip"]+list(args)),
(0, outList)) (0, outList), nestedOnly=False)
MyTime.setTime(MyTime.time() + 1)
_getBanListTest(jail, _getBanListTest(jail,
outList=[]) outList=[])
_getBanListTest(jail, banip="127.0.0.1", _getBanListTest(jail, banip="127.0.0.1", args=('--with-time',),
outList=["127.0.0.1"]) outList=["127.0.0.1 \t2005-08-14 12:00:01 + 600 = 2005-08-14 12:10:01"])
_getBanListTest(jail, banip="192.168.0.1", _getBanListTest(jail, banip="192.168.0.1", args=('--with-time',),
outList=["127.0.0.1", "192.168.0.1"]) outList=[
"127.0.0.1 \t2005-08-14 12:00:01 + 600 = 2005-08-14 12:10:01",
"192.168.0.1 \t2005-08-14 12:00:02 + 600 = 2005-08-14 12:10:02"])
_getBanListTest(jail, banip="192.168.1.10", _getBanListTest(jail, banip="192.168.1.10",
outList=["127.0.0.1", "192.168.0.1", "192.168.1.10"]) outList=["127.0.0.1", "192.168.0.1", "192.168.1.10"])
_getBanListTest(jail, unbanip="127.0.0.1", _getBanListTest(jail, unbanip="127.0.0.1",

@ -245,6 +245,17 @@ def with_tmpdir(f):
shutil.rmtree(tmp) shutil.rmtree(tmp)
return wrapper return wrapper
def with_alt_time(f):
"""Helper decorator to execute test in alternate (fixed) test time."""
@wraps(f)
def wrapper(self, *args, **kwargs):
setUpMyTime()
try:
return f(self, *args, **kwargs)
finally:
tearDownMyTime()
return wrapper
# backwards compatibility to python 2.6: # backwards compatibility to python 2.6:
if not hasattr(unittest, 'SkipTest'): # pragma: no cover if not hasattr(unittest, 'SkipTest'): # pragma: no cover

Loading…
Cancel
Save