better-organized unit tests

master
Y 2018-01-31 08:28:05 +01:00
parent 98be1c89db
commit 2680405c89
11 changed files with 79 additions and 58 deletions

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import time
from pyruse.actions.action_counterRaise import Action
@ -50,3 +50,9 @@ def whenGraceTimeThenCountIs0():
time.sleep(1)
raiseAct.act(entry)
assert entry["action_counterRaise4"] == 1
def unitTests():
whenNonExistingThenRaiseTo1()
whenKeepSecondsThenRaiseUntilTimeOut()
whenDifferentKeyThenDifferentCounter()
whenGraceTimeThenCountIs0()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import time
from pyruse.actions.action_counterReset import Action
@ -49,3 +49,9 @@ def whenGraceTimeThenRaiseWorksAtGraceEnd():
time.sleep(1)
raiseAct.act(entry)
assert entry["action_counterReset4"] == 1
def unitTests():
whenResetThenCountIs0()
whenNoGraceTimeThenRaiseWorks()
whenGraceTimeThenRaiseFails()
whenGraceTimeThenRaiseWorksAtGraceEnd()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import os
import re
@ -78,3 +78,8 @@ def whenReportThenNewSetOfMessages():
assert os.path.exists(mail_filename)
os.remove(mail_filename)
whenEmailThenCheckContents()
def unitTests():
whenNewDayThenReport()
whenEmailThenCheckContents()
whenReportThenNewSetOfMessages()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import os
import re
@ -71,3 +71,7 @@ def whenEmailWithoutSubjectThenCheckContents():
assert toOK
assert nbMsg == 1
os.remove(mail_filename)
def unitTests():
whenEmailWithSubjectThenCheckContents()
whenEmailWithoutSubjectThenCheckContents()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import json
import os
@ -137,3 +137,11 @@ def whenUnfinishedBanThenTimeoutReset():
assert line == "add element I4 ban {10.0.0.1 timeout 2s}\n", line
assert lineCount == 3, lineCount
_clean()
def unitTests():
whenBanIPv4ThenAddToIPv4Set()
whenBanIPv6ThenAddToIPv6Set()
whenBanTwoIPThenTwoLinesInState()
whenBanAnewThenNoDuplicate()
whenFinishedBanThenAsIfNotThere()
whenUnfinishedBanThenTimeoutReset()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_equals import Filter
@ -14,3 +14,9 @@ def whenEqualDiffTypeThenTrue():
def whenLowerThenFalse():
assert not Filter({"field": "v", "value": 2}).filter({"v": 0})
def unitTests():
whenGreaterThenFalse()
whenEqualSameTypeThenTrue()
whenEqualDiffTypeThenTrue()
whenLowerThenFalse()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_greaterOrEquals import Filter
@ -17,3 +17,10 @@ def whenEqualDiffTypeThenTrue():
def whenLowerThenFalse():
assert not Filter({"field": "v", "value": 2}).filter({"v": 0})
def unitTests():
whenGreaterPosIntThenTrue()
whenGreaterNegFloatThenTrue()
whenEqualSameTypeThenTrue()
whenEqualDiffTypeThenTrue()
whenLowerThenFalse()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_pcre import Filter
@ -18,3 +18,9 @@ def whenNamedGroupsThenFoundInEntry():
entry = {"v": "yet another test"}
Filter({"field": "v", "re": "^(?P<y>.).* .*(?P<e>.)r .*(?P<s>.).$"}).filter(entry)
assert entry["y"] + entry["e"] + entry["s"] == "yes"
def unitTests():
whenMatchesThenTrue()
whenNoMatchThenFalse()
whenSaveThenGroupsInEntry()
whenNamedGroupsThenFoundInEntry()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_pcreAny import Filter
@ -13,3 +13,8 @@ def whenNamedGroupsThenFoundInEntry():
entry = {"v": "It works or not"}
Filter({"field": "v", "re": ["^(?P<o>It)(?P<k> works)", "(?P<k>or)(?P<o> not)$"]}).filter(entry)
assert entry["o"] + entry["k"] == "It works"
def unitTests():
whenMatchesThenTrue()
whenNoMatchThenFalse()
whenNamedGroupsThenFoundInEntry()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
from pyruse.filters.filter_userExists import Filter
@ -8,3 +8,7 @@ def whenUserExistsThenTrue():
def whenGarbageThenFalse():
assert not Filter({"field": "user"}).filter({"user": "auietsnr"})
def unitTests():
whenUserExistsThenTrue()
whenGarbageThenFalse()

