diff --git a/student-tests/clean_clone.py b/student-tests/clean_clone.py index 20ed2a3..2c209b3 100644 --- a/student-tests/clean_clone.py +++ b/student-tests/clean_clone.py @@ -4,6 +4,7 @@ import json import os from pathlib import Path import subprocess +import socket import tempfile import uuid ROOT=Path(__file__).resolve().parents[1] @@ -13,7 +14,12 @@ lines=['Checked source commit: '+commit,'Isolated Compose project: '+project] with tempfile.TemporaryDirectory(prefix='mtusi-lab2-clean-') as directory: clone=Path(directory)/'repo' subprocess.run(['git','clone','--quiet','--local',str(ROOT),str(clone)],check=True) - env=dict(os.environ,COMPOSE_PROJECT_NAME=project,APP_PORT='0',SESSION_TTL_SECONDS='3600') + # A dynamically published port 0 may change on container stop/start. + # Select a free numeric port so persistence keeps the same base URL. + with socket.socket() as listener: + listener.bind(('127.0.0.1',0)) + host_port=listener.getsockname()[1] + env=dict(os.environ,COMPOSE_PROJECT_NAME=project,APP_PORT=str(host_port),SESSION_TTL_SECONDS='3600') def run(args): p=subprocess.run(args,cwd=clone,env=env,text=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) lines.append('$ '+' '.join(args)+'\n'+p.stdout)