Module: check_mk
Branch: master
Commit: 25be559f26593b57d81b04100d6765bc22213733
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=25be559f26593b…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Apr 4 15:32:40 2019 +0200
Fixed broken test which should find left over debug code
Change-Id: Ie3db511a4dd471821fe70886ef62e7069aa75cf4
---
tests/git/test_find_debug_print.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/git/test_find_debug_print.py b/tests/git/test_find_debug_print.py
index 733bda6..3dcc45b 100644
--- a/tests/git/test_find_debug_print.py
+++ b/tests/git/test_find_debug_print.py
@@ -38,7 +38,7 @@ def test_find_debug_code():
scanned += 1
for filename in filenames:
file_path = "%s/%s" % (dirpath, filename)
- if [folder in file_path for folder in exclude_folders]:
+ if [folder for folder in exclude_folders if folder in file_path]:
continue
for nr, line in enumerate(open(file_path)):
Module: check_mk
Branch: master
Commit: a73bb61628d32fab00dd8d6d897cdee055763758
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=a73bb61628d32f…
Author: Florian Kromer <fk(a)mathias-kettner.de>
Date: Thu Apr 4 12:43:16 2019 +0200
allow to freeze the time in generic dataset tests
Change-Id: I2e51c91ae36c6359e0998de71ce6c43eecba17cd
---
tests/unit/checks/generictests/__init__.py | 5 +-
.../unit/checks/generictests/datasets/uptime_1.py | 18 +++++-
tests/unit/checks/generictests/regression.py | 2 +
tests/unit/checks/generictests/run.py | 66 ++++++++++++++--------
4 files changed, 65 insertions(+), 26 deletions(-)
diff --git a/tests/unit/checks/generictests/__init__.py b/tests/unit/checks/generictests/__init__.py
index d9873c2..8566940 100644
--- a/tests/unit/checks/generictests/__init__.py
+++ b/tests/unit/checks/generictests/__init__.py
@@ -8,7 +8,7 @@ or specified as explicit modules (as in the datasets subpackage).
The minimal compliant dataset must have the attribute
- * `checkname` (str) : The name of the (main) check.
+ * `checkname` : [str] The name of the (main) check.
In order to actually do something, at least one of the following attributes
must be present. They are optional (but you must provide some input for the
@@ -37,6 +37,9 @@ check function(s)). In the following, 'sc-dict' refers to a dictionary with
Some more advanced ones are
+ * `freeze_time` : [str] Mocked time.
+ If present, its value are passed to
+ `freezegun.freeze_time`
* `extra_sections` : [sc-dict] Extra sections that are appended to the info
/ parsed variable.
If present, its values are extra sections (the actual
diff --git a/tests/unit/checks/generictests/datasets/uptime_1.py b/tests/unit/checks/generictests/datasets/uptime_1.py
index a3fc269..17ca1e3 100644
--- a/tests/unit/checks/generictests/datasets/uptime_1.py
+++ b/tests/unit/checks/generictests/datasets/uptime_1.py
@@ -1,4 +1,20 @@
# yapf: disable
+
+
checkname = 'uptime'
-info = [['3707973.23', '271038018.98']]
+
+freeze_time = '1970-02-12 22:59:33'
+
+
+info = [['3707973.23', '3707973.23']]
+
+
+discovery = {'': [(None, {})]}
+
+
+checks = {'': [(None,
+ {},
+ [(0,
+ 'Up since Thu Jan 1 01:59:59 1970 (42d 21:59:33)',
+ [('uptime', 3707973.23, None, None, None, None)])])]}
\ No newline at end of file
diff --git a/tests/unit/checks/generictests/regression.py b/tests/unit/checks/generictests/regression.py
index 5d05853..ce1d580 100644
--- a/tests/unit/checks/generictests/regression.py
+++ b/tests/unit/checks/generictests/regression.py
@@ -50,6 +50,7 @@ class WritableDataset(object):
self.filename = filename
self.writelist = (
'checkname',
+ 'freeze_time',
'info',
'parsed',
'discovery',
@@ -60,6 +61,7 @@ class WritableDataset(object):
)
self.checkname = init_dict.get('checkname', None)
self.info = init_dict.get('info', None)
+ self.freeze_time = init_dict.get('freeze_time', None)
self.parsed = init_dict.get('parsed', None)
self.discovery = init_dict.get('discovery', {})
self.checks = {}
diff --git a/tests/unit/checks/generictests/run.py b/tests/unit/checks/generictests/run.py
index 82be247..9d9bb8e 100644
--- a/tests/unit/checks/generictests/run.py
+++ b/tests/unit/checks/generictests/run.py
@@ -7,6 +7,8 @@ from checktestlib import DiscoveryResult, assertDiscoveryResultsEqual, \
Immutables, assertEqual
from testlib import MissingCheckInfoError
from generictests.checkhandler import checkhandler
+from contextlib import contextmanager
+import freezegun
class DiscoveryParameterTypeError(AssertionError):
@@ -163,6 +165,20 @@ def check_listed_result(check, list_entry, info_arg, immu):
assertCheckResultsEqual(result, result_expected)
+@contextmanager
+def optional_freeze_time(dataset):
+ """Optionally freeze of the time in generic dataset tests
+
+ If present and truish the datasets freeze_time attribute is passed to
+ freezegun.freeze_time.
+ """
+ if getattr(dataset, 'freeze_time', None):
+ with freezegun.freeze_time(dataset.freeze_time):
+ yield
+ else:
+ yield
+
+
def run(check_manager, dataset, write=False):
"""Run all possible tests on 'dataset'"""
print("START: %r" % dataset)
@@ -177,29 +193,31 @@ def run(check_manager, dataset, write=False):
# get the expected check results, if present
checks_expected = getattr(dataset, 'checks', {})
- # LOOP OVER ALL (SUB)CHECKS
- for sname in checklist:
- subcheck = (sname + '.').split('.')[1]
- check = check_manager.get_check(sname)
-
- info_arg = get_info_argument(dataset, subcheck, parsed)
- immu.test(' after get_info_argument ')
- immu.register(info_arg, 'info_arg')
-
- mock_is, mock_hec = get_mock_values(dataset, subcheck)
-
- with MockItemState(mock_is), MockHostExtraConf(check, mock_hec):
- # test discovery
- d_result = discovery(check, subcheck, dataset, info_arg, immu)
- if write:
- dataset.discovery[subcheck] = [e.tuple for e in d_result]
- # test checks
- for dr in d_result:
- cdr = check_discovered_result(check, dr, info_arg, immu)
- if write and cdr:
- dataset.checks.setdefault(subcheck, []).append(cdr)
- if not write:
- for entry in checks_expected.get(subcheck, []):
- check_listed_result(check, entry, info_arg, immu)
+ with optional_freeze_time(dataset):
+ # LOOP OVER ALL (SUB)CHECKS
+ for sname in checklist:
+ subcheck = (sname + '.').split('.')[1]
+ check = check_manager.get_check(sname)
+
+ info_arg = get_info_argument(dataset, subcheck, parsed)
+ immu.test(' after get_info_argument ')
+ immu.register(info_arg, 'info_arg')
+
+ mock_is, mock_hec = get_mock_values(dataset, subcheck)
+
+ with MockItemState(mock_is), MockHostExtraConf(check, mock_hec):
+ # test discovery
+ d_result = discovery(check, subcheck, dataset, info_arg, immu)
+ if write:
+ dataset.discovery[subcheck] = [e.tuple for e in d_result]
+ # test checks
+ for dr in d_result:
+ cdr = check_discovered_result(check, dr, info_arg, immu)
+ if write and cdr:
+ dataset.checks.setdefault(subcheck, []).append(cdr)
+ if not write:
+ for entry in checks_expected.get(subcheck, []):
+
+ check_listed_result(check, entry, info_arg, immu)
immu.test(' at end of subcheck loop %r ' % subcheck)