Discover the current Compose project and all override files from container labels. Add read-only preflight plus prepare/check/build/activate/rollback commands without editing the running .env, CA, database volumes or unrelated services. Preserve heterogeneous application image identities for rollback. Prepare a private keyring, independent router credential, pinned source snapshot and additive connector/router overlay. Refuse key overwrite and configuration/image drift. Build offline from the exact existing ARM64 runtime, smoke import and validate role settings through stdin-only inputs. Require explicit v2 client readiness before activation and recreate only connector/router with --no-deps. Document actual backup/image evidence, current client limitation and token-lifecycle rollback boundaries. Validation: 239 passed, 3 skipped (real PostgreSQL absent), 30 existing dependency deprecation warnings. Add 26 mocked Docker policy tests; host script parses as Python 3.7, CLI help/docs/diff checks pass. Docker policy mocks are not actual Compose or ARM64 build validation. No remote files, keys, credentials, containers or live configuration were changed. Stage 5 operational acceptance remains pending actual ECS build, real databases/platform, v2 client, key recovery and controlled rollout.
253 lines
11 KiB
Python
253 lines
11 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_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"])
|