Module: check_mk
Branch: master
Commit: 7b91d666bfb55d481db8640591c81fc4669932a9
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=7b91d666bfb55d…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu May 2 11:46:26 2019 +0200
Moved config.custom_service_attributes processing to ConfigCache
CMK-2030
Change-Id: Ib586fbe25216a0c1af3f021c92d368bb569a5cc0
---
cmk_base/config.py | 7 +++++++
cmk_base/core_config.py | 11 ++---------
tests/unit/cmk_base/test_config.py | 25 +++++++++++++++++++++++++
tests/unit/cmk_base/test_core_config.py | 21 ---------------------
4 files changed, 34 insertions(+), 30 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 9c4f97f..aa10f6f 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -34,6 +34,7 @@ import os
import py_compile
import struct
import sys
+import itertools
from typing import Set, Text, Any, Callable, Dict, List, Tuple, Union, Optional #
pylint: disable=unused-import
import six
@@ -3082,6 +3083,12 @@ class ConfigCache(object):
return "24X7"
return passive_check_period[0]
+ def custom_attributes_of_service(self, hostname, description):
+ # type: (str, Text) -> Dict[str, str]
+ return dict(
+ itertools.chain(
+ *self.service_extra_conf(hostname, description,
custom_service_attributes)))
+
def get_explicit_service_custom_variables(self, hostname, description):
# type: (str, Text) -> Dict[str, str]
try:
diff --git a/cmk_base/core_config.py b/cmk_base/core_config.py
index f5b3ea3..42f96c1 100644
--- a/cmk_base/core_config.py
+++ b/cmk_base/core_config.py
@@ -28,7 +28,6 @@ import abc
import numbers
import os
import sys
-import itertools
from typing import Text, Optional, Any, List, Dict # pylint: disable=unused-import
import cmk.utils.paths
@@ -322,8 +321,8 @@ def _extra_service_attributes(hostname, description, config_cache,
checkname, pa
# Add service custom_variables. Name conflicts are prevented by the GUI, but just
# to be sure, add them first. The other definitions will override the custom
attributes.
- for varname, value in _custom_service_attributes_of(config_cache, hostname,
- description).iteritems():
+ for varname, value in config_cache.custom_attributes_of_service(hostname,
+
description).iteritems():
attrs["_%s" % varname.upper()] = value
attrs.update(config_cache.get_extra_attributes_of_service(hostname, description))
@@ -340,12 +339,6 @@ def _extra_service_attributes(hostname, description, config_cache,
checkname, pa
return attrs
-def _custom_service_attributes_of(config_cache, hostname, service_description):
- return dict(
- itertools.chain(*config_cache.service_extra_conf(hostname, service_description,
-
config.custom_service_attributes)))
-
-
#.
# .--HostAttributes------------------------------------------------------.
# | _ _ _ _ _ _ _ _ _ |
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index 5e29a3f..d009cef 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -1107,6 +1107,31 @@ def test_config_cache_passive_check_period_of_service(monkeypatch,
hostname, res
assert config_cache.passive_check_period_of_service(hostname, "CPU load")
== result
+(a)pytest.mark.parametrize("hostname,result"sult", [
+ ("testhost1", {}),
+ ("testhost2", {
+ 'ATTR1': 'value1',
+ 'ATTR2': 'value2',
+ }),
+])
+def test_config_cache_custom_attributes_of_service(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_ruleset(
+ "custom_service_attributes",
+ [
+ ([
+ ("ATTR1", "value1"),
+ ("ATTR2", "value2"),
+ ], [], ["testhost2"], ["CPU load$"], {}),
+ ([
+ ("ATTR1", "value1"),
+ ], [], ["testhost2"], ["CPU load$"], {}),
+ ],
+ )
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.custom_attributes_of_service(hostname, "CPU load") ==
result
+
+
@pytest.mark.parametrize("edition_short,expected_cache_class_name,expected_host_class_name",
[
("cme", "CEEConfigCache", "CEEHostConfig"),
("cee", "CEEConfigCache", "CEEHostConfig"),
diff --git a/tests/unit/cmk_base/test_core_config.py
b/tests/unit/cmk_base/test_core_config.py
index 5d9a0af..1031ef4 100644
--- a/tests/unit/cmk_base/test_core_config.py
+++ b/tests/unit/cmk_base/test_core_config.py
@@ -70,24 +70,3 @@ def test_get_tag_attributes(tag_groups, result):
for k, v in attributes.items():
assert isinstance(k, unicode)
assert isinstance(v, unicode)
-
-
-def test_custom_service_attributes_of(monkeypatch):
- config_cache = config.get_config_cache()
- attributes = core_config._custom_service_attributes_of(config_cache,
"luluhost", "laladescr")
- assert attributes == {}
-
- ts = Scenario().add_host("luluhost", [])
- ts.set_ruleset("custom_service_attributes", [
- ([('deng', '1')], [], config.ALL_HOSTS, config.ALL_SERVICES,
{}),
- ([('ding', '2'), ('ding', '2a'),
- ('dong', '3')], [], config.ALL_HOSTS, config.ALL_SERVICES,
{}),
- ])
- config_cache = ts.apply(monkeypatch)
-
- attributes = core_config._custom_service_attributes_of(config_cache,
"luluhost", "laladescr")
- assert attributes == {
- "deng": "1",
- "ding": "2a",
- "dong": "3",
- }