diff --git a/pytest_check_mk/file_loader.py b/pytest_check_mk/file_loader.py index b201d03..4ef6c0a 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))