Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 77aaabae63 | |||
| d0b35b5506 | |||
| ea0c070cfe | |||
| 24df322c31 |
@@ -0,0 +1,47 @@
|
||||
"""Shared-gateway source-IP allocation for the consolidated docker backend
|
||||
(PRD 0070).
|
||||
|
||||
In the consolidated model one gateway container serves every bottle over a
|
||||
single shared docker network, and each agent bottle attaches with a pinned,
|
||||
deterministic address that the gateway uses as its **attribution key**. This
|
||||
allocates those addresses from the network's subnet, skipping the reserved
|
||||
ones — the network address and broadcast (excluded by `hosts()`), docker's
|
||||
router `.1`, and everything already in use (`taken`: the gateway container
|
||||
plus every live bottle, which the caller reads from the registry).
|
||||
|
||||
Pure `ipaddress` logic — the docker-specific bits (the subnet CIDR, the
|
||||
gateway container's own address) are gathered by the caller and passed in, so
|
||||
this stays testable without docker.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
from collections.abc import Iterable
|
||||
|
||||
|
||||
class NoFreeAddressError(RuntimeError):
|
||||
"""The shared gateway network's subnet is exhausted — every host address
|
||||
is reserved or already assigned to a bottle."""
|
||||
|
||||
|
||||
def next_free_ip(cidr: str, taken: Iterable[str]) -> str:
|
||||
"""The lowest host address in `cidr` not in `taken` and not docker's
|
||||
router (`.1`). `taken` must include the gateway container's own address
|
||||
and every live bottle's. Raises `NoFreeAddressError` if the subnet is
|
||||
full."""
|
||||
net = ipaddress.ip_network(cidr, strict=False)
|
||||
reserved = {str(a) for a in taken}
|
||||
# Docker assigns the network's first host (.1) to the bridge router; a
|
||||
# bottle must never be handed that address.
|
||||
reserved.add(str(net.network_address + 1))
|
||||
for host in net.hosts(): # hosts() already excludes network + broadcast
|
||||
candidate = str(host)
|
||||
if candidate not in reserved:
|
||||
return candidate
|
||||
raise NoFreeAddressError(
|
||||
f"no free address in {cidr} ({len(reserved)} reserved/assigned)"
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["next_free_ip", "NoFreeAddressError"]
|
||||
@@ -46,6 +46,7 @@ from .git_gate_render import (
|
||||
git_gate_known_hosts_line,
|
||||
git_gate_render_access_hook,
|
||||
git_gate_render_entrypoint,
|
||||
git_gate_render_provision,
|
||||
git_gate_render_gitconfig,
|
||||
git_gate_render_hook,
|
||||
git_gate_upstreams_for_bottle,
|
||||
@@ -155,6 +156,7 @@ __all__ = [
|
||||
"git_gate_render_gitconfig",
|
||||
"git_gate_known_hosts_line",
|
||||
"git_gate_render_entrypoint",
|
||||
"git_gate_render_provision",
|
||||
"git_gate_render_hook",
|
||||
"git_gate_render_access_hook",
|
||||
"provision_git_gate_dynamic_keys",
|
||||
|
||||
@@ -9,6 +9,7 @@ own; `git_gate` re-exports these names for API stability."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shlex
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
@@ -125,22 +126,18 @@ def git_gate_known_hosts_line(host: str, port: str, key: str) -> str:
|
||||
return f"{target} {key}\n"
|
||||
|
||||
|
||||
def git_gate_render_entrypoint(upstreams: tuple[GitGateUpstream, ...]) -> str:
|
||||
"""Posix-sh entrypoint. One `init_repo` call per upstream, then
|
||||
`exec git daemon`. The function reads
|
||||
`/git-gate/creds/<name>-{key,known_hosts}` (bind-mounted into
|
||||
the bundle by the renderer) and wires them into each bare repo's
|
||||
config; the access-hook + pre-receive hook pick those paths up
|
||||
at fetch / push time."""
|
||||
lines = [
|
||||
"#!/bin/sh",
|
||||
"set -eu",
|
||||
"",
|
||||
def _git_gate_init_repo_fn(repo_root: str, creds_dir: str) -> list[str]:
|
||||
"""The `init_repo` shell function, parameterized by the bare-repo root
|
||||
and the per-bottle creds dir. Single source of the credential-wiring
|
||||
logic, shared by the single-tenant daemon entrypoint (`/git`,
|
||||
`/git-gate/creds`) and the consolidated per-bottle provisioning
|
||||
(`/git/<bottle_id>`, `/git-gate/creds/<bottle_id>`)."""
|
||||
return [
|
||||
"init_repo() {",
|
||||
" name=$1",
|
||||
" upstream_url=$2",
|
||||
" keyfile=/git-gate/creds/${name}-key",
|
||||
" hostsfile=/git-gate/creds/${name}-known_hosts",
|
||||
f" keyfile={creds_dir}/${{name}}-key",
|
||||
f" hostsfile={creds_dir}/${{name}}-known_hosts",
|
||||
"",
|
||||
# `|| true`: PRD 0018 chunk 3+ bind-mounts these RO from the
|
||||
# host, so chmod-syscalls fail with EROFS. The files already
|
||||
@@ -153,14 +150,14 @@ def git_gate_render_entrypoint(upstreams: tuple[GitGateUpstream, ...]) -> str:
|
||||
" chmod 600 \"$hostsfile\" 2>/dev/null || true",
|
||||
" fi",
|
||||
"",
|
||||
" repo=/git/${name}.git",
|
||||
f" repo={repo_root}/${{name}}.git",
|
||||
" if [ ! -d \"$repo\" ]; then",
|
||||
" git init --bare \"$repo\" >/dev/null",
|
||||
# --mirror=fetch sets remote.origin.fetch = +refs/*:refs/* so",
|
||||
# a later `git fetch origin` mirrors the upstream's full ref",
|
||||
# graph (heads, tags, notes) into the bare repo at canonical",
|
||||
# paths. It does NOT set remote.origin.mirror=true, so an",
|
||||
# explicit `git push origin <ref>:<ref>` still pushes one ref.",
|
||||
# --mirror=fetch sets remote.origin.fetch = +refs/*:refs/* so a later
|
||||
# `git fetch origin` mirrors the upstream's full ref graph (heads,
|
||||
# tags, notes) into the bare repo at canonical paths. It does NOT set
|
||||
# remote.origin.mirror=true, so an explicit `git push origin
|
||||
# <ref>:<ref>` still pushes one ref.
|
||||
" git -C \"$repo\" remote add --mirror=fetch origin \"$upstream_url\"",
|
||||
" fi",
|
||||
" git -C \"$repo\" config git-gate.identityFile \"$keyfile\"",
|
||||
@@ -170,9 +167,19 @@ def git_gate_render_entrypoint(upstreams: tuple[GitGateUpstream, ...]) -> str:
|
||||
" git -C \"$repo\" config http.receivepack true",
|
||||
" install -m 755 /etc/git-gate/pre-receive \"$repo/hooks/pre-receive\"",
|
||||
"}",
|
||||
"",
|
||||
"mkdir -p /git",
|
||||
]
|
||||
|
||||
|
||||
def git_gate_render_entrypoint(upstreams: tuple[GitGateUpstream, ...]) -> str:
|
||||
"""Posix-sh entrypoint. One `init_repo` call per upstream, then
|
||||
`exec git daemon`. The function reads
|
||||
`/git-gate/creds/<name>-{key,known_hosts}` (bind-mounted into
|
||||
the bundle by the renderer) and wires them into each bare repo's
|
||||
config; the access-hook + pre-receive hook pick those paths up
|
||||
at fetch / push time."""
|
||||
lines = ["#!/bin/sh", "set -eu", ""]
|
||||
lines += _git_gate_init_repo_fn("/git", "/git-gate/creds")
|
||||
lines += ["", "mkdir -p /git"]
|
||||
for u in upstreams:
|
||||
lines.append(f"init_repo {shlex.quote(u.name)} {shlex.quote(u.upstream_url)}")
|
||||
lines.extend([
|
||||
@@ -190,6 +197,35 @@ def git_gate_render_entrypoint(upstreams: tuple[GitGateUpstream, ...]) -> str:
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
# A bottle id namespaces the consolidated gateway's repo + creds dirs; it is
|
||||
# embedded unquoted in the provisioning script, so restrict it to a shell- and
|
||||
# path-safe alphabet (registry ids are token_hex — this is defense in depth).
|
||||
_SAFE_BOTTLE_ID = re.compile(r"[A-Za-z0-9_-]+")
|
||||
|
||||
|
||||
def git_gate_render_provision(
|
||||
bottle_id: str, upstreams: tuple[GitGateUpstream, ...],
|
||||
) -> str:
|
||||
"""Posix-sh script that provisions ONE bottle's bare repos into the
|
||||
consolidated gateway (PRD 0070), under `/git/<bottle_id>/` with creds
|
||||
read from `/git-gate/creds/<bottle_id>/`. Init-only — no `git daemon`,
|
||||
since the shared gateway already serves every bottle; run inside the
|
||||
running gateway when the bottle is registered.
|
||||
|
||||
Isolating each bottle's repo root and creds dir by id is what keeps one
|
||||
bottle's push credentials out of another's repos on the shared gateway."""
|
||||
if not _SAFE_BOTTLE_ID.fullmatch(bottle_id):
|
||||
raise ValueError(f"git-gate: unsafe bottle id {bottle_id!r}")
|
||||
repo_root = f"/git/{bottle_id}"
|
||||
creds_dir = f"/git-gate/creds/{bottle_id}"
|
||||
lines = ["#!/bin/sh", "set -eu", ""]
|
||||
lines += _git_gate_init_repo_fn(repo_root, creds_dir)
|
||||
lines += ["", f"mkdir -p {shlex.quote(repo_root)}"]
|
||||
for u in upstreams:
|
||||
lines.append(f"init_repo {shlex.quote(u.name)} {shlex.quote(u.upstream_url)}")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
def git_gate_render_hook() -> str:
|
||||
"""The shared pre-receive hook: gitleaks-scan all incoming refs,
|
||||
then forward each accepted ref to the real upstream (`origin`)
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
"""Orchestrator process lifecycle (PRD 0070, docker slice).
|
||||
|
||||
Before the CLI can register or launch bottles against the consolidated
|
||||
model, exactly one orchestrator control plane — and the single per-host
|
||||
gateway it manages — must be running. This starts the orchestrator
|
||||
dev-harness (`python -m bot_bottle.orchestrator`) as a background host
|
||||
process and health-checks it.
|
||||
|
||||
It is an **idempotent singleton**: `ensure_running` returns immediately if a
|
||||
healthy control plane already answers on the port, and otherwise spawns one
|
||||
and waits for it to come up. The control-plane port is the singleton key —
|
||||
a second orchestrator can't bind it, so a stray double-start fails fast
|
||||
rather than forking a rival.
|
||||
|
||||
Host-process (not container) on purpose: the PRD sequences the orchestrator
|
||||
as a plain-process dev-harness first (fast iteration, and it already has the
|
||||
host user's docker access to broker launches), while the data-plane
|
||||
*gateway* it manages runs as a container. Wrapping the orchestrator itself
|
||||
in a backend-native unit is a later step.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
from .. import log
|
||||
from ..paths import bot_bottle_root
|
||||
|
||||
DEFAULT_HOST = "127.0.0.1"
|
||||
DEFAULT_PORT = 8080
|
||||
# Poll cadence + default ceiling while waiting for a freshly-spawned control
|
||||
# plane to answer /health (the first start also builds/boots the gateway).
|
||||
_HEALTH_POLL_SECONDS = 0.25
|
||||
DEFAULT_STARTUP_TIMEOUT_SECONDS = 45.0
|
||||
_HEALTH_REQUEST_TIMEOUT_SECONDS = 1.0
|
||||
|
||||
|
||||
class OrchestratorStartError(RuntimeError):
|
||||
"""The orchestrator process did not become healthy within the timeout."""
|
||||
|
||||
|
||||
class OrchestratorProcess:
|
||||
"""Manages the local orchestrator control-plane process for the docker
|
||||
backend. Backend-neutral callers only need `ensure_running()` + `url`."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
host: str = DEFAULT_HOST,
|
||||
port: int = DEFAULT_PORT,
|
||||
*,
|
||||
broker: str = "docker",
|
||||
gateway: bool = True,
|
||||
) -> None:
|
||||
self.host = host
|
||||
self.port = port
|
||||
self._broker = broker
|
||||
self._gateway = gateway
|
||||
|
||||
@property
|
||||
def url(self) -> str:
|
||||
"""The control-plane base URL — also what the data plane's
|
||||
BOT_BOTTLE_ORCHESTRATOR_URL points at."""
|
||||
return f"http://{self.host}:{self.port}"
|
||||
|
||||
def is_healthy(self, *, timeout: float = _HEALTH_REQUEST_TIMEOUT_SECONDS) -> bool:
|
||||
"""True iff a control plane answers `GET /health` with 200 — the
|
||||
singleton liveness check."""
|
||||
try:
|
||||
with urllib.request.urlopen(f"{self.url}/health", timeout=timeout) as resp:
|
||||
return resp.status == 200
|
||||
except (urllib.error.URLError, TimeoutError, OSError):
|
||||
return False
|
||||
|
||||
def ensure_running(
|
||||
self, *, startup_timeout: float = DEFAULT_STARTUP_TIMEOUT_SECONDS,
|
||||
) -> str:
|
||||
"""Return the control-plane URL, starting the orchestrator first if it
|
||||
isn't already healthy. Idempotent — a healthy control plane is left
|
||||
untouched. Raises `OrchestratorStartError` if a freshly-spawned one
|
||||
doesn't answer within `startup_timeout`."""
|
||||
if self.is_healthy():
|
||||
return self.url
|
||||
log.info("starting orchestrator", context={"url": self.url, "broker": self._broker})
|
||||
self._spawn()
|
||||
deadline = time.monotonic() + startup_timeout
|
||||
while time.monotonic() < deadline:
|
||||
if self.is_healthy():
|
||||
log.info("orchestrator healthy", context={"url": self.url})
|
||||
return self.url
|
||||
time.sleep(_HEALTH_POLL_SECONDS)
|
||||
raise OrchestratorStartError(
|
||||
f"orchestrator at {self.url} did not become healthy within "
|
||||
f"{startup_timeout:g}s"
|
||||
)
|
||||
|
||||
def _argv(self) -> list[str]:
|
||||
"""`python -m bot_bottle.orchestrator ...` — static flags only."""
|
||||
argv = [
|
||||
sys.executable, "-m", "bot_bottle.orchestrator",
|
||||
"--host", self.host, "--port", str(self.port),
|
||||
"--broker", self._broker,
|
||||
]
|
||||
if self._gateway:
|
||||
argv.append("--gateway")
|
||||
return argv
|
||||
|
||||
def _log_path(self) -> str:
|
||||
"""Where the detached orchestrator's stdout/stderr goes so a failed
|
||||
start is diagnosable after the CLI has moved on."""
|
||||
return str(bot_bottle_root() / "orchestrator.log")
|
||||
|
||||
def _spawn(self) -> None:
|
||||
"""Launch the orchestrator detached so it outlives this CLI process,
|
||||
with its output tee'd to a log file under the bot-bottle root."""
|
||||
root = bot_bottle_root()
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
logfile = open(self._log_path(), "a", encoding="utf-8") # noqa: SIM115 # pylint: disable=consider-using-with
|
||||
try:
|
||||
subprocess.Popen( # noqa: S603 # pylint: disable=consider-using-with
|
||||
self._argv(),
|
||||
stdout=logfile,
|
||||
stderr=subprocess.STDOUT,
|
||||
stdin=subprocess.DEVNULL,
|
||||
start_new_session=True,
|
||||
)
|
||||
finally:
|
||||
# The child inherits its own dup'd fd; this handle is ours to drop.
|
||||
logfile.close()
|
||||
|
||||
|
||||
__all__ = [
|
||||
"OrchestratorProcess",
|
||||
"OrchestratorStartError",
|
||||
"DEFAULT_HOST",
|
||||
"DEFAULT_PORT",
|
||||
"DEFAULT_STARTUP_TIMEOUT_SECONDS",
|
||||
]
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Consolidated registration inputs (PRD 0070, docker slice).
|
||||
|
||||
Bridges the existing per-bottle `prepare` output to the consolidated
|
||||
registry: turns a prepared bottle's egress plan into the backend-neutral
|
||||
inputs `Orchestrator.launch_bottle` takes — the egress **policy** blob and
|
||||
launch **metadata**.
|
||||
|
||||
The policy blob is the exact routes YAML the per-bottle egress sidecar used
|
||||
to read from a file; in the consolidated model the multi-tenant gateway's
|
||||
`PolicyResolver` fetches it from the registry per request (keyed by source
|
||||
IP) instead. Same render, so consolidated and single-tenant egress apply
|
||||
byte-identical policy — a bottle's allow-list doesn't change when it moves
|
||||
onto the shared gateway.
|
||||
|
||||
Host-side glue (imports `bot_bottle.egress`), used by the launch path — not
|
||||
by the lean orchestrator control-plane process itself.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
|
||||
from ..egress import EgressPlan, egress_render_routes
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RegistrationInputs:
|
||||
"""What `Orchestrator.launch_bottle` needs to register a bottle, derived
|
||||
from its prepared plan. `policy` is served verbatim by the gateway's
|
||||
`/resolve`; `metadata` is opaque forward-compat state — it carries the
|
||||
human slug so the console / supervise can show a name, not just the
|
||||
minted bottle id."""
|
||||
|
||||
policy: str
|
||||
metadata: str
|
||||
|
||||
|
||||
def egress_policy(plan: EgressPlan) -> str:
|
||||
"""The bottle's egress policy blob: the routes YAML the gateway serves
|
||||
and the addon parses with `load_config`. Identical to the per-bottle
|
||||
`routes.yaml` render, so the consolidated path applies the same
|
||||
allow-list."""
|
||||
return egress_render_routes(plan.routes, log=plan.log)
|
||||
|
||||
|
||||
def registration_inputs(plan: EgressPlan) -> RegistrationInputs:
|
||||
"""Assemble the orchestrator registration inputs from a prepared egress
|
||||
plan. `metadata` records the slug so the shared registry can map a minted
|
||||
bottle id back to its human name."""
|
||||
return RegistrationInputs(
|
||||
policy=egress_policy(plan),
|
||||
metadata=json.dumps({"slug": plan.slug}),
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["RegistrationInputs", "egress_policy", "registration_inputs"]
|
||||
@@ -0,0 +1,42 @@
|
||||
"""Unit: shared-gateway source-IP allocation (PRD 0070)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from bot_bottle.backend.docker.gateway_net import NoFreeAddressError, next_free_ip
|
||||
|
||||
|
||||
class TestNextFreeIp(unittest.TestCase):
|
||||
def test_skips_router_and_returns_first_host(self) -> None:
|
||||
# .1 is docker's router; the first assignable address is .2.
|
||||
self.assertEqual("172.18.0.2", next_free_ip("172.18.0.0/16", []))
|
||||
|
||||
def test_skips_taken_addresses(self) -> None:
|
||||
# Gateway container holds .2, a live bottle holds .3 -> next is .4.
|
||||
self.assertEqual(
|
||||
"172.18.0.4", next_free_ip("172.18.0.0/16", ["172.18.0.2", "172.18.0.3"]),
|
||||
)
|
||||
|
||||
def test_taken_order_does_not_matter(self) -> None:
|
||||
self.assertEqual(
|
||||
"172.18.0.2", next_free_ip("172.18.0.0/16", ["172.18.0.3", "172.18.0.5"]),
|
||||
)
|
||||
|
||||
def test_allocation_is_deterministic(self) -> None:
|
||||
taken = ["172.18.0.2"]
|
||||
self.assertEqual(next_free_ip("172.18.0.0/16", taken),
|
||||
next_free_ip("172.18.0.0/16", taken))
|
||||
|
||||
def test_raises_when_subnet_exhausted(self) -> None:
|
||||
# /30: hosts are .1 (router, reserved) and .2; taking .2 leaves none.
|
||||
with self.assertRaises(NoFreeAddressError):
|
||||
next_free_ip("10.9.9.0/30", ["10.9.9.2"])
|
||||
|
||||
def test_accepts_host_bits_set_cidr(self) -> None:
|
||||
# A container's inspected address arrives as e.g. 172.18.0.2/16.
|
||||
self.assertEqual("172.18.0.2", next_free_ip("172.18.0.5/16", []))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Unit: consolidated per-bottle git-gate provisioning render (PRD 0070)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from bot_bottle.git_gate_render import (
|
||||
GitGateUpstream,
|
||||
git_gate_render_entrypoint,
|
||||
git_gate_render_provision,
|
||||
)
|
||||
|
||||
|
||||
def _ups(*names: str) -> tuple[GitGateUpstream, ...]:
|
||||
return tuple(
|
||||
GitGateUpstream(
|
||||
name=n,
|
||||
upstream_url=f"ssh://git@github.com/x/{n}.git",
|
||||
upstream_host="github.com",
|
||||
upstream_port="22",
|
||||
identity_file="",
|
||||
known_host_key="",
|
||||
)
|
||||
for n in names
|
||||
)
|
||||
|
||||
|
||||
class TestProvisionRender(unittest.TestCase):
|
||||
def test_namespaces_repos_and_creds_by_bottle_id(self) -> None:
|
||||
script = git_gate_render_provision("bottleab12", _ups("foo"))
|
||||
self.assertIn("repo=/git/bottleab12/${name}.git", script)
|
||||
self.assertIn("keyfile=/git-gate/creds/bottleab12/${name}-key", script)
|
||||
self.assertIn("mkdir -p /git/bottleab12", script)
|
||||
|
||||
def test_one_init_repo_call_per_upstream(self) -> None:
|
||||
script = git_gate_render_provision("b1", _ups("foo", "bar"))
|
||||
calls = [l for l in script.splitlines() if l.startswith("init_repo ")]
|
||||
self.assertEqual(2, len(calls))
|
||||
|
||||
def test_provision_does_not_start_the_daemon(self) -> None:
|
||||
# The shared gateway already serves; provisioning is init-only.
|
||||
self.assertNotIn("git daemon", git_gate_render_provision("b1", _ups("foo")))
|
||||
|
||||
def test_installs_pre_receive_hook(self) -> None:
|
||||
script = git_gate_render_provision("b1", _ups("foo"))
|
||||
self.assertIn("install -m 755 /etc/git-gate/pre-receive", script)
|
||||
|
||||
def test_rejects_unsafe_bottle_id(self) -> None:
|
||||
for bad in ("../etc", "a/b", "a b", "a;rm", ""):
|
||||
with self.assertRaises(ValueError):
|
||||
git_gate_render_provision(bad, _ups("foo"))
|
||||
|
||||
|
||||
class TestEntrypointUnchanged(unittest.TestCase):
|
||||
"""The shared `_git_gate_init_repo_fn` refactor must not alter the
|
||||
single-tenant daemon entrypoint's output."""
|
||||
|
||||
def test_entrypoint_still_single_tenant_flat(self) -> None:
|
||||
script = git_gate_render_entrypoint(_ups("foo"))
|
||||
self.assertIn("repo=/git/${name}.git", script) # flat, not namespaced
|
||||
self.assertIn("keyfile=/git-gate/creds/${name}-key", script)
|
||||
self.assertIn("--base-path=/git", script)
|
||||
self.assertIn("exec git daemon", script)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,87 @@
|
||||
"""Unit: orchestrator process lifecycle — idempotent singleton (PRD 0070)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
import urllib.error
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from bot_bottle.orchestrator.lifecycle import (
|
||||
OrchestratorProcess,
|
||||
OrchestratorStartError,
|
||||
)
|
||||
from tests.unit import use_bottle_root
|
||||
|
||||
_URLOPEN = "bot_bottle.orchestrator.lifecycle.urllib.request.urlopen"
|
||||
_POPEN = "bot_bottle.orchestrator.lifecycle.subprocess.Popen"
|
||||
_SLEEP = "bot_bottle.orchestrator.lifecycle.time.sleep"
|
||||
_MONOTONIC = "bot_bottle.orchestrator.lifecycle.time.monotonic"
|
||||
|
||||
|
||||
def _health(status: int) -> MagicMock:
|
||||
"""A urlopen() context-manager whose `.status` is `status`."""
|
||||
m = MagicMock()
|
||||
m.__enter__.return_value.status = status
|
||||
return m
|
||||
|
||||
|
||||
class TestOrchestratorProcess(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self._tmp.cleanup)
|
||||
self.addCleanup(use_bottle_root(Path(self._tmp.name)))
|
||||
self.p = OrchestratorProcess(port=8099)
|
||||
|
||||
def test_url(self) -> None:
|
||||
self.assertEqual("http://127.0.0.1:8099", self.p.url)
|
||||
|
||||
def test_is_healthy_true_on_200(self) -> None:
|
||||
with patch(_URLOPEN, return_value=_health(200)):
|
||||
self.assertTrue(self.p.is_healthy())
|
||||
|
||||
def test_is_healthy_false_on_error(self) -> None:
|
||||
with patch(_URLOPEN, side_effect=urllib.error.URLError("refused")):
|
||||
self.assertFalse(self.p.is_healthy())
|
||||
|
||||
def test_ensure_running_noop_when_already_healthy(self) -> None:
|
||||
with patch(_URLOPEN, return_value=_health(200)), patch(_POPEN) as popen:
|
||||
self.assertEqual(self.p.url, self.p.ensure_running())
|
||||
popen.assert_not_called() # a live control plane is left untouched
|
||||
|
||||
def test_ensure_running_spawns_then_waits_for_health(self) -> None:
|
||||
# First check (before spawn) fails; after spawn the poll succeeds.
|
||||
with patch(_URLOPEN, side_effect=[urllib.error.URLError("down"), _health(200)]), \
|
||||
patch(_POPEN) as popen, patch(_SLEEP):
|
||||
url = self.p.ensure_running()
|
||||
self.assertEqual(self.p.url, url)
|
||||
popen.assert_called_once()
|
||||
|
||||
def test_ensure_running_raises_on_startup_timeout(self) -> None:
|
||||
with patch(_URLOPEN, side_effect=urllib.error.URLError("down")), \
|
||||
patch(_POPEN), patch(_SLEEP), \
|
||||
patch(_MONOTONIC, side_effect=[0.0, 0.5, 2.0]):
|
||||
with self.assertRaises(OrchestratorStartError):
|
||||
self.p.ensure_running(startup_timeout=1.0)
|
||||
|
||||
def test_argv_includes_gateway_and_broker(self) -> None:
|
||||
argv = OrchestratorProcess(port=8099, broker="docker", gateway=True)._argv()
|
||||
self.assertIn("--gateway", argv)
|
||||
self.assertIn("bot_bottle.orchestrator", argv)
|
||||
self.assertEqual("docker", argv[argv.index("--broker") + 1])
|
||||
|
||||
def test_argv_omits_gateway_when_disabled(self) -> None:
|
||||
self.assertNotIn("--gateway", OrchestratorProcess(gateway=False)._argv())
|
||||
|
||||
def test_spawn_launches_detached_and_logs(self) -> None:
|
||||
with patch(_POPEN) as popen:
|
||||
self.p._spawn()
|
||||
popen.assert_called_once()
|
||||
kwargs = popen.call_args.kwargs
|
||||
self.assertTrue(kwargs["start_new_session"]) # outlives the CLI
|
||||
self.assertTrue((Path(self._tmp.name) / "orchestrator.log").exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Unit: consolidated registration inputs — egress policy round-trip (PRD 0070)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from bot_bottle.egress import EgressPlan, EgressRoute
|
||||
from bot_bottle.egress_addon_core import LOG_BLOCKS, load_config
|
||||
from bot_bottle.orchestrator.registration import (
|
||||
RegistrationInputs,
|
||||
egress_policy,
|
||||
registration_inputs,
|
||||
)
|
||||
|
||||
|
||||
def _plan(routes: tuple[EgressRoute, ...], *, slug: str = "demo", log: int = 0) -> EgressPlan:
|
||||
return EgressPlan(
|
||||
slug=slug,
|
||||
routes_path=Path("/unused/routes.yaml"),
|
||||
routes=routes,
|
||||
token_env_map={},
|
||||
log=log,
|
||||
)
|
||||
|
||||
|
||||
class TestEgressPolicy(unittest.TestCase):
|
||||
def test_policy_round_trips_through_load_config(self) -> None:
|
||||
# The policy the gateway serves must parse back to the same allow-list
|
||||
# the per-bottle sidecar applied — moving onto the shared gateway must
|
||||
# not change a bottle's egress.
|
||||
routes = (EgressRoute(host="api.example.com"), EgressRoute(host="pypi.org"))
|
||||
cfg = load_config(egress_policy(_plan(routes)))
|
||||
self.assertEqual(("api.example.com", "pypi.org"), tuple(r.host for r in cfg.routes))
|
||||
|
||||
def test_policy_preserves_log_level(self) -> None:
|
||||
plan = _plan((EgressRoute(host="x.example.com"),), log=LOG_BLOCKS)
|
||||
self.assertEqual(LOG_BLOCKS, load_config(egress_policy(plan)).log)
|
||||
|
||||
def test_empty_routes_yield_deny_all(self) -> None:
|
||||
cfg = load_config(egress_policy(_plan(())))
|
||||
self.assertEqual((), cfg.routes) # no routes → default-deny
|
||||
|
||||
|
||||
class TestRegistrationInputs(unittest.TestCase):
|
||||
def test_bundles_policy_and_slug_metadata(self) -> None:
|
||||
plan = _plan((EgressRoute(host="api.example.com"),), slug="my-bot")
|
||||
inputs = registration_inputs(plan)
|
||||
self.assertIsInstance(inputs, RegistrationInputs)
|
||||
self.assertEqual("my-bot", json.loads(inputs.metadata)["slug"])
|
||||
self.assertEqual(egress_policy(plan), inputs.policy) # same blob egress_policy renders
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user