Module: check_mk
Branch: master
Commit: 4fbc47264cbc57dc357e75f563348fe534f93ce8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4fbc47264cbc57…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Fri Jun 29 10:19:43 2018 +0200
check_http: add tests for service description and argument parsing
Change-Id: Ie3649429e8e96dca50a5fec5ba5c7334c4a26877
---
tests/testlib/__init__.py | 26 ++++++++++++----
tests/unit/checks/test_check_http.py | 60 ++++++++++++++++++++++++++++++++++++
2 files changed, 80 insertions(+), 6 deletions(-)
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index 3a68960..d3a6457 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -1779,11 +1779,15 @@ class CheckManager(object):
config.load_checks(check_api.get_check_api_context,
map(lambda f: os.path.join(cmk.paths.checks_dir, f),
file_names))
-
def get_check(self, name):
- self.load([name.split(".", 1)[0]])
+ main_check = name.split(".", 1)[0]
+ self.load([main_check])
return Check(name)
+ def get_active_check(self, name):
+ self.load([name])
+ return ActiveCheck(name)
+
class Check(object):
def __init__(self, name):
@@ -1792,13 +1796,11 @@ class Check(object):
self.info = config.check_info[name]
self.context = config._check_contexts[name]
-
def default_parameters(self):
import cmk_base.config as config
params = {}
return config._update_with_default_check_parameters(self.name, params)
-
def run_parse(self, info):
parse_func = self.info.get("parse_function")
if not parse_func:
@@ -1806,12 +1808,10 @@ class Check(object):
return parse_func(info)
-
def run_discovery(self, info):
# TODO: use standard sanitizing code
return self.info["inventory_function"](info)
-
def run_check(self, item, params, info):
# TODO: use standard sanitizing code
return self.info["check_function"](item, params, info)
@@ -1837,3 +1837,17 @@ class Check(object):
# # TODO: use standard sanitizing code
# return self.info["check_function"](item, params, info)
+
+
+class ActiveCheck(object):
+ def __init__(self, name):
+ import cmk_base.config as config
+ self.name = name
+ assert name.startswith('check_'), 'Specify the full name of the
active check, e.g. check_http'
+ self.info = config.active_check_info[name[len('check_'):]]
+
+ def run_argument_function(self, params):
+ return self.info['argument_function'](params)
+
+ def run_service_description(self, params):
+ return self.info['service_description'](params)
diff --git a/tests/unit/checks/test_check_http.py b/tests/unit/checks/test_check_http.py
new file mode 100644
index 0000000..8cf89b0
--- /dev/null
+++ b/tests/unit/checks/test_check_http.py
@@ -0,0 +1,60 @@
+import pytest
+
+pytestmark = pytest.mark.checks
+
+
+(a)pytest.mark.parametrize('params,expected_args'rgs', [(
+ (None, {
+ 'onredirect': 'follow',
+ 'port': 80,
+ 'uri': '/images',
+ 'urlize': True,
+ 'virthost': ('www.test123.de', True)
+ }),
+ ['-H', 'www.test123.de', '-p', 80, '-u',
'/images', '--onredirect=follow', '-L'],
+), (
+ (None, {
+ 'extended_perfdata': True,
+ 'method': 'CONNECT',
+ 'port': 3128,
+ 'proxy': '163.172.86.64',
+ 'ssl': 'auto',
+ 'uri': '/images',
+ 'virthost': ('www.test123.de', True)
+ }),
+ [
+ '-I', '163.172.86.64', '-H', 'www.test123.de',
'-p', 3128, '-u', '/images', '--ssl',
+ '--extended-perfdata', '-j', 'CONNECT'
+ ],
+), (
+ (None, {
+ 'cert_days': (10, 20),
+ 'cert_host': 'www.test123.com'
+ }),
+ ['-I', 'www.test123.com', '-H', '$_HOSTADDRESS_4$',
'-C', '10,20'],
+)])
+def test_check_http_argument_parsing(check_manager, params, expected_args):
+ """Tests if all required arguments are present. The tests do not check
the order of arguments."""
+ active_check = check_manager.get_active_check('check_http')
+ assert sorted(active_check.run_argument_function(params)) == sorted(expected_args)
+
+
+(a)pytest.mark.parametrize('params,expected_description'ion', [
+ (
+ (u'No SSL Test', {}),
+ u'HTTP No SSL Test',
+ ),
+ (
+ (u'Test with SSL', {
+ 'ssl': "auto"
+ }),
+ u'HTTPS Test with SSL',
+ ),
+ (
+ (u'^No Prefix Test', {}),
+ u'No Prefix Test',
+ ),
+])
+def test_check_http_service_description(check_manager, params, expected_description):
+ active_check = check_manager.get_active_check('check_http')
+ assert active_check.run_service_description(params) == expected_description