From 5898503088961b740b3fc6e9ea2538756ebad5e5 Mon Sep 17 00:00:00 2001 From: Simon Meggle Date: Thu, 23 Jan 2020 11:08:05 +0000 Subject: [PATCH 1/2] Added inventory_mk and check_mk to use/test with MK-context data (list of lists) --- pytest_check_mk/wrapper.py | 55 +++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/pytest_check_mk/wrapper.py b/pytest_check_mk/wrapper.py index c809361..f201d04 100644 --- a/pytest_check_mk/wrapper.py +++ b/pytest_check_mk/wrapper.py @@ -29,6 +29,7 @@ def __getitem__(self, key): return CheckWrapper(self, key) + class CheckWrapper(object): def __init__(self, check_file, name): @@ -55,18 +56,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 +86,37 @@ 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 +146,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()) From 66ec8427c49f23cf43f0a1edf758490dc8e159d4 Mon Sep 17 00:00:00 2001 From: Simon Meggle Date: Thu, 16 Apr 2020 16:08:50 +0200 Subject: [PATCH 2/2] PEP8 fix --- pytest_check_mk/wrapper.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/pytest_check_mk/wrapper.py b/pytest_check_mk/wrapper.py index f201d04..e346dec 100644 --- a/pytest_check_mk/wrapper.py +++ b/pytest_check_mk/wrapper.py @@ -28,8 +28,6 @@ def check_info(self): def __getitem__(self, key): return CheckWrapper(self, key) - - class CheckWrapper(object): def __init__(self, check_file, name): @@ -61,9 +59,9 @@ def parse(self, info): parse_function = self.check_info['parse_function'] return parse_function(info) - ### inventory / check function --------------------------------------------- + # 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). + # 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. @@ -86,22 +84,21 @@ def check(self, item, params, plugin_output): result = check_function(item, params, info) return self._convert_check_result(result) - ### inventory_mk / check_mk function --------------------------------------- + # inventory_mk / check_mk function --------------------------------------- # "_mk" = Data are coming from MK - # Use this functions to test the inventory/check with exactly the same data + # 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.''' + '''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: + if "parse_function" in self.check_info: parse_function = self.check_info['parse_function'] parsed = parse_function(mk_output) - else: - parsed = mk_output - + else: + parsed = mk_output inventory_function = self.check_info['inventory_function'] return inventory_function(parsed) @@ -111,8 +108,7 @@ def check_mk(self, item, params, mk_output): parse_function = self.check_info['parse_function'] parsed = parse_function(mk_output) else: - parsed = mk_output - + parsed = mk_output check_function = self.check_info['check_function'] result = check_function(item, params, parsed) return self._convert_check_result(result)