Module: check_mk
Branch: master
Commit: dc747b848348d6f8c8ecffcfe77461ba7bb4549d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=dc747b848348d6…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri May 18 10:00:26 2018 +0200
Add integration tests for active checks
a) Verify active checks work at all
b) Verify standard macro resolution
Change-Id: I3cb94a3792ce394a66dee29e1538fcfd34ceeb22
---
tests/integration/core/test_active_checks.py | 144 +++++++++++++++++++++++++++
tests/integration/web/test_webapi.py | 4 +-
tests/testlib/__init__.py | 63 +++++++++---
3 files changed, 196 insertions(+), 15 deletions(-)
diff --git a/tests/integration/core/test_active_checks.py
b/tests/integration/core/test_active_checks.py
new file mode 100644
index 0000000..0f17a7b
--- /dev/null
+++ b/tests/integration/core/test_active_checks.py
@@ -0,0 +1,144 @@
+#!/usr/bin/env python
+# encoding: utf-8
+
+import pytest
+
+from testlib import web
+
+import cmk_base.rulesets as rulesets
+
+(a)pytest.fixture(scope="module")
+def test_cfg(web, site):
+ print "Applying default config"
+ web.add_host("test-host", attributes={
+ "ipaddress": "127.0.0.1",
+ "tag_agent": "no-agent",
+ })
+
+ web.activate_changes()
+ yield None
+
+ #
+ # Cleanup code
+ #
+ print "Cleaning up test config"
+
+ web.delete_host("test-host")
+
+
+(a)pytest.mark.parametrize(("core")ore"), [ "nagios", "cmc" ])
+def test_active_check_execution(test_cfg, site, web, core):
+ site.set_config("CORE", core, with_restart=True)
+
+ try:
+ web.set_ruleset("custom_checks", {
+ "ruleset": {
+ # Main folder
+ "": [
+ {
+ "value": {
+ 'service_description': u'\xc4ctive-Check',
+ 'command_line': 'echo "123"'
+ },
+ "conditions": {
+ "host_specs": rulesets.ALL_HOSTS,
+ "host_tags": [],
+ },
+ "options": {},
+ },
+ ],
+ }
+ })
+ web.activate_changes()
+
+ site.schedule_check("test-host", u'\xc4ctive-Check', 0)
+
+ result = site.live.query_row("GET services\nColumns: host_name description
state plugin_output has_been_checked\nFilter: host_name = test-host\nFilter: description =
Äctive-Check")
+ assert result[4] == 1
+ assert result[0] == "test-host"
+ assert result[1] == u'\xc4ctive-Check'
+ assert result[2] == 0
+ assert result[3] == "123"
+ finally:
+ web.set_ruleset("custom_checks", {
+ "ruleset": {
+ "": [], # -> folder
+ }
+ })
+ web.activate_changes()
+
+
+(a)pytest.mark.parametrize(("core")ore"), [ "nagios", "cmc" ])
+def test_active_check_macros(test_cfg, site, web, core):
+ site.set_config("CORE", core, with_restart=True)
+
+ macros = {
+ "$HOSTADDRESS$": "127.0.0.1",
+ "$HOSTNAME$": "test-host",
+ "$_HOSTTAGS$": "/wato/ ip-v4 ip-v4-only lan no-agent no-snmp ping
prod site:%s wato" % site.id,
+ "$_HOSTADDRESS_4$": "127.0.0.1",
+ "$_HOSTADDRESS_6$": "",
+ "$_HOSTADDRESS_FAMILY$": "4",
+ "$USER1$": "/omd/sites/%s/lib/nagios/plugins" % site.id,
+ "$USER2$": "/omd/sites/%s/local/lib/nagios/plugins" %
site.id,
+ "$USER3$": site.id,
+ "$USER4$": site.root,
+ }
+
+ def descr(var):
+ return "Macro %s" % var.strip("$")
+
+ ruleset = []
+ for var, value in macros.items():
+ ruleset.append({
+ "value": {
+ 'service_description': descr(var),
+ 'command_line': 'echo "Output: %s"' % var,
+ },
+ "conditions": {
+ "host_specs": rulesets.ALL_HOSTS,
+ "host_tags": [],
+ },
+ })
+
+ try:
+ web.set_ruleset("custom_checks", {
+ "ruleset": {
+ # Main folder
+ "": ruleset,
+ }
+ })
+ web.activate_changes()
+
+ for var, value in macros.items():
+ description = descr(var)
+ site.schedule_check("test-host", description, 0)
+
+ row = site.live.query_row(
+ "GET services\n"
+ "Columns: host_name description state plugin_output
has_been_checked\n"
+ "Filter: host_name = test-host\n"
+ "Filter: description = %s\n" % description
+ )
+
+ name, description, state, plugin_output, has_been_checked = row
+
+ assert name == "test-host"
+ assert has_been_checked == 1
+ assert state == 0
+
+ expected_output = "Output: %s" % value
+ # TODO: Cleanup difference between nagios/cmc
+ if core == "nagios":
+ expected_output = expected_output.strip()
+
+ assert expected_output == plugin_output, \
+ "Macro %s has wrong value (%r instead of %r)" % (var,
plugin_output, expected_output)
+
+ finally:
+ web.set_ruleset("custom_checks", {
+ "ruleset": {
+ "": [], # -> folder
+ }
+ })
+ web.activate_changes()
diff --git a/tests/integration/web/test_webapi.py b/tests/integration/web/test_webapi.py
index 61d7945..cad20c1 100644
--- a/tests/integration/web/test_webapi.py
+++ b/tests/integration/web/test_webapi.py
@@ -356,9 +356,7 @@ def test_get_graph(web, site):
})
web.discover_services("test-host-get-graph")
web.activate_changes()
-
- # Issue a reschedule
- site.live.command("[%d]
SCHEDULE_FORCED_SERVICE_CHECK;test-host-get-graph;Check_MK;%d" % (int(time.time()),
int(time.time())))
+ web.schedule_check("test-host-get-graph", "Check_MK")
# Wait for RRD file creation
# Isn't this a bug that the graph is not instantly available?
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index ecfb6ca..6e6a1b4 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -293,11 +293,7 @@ class Site(object):
if expected_state is None:
expected_state = state
- last_check_before = self.live.query_value(
- "GET hosts\n" \
- "Columns: last_check\n" \
- "Filter: host_name = %s\n" % (hostname))
-
+ last_check_before = self._get_last_check(hostname)
schedule_ts, wait_timeout = time.time(), 20
# Ensure the next check result is not in same second as the previous check
@@ -308,26 +304,69 @@ class Site(object):
print "last_check_before", last_check_before, "schedule_ts",
schedule_ts
self.live.command("[%d] PROCESS_HOST_CHECK_RESULT;%s;%d;%s" %
(schedule_ts, hostname, state, output))
+ self._wait_for_next_check(hostname, last_check_before, schedule_ts, wait_timeout,
expected_state)
+
+
+ def schedule_check(self, hostname, service_description, expected_state):
+ last_check_before = self._get_last_check(hostname)
+ schedule_ts, wait_timeout = int(time.time()), 20
+
+ # Ensure the next check result is not in same second as the previous check
+ while int(last_check_before) == int(schedule_ts):
+ schedule_ts = time.time()
+ time.sleep(0.1)
+
+ print "last_check_before", last_check_before, "schedule_ts",
schedule_ts
+ self.live.command("[%d] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%d" %
+ (schedule_ts, hostname, service_description.encode("utf-8"),
schedule_ts))
+
+ self._wait_for_next_check(hostname, last_check_before, schedule_ts, wait_timeout,
expected_state, service_description=service_description)
+
- last_check, host_state = self.live.query_row(
- "GET hosts\n" \
+
+ def _wait_for_next_check(self, hostname, last_check_before, schedule_ts,
wait_timeout, expected_state, service_description=None):
+ if not service_description:
+ table = "hosts"
+ filt = "Filter: host_name = %s\n" % hostname
+ wait_obj = "%s" % hostname
+ else:
+ table = "services"
+ filt = "Filter: host_name = %s\nFilter: description = %s\n" %
(hostname, service_description)
+ wait_obj = "%s;%s" % (hostname, service_description)
+
+ last_check, state = self.live.query_row(
+ "GET %s\n" \
"Columns: last_check state\n" \
- "Filter: host_name = %s\n" \
+ "%s" \
"WaitObject: %s\n" \
"WaitTimeout: %d\n" \
"WaitCondition: last_check > %d\n" \
"WaitCondition: state = %d\n" \
- "WaitTrigger: check\n" % (hostname, hostname, wait_timeout*1000,
last_check_before, expected_state))
+ "WaitTrigger: check\n" % (table, filt, wait_obj, wait_timeout*1000,
last_check_before, expected_state))
- print "processing host check result took %0.2f seconds" % (time.time()
- schedule_ts)
+ print "processing check result took %0.2f seconds" % (time.time() -
schedule_ts)
assert last_check > last_check_before, \
"Check result not processed within %d seconds (last check before
reschedule: %d, " \
"scheduled at: %d, last check: %d)" % \
(wait_timeout, last_check_before, schedule_ts, last_check)
- assert host_state == expected_state, \
- "Expected %d state, got %d state" % (expected_state, host_state)
+ assert state == expected_state, \
+ "Expected %d state, got %d state" % (expected_state, state)
+
+
+ def _get_last_check(self, hostname, service_description=None):
+ if not service_description:
+ return self.live.query_value(
+ "GET hosts\n" \
+ "Columns: last_check\n" \
+ "Filter: host_name = %s\n" % (hostname))
+ else:
+ return self.live.query_value(
+ "GET services\n" \
+ "Columns: last_check\n" \
+ "Filter: host_name = %s\n" \
+ "Filter: service_description = %s\n" % (hostname,
service_description))
def get_host_state(self, hostname):