diff --git a/roles/svc-ai-agent-broker/files/python/engine.py b/roles/svc-ai-agent-broker/files/python/engine.py index 0fb6e27d48..18bd7ce585 100644 --- a/roles/svc-ai-agent-broker/files/python/engine.py +++ b/roles/svc-ai-agent-broker/files/python/engine.py @@ -3,6 +3,7 @@ from __future__ import annotations import http.client import json import socket +import time import urllib.parse LABEL_PLATFORM = "infinito.agent.platform" @@ -10,7 +11,14 @@ LABEL_OWNER = "infinito.agent.owner" class EngineError(RuntimeError): - pass + """Args: + message: what the call was and how it answered. + status: HTTP status the engine returned, or None when it never answered. + """ + + def __init__(self, message, status=None): + super().__init__(message) + self.status = status class _UnixConnection(http.client.HTTPConnection): @@ -53,7 +61,7 @@ class Engine: finally: connection.close() if status not in expect: - raise EngineError(f"{method} {path} -> {status}: {raw[:400]!r}") + raise EngineError(f"{method} {path} -> {status}: {raw[:400]!r}", status) if not raw: return None try: @@ -99,17 +107,44 @@ class Engine: ) return created["Id"] - def connect(self, network_id, container, alias): - detail = self.call("GET", f"/networks/{network_id}") - attached = detail.get("Containers") or {} - if any(key.startswith(container) for key in attached): + def connect(self, network_id, container, alias, realize_timeout=120, pause=2): + """Attach ``container`` to ``network_id`` once the network is usable. + + Exception: a swarm-scoped overlay answers GET from the control plane + before its allocator has realized it on any node, while connect is + node-local, so connect returns 404 for a network that does exist. A 404 + that outlives ``realize_timeout`` is raised, because then the network + really is gone. + + Args: + network_id: id ``ensure_network`` returned. + container: container id or name to attach. + alias: name the other members of the network reach it by. + realize_timeout: seconds to allow the swarm allocator. + pause: seconds between attempts. + """ + deadline = time.monotonic() + realize_timeout + while True: + detail = self.call("GET", f"/networks/{network_id}") + attached = detail.get("Containers") or {} + if any(key.startswith(container) for key in attached): + return + try: + self.call( + "POST", + f"/networks/{network_id}/connect", + body={ + "Container": container, + "EndpointConfig": {"Aliases": [alias]}, + }, + expect=(200, 204), + ) + except EngineError as error: + if error.status != 404 or time.monotonic() >= deadline: + raise + time.sleep(pause) + continue return - self.call( - "POST", - f"/networks/{network_id}/connect", - body={"Container": container, "EndpointConfig": {"Aliases": [alias]}}, - expect=(200, 204), - ) class ComposeBackend: diff --git a/tests/unit/python/roles/svc-ai-agent-broker/files/test_engine.py b/tests/unit/python/roles/svc-ai-agent-broker/files/test_engine.py index 335412b1c6..ac2e09e41b 100644 --- a/tests/unit/python/roles/svc-ai-agent-broker/files/test_engine.py +++ b/tests/unit/python/roles/svc-ai-agent-broker/files/test_engine.py @@ -107,6 +107,56 @@ class TestUnixTransport(unittest.TestCase): engine.Engine(self.path, timeout=5).call("GET", "/containers/x/exec") +class _ConnectEngine(engine.Engine): + """Engine whose connect answers 404 for the first ``misses`` attempts.""" + + def __init__(self, misses): + self.misses = misses + self.attempts = 0 + self.slept = 0.0 + + def call(self, method, path, body=None, query=None, expect=(200,), timeout=None): + if method == "GET": + return {"Containers": {}} + self.attempts += 1 + if self.attempts <= self.misses: + raise engine.EngineError(f"POST {path} -> 404: not found", 404) + return None + + +class TestConnect(unittest.TestCase): + def setUp(self): + self.real_sleep = engine.time.sleep + engine.time.sleep = lambda seconds: None + + def tearDown(self): + engine.time.sleep = self.real_sleep + + def test_a_swarm_overlay_is_joined_once_its_allocator_realizes_it(self): + fake = _ConnectEngine(misses=3) + fake.connect("net-1", "broker", "broker-alias") + self.assertEqual(fake.attempts, 4) + + def test_a_network_that_stays_missing_is_not_waited_on_forever(self): + fake = _ConnectEngine(misses=10**6) + with self.assertRaises(engine.EngineError): + fake.connect("net-1", "broker", "broker-alias", realize_timeout=0) + self.assertEqual(fake.attempts, 1) + + def test_a_refusal_that_is_not_a_missing_network_is_raised_at_once(self): + fake = _ConnectEngine(misses=0) + + def deny(method, path, body=None, query=None, expect=(200,), timeout=None): + if method == "GET": + return {"Containers": {}} + raise engine.EngineError(f"POST {path} -> 403: denied", 403) + + fake.call = deny + with self.assertRaises(engine.EngineError) as caught: + fake.connect("net-1", "broker", "broker-alias") + self.assertEqual(caught.exception.status, 403) + + class TestSwarmBackend(unittest.TestCase): def test_a_service_is_placed_on_the_sandbox_nodes_and_starts_scaled_down(self): fake = RecordingEngine(runtime="runsc")