195 lines
5.3 KiB
Python
195 lines
5.3 KiB
Python
"""PostgreSQL integration test for the Agent registry projection."""
|
|
|
|
import unittest
|
|
from uuid import uuid4
|
|
|
|
from app.agent_registry.repository import (
|
|
AgentRegistryRepository,
|
|
default_connection_factory,
|
|
)
|
|
from app.agent_registry.service import AgentRegistryService
|
|
|
|
|
|
class AgentRegistryPostgresTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.site_id = uuid4()
|
|
self.zone_id = uuid4()
|
|
self.agent_id = f"agent-registry-{uuid4().hex}"
|
|
|
|
connection = default_connection_factory()
|
|
|
|
try:
|
|
with connection.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO sites (id, name)
|
|
VALUES (%s, %s)
|
|
""",
|
|
(
|
|
self.site_id,
|
|
"Registry Integration Site",
|
|
),
|
|
)
|
|
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO agent_zones (
|
|
id,
|
|
site_id,
|
|
name,
|
|
description,
|
|
is_enabled
|
|
)
|
|
VALUES (%s, %s, %s, %s, true)
|
|
""",
|
|
(
|
|
self.zone_id,
|
|
self.site_id,
|
|
"Registry Network",
|
|
"Integration-test network",
|
|
),
|
|
)
|
|
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO agent_zone_network_ranges (
|
|
agent_zone_id,
|
|
network
|
|
)
|
|
VALUES (%s, '10.251.0.0/24'::cidr)
|
|
""",
|
|
(self.zone_id,),
|
|
)
|
|
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO minerops_site_agents (
|
|
agent_id,
|
|
site_name,
|
|
site_id,
|
|
agent_zone_id,
|
|
display_name,
|
|
hostname,
|
|
version,
|
|
ip_address,
|
|
agent_secret_hash,
|
|
credential_created_at
|
|
)
|
|
VALUES (
|
|
%s,
|
|
%s,
|
|
%s,
|
|
%s,
|
|
%s,
|
|
%s,
|
|
%s,
|
|
%s,
|
|
%s,
|
|
now()
|
|
)
|
|
""",
|
|
(
|
|
self.agent_id,
|
|
"Registry Integration Site",
|
|
self.site_id,
|
|
self.zone_id,
|
|
"Registry Test Agent",
|
|
"registry-agent",
|
|
"1.0.0-alpha",
|
|
"10.251.0.10",
|
|
"a" * 64,
|
|
),
|
|
)
|
|
|
|
connection.commit()
|
|
finally:
|
|
connection.close()
|
|
|
|
def tearDown(self):
|
|
connection = default_connection_factory()
|
|
|
|
try:
|
|
with connection.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
DELETE FROM minerops_site_agents
|
|
WHERE agent_id = %s
|
|
""",
|
|
(self.agent_id,),
|
|
)
|
|
|
|
cur.execute(
|
|
"""
|
|
DELETE FROM agent_zone_network_ranges
|
|
WHERE agent_zone_id = %s
|
|
""",
|
|
(self.zone_id,),
|
|
)
|
|
|
|
cur.execute(
|
|
"""
|
|
DELETE FROM agent_zones
|
|
WHERE id = %s
|
|
""",
|
|
(self.zone_id,),
|
|
)
|
|
|
|
cur.execute(
|
|
"""
|
|
DELETE FROM sites
|
|
WHERE id = %s
|
|
""",
|
|
(self.site_id,),
|
|
)
|
|
|
|
connection.commit()
|
|
finally:
|
|
connection.close()
|
|
|
|
def test_registry_joins_site_zone_network_and_redacts_secret_hash(self):
|
|
data = AgentRegistryService(
|
|
AgentRegistryRepository()
|
|
).list_registry()
|
|
|
|
agents = [
|
|
item
|
|
for item in data.agents
|
|
if item.agent_id == self.agent_id
|
|
]
|
|
|
|
self.assertEqual(len(agents), 1)
|
|
|
|
agent = agents[0]
|
|
|
|
self.assertEqual(
|
|
agent.site_name,
|
|
"Registry Integration Site",
|
|
)
|
|
self.assertEqual(
|
|
agent.agent_zone_name,
|
|
"Registry Network",
|
|
)
|
|
self.assertEqual(agent.credential_state, "active")
|
|
self.assertEqual(agent.routing_state, "assigned")
|
|
|
|
zones = [
|
|
zone
|
|
for zone in data.zones
|
|
if zone.id == self.zone_id
|
|
]
|
|
|
|
self.assertEqual(len(zones), 1)
|
|
self.assertEqual(
|
|
zones[0].networks,
|
|
["10.251.0.0/24"],
|
|
)
|
|
|
|
serialized = data.model_dump_json()
|
|
|
|
self.assertNotIn("agent_secret_hash", serialized)
|
|
self.assertNotIn("a" * 64, serialized)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|