Files
ecs-relay-service/deployable/tests/test_broker_deploy.py
T
ex_tianzy d3ce0099b7 fix(deploy): include legacy longconn profile during migration checks
The exported live-deployment baseline declares longconn under a Compose
profile. Default profile filtering can omit the already-running receiver
and make preflight report an unexpected service set.

Explicitly select only the known longconn profile for config and targeted
migration commands. Preserve strict service-set validation and report only
missing/extra service names, never resolved environment or secrets. Up still
targets connector/router with --no-deps, without starting another receiver.

Add regressions for profiled service omission and safe mismatch diagnostics.
Validation: 242 passed, 3 skipped (no real PostgreSQL), 30 existing dependency
warnings. Tool-specific coverage: 29 passed. Python 3.7 syntax and diff pass.
Actual ECS preflight must be rerun to verify its current service set; no
remote configuration, key material or running containers were changed.
2026-11-04 12:27:49 +08:00

290 lines
13 KiB
Python

"""Migration tooling policy tests; no host Docker or real deployment is used."""
import argparse
import copy
import importlib.util
import json
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("broker_deploy_test",ROOT/"scripts/broker_deploy.py")
deploy = importlib.util.module_from_spec(spec)
spec.loader.exec_module(deploy)
@pytest.fixture
def scenario(tmp_path,monkeypatch):
live = tmp_path/"live"
live.mkdir()
(live/".env").write_text("FAKE_SECRET=synthetic-do-not-print\n")
files = []
for i in range(7):
p = live/("compose"+str(i)+".yaml")
p.write_text("services: {}\n")
files.append(str(p))
caddy = live/"Caddyfile"
caddy.write_text("@private path /admin/* /internal/* /ready\nrespond @private 404\n")
labels = {"com.docker.compose.project":"hainuo-relay",
"com.docker.compose.project.working_dir":str(live),
"com.docker.compose.project.config_files":",".join(files)}
roles = {name:{"id":"container-"+name,"image":"sha256:"+str(i+1)*64,"image_ref":"old:tag","labels":labels}
for i,name in enumerate(("connector","router","longconn","enrollment-signer","edge"))}
env = {"HN_ENVIRONMENT":"production","HN_DATABASE_URL":"postgresql://fake@postgres/fake",
"HN_FEISHU_ENABLED":"true","HN_OAUTH_ENABLED":"true","HN_FEISHU_APP_ID":"cli_test",
"HN_FEISHU_APP_SECRET":"synthetic-app-secret","HN_ADMIN_SECRET":"a"*40,"HN_INTERNAL_SECRET":"b"*40}
base = {"services":{name:{"image":"old:tag","environment":{},"networks":{"backend":None},"volumes":[]}
for name in [*roles,"postgres","redis"]},"volumes":{"metadata":{}},"networks":{"backend":{}}}
base["services"]["connector"]["environment"] = env
base["services"]["connector"]["volumes"] = [{"type":"bind","source":str(live/"runtime"),"target":"/data"}]
base["services"]["edge"]["volumes"] = [{"type":"bind","source":str(caddy),"target":"/etc/caddy/Caddyfile"}]
profiles = tmp_path/"product.json"
profiles.write_text(json.dumps({"product-v1":["read"]}))
output = tmp_path/"migration"
calls = []
def fake_run(args,json_result=False,optional=False,input_text=None):
calls.append(args)
if args[:2] == ["docker","ps"]:
name = next(arg.rsplit('=',1)[1] for arg in args if arg.startswith('label=com.docker.compose.service='))
return "container-"+name
if args[:2] == ["docker","inspect"]:
name = args[2].replace("container-", "").replace("hainuo-relay-", "").removesuffix("-1")
return roles[name]
if args[:3] == ["docker","image","inspect"]:
if optional:
return None
return "arm64" if "{{.Architecture}}" in args else "sha256:"+"a"*64
if args[:2] == ["docker","compose"]:
if "config" not in args:
return ""
result = copy.deepcopy(base)
candidates = [Path(args[i+1]) for i,value in enumerate(args[:-1]) if value == "-f"]
if candidates[-1].name == "compose.broker.json":
overlay = json.loads(candidates[-1].read_text())
for name,fields in overlay["services"].items():
result["services"][name]["image"] = fields["image"]
for key,value in fields["environment"].items():
if value.startswith("${"):
value = env[value[2:].split(":")[0]]
result["services"][name]["environment"][key] = value
if "volumes" in fields:
result["services"][name]["volumes"].extend(fields["volumes"])
return result
return ""
monkeypatch.setattr(deploy,"run",fake_run)
args = argparse.Namespace(deployment_root=str(live),source_root=str(ROOT),profiles=str(profiles),
output=str(output),connector_container="hainuo-relay-connector-1")
return dict(args=args,live=live,output=output,calls=calls,roles=roles,base=base,profiles=profiles,caddy=caddy)
def prepared(scenario):
deploy.prepare(scenario["args"])
return deploy.load_plan(scenario["output"])
def test_preflight_is_read_only_and_redacted(scenario,capsys):
deploy.preflight(scenario["args"])
assert not scenario["output"].exists()
assert "synthetic" not in capsys.readouterr().out
assert not any("up" in c or "build" in c or "tag" in c for c in scenario["calls"])
def test_legacy_longconn_profile_included_without_enabling_all_profiles(scenario,monkeypatch):
original = deploy.run
def profiled(args,*a,**kw):
result = original(args,*a,**kw)
if args[:2] == ["docker","compose"] and "config" in args:
# Mirror Compose's omission of disabled profile services.
result["services"]["longconn"]["profiles"] = ["longconn"]
if "--profile" not in args or args[args.index("--profile")+1] != "longconn":
result["services"].pop("longconn")
return result
monkeypatch.setattr(deploy,"run",profiled)
deploy.preflight(scenario["args"])
plan = prepared(scenario)
deploy.check(scenario["output"],plan)
deploy.build(scenario["output"],plan)
deploy.activate(scenario["output"],plan,True)
for command in scenario["calls"]:
if command[:2] == ["docker","compose"]:
assert command[command.index("--profile")+1] == "longconn"
assert "*" not in command
if "up" in command:
assert command[-2:] == ["connector","router"] and "--no-deps" in command
@pytest.mark.parametrize("missing,extra", [("longconn",None),(None,"unexpected-service")])
def test_service_mismatch_reports_only_names_and_still_stops(scenario,missing,extra):
if missing:
scenario["base"]["services"].pop(missing)
if extra:
scenario["base"]["services"][extra] = {"environment":{"SECRET":"synthetic-do-not-print"}}
with pytest.raises(deploy.DeployError) as error:
deploy.preflight(scenario["args"])
assert (missing or extra) in str(error.value)
assert "synthetic" not in str(error.value)
assert not scenario["output"].exists()
def test_prepare_preserves_all_seven_files_runtime_identity_and_no_service_changes(scenario,capsys):
original = (scenario["live"]/".env").read_bytes()
plan = prepared(scenario)
assert len(plan["compose_files"]) == 7
assert (scenario["live"]/".env").read_bytes() == original
assert not (scenario["live"]/"runtime").exists()
overlay = json.loads((scenario["output"]/"compose.broker.json").read_text())
assert set(overlay["services"]) == {"connector","router"}
assert overlay["services"]["connector"]["environment"]["HN_CREDENTIAL_MODE"] == "broker"
assert len(json.loads((scenario["output"]/"credential-keyring.json").read_text())["keys"]) == 1
rollback = json.loads((scenario["output"]/"compose.rollback.json").read_text())
assert rollback["services"]["connector"]["image"] != rollback["services"]["router"]["image"]
assert not any("up" in c or "build" in c for c in scenario["calls"])
assert "synthetic" not in capsys.readouterr().out
def test_prepare_refuses_existing_output_and_preserves_key(scenario):
prepared(scenario)
key = (scenario["output"]/"credential-keyring.json").read_bytes()
with pytest.raises(deploy.DeployError):
deploy.prepare(scenario["args"])
assert (scenario["output"]/"credential-keyring.json").read_bytes() == key
def test_prepare_refuses_live_subdirectory(scenario):
scenario["args"].output = str(scenario["live"]/"broker")
with pytest.raises(deploy.DeployError,match="outside"):
deploy.prepare(scenario["args"])
@pytest.mark.parametrize("value", [{}, {"v1":[]}, {"v1":["bad scope"]}, {"bad profile":["read"]},[]])
def test_invalid_product_profiles_never_generate_keys(scenario,value):
scenario["profiles"].write_text(json.dumps(value))
with pytest.raises(deploy.DeployError):
deploy.prepare(scenario["args"])
assert not scenario["output"].exists()
@pytest.mark.parametrize("target", [".env","compose0.yaml"])
def test_check_refuses_changed_live_configuration(scenario,target):
plan = prepared(scenario)
with (scenario["live"]/target).open('a') as f:
f.write("# drift\n")
with pytest.raises(deploy.DeployError,match="changed"):
deploy.check(scenario["output"],plan)
@pytest.mark.parametrize("target", ["credential-keyring.json","compose.broker.json","compose.rollback.json","build/relay/credential_api.py"])
def test_check_refuses_changed_prepared_keys_or_payload(scenario,target):
plan = prepared(scenario)
with (scenario["output"]/target).open('a') as f:
f.write("\n")
with pytest.raises(deploy.DeployError,match="changed"):
deploy.check(scenario["output"],plan)
def test_check_refuses_unreviewed_edge_private_rules(scenario):
plan = prepared(scenario)
scenario["caddy"].write_text("reverse_proxy connector:8000\n")
with pytest.raises(deploy.DeployError,match="private"):
deploy.check(scenario["output"],plan)
def test_check_refuses_other_service_image_drift(scenario):
plan = prepared(scenario)
scenario["roles"]["longconn"]["image"] = "sha256:"+"f"*64
with pytest.raises(deploy.DeployError,match="longconn"):
deploy.check(scenario["output"],plan)
def test_activate_requires_client_readiness_before_any_docker_command(scenario):
plan = prepared(scenario)
scenario["calls"].clear()
with pytest.raises(deploy.DeployError,match="v2 client"):
deploy.activate(scenario["output"],plan,False)
assert not scenario["calls"]
def test_build_reuses_exact_runtime_without_download_or_service_restart(scenario):
plan = prepared(scenario)
scenario["calls"].clear()
deploy.build(scenario["output"],plan)
build = next(c for c in scenario["calls"] if c[:2] == ["docker","build"])
assert "--pull=false" in build and build[build.index('--network')+1] == 'none'
assert not any("up" in c for c in scenario["calls"])
dockerfile = (scenario["output"]/"build/Dockerfile").read_text()
assert "pip install" not in dockerfile and "apt" not in dockerfile
assert (scenario["output"]/"candidate.json").exists()
probes = [c for c in scenario["calls"] if c[:2] == ["docker","run"]]
assert len(probes) == 3
assert all("synthetic-app-secret" not in " ".join(c) for c in probes)
def test_failed_runtime_smoke_does_not_mark_candidate_ready(scenario,monkeypatch):
plan = prepared(scenario)
original = deploy.run
def fail(args,*a,**kw):
if args[:2] == ["docker","run"]:
raise deploy.DeployError("synthetic image smoke failure")
return original(args,*a,**kw)
monkeypatch.setattr(deploy,"run",fail)
with pytest.raises(deploy.DeployError,match="smoke"):
deploy.build(scenario["output"],plan)
assert not (scenario["output"]/"candidate.json").exists()
def test_activate_targets_only_connector_router_and_preserves_other_services(scenario):
plan = prepared(scenario)
deploy.build(scenario["output"],plan)
scenario["calls"].clear()
deploy.activate(scenario["output"],plan,True)
command = next(c for c in scenario["calls"] if "up" in c)
assert command[-2:] == ["connector","router"] and "--no-deps" in command
assert len([c for c in scenario["calls"] if "up" in c]) == 1
def test_activate_rejects_retagged_candidate(scenario,monkeypatch):
plan = prepared(scenario)
deploy.build(scenario["output"],plan)
original = deploy.run
def altered(args,*a,**kw):
if args[:3] == ["docker","image","inspect"] and args[3] == plan["candidate_image"]:
return "sha256:"+"f"*64
return original(args,*a,**kw)
monkeypatch.setattr(deploy,"run",altered)
scenario["calls"].clear()
with pytest.raises(deploy.DeployError,match="identity changed"):
deploy.activate(scenario["output"],plan,True)
assert not any("up" in c for c in scenario["calls"])
def test_activation_validation_failure_never_recreates_services(scenario,monkeypatch):
plan = prepared(scenario)
deploy.build(scenario["output"],plan)
def fail(*args,**kwargs):
raise deploy.DeployError("runtime settings rejected")
monkeypatch.setattr(deploy,"runtime_check",fail)
scenario["calls"].clear()
with pytest.raises(deploy.DeployError,match="settings"):
deploy.activate(scenario["output"],plan,True)
assert not any("up" in c for c in scenario["calls"])
def test_catchall_before_private_block_requires_manual_review(scenario):
plan = prepared(scenario)
original = scenario["caddy"].read_text()
scenario["caddy"].write_text("handle { reverse_proxy connector:8000 }\n"+original)
with pytest.raises(deploy.DeployError,match="private"):
deploy.check(scenario["output"],plan)
@pytest.mark.parametrize("field", ["volumes","networks"])
def test_overlay_validator_rejects_database_volume_or_network_change(scenario,field):
plan = prepared(scenario)
base = scenario["base"]
merged = deploy.run(deploy.compose(plan,scenario["output"]/"compose.broker.json")+["config"],True)
merged[field] = {"unexpected":{}}
with pytest.raises(deploy.DeployError):
deploy.validate_overlay(base,merged,plan["candidate_image"])