Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions pytest_check_mk/file_loader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import imp
import os


from string import Template
from pytest_check_mk import MissingFileError


Expand Down Expand Up @@ -31,16 +30,42 @@ 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

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)

Expand Down
2 changes: 2 additions & 0 deletions pytest_check_mk/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))