Files
leleka/scripts/smoke-runtime-image.py
T

89 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Actual combined CPU image proof. Never use with a model/CUDA image."""
import importlib.util
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import time
import uuid
from docker_smoke_http import ISOLATION, PROBE_IMAGE, docker, prepare_probe, request as probe_request
def main(receipt):
proof = json.loads(Path(receipt).read_text())
assert proof['kind'] == 'cpu-fixture' and proof['gpu_tested'] is False
image = proof['image']
label = docker('inspect', '--format', '{{index .Config.Labels "io.strata.package-kind"}}', image).stdout.strip()
assert label == 'cpu-fixture'
spec = importlib.util.spec_from_file_location('helper', Path(__file__).with_name('smoke-worker-cancel.py'))
helper = importlib.util.module_from_spec(spec); spec.loader.exec_module(helper)
name = 'strata-runtime-smoke-' + uuid.uuid4().hex
volume = name + '-fixture'
staging = name + '-stage'
prepare_probe()
with tempfile.TemporaryDirectory(prefix='strata-container-fixture-') as tmp:
root = Path(tmp); root.chmod(0o755)
helper.tokenizer(root)
(root/'config.json').write_text(json.dumps({'bind': '127.0.0.1:8081', 'model': 'embedded-fixture',
'engine_probe': True, 'engine': {'executable': '@embedded', 'args': ['/tmp/worker.pid'],
'tokenizer_dir': '/fixture', 'startup_timeout_s': 5, 'max_restarts': 1}}))
for path in root.iterdir(): path.chmod(0o644)
try:
docker('volume', 'create', volume)
docker('run', '-d', '--name', staging, *ISOLATION, '--network', 'none',
'--mount', f'type=volume,src={volume},dst=/fixture',
'--entrypoint', 'python3', PROBE_IMAGE, '-c', 'import time; time.sleep(120)')
# Stream files through the Docker API; runner paths need not exist
# on the daemon host. The serving container gets a read-only volume.
docker('cp', str(root) + '/.', staging + ':/fixture')
docker('rm', '-f', staging)
docker('run', '-d', '--name', name, '--platform', 'linux/amd64', '--read-only',
'--cap-drop', 'ALL', '--security-opt', 'no-new-privileges', '--user', '65532:65532',
'--tmpfs', '/tmp:rw,nosuid,nodev,size=64m', '-e', 'CUDA_VISIBLE_DEVICES=',
'-e', 'NVIDIA_VISIBLE_DEVICES=void', '--network', 'none',
'--mount', f'type=volume,src={volume},dst=/fixture,readonly', image,
'serve', '--config', '/fixture/config.json')
def request(route, body=None):
status, result = probe_request(name, route, body)
assert status == 200, (status, result)
return result
def ready():
deadline = time.monotonic()+15
while time.monotonic()<deadline:
try:
if request('/status')['ready']: return
except (OSError, subprocess.CalledProcessError): pass
time.sleep(.1)
raise AssertionError('container worker did not become ready')
def generate():
result = request('/v1/responses', {'model':'embedded-fixture','input':'Reply A',
'max_output_tokens':1,'reasoning':{'effort':'none'}})
assert result['status']=='completed'
assert result['output'][0]['content'][0]['text']=='A'
ready(); generate()
before = docker('exec', name, 'cat', '/tmp/worker.pid').stdout.strip()
assert before.isascii() and before.isdigit()
docker('exec', name, '/bin/sh', '-c', 'kill -KILL "$1"', 'strata-smoke', before)
deadline = time.monotonic()+15
while docker('exec', name, 'cat', '/tmp/worker.pid').stdout.strip()==before:
assert time.monotonic()<deadline
time.sleep(.1)
ready(); generate()
docker('stop', '--time', '10', name)
state = json.loads(docker('inspect','--format','{{json .State}}',name).stdout)
assert state['ExitCode']==0 and not state['OOMKilled']
print('Combined CPU OCI: Responses A exact, worker crash/restart A exact, nonroot/read-only, graceful stop. No GPU/model qualification.')
except Exception:
logs = docker('logs', name, check=False)
print('Owned CPU fixture container logs:', logs.stdout, logs.stderr, file=sys.stderr)
raise
finally:
docker('rm','-f',name,check=False)
docker('rm','-f',staging,check=False)
docker('volume','rm',volume,check=False)
if __name__=='__main__': main(sys.argv[1])