View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
import os
import subprocess
@ -31,52 +31,16 @@ def main():
import filter_equals, filter_greaterOrEquals, filter_pcre, filter_pcreAny, filter_userExists
import action_counterRaise, action_counterReset, action_dailyReport, action_email, action_nftBan
filter_equals.whenGreaterThenFalse()
filter_equals.whenEqualSameTypeThenTrue()
filter_equals.whenEqualDiffTypeThenTrue()
filter_equals.whenLowerThenFalse()
filter_greaterOrEquals.whenGreaterPosIntThenTrue()
filter_greaterOrEquals.whenGreaterNegFloatThenTrue()
filter_greaterOrEquals.whenEqualSameTypeThenTrue()
filter_greaterOrEquals.whenEqualDiffTypeThenTrue()
filter_greaterOrEquals.whenLowerThenFalse()
filter_pcre.whenMatchesThenTrue()
filter_pcre.whenNoMatchThenFalse()
filter_pcre.whenSaveThenGroupsInEntry()
filter_pcre.whenNamedGroupsThenFoundInEntry()
filter_pcreAny.whenMatchesThenTrue()
filter_pcreAny.whenNoMatchThenFalse()
filter_pcreAny.whenNamedGroupsThenFoundInEntry()
filter_userExists.whenUserExistsThenTrue()
filter_userExists.whenGarbageThenFalse()
action_counterRaise.whenNonExistingThenRaiseTo1()
action_counterRaise.whenKeepSecondsThenRaiseUntilTimeOut()
action_counterRaise.whenDifferentKeyThenDifferentCounter()
action_counterRaise.whenGraceTimeThenCountIs0()
action_counterReset.whenResetThenCountIs0()
action_counterReset.whenNoGraceTimeThenRaiseWorks()
action_counterReset.whenGraceTimeThenRaiseFails()
action_counterReset.whenGraceTimeThenRaiseWorksAtGraceEnd()
action_dailyReport.whenNewDayThenReport()
action_dailyReport.whenEmailThenCheckContents()
action_dailyReport.whenReportThenNewSetOfMessages()
action_email.whenEmailWithSubjectThenCheckContents()
action_email.whenEmailWithoutSubjectThenCheckContents()
action_nftBan.whenBanIPv4ThenAddToIPv4Set()
action_nftBan.whenBanIPv6ThenAddToIPv6Set()
action_nftBan.whenBanTwoIPThenTwoLinesInState()
action_nftBan.whenBanAnewThenNoDuplicate()
action_nftBan.whenFinishedBanThenAsIfNotThere()
action_nftBan.whenUnfinishedBanThenTimeoutReset()
filter_equals.unitTests()
filter_greaterOrEquals.unitTests()
filter_pcre.unitTests()
filter_pcreAny.unitTests()
filter_userExists.unitTests()
action_counterRaise.unitTests()
action_counterReset.unitTests()
action_dailyReport.unitTests()
action_email.unitTests()
action_nftBan.unitTests()
# Integration test
wf = workflow.Workflow(conf.asMap().get("actions", {}))
@ -100,7 +64,7 @@ def main():
actions.action_dailyReport.Action._hour = 25
wf.run(entry("bck", "login", "Failed password for root from ::1", 11))
for f in ['acted_on.log', 'email.dump', 'nftBan.cmd', 'unfiltered.log']:
assert os.path.exists(f)
assert os.path.exists(f), "file should exist: " + f
try:
subprocess.run(
[ "/usr/bin/bash",