Module: check_mk
Branch: master
Commit: 0652c935f9763a654b894de2387c479647535af9
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0652c935f9763a…
Author: Konstantin Büttner <kb(a)mathias-kettner.de>
Date: Mon Dec 18 12:24:51 2017 +0100
CompoundCheckResult: Provide a way to check for the existence of a matching subresult
Change-Id: I7ea505752c1168da4784375330b38429c13c88ed
---
tests/checks/checktestlib.py | 31 +++++++++++++++++++++++++++++--
tests/checks/test_statgrab_cpu_check.py | 3 ++-
2 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/tests/checks/checktestlib.py b/tests/checks/checktestlib.py
index 221ce7b..d785241 100644
--- a/tests/checks/checktestlib.py
+++ b/tests/checks/checktestlib.py
@@ -42,14 +42,28 @@ class BasicCheckResult(object):
"""Check whether this result's perfdata matches a given value.
Exact match."""
return expected_perfdata == self.perfdata
- def assert_result(self, expected_result):
- """Assert that a result matches certain criteria
+ def match(self, expected_result):
+ """Check whether a result matches certain criteria
expected_result is a Dictionary defining the criteria, allowing to not
rigidly define every detail about the check result, but only what we
really want to test. Unset fields mean we don't care.
"""
+ if "status" in expected_result and not
match_status(expected_result["status"]):
+ return False
+ if "infotext" in expected_result and not
match_infotext(expected_result["infotext"]):
+ return False
+ if "perfdata" in expected_result and not
match_perfdata(expected_result["perfdata"]):
+ return False
+ return True
+
+ def assert_result(self, expected_result):
+ """Assert that a result matches certain criteria
+
+ expected_result works as in match_result
+ """
+
if "status" in expected_result:
assert match_status(expected_result["status"])
if "infotext" in expected_result:
@@ -67,3 +81,16 @@ class CompoundCheckResult(object):
self.subresults = []
for subresult in result:
self.subresults.append(BasicCheckResult(*subresult))
+
+ def match_subresult(self, expected_result):
+ """Checks whether a subresult matching certain criteria is
contained in this compound result"""
+
+ for subresult in self.subresults:
+ if subresult.match(expected_result):
+ return True
+ return False
+
+ def assert_result(self, expected_result):
+ """Assert that a subresult matching certain criteria is contained
in this compound result"""
+
+ assert self.match_subresult(expected_result)
diff --git a/tests/checks/test_statgrab_cpu_check.py
b/tests/checks/test_statgrab_cpu_check.py
index 446aa27..ed41637 100644
--- a/tests/checks/test_statgrab_cpu_check.py
+++ b/tests/checks/test_statgrab_cpu_check.py
@@ -21,7 +21,7 @@ pytestmark = pytest.mark.checks
[u'total', u'%d' % int(t*50)],
[u'user', u'%d' % int(t*50)],
[u'vctxsw', u'%d' % int(t*50)]],
- {}, None),
+ {}, {}),
])
def test_statgrab_cpu_check(check_manager, monkeypatch, time_to_info, params,
expected_result):
import time
@@ -32,3 +32,4 @@ def test_statgrab_cpu_check(check_manager, monkeypatch, time_to_info,
params, ex
pass
monkeypatch.setattr("time.time", lambda: 60)
result = checktestlib.CompoundCheckResult(check.run_check(None, params,
time_to_info(60)))
+ result.assert_result(expected_result)