Skip to content

Commit

Permalink
RuleSpec: require ID key
Browse files Browse the repository at this point in the history
CMK-14467

Change-Id: If4501edfe5d86e418e80860b2e7a6dd5e5cc64bb
  • Loading branch information
Synss committed Oct 25, 2023
1 parent 1e882c1 commit f0451d5
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmk/utils/rulesets/ruleset_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class RuleConditionsSpec(TypedDict, total=False):
class RuleSpec(Generic[TRuleValue], TypedDict, total=False):
value: Required[TRuleValue]
condition: Required[RuleConditionsSpec]
id: str # Should not be optional but nearly not test has that attribute set!
id: Required[str] # a UUID if provided by either the GUI or the REST API
options: RuleOptionsSpec


Expand Down
3 changes: 3 additions & 0 deletions tests/unit/cmk/base/test_unit_automations.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ def test_service_labels(monkeypatch):
[
{
"condition": {"service_description": [{"$regex": "CPU load"}]},
"id": "01",
"value": {"label1": "val1"},
},
{
"condition": {"service_description": [{"$regex": "CPU load"}]},
"id": "02",
"value": {"label2": "val2"},
},
{
"condition": {"service_description": [{"$regex": "CPU temp"}]},
"id": "03",
"value": {"label1": "val1"},
},
]
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/cmk/utils/rulesets/test_ruleset_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ def test_labels_of_service(monkeypatch: MonkeyPatch) -> None:
"service_description": [{"$regex": "CPU load$"}],
"host_tags": {TagGroupID("agent"): TagID("no-agent")},
},
"id": "01",
"value": {"label1": "val1"},
},
{
"condition": {
"service_description": [{"$regex": "CPU load$"}],
"host_tags": {TagGroupID("agent"): TagID("no-agent")},
},
"id": "02",
"value": {"label2": "val2"},
},
],
Expand Down
165 changes: 109 additions & 56 deletions tests/unit/cmk/utils/rulesets/test_tuple_rulesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# pylint: disable=redefined-outer-name

from collections.abc import Mapping, Sequence
from typing import Final

import pytest

Expand Down Expand Up @@ -47,47 +48,59 @@ def ts(monkeypatch):


def test_service_extra_conf(ts: Scenario) -> None:
ruleset: Sequence[RuleSpec[str]] = [
{"condition": {}, "options": {}, "value": "1"},
{"condition": {}, "options": {}, "value": "2"},
ruleset: Final[Sequence[RuleSpec[str]]] = [
{"condition": {}, "id": "01", "options": {}, "value": "1"},
{"condition": {}, "id": "02", "options": {}, "value": "2"},
{
"condition": {"host_tags": {TagGroupID("agent"): TagID("no-agent")}},
"id": "03",
"options": {},
"value": "3",
},
{
"condition": {"host_tags": {TagGroupID("criticality"): TagID("test")}},
"id": "04",
"options": {},
"value": "4",
},
{
"condition": {"host_tags": {TagGroupID("tag3"): TagID("tag3")}},
"id": "05",
"options": {},
"value": "5",
},
{
"condition": {"host_tags": {TagGroupID("tag3"): TagID("tag3")}, "host_name": ["host1"]},
"id": "06",
"options": {},
"value": "6",
},
{"condition": {"host_name": ["host1"]}, "options": {}, "value": "7"},
{"condition": {"host_name": ["host1"]}, "options": {}, "id": "07", "value": "7"},
{
"condition": {"service_description": [{"$regex": "service1$"}], "host_name": ["host1"]},
"id": "08",
"options": {},
"value": "8",
},
{
"condition": {"service_description": [{"$regex": "ser$"}], "host_name": ["host1"]},
"id": "09",
"options": {},
"value": "9",
},
{
"condition": {"service_description": [{"$regex": "^serv$"}], "host_name": ["host1"]},
"id": "10",
"options": {},
"value": "10",
},
{"condition": {"host_name": [{"$regex": "host"}]}, "options": {}, "value": "11"},
{"condition": {"host_name": {"$nor": ["host2"]}}, "options": {}, "value": "12"},
{
"condition": {"host_name": [{"$regex": "host"}]},
"id": "11",
"options": {},
"value": "11",
},
{"condition": {"host_name": {"$nor": ["host2"]}}, "id": "12", "options": {}, "value": "12"},
]

matcher = ts.config_cache.ruleset_matcher
Expand Down Expand Up @@ -121,47 +134,60 @@ def test_service_extra_conf(ts: Scenario) -> None:
]


