"""What a rule's Send test carries (#5466): the rule's own name and condition, marked TEST, on its resource at the latest reading; a rule covering hosts tests on its first host.""" from types import SimpleNamespace from steward.alerts import service as alert_svc from steward.models.alerts import AlertOperator def _rule(**kw): base = dict(id="r1", name="Hot", source_module="host_agent", resource_name="web01", metric_name="cpu_pct", operator=AlertOperator.gt, threshold=90.0, covers="resource", cover_group=None, parent_id=None) return SimpleNamespace(**{**base, **kw}) class _Db: """Answers the one query test_alert makes: a covering rule's first host's rule.""" def __init__(self, child): self.child = child async def execute(self, stmt): return SimpleNamespace(scalar_one_or_none=lambda: self.child) def _readings(monkeypatch, values): async def latest(db, rule): return values.get(rule.resource_name) monkeypatch.setattr(alert_svc, "latest_value", latest) async def test_a_rule_tests_on_its_own_resource_at_its_latest_reading(monkeypatch): _readings(monkeypatch, {"web01": 42.0}) got = await alert_svc.test_alert(_Db(None), _rule()) assert {k: got[k] for k in ("rule_name", "state", "resource", "value", "operator", "threshold")} == { "rule_name": "Hot", "state": "TEST", "resource": "web01", "value": 42.0, "operator": ">", "threshold": 90.0} async def test_a_covering_rule_tests_on_its_first_host(monkeypatch): _readings(monkeypatch, {"db01": 12.0}) parent = _rule(resource_name="", covers="group", cover_group="web") got = await alert_svc.test_alert(_Db(_rule(id="c1", name="Hot: db01", resource_name="db01", parent_id="r1")), parent) assert (got["rule_name"], got["resource"], got["value"]) == ("Hot", "db01", 12.0) async def test_a_covering_rule_with_no_hosts_yet_says_what_it_covers(monkeypatch): _readings(monkeypatch, {}) got = await alert_svc.test_alert(_Db(None), _rule(resource_name="", covers="all_hosts")) assert (got["resource"], got["value"]) == ("every host", None)