Module: check_mk
Branch: master
Commit: 16c8434e09e476e79aafe6e6ce0916491643dcf7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=16c8434e09e476…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sun Apr 21 19:40:22 2019 +0200
Add unit tests for host alias setting
CMK-2030
Change-Id: I2d557290b86aa9aa717b265adc74bad1064ef41e
---
cmk_base/core_nagios.py | 7 ++-
tests/testlib/base.py | 5 +-
tests/unit/cmk_base/test_core_nagios.py | 107 +++++++++++++++++++++++++++++++-
3 files changed, 114 insertions(+), 5 deletions(-)
diff --git a/cmk_base/core_nagios.py b/cmk_base/core_nagios.py
index 75f4cd2..6ef770b 100644
--- a/cmk_base/core_nagios.py
+++ b/cmk_base/core_nagios.py
@@ -170,11 +170,12 @@ def _create_nagios_config_host(cfg, config_cache, hostname):
cfg.outfile.write("#
----------------------------------------------------\n")
host_attrs = core_config.get_host_attributes(hostname, config_cache)
if config.generate_hostconf:
- _create_nagios_hostdefs(cfg, config_cache, hostname, host_attrs)
+ host_spec = _create_nagios_host_spec(cfg, config_cache, hostname, host_attrs)
+ cfg.outfile.write(_format_nagios_object("host",
host_spec).encode("utf-8"))
_create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs)
-def _create_nagios_hostdefs(cfg, config_cache, hostname, attrs):
+def _create_nagios_host_spec(cfg, config_cache, hostname, attrs):
host_config = config_cache.get_host_config(hostname)
is_clust = config.is_cluster(hostname)
@@ -253,7 +254,7 @@ def _create_nagios_hostdefs(cfg, config_cache, hostname, attrs):
# Custom configuration last -> user may override all other values
host_spec.update(
_extra_host_conf_of(config_cache, hostname, exclude=["parents"] if
is_clust else []))
- cfg.outfile.write(_format_nagios_object("host",
host_spec).encode("utf-8"))
+ return host_spec
def _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs):
diff --git a/tests/testlib/base.py b/tests/testlib/base.py
index 712c39f..d31bc4b 100644
--- a/tests/testlib/base.py
+++ b/tests/testlib/base.py
@@ -24,7 +24,9 @@ class Scenario(object):
#self.config["host_tags"][hostname] = tags
return self
- def add_cluster(self, hostname, tags, folder="/", nodes=None):
+ def add_cluster(self, hostname, tags=None, folder="/", nodes=None):
+ if tags is None:
+ tags = []
if nodes is None:
nodes = []
name_with_tags = "%s|%s" % (hostname, "|".join(tags))
@@ -45,5 +47,6 @@ class Scenario(object):
for key, value in self.config.items():
monkeypatch.setattr(config, key, value)
+ self.config_cache = config.get_config_cache()
self.config_cache.initialize()
return self.config_cache
diff --git a/tests/unit/cmk_base/test_core_nagios.py
b/tests/unit/cmk_base/test_core_nagios.py
index 81c4869..60f863b 100644
--- a/tests/unit/cmk_base/test_core_nagios.py
+++ b/tests/unit/cmk_base/test_core_nagios.py
@@ -1,6 +1,12 @@
# encoding: utf-8
-
+# pylint: disable=redefined-outer-name
import itertools
+from StringIO import StringIO
+
+import pytest # type: ignore
+from testlib.base import Scenario
+
+import cmk_base.core_config as core_config
import cmk_base.core_nagios as core_nagios
@@ -21,3 +27,102 @@ def test_format_nagios_object():
}
""" % tuple(itertools.chain(*sorted(spec.items(), key=lambda x: x[0])))
+
+
+def ts():
+ ts1 = Scenario().add_host("localhost")
+ result1 = {
+ '_ADDRESS_4': '127.0.0.1',
+ '_ADDRESS_6': '',
+ '_ADDRESS_FAMILY': '4',
+ '_TAGS': '',
+ '_FILENAME': '/',
+ 'address': '127.0.0.1',
+ 'alias': 'localhost',
+ 'check_command': 'check-mk-host-ping!-w 200.00,80.00% -c
500.00,100.00%',
+ 'host_name': 'localhost',
+ 'hostgroups': 'check_mk',
+ 'use': 'check_mk_host',
+ }
+
+ yield ts1, result1
+
+ ts2 = Scenario().add_host("localhost")
+ ts2.set_option("extra_host_conf", {
+ "alias": [(u'lOCALhost', ['localhost']),],
+ })
+ result2 = {
+ '_ADDRESS_4': '127.0.0.1',
+ '_ADDRESS_6': '',
+ '_ADDRESS_FAMILY': '4',
+ '_FILENAME': '/',
+ '_TAGS': '',
+ 'address': '127.0.0.1',
+ 'alias': u'lOCALhost',
+ 'check_command': 'check-mk-host-ping!-w 200.00,80.00% -c
500.00,100.00%',
+ 'host_name': 'localhost',
+ 'hostgroups': 'check_mk',
+ 'use': 'check_mk_host',
+ }
+
+ yield ts2, result2
+
+ ts3 = Scenario().add_cluster("localhost")
+ result3 = {
+ '_ADDRESS_4': '127.0.0.1',
+ '_ADDRESS_6': '',
+ '_ADDRESS_FAMILY': '4',
+ '_FILENAME': '/',
+ '_NODEIPS': '',
+ '_NODEIPS_4': '',
+ '_NODEIPS_6': '',
+ '_NODENAMES': '',
+ '_TAGS': '',
+ 'address': '127.0.0.1',
+ 'alias': 'localhost',
+ 'check_command': 'check-mk-host-ping!-w 200.00,80.00% -c
500.00,100.00%',
+ 'host_name': 'localhost',
+ 'hostgroups': 'check_mk',
+ 'parents': '',
+ 'use': 'check_mk_cluster',
+ }
+
+ yield ts3, result3
+
+ ts4 = Scenario().add_cluster("localhost")
+ ts4.set_option("extra_host_conf", {
+ "alias": [(u'lOCALhost', ['localhost']),],
+ })
+ result4 = {
+ '_ADDRESS_4': '127.0.0.1',
+ '_ADDRESS_6': '',
+ '_ADDRESS_FAMILY': '4',
+ '_FILENAME': '/',
+ '_NODEIPS': '',
+ '_NODEIPS_4': '',
+ '_NODEIPS_6': '',
+ '_NODENAMES': '',
+ '_TAGS': '',
+ 'address': '127.0.0.1',
+ 'alias': u'lOCALhost',
+ 'check_command': 'check-mk-host-ping!-w 200.00,80.00% -c
500.00,100.00%',
+ 'host_name': 'localhost',
+ 'hostgroups': 'check_mk',
+ 'parents': '',
+ 'use': 'check_mk_cluster',
+ }
+
+ yield ts4, result4
+
+
+(a)pytest.mark.parametrize("ts,result"sult", ts())
+def test_create_nagios_host_spec(ts, result, monkeypatch):
+ outfile = StringIO()
+ cfg = core_nagios.NagiosConfig(outfile, ["localhost"])
+
+ config_cache = ts.apply(monkeypatch)
+ host_attrs = core_config.get_host_attributes("localhost", config_cache)
+
+ host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache,
"localhost", host_attrs)
+
+ assert host_spec == result