Module: check_mk
Branch: master
Commit: 3cf18f194f29ff6ffc6faab164e9e3939d6e145c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3cf18f194f29ff…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Apr 25 21:21:04 2019 +0200
Separate CEE specific ConfigCache/HostConfig
The CEE specific configuration settings should only be available
when using CEE/CME and should be located below cmk_base.cee in
the module hierarchy.
Currently this can not be done easily. The best we can do at the
moment is to separate them into dedicated classes which can then
be moved over later.
CMK-2030
Change-Id: If7fe3973986c995af361e8929302860f1ab72c3b
---
cmk_base/config.py | 51 ++++++++++++++++++++++++--------------
tests/unit/cmk_base/test_config.py | 50 ++++++++-----------------------------
2 files changed, 43 insertions(+), 58 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 845c0d8..92681db 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -38,6 +38,7 @@ from typing import Set, Text, Any, Callable, Dict, List, Tuple, Union,
Optional
import six
+import cmk
import cmk.utils.debug
import cmk.utils.paths
from cmk.utils.regex import regex, is_regex
@@ -2746,14 +2747,6 @@ class HostConfig(object):
return list(set(cgrs))
@property
- def rrd_config(self):
- # type: () -> Optional[Dict]
- entries = self._config_cache.host_extra_conf(self.hostname, cmc_host_rrd_config)
- if not entries:
- return None
- return entries[0]
-
- @property
def management_address(self):
# type: () -> Optional[str]
attributes_of_host = host_attributes.get(self.hostname, {})
@@ -2986,7 +2979,8 @@ class ConfigCache(object):
if host_config:
return host_config
- host_config = self._host_configs[hostname] = HostConfig(self, hostname)
+ config_class = HostConfig if cmk.is_raw_edition() else CEEHostConfig
+ host_config = self._host_configs[hostname] = config_class(self, hostname)
return host_config
def _collect_hosttags(self):
@@ -3057,14 +3051,6 @@ class ConfigCache(object):
return match_object
- def rrd_config_of_service(self, hostname, description):
- # type: (str, Text) -> Optional[Dict]
- rrdconf = self.service_extra_conf(hostname, description, cmc_service_rrd_config)
- if not rrdconf:
- return None
-
- return rrdconf[0]
-
def set_all_processed_hosts(self, all_processed_hosts):
self._all_processed_hosts = set(all_processed_hosts)
@@ -3617,5 +3603,34 @@ def get_config_cache():
# type: () -> ConfigCache
config_cache = cmk_base.config_cache.get_dict("config_cache")
if not config_cache:
- config_cache["cache"] = ConfigCache()
+ cache_class = ConfigCache if cmk.is_raw_edition() else CEEConfigCache
+ config_cache["cache"] = cache_class()
return config_cache["cache"]
+
+
+# TODO: Find a clean way to move this to cmk_base.cee. This will be possible once the
+# configuration settings are not held in cmk_base.config namespace anymore.
+class CEEConfigCache(ConfigCache):
+ """Encapsulates the CEE specific functionality"""
+
+ def rrd_config_of_service(self, hostname, description):
+ # type: (str, Text) -> Optional[Dict]
+ rrdconf = self.service_extra_conf(hostname, description, cmc_service_rrd_config)
+ if not rrdconf:
+ return None
+
+ return rrdconf[0]
+
+
+# TODO: Find a clean way to move this to cmk_base.cee. This will be possible once the
+# configuration settings are not held in cmk_base.config namespace anymore.
+class CEEHostConfig(HostConfig):
+ """Encapsulates the CEE specific functionality"""
+
+ @property
+ def rrd_config(self):
+ # type: () -> Optional[Dict]
+ entries = self._config_cache.host_extra_conf(self.hostname, cmc_host_rrd_config)
+ if not entries:
+ return None
+ return entries[0]
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index b2d0105..455680e 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -711,26 +711,6 @@ def test_host_config_contactgroups(monkeypatch, hostname, result):
@pytest.mark.parametrize("hostname,result", [
- ("testhost1", None),
- ("testhost2", {
- "1": 1
- }),
-])
-def test_host_config_rrd_config(monkeypatch, hostname, result):
- ts = Scenario().add_host(hostname)
- ts.set_ruleset("cmc_host_rrd_config", [
- ({
- "1": 1
- }, [], ["testhost2"], {}),
- ({
- "2": 2
- }, [], ["testhost2"], {}),
- ])
- config_cache = ts.apply(monkeypatch)
- assert config_cache.get_host_config(hostname).rrd_config == result
-
-
-(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", {}),
("testhost2", {
'empty_output': 1
@@ -1039,31 +1019,21 @@ def test_labels_of_service(monkeypatch):
}
-(a)pytest.mark.parametrize("hostname,result"sult", [
- ("testhost1", None),
- ("testhost2", {
- "1": 1
- }),
+@pytest.mark.parametrize("edition_short,expected_cache_class_name,expected_host_class_name",
[
+ ("cme", "CEEConfigCache", "CEEHostConfig"),
+ ("cee", "CEEConfigCache", "CEEHostConfig"),
+ ("cre", "ConfigCache", "HostConfig"),
])
-def test_config_cache_rrd_config_of_service(monkeypatch, hostname, result):
- ts = Scenario().add_host(hostname)
- ts.set_ruleset("cmc_service_rrd_config", [
- ({
- "1": 1
- }, [], ["testhost2"], ["CPU load$"], {}),
- ({
- "2": 2
- }, [], ["testhost2"], ["CPU load$"], {}),
- ])
- config_cache = ts.apply(monkeypatch)
- assert config_cache.rrd_config_of_service(hostname, "CPU load") == result
-
+def test_config_cache_get_host_config(monkeypatch, edition_short,
expected_cache_class_name,
+ expected_host_class_name):
+ monkeypatch.setattr(cmk, "edition_short", lambda: edition_short)
-def test_config_cache_get_host_config():
- cache = config.ConfigCache()
+ cache = config.get_config_cache()
+ assert cache.__class__.__name__ == expected_cache_class_name
assert cache._host_configs == {}
host_config = cache.get_host_config("xyz")
+ assert host_config.__class__.__name__ == expected_host_class_name
assert isinstance(host_config, config.HostConfig)
assert host_config is cache.get_host_config("xyz")