#!/usr/bin/env python3 """Prove a confirmed queued message survives broker container replacement, not only restart.""" from pathlib import Path import json import subprocess import sys import time ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'tests')) from client import Client,check def compose(*args): r=subprocess.run([str(ROOT/'scripts/compose.sh'),*args],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,timeout=150) check(r.returncode==0,'broker persistence infrastructure command failed: '+args[0]);return r.stdout.strip() def queued(): r=subprocess.run([str(ROOT/'scripts/compose.sh'),'exec','-T','broker','rabbitmqctl','-q','list_queues','name','messages'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,timeout=30) if r.returncode:return 0 out=r.stdout for line in out.splitlines(): fields=line.split() if len(fields)==2 and fields[0]=='attachments.v1':return int(fields[1]) return 0 def main(): c=Client('http://localhost:18086');alice,bob,eve,chat=c.bootstrap() try: compose('stop','worker');att,data=c.attachment(alice,chat) deadline=time.monotonic()+20 while queued()<1 and time.monotonic()=1,'message not durably queued before replacement') published=compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-c',"SELECT bool_and(status='published') FROM outbox WHERE job_id=(SELECT id FROM attachment_jobs WHERE attachment_id='%s')"%att['id']) check(published=='t','outbox not marked published before replacement') compose('stop','dispatcher') old=compose('ps','-q','broker') compose('up','-d','--no-deps','--force-recreate','--wait','--wait-timeout','120','broker') new=compose('ps','-q','broker');check(new!=old,'broker container was not replaced') deadline=time.monotonic()+30 while queued()<1 and time.monotonic()=1,'confirmed queue message lost after replacement') compose('start','worker');c.wait_ready(bob,att) check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==data,'original changed after broker replacement') result={'result':'PASS','attachment_id':att['id'],'broker_hostname':'broker','old_container_id':old,'new_container_id':new,'message_present_before_and_after':True,'outbox_published_before_replacement':True,'dispatcher_stopped_during_replacement':True} (ROOT/'evidence/broker-persistence.json').write_text(json.dumps(result,indent=2)+'\n') print('PASS: confirmed message survived broker container replacement with the same volume and stable node identity') finally:compose('start','worker','dispatcher') if __name__=='__main__':main()