import pathlib,json,subprocess,shutil
R=pathlib.Path(__file__).resolve().parent.parent;repo=pathlib.Path('/workspace/contextra-analytics-runtime-lifecycle-refactor')
image=json.loads((R/'candidate-image.json').read_text())['image'];work=R/'candidate-tests'
name='ct-offline-prep-source-copy'
subprocess.run(['docker','create','--runtime=runc','--name',name,'--network=none','--entrypoint','true',image],check=True,stdout=subprocess.DEVNULL)
try:subprocess.run(['docker','cp',name+':/opt/contextra/app',str(work/'app')],check=True)
finally:subprocess.run(['docker','rm',name],check=True,stdout=subprocess.DEVNULL)
p=work/'tools/icerti_offline';p.mkdir(parents=True)
shutil.copy2(repo/'tools/icerti_offline/fight_r5_settings.py',p/'fight_r5_settings.py')
names=['test_fight_r5.py','test_fight_r5_site.py','test_fight_display_hold.py','test_fight_notification.py','test_fight_notification_runtime.py','test_fall_r2.py','test_fall_r2_config.py','test_multi_roi_runtime.py','test_multi_roi_contract.py','test_event_cooldown.py','test_protected_packet_cache.py']
code="import sys,pytest;sys.path[:0]=['/opt/contextra','/work'];raise SystemExit(pytest.main("+repr(['-q','-p','no:cacheprovider',*['/work/tests/'+n for n in names]])+"))"
with (R/'candidate-tests-r02.log').open('wb') as f:
 p=subprocess.run(['docker','run','--rm','--pull=never','--runtime=runc','--name','ct-offline-prep-cpu-tests','--network=none','--read-only','--cpus=2','--memory=8g','--pids-limit=256','--cap-drop=ALL','--security-opt=no-new-privileges','--tmpfs','/tmp:rw,size=256m','-e','NVIDIA_VISIBLE_DEVICES=void','-e','PYTHONDONTWRITEBYTECODE=1','-e','OPENBLAS_NUM_THREADS=1','-v',str(work)+':/work:ro','--entrypoint','python3',image,'-B','-c',code],stdout=f,stderr=subprocess.STDOUT,timeout=600)
(R/'candidate-tests-r02-result.json').write_text(json.dumps(dict(returncode=p.returncode)))
print('test_exit',p.returncode)
