"""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"]) def test_prepare_update_preserves_key_material_and_parent_plan_without_restart(scenario): parent=prepared(scenario) root=scenario['output'] original_key=(root/'credential-keyring.json').read_bytes() original_plan=(root/'plan.json').read_bytes() image='sha256:'+'a'*64 for name in ('connector','router'):scenario['roles'][name]['image']=image deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']}) output=root.parent/'update' args=argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output)) scenario['calls'].clear() deploy.prepare_update(args) updated=deploy.load_plan(output) assert (output/'credential-keyring.json').read_bytes()==original_key assert (root/'plan.json').read_bytes()==original_plan assert updated['base_image']==image and updated['roles']['connector']['image']==image assert updated['candidate_image']!=parent['candidate_image'] assert updated['update_preserves_credentials'] is True overlay=json.loads((output/'compose.broker.json').read_text()) old=json.loads((root/'compose.broker.json').read_text()) assert overlay['services']['connector']['environment']==old['services']['connector']['environment'] rollback=json.loads((output/'compose.rollback.json').read_text()) assert rollback['services']['connector']['environment']==old['services']['connector']['environment'] assert rollback['services']['connector']['volumes']==overlay['services']['connector']['volumes'] assert rollback['services']['connector']['image']==image assert not any('up' in call or 'build' in call or 'tag' in call for call in scenario['calls']) def test_prepare_update_rejects_partial_switch_and_existing_output(scenario): parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64 deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']}) scenario['roles']['connector']['image']=image output=root.parent/'update' args=argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output)) with pytest.raises(deploy.DeployError,match='both services'):deploy.prepare_update(args) assert not output.exists() scenario['roles']['router']['image']=image args.output=str(root) with pytest.raises(deploy.DeployError,match='new absolute'):deploy.prepare_update(args) def permission_scenario(scenario,monkeypatch): plan=prepared(scenario) image='sha256:'+'a'*64 deploy.write_json(scenario['output']/'candidate.json',{'image':image,'tag':plan['candidate_image']}) for name in ('connector','router'): scenario['roles'][name]['image']=image scenario['roles'][name]['state']={'status':'running','health':'healthy'} profiles=scenario['profiles'];profiles.write_text(json.dumps({'product-v1':['read','wiki:wiki']})) original=deploy.run def scoped_run(args,*a,**kw): if args[:2]==['docker','inspect'] and args[-1]=='{{json .Config.Env}}': overlay=json.loads((scenario['output']/'compose.broker.json').read_text()) return ['HN_CREDENTIAL_PERMISSION_PROFILES='+overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']] return original(args,*a,**kw) monkeypatch.setattr(deploy,'run',scoped_run) return plan def test_permission_review_is_read_only_and_redacted(scenario,monkeypatch,capsys): plan=permission_scenario(scenario,monkeypatch) before=(scenario['output']/'compose.broker.json').read_bytes() deploy.update_permissions(scenario['output'],plan,scenario['profiles']) assert (scenario['output']/'compose.broker.json').read_bytes()==before assert not any('up' in c for c in scenario['calls']) text=capsys.readouterr().out assert 'wiki:wiki' in text and 'synthetic-app-secret' not in text def test_permission_apply_changes_only_connector_scope_and_preserves_keyring(scenario,monkeypatch): plan=permission_scenario(scenario,monkeypatch) key=(scenario['output']/'credential-keyring.json').read_bytes() deploy.update_permissions(scenario['output'],plan,scenario['profiles'],apply=True) overlay=json.loads((scenario['output']/'compose.broker.json').read_text()) assert json.loads(overlay['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES'])=={'product-v1':['read','wiki:wiki']} updated=deploy.load_plan(scenario['output']) assert updated['prepared_hashes']['compose.broker.json']==deploy.digest(scenario['output']/'compose.broker.json') assert (scenario['output']/'credential-keyring.json').read_bytes()==key calls=[c for c in scenario['calls'] if 'up' in c] assert len(calls)==1 and calls[0][-1]=='connector' and '--no-deps' in calls[0] before=len(scenario['calls']) deploy.update_permissions(scenario['output'],updated,scenario['profiles'],apply=True) assert not any('up' in c for c in scenario['calls'][before:]) def test_permission_update_rejects_scope_removal(scenario,monkeypatch): plan=permission_scenario(scenario,monkeypatch) scenario['profiles'].write_text(json.dumps({'product-v1':['wiki:wiki']})) with pytest.raises(deploy.DeployError,match='additive'):deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True) assert not any('up' in c for c in scenario['calls']) def test_permission_failed_restart_restores_overlay_and_plan(scenario,monkeypatch): plan=permission_scenario(scenario,monkeypatch) before={name:(scenario['output']/name).read_bytes() for name in ('compose.broker.json','plan.json')} original=deploy.run;calls=[0] def fail_once(args,*a,**kw): if 'up' in args: calls[0]+=1 if calls[0]==1:raise deploy.DeployError('synthetic restart failure') return original(args,*a,**kw) monkeypatch.setattr(deploy,'run',fail_once) with pytest.raises(deploy.DeployError,match='restored'):deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True) assert calls[0]==2 for name,value in before.items():assert json.loads((scenario['output']/name).read_text())==json.loads(value) def test_permission_removal_requires_exact_explicit_scope_and_is_reviewed(scenario,monkeypatch,capsys): plan=permission_scenario(scenario,monkeypatch) # Existing synthetic profile has only read; replacing it must be explicit. scenario['profiles'].write_text(json.dumps({'product-v1':['wiki:wiki']})) with pytest.raises(deploy.DeployError,match='exact'): deploy.update_permissions(scenario['output'],plan,scenario['profiles'],False,['other:scope']) deploy.update_permissions(scenario['output'],plan,scenario['profiles'],False,['read']) assert 'Product scope removals' in capsys.readouterr().out assert not any('up' in c for c in scenario['calls']) deploy.update_permissions(scenario['output'],plan,scenario['profiles'],True,['read']) scopes=json.loads(json.loads((scenario['output']/'compose.broker.json').read_text())['services']['connector']['environment']['HN_CREDENTIAL_PERMISSION_PROFILES']) assert scopes=={'product-v1':['wiki:wiki']} def test_product_explicit_request_does_not_copy_implicit_token_scope(): scopes=json.loads((ROOT/'examples/work-product-profiles.expanded.json').read_text())['current-config-v1'] assert 'auth:user.id:read' not in scopes assert {'drive:drive','wiki:wiki','calendar:calendar'}<=set(scopes) def test_private_approved_profile_excludes_pending_review_scopes(): all_scopes=set(json.loads((ROOT/'examples/work-product-profiles.expanded.json').read_text())['current-config-v1']) approved=set(json.loads((ROOT/'examples/work-product-profiles.approved.json').read_text())['current-config-v1']) assert all_scopes-approved=={'contact:department.base:readonly','contact:department.organize:readonly','task:tasklist:write'} assert len(approved)==20 and {'drive:drive','wiki:wiki','bitable:app','task:task:write'}<=approved def test_prepare_update_file_feature_is_explicit_and_rollback_restores_parent_flag(scenario): parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64 for name in ('connector','router'):scenario['roles'][name]['image']=image deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']}) output=root.parent/'update-files' args=argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output),enable_work_files=True) deploy.prepare_update(args) current=json.loads((output/'compose.broker.json').read_text()) rollback=json.loads((output/'compose.rollback.json').read_text()) old=json.loads((root/'compose.broker.json').read_text()) assert current['services']['connector']['environment']['HN_WORK_FILES_ENABLED']=='true' assert rollback['services']['connector']['environment']==old['services']['connector']['environment'] assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes() def test_prepare_update_cards_are_opt_in_and_rollback_keeps_parent_environment(scenario): parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64 for name in ('connector','router'):scenario['roles'][name]['image']=image deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']}) output=root.parent/'update-cards' deploy.prepare_update(argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output),enable_task_cards=True)) current=json.loads((output/'compose.broker.json').read_text()) rollback=json.loads((output/'compose.rollback.json').read_text()) old=json.loads((root/'compose.broker.json').read_text()) assert current['services']['connector']['environment']['HN_WORK_TASK_CARDS_ENABLED']=='true' assert rollback['services']['connector']['environment']==old['services']['connector']['environment'] assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes() def approval_update(scenario,**changes): parent=prepared(scenario);root=scenario['output'];image='sha256:'+'a'*64 for name in ('connector','router'):scenario['roles'][name]['image']=image deploy.write_json(root/'candidate.json',{'image':image,'tag':parent['candidate_image']}) env=scenario['base']['services']['connector']['environment'] scenario['base']['services']['longconn']['environment'].update( HN_FEISHU_APP_ID=env['HN_FEISHU_APP_ID'],HN_INTERNAL_SECRET=env['HN_INTERNAL_SECRET'],HN_FEISHU_EVENT_SOURCE='longconn') identity=root.parent/'callback-private.json' identity.write_text(json.dumps({'tenant_key':'tenant_test','verification_token':'synthetic-verify-token'})) identity.chmod(0o600) args=argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(root.parent/'approvals'), enable_remote_approvals=True,callback_identity=str(identity)) for key,value in changes.items():setattr(args,key,value) return args def test_approval_update_preserves_runtime_ancestry_and_original_longconn_rollback(scenario): args=approval_update(scenario);deploy.prepare_update(args) root=Path(args.output);plan=deploy.load_plan(root) candidate=json.loads((root/'compose.broker.json').read_text());rollback=json.loads((root/'compose.rollback.json').read_text()) assert deploy.targets(plan)==('connector','router','longconn') assert plan['longconn_base_image']==scenario['roles']['longconn']['image'] assert candidate['services']['longconn']['image']!=candidate['services']['connector']['image'] assert candidate['services']['longconn']['environment']['HN_CREDENTIAL_MODE']=='broker' assert candidate['services']['connector']['environment']['HN_APPROVAL_RETENTION_ENABLED']=='false' assert rollback['services']['longconn']['image']==scenario['roles']['longconn']['image'] assert rollback['services']['longconn']['environment']['HN_CREDENTIAL_MODE']=='legacy' assert rollback['services']['longconn']['environment']['HN_WORK_REMOTE_APPROVALS_ENABLED']=='false' assert (root/'credential-keyring.json').read_bytes()==(Path(args.plan)/'credential-keyring.json').read_bytes() def test_second_approval_update_uses_active_parent_images_and_retains_off(scenario): args=approval_update(scenario);deploy.prepare_update(args) root=Path(args.output);parent=deploy.load_plan(root) connector='sha256:'+'c'*64;longconn='sha256:'+'d'*64 for name in ('connector','router'):scenario['roles'][name]['image']=connector scenario['roles']['longconn']['image']=longconn deploy.write_json(root/'candidate.json',{'image':connector,'tag':parent['candidate_image']}) deploy.write_json(root/'candidate-longconn.json',{'image':longconn,'tag':parent['longconn_candidate_image']}) output=root.parent/'approvals-m6-fix' deploy.prepare_update(argparse.Namespace(plan=str(root),source_root=str(ROOT),output=str(output), enable_remote_approvals=True,callback_identity=args.callback_identity)) result=deploy.load_plan(output) candidate=json.loads((output/'compose.broker.json').read_text()) rollback=json.loads((output/'compose.rollback.json').read_text()) assert result['base_image']==connector and result['longconn_base_image']==longconn assert candidate['services']['connector']['environment']['HN_APPROVAL_RETENTION_ENABLED']=='false' assert rollback['services']['connector']['image']==connector assert rollback['services']['longconn']['image']==longconn assert rollback['services']['longconn']['environment']['HN_WORK_REMOTE_APPROVALS_ENABLED']=='true' assert (output/'credential-keyring.json').read_bytes()==(root/'credential-keyring.json').read_bytes() def test_approval_runtime_checks_three_roles_and_sdk_without_network(scenario): args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root) scenario['calls'].clear();deploy.runtime_check(root,plan) probes=[c for c in scenario['calls']if c[:2]==['docker','run']] assert len(probes)==3 assert all('--network'in c and c[c.index('--network')+1]=='none'for c in probes) assert any(plan['longconn_candidate_image']in c and 'register_p2_card_action_trigger'in c[-1]for c in probes) assert not any('synthetic-verify-token'in part for c in probes for part in c) def test_approval_activation_targets_original_three_services_only(scenario): args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root) image='sha256:'+'a'*64 deploy.write_json(root/'candidate.json',{'image':image,'tag':plan['candidate_image']}) deploy.write_json(root/'candidate-longconn.json',{'image':image,'tag':plan['longconn_candidate_image']}) scenario['calls'].clear();deploy.activate(root,plan,True) up=[c for c in scenario['calls']if 'up'in c] assert len(up)==1 and up[0][-3:]==['connector','router','longconn'] assert '--no-deps'in up[0]and '--pull'in up[0] def test_approval_overlay_rejects_longconn_command_or_network_change(scenario): args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root) base=deploy.run(deploy.compose(plan)+['config','--format','json'],True) merged=deploy.run(deploy.compose(plan,root/'compose.broker.json')+['config','--format','json'],True) merged['services']['longconn']['entrypoint']=['sh','-c','unreviewed'] with pytest.raises(deploy.DeployError,match='unrelated longconn'): deploy.validate_overlay(base,merged,plan['candidate_image'],approval_plan=plan) def test_approval_update_rejects_different_app_or_missing_identity(scenario): args=approval_update(scenario) scenario['base']['services']['longconn']['environment']['HN_FEISHU_APP_ID']='other' with pytest.raises(deploy.DeployError,match='does not match'):deploy.prepare_update(args) assert not Path(args.output).exists() def test_approval_build_reuses_distinct_longconn_base_and_saves_both_receipts(scenario): args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output);plan=deploy.load_plan(root) scenario['calls'].clear();deploy.build(root,plan) commands=[c for c in scenario['calls']if c[:2]==['docker','build']] assert len(commands)==2 assert 'BASE_IMAGE='+scenario['roles']['longconn']['image']in commands[1] assert plan['longconn_candidate_image']in commands[1] assert all('--network'in c and c[c.index('--network')+1]=='none'for c in commands) assert (root/'candidate.json').is_file()and(root/'candidate-longconn.json').is_file() def test_approval_cli_rollback_restores_original_longconn_too(scenario,monkeypatch): args=approval_update(scenario);deploy.prepare_update(args);root=Path(args.output) monkeypatch.setattr(deploy.sys,'argv',['broker_deploy.py','rollback','--plan',str(root),'--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][-3:]==['connector','router','longconn'] assert str(root/'compose.rollback.json')in commands[0]