test(alerts): seed the agent registration with SQL, not a second model import (#5060)
Importing plugins.host_agent.models after the app loaded the plugin defines host_agent_registrations twice (run 8310). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
"""Integration: a host-agent ingest POST evaluates alert rules on the host's
|
"""Integration: a host-agent ingest POST evaluates alert rules on the host's
|
||||||
metrics (#5060). Before, ingest wrote rows straight to plugin_metrics and no
|
metrics (#5060). Before, ingest wrote rows straight to plugin_metrics and no
|
||||||
host_agent rule could ever fire."""
|
host_agent rule could ever fire.
|
||||||
|
|
||||||
|
The agent registration is seeded with plain SQL: importing the plugin's
|
||||||
|
models again after the app loaded the plugin raises "Table already defined"
|
||||||
|
(see test_mcp_plugin_tools)."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -23,8 +27,7 @@ def app():
|
|||||||
|
|
||||||
|
|
||||||
def test_ingest_fires_a_host_rule_from_a_backlog(app):
|
def test_ingest_fires_a_host_rule_from_a_backlog(app):
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select, text
|
||||||
from plugins.host_agent.models import HostAgentRegistration
|
|
||||||
from steward.alerts import service as alert_svc
|
from steward.alerts import service as alert_svc
|
||||||
from steward.models.alerts import AlertEvent, AlertState, AlertStateEnum
|
from steward.models.alerts import AlertEvent, AlertState, AlertStateEnum
|
||||||
from steward.models.hosts import Host
|
from steward.models.hosts import Host
|
||||||
@@ -41,8 +44,12 @@ def test_ingest_fires_a_host_rule_from_a_backlog(app):
|
|||||||
host = Host(name=hostname, address="10.9.9.9")
|
host = Host(name=hostname, address="10.9.9.9")
|
||||||
s.add_all([user, host])
|
s.add_all([user, host])
|
||||||
await s.flush()
|
await s.flush()
|
||||||
s.add(HostAgentRegistration(
|
await s.execute(text(
|
||||||
host_id=host.id, token_hash=hashlib.sha256(token.encode()).hexdigest()))
|
"INSERT INTO host_agent_registrations "
|
||||||
|
"(id, host_id, token_hash, token_created_at, created_at, updated_at) "
|
||||||
|
"VALUES (:id, :host, :hash, now(), now(), now())"),
|
||||||
|
{"id": str(uuid.uuid4()), "host": host.id,
|
||||||
|
"hash": hashlib.sha256(token.encode()).hexdigest()})
|
||||||
s.add(PluginMetric(source_module="host_agent", resource_name=hostname,
|
s.add(PluginMetric(source_module="host_agent", resource_name=hostname,
|
||||||
metric_name="cpu_pct", value=5.0))
|
metric_name="cpu_pct", value=5.0))
|
||||||
await s.flush()
|
await s.flush()
|
||||||
|
|||||||
Reference in New Issue
Block a user