Files

28 lines
1.6 KiB
Python

#!/usr/bin/env python3
import json,subprocess
from pathlib import Path
root=Path('/home/ubuntu/apps/syly2-uat')
fmt='{"id":{{json .Id}},"name":{{json .Name}},"startedAt":{{json .State.StartedAt}},"running":{{json .State.Running}},"restartCount":{{json .RestartCount}}}'
before=[json.loads(x) for x in (root/'evidence/containers-before.jsonl').read_text().splitlines()]
after=[]
for old in before:
after.append(json.loads(subprocess.check_output(['docker','inspect','--format',fmt,old['id']],text=True)))
images={}
runtime=[]
for service in ['db','app','web']:
cid=subprocess.check_output(['docker','compose','-f',str(root/'compose.json'),'ps','-q',service],text=True).strip()
assert cid, service
state=json.loads(subprocess.check_output(['docker','inspect','--format',fmt,cid],text=True))
state['service']=service
state['image']=subprocess.check_output(['docker','inspect','--format','{{.Image}}',cid],text=True).strip()
state['network_names']=json.loads(subprocess.check_output(['docker','inspect','--format','{{json .NetworkSettings.Networks}}',cid],text=True)).keys()
state['network_names']=list(state['network_names'])
runtime.append(state)
info={'original_container_count':len(before),'original_containers_unchanged':before==after,'runtime':runtime}
(root/'evidence/containers-after.json').write_text(json.dumps(after,indent=2))
(root/'evidence/runtime-verification.json').write_text(json.dumps(info,indent=2))
print(json.dumps(info,indent=2))
assert before==after
assert all(x['running'] for x in runtime)
assert all(x['network_names']==['syly2-uat_backend'] for x in runtime if x['service'] in ['app','db'])