diff --git a/pytest_check_mk/wrapper.py b/pytest_check_mk/wrapper.py index c809361..e346dec 100644 --- a/pytest_check_mk/wrapper.py +++ b/pytest_check_mk/wrapper.py @@ -28,7 +28,6 @@ def check_info(self): def __getitem__(self, key): return CheckWrapper(self, key) - class CheckWrapper(object): def __init__(self, check_file, name): @@ -55,18 +54,29 @@ def has_perfdata(self): def service_description(self): return self.check_info['service_description'] - def inventory(self, check_output): + def parse(self, info): + __tracebackhide__ = True + parse_function = self.check_info['parse_function'] + return parse_function(info) + + # inventory / check function --------------------------------------------- + # Normally, you can't feed the plugin output directly into a check, because + # the check expects the data as a list of lists + without the fist line (section). + # This transformation does CheckMK for you. + # Use this functions to test the inventory/check isolated from a monitoring core. + + def inventory(self, plugin_output): __tracebackhide__ = True - section, info = parse_info(check_output.strip()) + section, info = parse_info(plugin_output.strip()) if section != self.section: raise ValueError('Wrong section name in test data: expected "{}", got "{}"'.format(self.section, section)) inventory_function = self.check_info['inventory_function'] return inventory_function(info) - def check(self, item, params, check_output): + def check(self, item, params, plugin_output): __tracebackhide__ = True - section, info = parse_info(check_output.strip()) + section, info = parse_info(plugin_output.strip()) if section != self.section: raise ValueError('Wrong section name in test data: expected "{}", got "{}"'.format(self.section, section)) @@ -74,6 +84,35 @@ def check(self, item, params, check_output): result = check_function(item, params, info) return self._convert_check_result(result) + # inventory_mk / check_mk function --------------------------------------- + # "_mk" = Data are coming from MK + # Use this functions to test the inventory/check with exactly the same data + # format which CheckMK hands over to the check (=list of lists). + # A parse_function is respected, if existing. + + def inventory_mk(self, mk_output): + '''Use this function to test the inventory with exactly the data which + are handed over by the CheckMK system.''' + __tracebackhide__ = True + if "parse_function" in self.check_info: + parse_function = self.check_info['parse_function'] + parsed = parse_function(mk_output) + else: + parsed = mk_output + inventory_function = self.check_info['inventory_function'] + return inventory_function(parsed) + + def check_mk(self, item, params, mk_output): + __tracebackhide__ = True + if "parse_function" in self.check_info: + parse_function = self.check_info['parse_function'] + parsed = parse_function(mk_output) + else: + parsed = mk_output + check_function = self.check_info['check_function'] + result = check_function(item, params, parsed) + return self._convert_check_result(result) + def _convert_check_result(self, result): __tracebackhide__ = True # Most of this function is taken from check_mk_base.convert_check_result, @@ -103,9 +142,9 @@ def _convert_check_result(self, result): return status, ", ".join(infotexts), perfdata -def parse_info(check_output): +def parse_info(plugin_output): __tracebackhide__ = True - lines = check_output.splitlines(True) + lines = plugin_output.splitlines(True) section_name, section_options = parse_header(lines[0].strip())