Browse Source

better-organized unit tests

master
Y 3 years ago
parent
commit
2680405c89
11 changed files with 79 additions and 58 deletions
  1. +7
    -1
      tests/action_counterRaise.py
  2. +7
    -1
      tests/action_counterReset.py
  3. +6
    -1
      tests/action_dailyReport.py
  4. +5
    -1
      tests/action_email.py
  5. +9
    -1
      tests/action_nftBan.py
  6. +7
    -1
      tests/filter_equals.py
  7. +8
    -1
      tests/filter_greaterOrEquals.py
  8. +7
    -1
      tests/filter_pcre.py
  9. +6
    -1
      tests/filter_pcreAny.py
  10. +5
    -1
      tests/filter_userExists.py
  11. +12
    -48
      tests/main.py

+ 7
- 1
tests/action_counterRaise.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 7
- 1
tests/action_counterReset.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 6
- 1
tests/action_dailyReport.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 5
- 1
tests/action_email.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 9
- 1
tests/action_nftBan.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 7
- 1
tests/filter_equals.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 8
- 1
tests/filter_greaterOrEquals.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 7
- 1
tests/filter_pcre.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 6
- 1
tests/filter_pcreAny.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 5
- 1
tests/filter_userExists.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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()

+ 12
- 48
tests/main.py View File

@ -1,5 +1,5 @@
# pyruse is intended as a replacement to both fail2ban and epylog
# Copyright © 2017 Y. Gablin
# Copyright © 2017–2018 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",


Loading…
Cancel
Save