@pytest.fixture(scope="function")
def host_ruleset():
return [
{"condition": {}, "options": {}, "value": {"1": True}},
{
"condition": {"host_tags": {TagGroupID("agent"): TagID("no-agent")}},
"options": {},
"value": {"2": True},
},
{
"condition": {"host_tags": {TagGroupID("criticality"): TagID("test")}},
"options": {},
"value": {"3": True},
},
{
"condition": {"host_tags": {TagGroupID("tag3"): TagID("tag3")}},
"options": {},
"value": {"4": True},
},
{
"condition": {
"host_tags": {TagGroupID("agent"): TagID("no-agent")},
"host_name": ["host1"],
},
"options": {},
"value": {"5": True},
},
{
"condition": {"host_tags": {TagGroupID("tag3"): TagID("tag3")}, "host_name": ["host1"]},
"options": {},
"value": {"6": True},
HOST_RULESET: Final[Sequence[RuleSpec[Mapping[str, bool]]]] = [
{"condition": {}, "id": "01", "options": {}, "value": {"1": True}},
{
"condition": {"host_tags": {TagGroupID("agent"): TagID("no-agent")}},
"id": "02",
"options": {},
"value": {"2": True},
},
{
"condition": {"host_tags": {TagGroupID("criticality"): TagID("test")}},
"id": "03",
"options": {},
"value": {"3": True},
},
{
"condition": {"host_tags": {TagGroupID("tag3"): TagID("tag3")}},
"id": "04",
"options": {},
"value": {"4": True},
},
{
"condition": {
"host_tags": {TagGroupID("agent"): TagID("no-agent")},
"host_name": ["host1"],
},
{"condition": {"host_name": ["host1"]}, "options": {}, "value": {"7": True}},
{"condition": {"host_name": [{"$regex": "host"}]}, "options": {}, "value": {"8": True}},
{"condition": {"host_name": {"$nor": ["host2"]}}, "options": {}, "value": {"9": True}},
]


def test_get_host_values(ts: Scenario, host_ruleset: Sequence[RuleSpec[object]]) -> None:
"id": "05",
"options": {},
"value": {"5": True},
},
{
"condition": {"host_tags": {TagGroupID("tag3"): TagID("tag3")}, "host_name": ["host1"]},
"id": "06",
"options": {},
"value": {"6": True},
},
{"condition": {"host_name": ["host1"]}, "id": "07", "options": {}, "value": {"7": True}},
{
"condition": {"host_name": [{"$regex": "host"}]},
"id": "08",
"options": {},
"value": {"8": True},
},
{
"condition": {"host_name": {"$nor": ["host2"]}},
"id": "09",
"options": {},
"value": {"9": True},
},
]


def test_get_host_values(ts: Scenario) -> None:
ruleset_matcher = ts.config_cache.ruleset_matcher
assert ruleset_matcher.get_host_values(HostName("host1"), host_ruleset) == [
assert ruleset_matcher.get_host_values(HostName("host1"), HOST_RULESET) == [
{"1": True},
{"2": True},
{"3": True},
Expand All @@ -171,18 +197,16 @@ def test_get_host_values(ts: Scenario, host_ruleset: Sequence[RuleSpec[object]])
{"9": True},
]

assert ruleset_matcher.get_host_values(HostName("host2"), host_ruleset) == [
assert ruleset_matcher.get_host_values(HostName("host2"), HOST_RULESET) == [
{"1": True},
{"2": True},
{"8": True},
]


def test_get_host_merged_dict(
ts: Scenario, host_ruleset: Sequence[RuleSpec[Mapping[str, object]]]
) -> None:
def test_get_host_merged_dict(ts: Scenario) -> None:
ruleset_matcher = ts.config_cache.ruleset_matcher
assert ruleset_matcher.get_host_merged_dict(HostName("host1"), host_ruleset) == {
assert ruleset_matcher.get_host_merged_dict(HostName("host1"), HOST_RULESET) == {
"1": True,
"2": True,
"3": True,
Expand All @@ -192,7 +216,7 @@ def test_get_host_merged_dict(
"9": True,
}

assert ruleset_matcher.get_host_merged_dict(HostName("host2"), host_ruleset) == {
assert ruleset_matcher.get_host_merged_dict(HostName("host2"), HOST_RULESET) == {
"1": True,
"2": True,
"8": True,
Expand All @@ -205,20 +229,28 @@ def test_get_host_merged_dict(
# ruleset, outcome host1, outcome host2
([], False, False),
(
[{"condition": {}, "options": {}, "value": False}],
[{"condition": {}, "id": "01", "options": {}, "value": False}],
False,
False,
),
([{"condition": {}, "options": {}, "value": True}], True, True),
([{"condition": {}, "id": "02", "options": {}, "value": True}], True, True),
(
[{"condition": {"host_name": {"$nor": ["host1"]}}, "options": {}, "value": True}],
[
{
"condition": {"host_name": {"$nor": ["host1"]}},
"id": "03",
"options": {},
"value": True,
}
],
False,
True,
),
(
[
{
"condition": {"host_name": {"$nor": ["host1", "host2"]}},
"id": "04",
"options": {},
"value": True,
}
Expand All @@ -230,6 +262,7 @@ def test_get_host_merged_dict(
[
{
"condition": {"host_tags": {TagGroupID("criticality"): TagID("test")}},
"id": "05",
"options": {},
"value": True,
}
Expand All @@ -244,6 +277,7 @@ def test_get_host_merged_dict(
"host_tags": {TagGroupID("criticality"): TagID("test")},
"host_name": {"$nor": ["host1"]},
},
"id": "06",
"options": {},
"value": True,
}
Expand All @@ -252,12 +286,26 @@ def test_get_host_merged_dict(
False,
),
(
[{"condition": {"host_name": {"$nor": ["host1"]}}, "options": {}, "value": True}],
[
{
"condition": {"host_name": {"$nor": ["host1"]}},
"id": "07",
"options": {},
"value": True,
}
],
False,
True,
),
(
[{"condition": {"host_name": {"$nor": ["host1"]}}, "options": {}, "value": False}],
[
{
"condition": {"host_name": {"$nor": ["host1"]}},
"id": "08",
"options": {},
"value": False,
}
],
False,
False,
),
Expand All @@ -268,6 +316,7 @@ def test_get_host_merged_dict(
"host_tags": {TagGroupID("criticality"): TagID("test")},
"host_name": {"$nor": ["host1"]},
},
"id": "08",
"options": {},
"value": False,
}
Expand All @@ -279,6 +328,7 @@ def test_get_host_merged_dict(
[
{
"condition": {"service_description": [{"$regex": "serv"}]},
"id": "09",
"options": {},
"value": True,
}
Expand All @@ -290,6 +340,7 @@ def test_get_host_merged_dict(
[
{
"condition": {"service_description": [{"$regex": "serv"}]},
"id": "10",
"options": {},
"value": False,
}
Expand All @@ -301,6 +352,7 @@ def test_get_host_merged_dict(
[
{
"condition": {"service_description": [{"$regex": "service1"}]},
"id": "11",
"options": {},
"value": False,
}
Expand All @@ -314,6 +366,7 @@ def test_get_host_merged_dict(
[
{
"condition": {"service_description": [{"$regex": "service1"}]},
"id": "12",
"options": {},
"value": False,
},
Expand Down

0 comments on commit f0451d5

Please sign in to comment.