126 lines
8.2 KiB
Python
126 lines
8.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Run bounded native Workbench smoke tests inside a windowless Gamescope display.
|
|
Requires Linux, gamescope, Proton, and a disposable COPY of a working Tools compat prefix.
|
|
No desktop rules or existing Wine processes are changed. This is not a dedicated server.
|
|
"""
|
|
import argparse, fcntl, json, os, shutil, signal, subprocess, time
|
|
from pathlib import Path
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument('--steam', type=Path, default=Path.home()/'.local/share/Steam')
|
|
p.add_argument('--compat', type=Path, required=True, help='Disposable cloned Proton compatdata directory outside Steam')
|
|
p.add_argument('--profile', type=Path, required=True, help='Separate disposable successor Workbench profile')
|
|
p.add_argument('--smoke', choices=['victory', 'lost-helicopter', 'player-death', 'radio-network', 'hq-response', 'mortar-inventory'], default='victory')
|
|
p.add_argument('--rounds', type=int, default=2)
|
|
p.add_argument('--timeout', type=int, default=360)
|
|
a = p.parse_args()
|
|
steam, compat, profile = a.steam.resolve(), a.compat.resolve(), a.profile.resolve()
|
|
if not 1 <= a.rounds <= 10 or not 60 <= a.timeout <= 1800:
|
|
p.error('Use 1..10 rounds and a 60..1800 second timeout')
|
|
if compat.is_relative_to(steam) or not (compat/'pfx/system.reg').is_file():
|
|
p.error('--compat must be a disposable cloned prefix outside the Steam installation')
|
|
prefix_lock = (compat/'.erri-workbench.lock').open('a')
|
|
try: fcntl.flock(prefix_lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
except BlockingIOError: p.error('Another test runner owns this disposable Wine prefix')
|
|
if profile.is_relative_to(ROOT) or profile.is_relative_to(steam):
|
|
p.error('--profile must be outside source and Steam directories')
|
|
if not shutil.which('gamescope') or not shutil.which('pactl'): p.error('gamescope and pactl are required')
|
|
wb = steam/'steamapps/common/Arma Reforger Tools/Workbench'
|
|
proton = steam/'steamapps/common/Proton - Experimental/proton'
|
|
vanilla = steam/'steamapps/common/Arma Reforger/addons/data'
|
|
if not proton.is_file() or not (wb/'ArmaReforgerWorkbenchSteamDiag.exe').is_file():
|
|
p.error('Stable Reforger Tools and Proton Experimental must already be installed')
|
|
profile.mkdir(parents=True, exist_ok=True)
|
|
addons = profile/'addons'
|
|
addons.mkdir(exist_ok=True)
|
|
link = addons/'ArmaReforger'
|
|
if not link.exists(): link.symlink_to(vanilla, target_is_directory=True)
|
|
if link.resolve() != vanilla.resolve() or any(x.name not in {'ArmaReforger', 'saves'} for x in addons.iterdir()) or any((addons/'saves').rglob('*.gproj')):
|
|
p.error('Test profile addons must contain only the intended vanilla link')
|
|
profile_arg = 'Z:'+str(profile)
|
|
# Refuse concurrency instead of terminating anybody else's editor.
|
|
for proc in Path('/proc').iterdir():
|
|
if not proc.name.isdigit(): continue
|
|
try:
|
|
args = (proc/'cmdline').read_bytes().split(b'\0')
|
|
if profile_arg.encode() in args and any(x.endswith(b'ArmaReforgerWorkbenchSteamDiag.exe') for x in args):
|
|
p.error('Another Workbench already uses this test profile')
|
|
except (FileNotFoundError, PermissionError, ProcessLookupError): pass
|
|
command = ['gamescope', '--backend', 'headless', '-W', '1280', '-H', '720', '-w', '1280', '-h', '720', '-r', '30', '--', str(proton), 'run', str(wb/'ArmaReforgerWorkbenchSteamDiag.exe'), '-gproj', 'Z:'+str(ROOT/'addons/chat/addon.gproj'), '-addonsDir', 'Z:'+str(ROOT/'addons'), '-profile', profile_arg, '-logconsole', '-wbModule=WorldEditor', '-load', 'Worlds/ERR_Everon.ent', '-plugin=ERR_ValidateWorldPlugin', '-errPlay', '1', '-errSmoke', '1', '-errSmokeOutcome', a.smoke, '-noThrow']
|
|
env = os.environ.copy()
|
|
env.update(STEAM_COMPAT_DATA_PATH=str(compat), STEAM_COMPAT_CLIENT_INSTALL_PATH=str(steam), WINEPREFIX=str(compat/'pfx'))
|
|
# A test-only sink prevents startup sounds; a stream check also catches a saved
|
|
# explicit Wine device selection. Never change the user's default sink.
|
|
silent_sink = 'erri_tests_silent'
|
|
sinks = json.loads(subprocess.check_output(['pactl', '-f', 'json', 'list', 'short', 'sinks'], text=True))
|
|
if not any(sink['name'] == silent_sink for sink in sinks):
|
|
subprocess.run(['pactl', 'load-module', 'module-null-sink', 'sink_name='+silent_sink, 'sink_properties=device.description=ERRI_Silent_Tests'], check=True, stdout=subprocess.DEVNULL)
|
|
subprocess.run(['pactl', 'set-sink-mute', silent_sink, '1'], check=True)
|
|
subprocess.run(['pactl', 'set-sink-volume', silent_sink, '0%'], check=True)
|
|
sinks = json.loads(subprocess.check_output(['pactl', '-f', 'json', 'list', 'short', 'sinks'], text=True))
|
|
silent_index = next(sink['index'] for sink in sinks if sink['name'] == silent_sink)
|
|
env.update(PULSE_SINK=silent_sink, PULSE_PROP='application.id=org.escapistsrereintroduced.tests')
|
|
muted_streams = set()
|
|
def silence_test_streams():
|
|
streams = json.loads(subprocess.check_output(['pactl', '-f', 'json', 'list', 'sink-inputs'], text=True))
|
|
for stream in streams:
|
|
props = stream.get('properties', {})
|
|
owned = props.get('application.id') == 'org.escapistsrereintroduced.tests'
|
|
pid = props.get('application.process.id', '')
|
|
if not owned and pid.isdigit():
|
|
try:
|
|
owned = ('WINEPREFIX='+str(compat/'pfx')).encode() in (Path('/proc')/pid/'environ').read_bytes().split(b'\0')
|
|
except (FileNotFoundError, PermissionError, ProcessLookupError): pass
|
|
if not owned: continue
|
|
index = str(stream['index'])
|
|
if stream.get('sink') != silent_index:
|
|
subprocess.run(['pactl', 'move-sink-input', index, silent_sink], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
if not stream.get('mute'):
|
|
subprocess.run(['pactl', 'set-sink-input-mute', index, '1'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
if index not in muted_streams:
|
|
muted_streams.add(index)
|
|
print('Test audio routed to silent output and muted (stream '+index+')', flush=True)
|
|
started = time.time()
|
|
log_root = profile/'logs'
|
|
previous = set(log_root.glob('logs_*')) if log_root.exists() else set()
|
|
result, session, complete_at = 'timeout', None, None
|
|
with (profile/'headless-launch.log').open('w') as output:
|
|
child = subprocess.Popen(command, cwd=wb, env=env, stdout=output, stderr=subprocess.STDOUT, start_new_session=True)
|
|
try:
|
|
while time.time()-started < a.timeout:
|
|
silence_test_streams()
|
|
candidates = [x for x in log_root.glob('logs_*') if x not in previous]
|
|
if candidates:
|
|
session = max(candidates, key=lambda x:x.stat().st_mtime)
|
|
console = session/'console.log'
|
|
content = console.read_text(errors='replace') if console.exists() else ''
|
|
if 'ERR SMOKE FAIL:' in content or 'Virtual Machine Exception' in content or 'Can\'t compile "Game"' in content:
|
|
result = 'test-failed'; break
|
|
if content.count('ERR SMOKE COMPLETE:') >= a.rounds:
|
|
if complete_at is None: complete_at = time.time()
|
|
if time.time()-complete_at >= 6: result = 'smoke-completed'; break
|
|
if child.poll() is not None: result = 'process-exited'; break
|
|
time.sleep(1)
|
|
finally:
|
|
# Stop Wine in this disposable prefix only. Proton's reaper may create a
|
|
# separate process group, so killing Gamescope alone can leave Wine alive.
|
|
wine_server = proton.parent/'files/bin/wineserver'
|
|
subprocess.run([str(wine_server), '-k'], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=10)
|
|
# Only this fresh process group belongs to the runner and disposable prefix.
|
|
try: os.killpg(child.pid, signal.SIGTERM)
|
|
except ProcessLookupError: pass
|
|
try: child.wait(timeout=10)
|
|
except subprocess.TimeoutExpired:
|
|
os.killpg(child.pid, signal.SIGKILL); child.wait()
|
|
# Gamescope's reaper can outlive its parent and ignores SIGTERM.
|
|
try: os.killpg(child.pid, signal.SIGKILL)
|
|
except ProcessLookupError: pass
|
|
summary = {'result':result, 'session':str(session) if session else None, 'rounds_required':a.rounds, 'display':'headless Gamescope 1280x720/30Hz', 'dedicated_server':False, 'audio':'silent sink and per-test-stream mute'}
|
|
if session:
|
|
(session/'runner.json').write_text(json.dumps(summary, indent=2)+'\n')
|
|
shutil.copyfile(profile/'headless-launch.log', session/'headless-launch.log')
|
|
print(json.dumps(summary, indent=2), flush=True)
|
|
if result != 'smoke-completed': raise SystemExit(1)
|
|
# All native errors still fail the report even when the scripted sequence completed.
|
|
raise SystemExit(subprocess.call(['python3', str(ROOT/'tools/native_report.py'), str(session/'console.log'), '--require-completions', str(a.rounds)]))
|