From 39e4f51a4d0b9140dddf0356a3bf68ed26ecb4ef Mon Sep 17 00:00:00 2001 From: Simon Meggle Date: Thu, 23 Jan 2020 11:15:35 +0000 Subject: [PATCH 1/2] Added WATO_DATA placeholder to module creation; enables monkeypatching of WATO functions and variables --- pytest_check_mk/file_loader.py | 31 ++++++++++++++++++++++++++++--- pytest_check_mk/plugin.py | 2 ++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/pytest_check_mk/file_loader.py b/pytest_check_mk/file_loader.py index b201d03..102614a 100644 --- a/pytest_check_mk/file_loader.py +++ b/pytest_check_mk/file_loader.py @@ -1,7 +1,6 @@ import imp import os - - +from string import Template from pytest_check_mk import MissingFileError @@ -31,8 +30,32 @@ def regex(r): snmp_scan_functions = {} # SNMP autodetection active_check_info = {} # definitions of active "legacy" checks special_agent_info = {} + ''' +# E.g. host_extra_conf_merged is a WATO function which is only available when the +# check is run in MK's context. The same applies to the inventory rules etc. +# We can monkeypatch these data in our test file only when they are present; monkey- +# patch cannot create new data. + +# The following code gets appended to the module. All runtime relevant data and +# functions must be initialised here. +# Example (file: test_foobar.py): +# mock_inventory_foobar_rules = [ ...(rules) ...] +# monkeypatch.setattr(checks.module, "inventory_foobar_rules", mock_inventory_foobar_rules) + +WATO_DATA = Template(''' +# function to retrieve host relevant config +def host_extra_conf_merged(self): + pass + +# WATO rules +inventory_${name}_rules = { } + +def host_name(): + return "localhost" +''') + def check_module_from_source(name, path): __tracebackhide__ = True @@ -40,7 +63,9 @@ def check_module_from_source(name, path): if not os.path.exists(path): raise MissingFileError(path) - source = open(path, 'r').read() + source = open(path, 'r').read() + WATO_DATA.substitute({ + 'name': name + }) code = compile(source, path, 'exec') module = imp.new_module(name) diff --git a/pytest_check_mk/plugin.py b/pytest_check_mk/plugin.py index ebc3918..a614bd9 100644 --- a/pytest_check_mk/plugin.py +++ b/pytest_check_mk/plugin.py @@ -13,9 +13,11 @@ def _get_check_name(request): @pytest.fixture def agents(request): + '''returns a checkMK agent instance''' return AgentDirectoryWrapper() @pytest.fixture def checks(request): + '''returns a checkMK check instance''' return create_check_file_wrapper(_get_check_name(request)) From c5ec625ab6bec260a8fda3637dec2d4a65eef3ee Mon Sep 17 00:00:00 2001 From: Simon Meggle Date: Thu, 16 Apr 2020 15:56:50 +0200 Subject: [PATCH 2/2] PEP8 fix --- pytest_check_mk/file_loader.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pytest_check_mk/file_loader.py b/pytest_check_mk/file_loader.py index 102614a..4ef6c0a 100644 --- a/pytest_check_mk/file_loader.py +++ b/pytest_check_mk/file_loader.py @@ -33,14 +33,14 @@ def regex(r): ''' -# E.g. host_extra_conf_merged is a WATO function which is only available when the -# check is run in MK's context. The same applies to the inventory rules etc. +# E.g. host_extra_conf_merged is a WATO function which is only available when the +# check is run in MK's context. The same applies to the inventory rules etc. # We can monkeypatch these data in our test file only when they are present; monkey- -# patch cannot create new data. +# patch cannot create new data. -# The following code gets appended to the module. All runtime relevant data and +# The following code gets appended to the module. All runtime relevant data and # functions must be initialised here. -# Example (file: test_foobar.py): +# Example (file: test_foobar.py): # mock_inventory_foobar_rules = [ ...(rules) ...] # monkeypatch.setattr(checks.module, "inventory_foobar_rules", mock_inventory_foobar_rules)