Module: check_mk
Branch: master
Commit: 225faa2be39ddaf5c367b2801d7a79da0ffc9a57
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=225faa2be39dda…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Nov 27 16:38:58 2018 +0100
Refactored rule group registration to plugin registry
* The internal construct g_rulegroups has been replaced by the rulespec_group_registry
* A group is now represented by a RulespecGroup() object
* The register_rulegroup() call, which may be used by WATO plugins, is now deprecated.
It will be kept working for 1 or 2 versions to keep the plugins
compatible.
Change-Id: I64718130273ab6b41680cd9593fed4bcea036fda
---
cmk/gui/watolib.py | 63 +++++++++++++++++++++++++++++---------
tests/unit/cmk/gui/test_watolib.py | 63 ++++++++++++++++++++++++++++++++++++++
2 files changed, 111 insertions(+), 15 deletions(-)
diff --git a/cmk/gui/watolib.py b/cmk/gui/watolib.py
index d75909c..c5d164b 100644
--- a/cmk/gui/watolib.py
+++ b/cmk/gui/watolib.py
@@ -211,7 +211,6 @@ if config.mkeventd_enabled:
backup_domains = {}
g_rulespecs = None
-g_rulegroups = {}
# Global datastructure holding all attributes (in a defined order)
# as pairs of (attr, topic). Topic is the title under which the
@@ -7522,20 +7521,58 @@ def user_script_title(what, name):
# '----------------------------------------------------------------------
-# TODO: Better rename this and also get_rulegroup() to rulespec group
-class Rulegroup(object):
- def __init__(self, name, title=None, help_text=None):
- self.name = name
- self.title = title or name
- self.help = help_text
+class RulespecGroup(object):
+ __metaclass__ = abc.ABCMeta
+
+ @abc.abstractproperty
+ def name(self):
+ # type: () -> Text
+ """Unique internal key of this group"""
+ raise NotImplementedError()
+
+ @abc.abstractproperty
+ def title(self):
+ # type: () -> Text
+ """Human readable title of this group"""
+ raise NotImplementedError()
+
+ @abc.abstractproperty
+ def help(self):
+ # type: () -> Text
+ """Helpful description of this group"""
+ raise NotImplementedError()
+class RulespecGroupRegistry(cmk.plugin_registry.ClassRegistry):
+ def plugin_base_class(self):
+ return RulespecGroup
+
+ def _register(self, plugin_class):
+ self._entries[plugin_class().name] = plugin_class
+
+
+rulespec_group_registry = RulespecGroupRegistry()
+
+
+# TODO: Kept for compatibility with pre 1.6 plugins
def register_rulegroup(group_name, title, help_text):
- g_rulegroups[group_name] = Rulegroup(group_name, title, help_text)
+ rulespec_group_registry.register_plugin(
+ _get_legacy_rulespec_group_class(group_name, title, help_text))
def get_rulegroup(group_name):
- return g_rulegroups.get(group_name, Rulegroup(group_name))
+ group_class = rulespec_group_registry.get(
+ group_name, _get_legacy_rulespec_group_class(group_name, group_title=None,
help_text=None))
+ return group_class()
+
+
+def _get_legacy_rulespec_group_class(group_name, group_title, help_text):
+ group_title = group_title or group_name
+ return type("LegacyRulespecGroup%s" % group_name.title(), (RulespecGroup,),
{
+ "name": group_name,
+ "title": group_title,
+ "help": help_text,
+ })
class Rulespecs(object):
@@ -7585,18 +7622,14 @@ class Rulespecs(object):
choices = []
for main_group_name in self.get_main_groups():
- main_group = g_rulegroups.get(main_group_name)
- if main_group:
- main_group_title = main_group.title
- else:
- main_group_title = main_group_name
+ main_group = get_rulegroup(main_group_name)
if mode == "static_checks" and main_group_name !=
"static":
continue
elif mode != "static_checks" and main_group_name ==
"static":
continue
- choices.append((main_group_name, main_group_title))
+ choices.append((main_group_name, main_group.title))
for group_name in self._by_group:
if group_name.startswith(main_group_name + "/"):
diff --git a/tests/unit/cmk/gui/test_watolib.py b/tests/unit/cmk/gui/test_watolib.py
index 3579145..bb6eb16 100644
--- a/tests/unit/cmk/gui/test_watolib.py
+++ b/tests/unit/cmk/gui/test_watolib.py
@@ -279,3 +279,66 @@ def test_legacy_configvar_order_access():
with pytest.raises(NotImplementedError) as e:
configvar_order()["x"] = 10
assert "werk #6911" in "%s" % e
+
+
+def test_registered_rulespec_groups():
+ registered = sorted(watolib.rulespec_group_registry.keys())
+ assert registered == sorted([
+ 'activechecks',
+ 'agent',
+ 'agents',
+ 'checkparams',
+ 'datasource_programs',
+ 'eventconsole',
+ 'grouping',
+ 'inventory',
+ 'monconf',
+ 'static',
+ 'user_interface',
+ ])
+
+
+def test_legacy_register_rulegroup(monkeypatch):
+ monkeypatch.setattr(watolib, "rulespec_group_registry",
watolib.RulespecGroupRegistry())
+ watolib.register_rulegroup("abc", "A B C", "abc 123")
+
+ group = watolib.get_rulegroup("abc")
+ assert isinstance(group, watolib.RulespecGroup)
+ assert group.name == "abc"
+ assert group.title == "A B C"
+ assert group.help == "abc 123"
+
+
+def test_legacy_get_not_existing_rulegroup(monkeypatch):
+ monkeypatch.setattr(watolib, "rulespec_group_registry",
watolib.RulespecGroupRegistry())
+
+ group = watolib.get_rulegroup("xyz")
+ assert isinstance(group, watolib.RulespecGroup)
+ assert group.name == "xyz"
+ assert group.title == "xyz"
+ assert group.help is None
+
+
+(a)pytest.mark.parametrize("mode,result"sult", [
+ ("rulesets", [
+ ('activechecks', u'Active checks (HTTP, TCP, etc.)'),
+ ('agent', u'Access to Agents'),
+ ('agents', u'Monitoring Agents'),
+ ('checkparams', u'Parameters for discovered services'),
+ ('datasource_programs', u'Datasource Programs'),
+ ('eventconsole', u'Event Console'),
+ ('grouping', u'Grouping'),
+ ('inventory', u'Hardware/Software-Inventory'),
+ ('monconf', u'Monitoring Configuration'),
+ ('user_interface', u'User Interface'),
+ ]),
+ ("static_checks", [
+ ('static', 'Manual Checks'),
+ ]),
+])
+def test_rulespec_group_choices(mode, result, monkeypatch):
+ monkeypatch.setattr(watolib.Rulespecs, "get_main_groups",
+ lambda self: watolib.rulespec_group_registry.keys())
+
+ rulespecs = watolib.Rulespecs()
+ assert sorted(rulespecs.get_group_choices(mode=mode)) == sorted(result)