"""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} env.update(HN_PUBLIC_URL="https://relay.example.test",HN_ROUTER_URL="wss://relay.example.test/device/ws", HN_FEISHU_API_BASE="https://work.example.test",HN_FEISHU_AUTHORIZE_URL="https://work.example.test/open-apis/authen/v1/index") 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 trust=live/'runtime/trust';trust.mkdir(parents=True) (trust/'connector.pub').write_text('-----BEGIN PUBLIC KEY-----\nsynthetic-public-material\n-----END PUBLIC KEY-----\n') base['services']['router']['environment']['HN_CA_CERT']='/trust/ca.pem' base['services']['router']['volumes']=[{'type':'bind','source':str(trust),'target':'/trust','read_only':True}] 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 sorted(p.relative_to(scenario['live']/'runtime').as_posix() for p in (scenario['live']/'runtime').rglob('*') if p.is_file())==['trust/connector.pub'] assert not (scenario['live']/'runtime/issuer').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 overlay['services']['router']['environment']['HN_CONNECTOR_PUBLIC_KEY']=='/trust/connector.pub' 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 @pytest.mark.parametrize("environment", ["development","test","production"]) def test_preserves_existing_environment_namespace_with_tls(scenario,environment): scenario["base"]["services"]["connector"]["environment"]["HN_ENVIRONMENT"] = environment plan = prepared(scenario) deploy.check(scenario["output"],plan) merged = deploy.run(deploy.compose(plan,scenario["output"]/"compose.broker.json")+["config"],True) assert merged["services"]["connector"]["environment"]["HN_ENVIRONMENT"] == environment assert merged["services"]["router"]["environment"]["HN_ENVIRONMENT"] == environment @pytest.mark.parametrize("name,value", [("HN_DATABASE_URL","sqlite://synthetic-secret"), ("HN_ENVIRONMENT","invalid-private-value"),("HN_FEISHU_ENABLED","false"), ("HN_PUBLIC_URL","http://synthetic-secret"),("HN_ROUTER_URL","ws://synthetic-secret"), ("HN_FEISHU_API_BASE","http://synthetic-secret")]) def test_prerequisite_failure_reports_field_before_generating_keys(scenario,name,value): scenario["base"]["services"]["connector"]["environment"][name] = value with pytest.raises(deploy.DeployError) as error: deploy.prepare(scenario["args"]) assert name in str(error.value) and "synthetic-secret" not in str(error.value) assert "invalid-private-value" not in str(error.value) assert not scenario["output"].exists() def test_diagnose_preserves_previously_generated_key_and_never_restarts(scenario,capsys): plan = prepared(scenario) before = {p.name:p.read_bytes() for p in scenario["output"].iterdir() if p.is_file()} scenario["base"]["services"]["connector"]["environment"]["HN_ENVIRONMENT"] = "development" scenario["calls"].clear() deploy.diagnose(scenario["output"],plan) assert "development" in capsys.readouterr().out assert before == {p.name:p.read_bytes() for p in scenario["output"].iterdir() if p.is_file()} assert not any("up" in c or "tag" in c or "build" in c for c in scenario["calls"]) def test_diagnose_never_echoes_database_credentials(scenario,capsys): plan = prepared(scenario) env = scenario["base"]["services"]["connector"]["environment"] env["HN_DATABASE_URL"] = "unsupported://synthetic-db-secret@host" with pytest.raises(deploy.DeployError): deploy.diagnose(scenario["output"],plan) output = capsys.readouterr().out assert "HN_DATABASE_URL" in output and "synthetic-db-secret" not in output 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_state_sees_exited_candidate_without_showing_logs_or_secrets(scenario,monkeypatch,capsys): plan = prepared(scenario) deploy.write_json(scenario['output']/'candidate.json',{'image':scenario['roles']['connector']['image'],'tag':plan['candidate_image']}) scenario['roles']['connector']['state']={'status':'exited','health':'unhealthy','exit_code':1,'restart_count':7} original=deploy.run def logs(args,*a,**kw): if args[:2]==['docker','logs']: scenario['calls'].append(args) return 'Traceback PermissionError synthetic-private-password https://secret.invalid/private' return original(args,*a,**kw) monkeypatch.setattr(deploy,'run',logs) scenario['calls'].clear() deploy.deployment_state(scenario['output'],plan) output=capsys.readouterr().out assert 'image=candidate status=exited health=unhealthy' in output assert 'PermissionError' in output and 'synthetic-private-password' not in output and 'secret.invalid' not in output assert all('-aq' in c for c in scenario['calls'] if c[:2]==['docker','ps']) assert not any('up' in c or 'tag' in c for c in scenario['calls']) def test_containers_without_healthcheck_do_not_break_state_or_router_repair(scenario,monkeypatch,capsys): plan=partial_old_plan(scenario) original=deploy.run inspected=[] def optional_health(args,*a,**kw): if args[:2]==['docker','inspect']: template=args[args.index('--format')+1] name=args[2].replace('container-','').replace('hainuo-relay-','').removesuffix('-1') inspected.append(name) if name in {'longconn','edge'}: # Docker map templates fail on missing .State.Health; index # returns nil and the with/else branch emits health="none". if '.State.Health' in template: raise deploy.DeployError('container-inspect failed; no map entry') assert 'index .State "Health"' in template result=copy.deepcopy(original(args,*a,**kw)) result['state']={'status':'running','health':'none','exit_code':0,'restart_count':0} return result return original(args,*a,**kw) monkeypatch.setattr(deploy,'run',optional_health) deploy.deployment_state(scenario['output'],plan) output=capsys.readouterr().out assert 'longconn: image=original status=running health=none' in output assert 'edge: image=original status=running health=none' in output deploy.repair_router(scenario['output'],plan) assert {'longconn','edge','connector','router'}.issubset(inspected) commands=[c for c in scenario['calls'] if 'up' in c] assert len(commands)==1 and commands[0][-1]=='router' def test_state_does_not_misreport_inspect_failure_as_missing_container(scenario,monkeypatch,capsys): plan=prepared(scenario) original=deploy.run def failure(args,*a,**kw): if args[:2]==['docker','inspect'] and args[2]=='container-edge': raise deploy.DeployError('container-inspect failed') return original(args,*a,**kw) monkeypatch.setattr(deploy,'run',failure) deploy.deployment_state(scenario['output'],plan) output=capsys.readouterr().out assert 'edge: container inspection failed' in output assert 'edge: no unique existing container' not in output def test_rollback_checks_allow_stopped_known_candidate_but_reject_other_image(scenario): plan=prepared(scenario) scenario['roles']['connector']['image']='sha256:'+'a'*64 deploy.write_json(scenario['output']/'candidate.json',{'image':'sha256:'+'a'*64,'tag':plan['candidate_image']}) scenario['calls'].clear() deploy.check(scenario['output'],plan,baseline=False) assert any(c[:2]==['docker','ps'] and '-aq' in c for c in scenario['calls']) scenario['roles']['router']['image']='sha256:'+'f'*64 with pytest.raises(deploy.DeployError,match='unreviewed target image'): deploy.check(scenario['output'],plan,baseline=False) def partial_old_plan(scenario): plan=prepared(scenario) overlay_path=scenario['output']/'compose.broker.json' overlay=json.loads(overlay_path.read_text()) overlay['services']['router']['environment'].pop('HN_CONNECTOR_PUBLIC_KEY') deploy.replace_json(overlay_path,overlay) plan['prepared_hashes']['compose.broker.json']=deploy.digest(overlay_path) deploy.replace_json(scenario['output']/'plan.json',plan) image='sha256:'+'a'*64 deploy.write_json(scenario['output']/'candidate.json',{'image':image,'tag':plan['candidate_image']}) scenario['roles']['connector']['image']=image scenario['roles']['connector']['state']={'status':'running','health':'healthy'} scenario['roles']['router']['image']=image scenario['roles']['router']['state']={'status':'restarting','health':'unhealthy'} return plan def test_repair_old_partial_switch_only_restarts_router_preserving_keys(scenario): plan=partial_old_plan(scenario) key=(scenario['output']/'credential-keyring.json').read_bytes() original_env=(scenario['live']/'.env').read_bytes() scenario['calls'].clear() deploy.repair_router(scenario['output'],plan) assert (scenario['output']/'credential-keyring.json').read_bytes()==key assert (scenario['live']/'.env').read_bytes()==original_env fixed=json.loads((scenario['output']/'compose.broker.json').read_text()) assert fixed['services']['router']['environment']['HN_CONNECTOR_PUBLIC_KEY']=='/trust/connector.pub' saved=deploy.load_plan(scenario['output']) assert saved['prepared_hashes']['compose.broker.json']==deploy.digest(scenario['output']/'compose.broker.json') commands=[c for c in scenario['calls'] if 'up' in c] assert len(commands)==1 and commands[0][-1]=='router' and commands[0][-2]=='180' assert '--no-deps' in commands[0] and commands[0][-2:]!=['connector','router'] assert (scenario['output']/'plan.before-router-key.json').is_file() assert (scenario['output']/'compose.broker.before-router-key.json').is_file() def test_repair_retry_does_not_regenerate_key_or_overwrite_backup(scenario): plan=partial_old_plan(scenario) deploy.repair_router(scenario['output'],plan) files={p.name:p.read_bytes() for p in scenario['output'].iterdir() if p.is_file()} deploy.repair_router(scenario['output'],deploy.load_plan(scenario['output'])) assert files=={p.name:p.read_bytes() for p in scenario['output'].iterdir() if p.is_file()} def test_repair_requires_healthy_known_connector(scenario): plan=partial_old_plan(scenario) scenario['roles']['connector']['state']['health']='unhealthy' before=(scenario['output']/'compose.broker.json').read_bytes() with pytest.raises(deploy.DeployError,match='healthy'): deploy.repair_router(scenario['output'],plan) assert (scenario['output']/'compose.broker.json').read_bytes()==before assert not any('up' in c for c in scenario['calls']) def test_old_plan_fails_normal_check_until_explicit_repair(scenario): plan=partial_old_plan(scenario) with pytest.raises(deploy.DeployError,match='HN_CONNECTOR_PUBLIC_KEY'): deploy.check(scenario['output'],plan,baseline=False) def test_cli_rollback_of_old_partial_plan_does_not_require_new_router_key(scenario,monkeypatch): partial_old_plan(scenario) monkeypatch.setattr(deploy.sys,'argv',['broker_deploy.py','rollback','--plan',str(scenario['output']),'--stop-broker-use']) scenario['calls'].clear() deploy.main() commands=[c for c in scenario['calls'] if 'up' in c] assert len(commands)==1 and commands[0][-2:]==['connector','router'] assert str(scenario['output']/'compose.rollback.json') in commands[0] def test_missing_trust_public_key_refused_before_any_new_credentials(scenario): trust=Path(scenario['base']['services']['router']['volumes'][0]['source']) (trust/'connector.pub').unlink() with pytest.raises(deploy.DeployError,match='public key'): deploy.prepare(scenario['args']) assert not scenario['output'].exists() def test_run_failure_identifies_stage_without_echoing_command_secrets(monkeypatch): import types def fail(*a,**kw): return types.SimpleNamespace(returncode=17,stdout='synthetic-access',stderr='synthetic-secret') monkeypatch.setattr(deploy.subprocess,'run',fail) with pytest.raises(deploy.DeployError) as error: deploy.run(['docker','compose','--env-file','sensitive-path','up','connector']) assert 'compose-up' in str(error.value) and '17' in str(error.value) assert 'synthetic' not in str(error.value) and 'sensitive-path' not in str(error.value) def test_log_signals_only_emit_fixed_error_classes(): assert deploy.log_signals('PermissionError: synthetic-secret\nread-only file system')==['PermissionError','read-only-filesystem'] assert deploy.log_signals('fake-secret https://secret.invalid')==[] 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"])