Files
trex-core/tests/runtime/test_instance_config.py
T
genichinandClaude Opus 4.8 9f83c64a40 feat(multi-instance): dcy-009 BAT-013 — RabbitMQ 다중 인스턴스 토폴로지 (REQ-048~051)
dcy-009(다중 인스턴스 RabbitMQ 토폴로지) 전체 SDLC: draft-req → confirm-req →
draft-batch(BAT-013) → run-batch-delivery → confirm-req-implemented.

- REQ-048 (Configuration): DaemonInstanceConfig.from_toml + DaemonConfig.from_config
  (env 병합, portfolio_hub_token 주입) + 데몬 --config 필수 argparse + .gitignore 인스턴스
  TOML + config/instance.example.toml
- REQ-049 (Interface): topology 동적 큐명 빌더 intent/snapshot/control_queue(instance_key)
  — default=리터럴 큐명(하위호환), named=`{base}.{instance_key}` + daemon per-instance 구독
- REQ-050 (Integration): trex.direct(topic)/trex.broadcast(fanout) exchange +
  declare_topology(instance_key) per-instance binding + control dual binding + direct_routing_key
- REQ-051 (Documentation): interface-contract/runtime-flows/project-structure 정합

하위호환: instance_key="default" → 기존 리터럴 큐명·RPC 경로 무회귀(FR-5).
split-brain 방지(NFR-3): 인스턴스 전용 큐. 민감값(NFR-4): TOML + gitignore.

검증: 신규 9 단위 + RabbitMQ 라우팅 2, 단위 271 passed,
인프라 게이트 301 passed(directed 격리 S-2 + broadcast dual S-3 + default 무회귀 S-4),
ruff clean. REQ-048~051 Implemented, BAT-013 release-candidate.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-07 15:10:36 +09:00

69 lines
2.5 KiB
Python

"""Per-instance TOML config loader tests (REQ-048 / BAT-013)."""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
import pytest
from trex_core.runtime.config import DaemonConfig, DaemonInstanceConfig
_REPO = Path(__file__).resolve().parents[2]
def _write_toml(tmp_path, body: str) -> str:
p = tmp_path / "inst.toml"
p.write_text(body)
return str(p)
def test_from_toml_parses(tmp_path):
"""AC-1: from_toml reads instance_key + portfolio_hub_token."""
path = _write_toml(tmp_path, 'instance_key = "inst-1"\nportfolio_hub_token = "tok"\n')
cfg = DaemonInstanceConfig.from_toml(path)
assert cfg.instance_key == "inst-1"
assert cfg.portfolio_hub_token == "tok"
def test_from_toml_missing_instance_key_raises(tmp_path):
"""AC-4: missing required instance_key → error (no silent default)."""
path = _write_toml(tmp_path, 'portfolio_hub_token = "tok"\n')
with pytest.raises(RuntimeError):
DaemonInstanceConfig.from_toml(path)
def test_from_config_injects_token(tmp_path, monkeypatch):
"""AC-3 / S-5: TOML token is injected into PortfolioHubConfig when base_url is set,
even with TREX_PORTFOLIO_HUB_TOKEN absent from the environment."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
monkeypatch.setenv("TREX_PORTFOLIO_HUB_BASE_URL", "http://hub:18000")
monkeypatch.delenv("TREX_PORTFOLIO_HUB_TOKEN", raising=False)
path = _write_toml(tmp_path, 'instance_key = "inst-9"\nportfolio_hub_token = "toml-tok"\n')
cfg = DaemonConfig.from_config(path)
assert cfg.instance_key == "inst-9"
assert cfg.portfolio_hub is not None
assert cfg.portfolio_hub.token == "toml-tok"
def test_default_instance_key_when_from_env(monkeypatch):
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
assert DaemonConfig.from_env().instance_key == "default"
def test_config_required_cli_exits_nonzero():
"""AC-2: daemon entry without --config exits non-zero (argparse required)."""
proc = subprocess.run(
[sys.executable, "-m", "trex_core.runtime.daemon"],
capture_output=True, text=True, timeout=30, cwd=str(_REPO),
)
assert proc.returncode != 0
assert "config" in (proc.stderr + proc.stdout).lower()
def test_gitignore_excludes_instance_toml():
"""AC-5: .gitignore excludes instance TOML but keeps the example."""
gi = (_REPO / ".gitignore").read_text()
assert "/config/*.toml" in gi
assert "!/config/instance.example.toml" in gi