#!/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 PROBE_SCRIPT = r'''#!/bin/bash set -euo pipefail request() { local method="$1" path="$2" body="${3-}" request status line response if ! { exec 3<>/dev/tcp/127.0.0.1/8081; } 2>/dev/null; then return 1; fi if [[ -n "$body" ]]; then printf -v request '%s %s HTTP/1.0\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: %s\r\n\r\n%s' \ "$method" "$path" "${#body}" "$body" else printf -v request '%s %s HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n' "$method" "$path" fi printf '%s' "$request" >&3 IFS= read -r status <&3 || { exec 3<&-; exec 3>&-; return 1; } status="${status%$'\r'}" while IFS= read -r line <&3; do line="${line%$'\r'}" [[ -n "$line" ]] || break done response="$(cat <&3)" exec 3<&- exec 3>&- [[ "${status:9:3}" == 200 ]] || { echo "HTTP request failed: $status $response" >&2; return 1; } printf '%s' "$response" } deadline=$((SECONDS + 15)) until status="$(request GET /status)"; do if (( SECONDS >= deadline )); then echo 'worker did not become ready within 15s' >&2; exit 1; fi sleep 0.1 done [[ "$status" == *'"ready":true'* ]] || { echo "unexpected /status response: $status" >&2; exit 1; } result="$(request POST /v1/responses '{"model":"embedded-fixture","input":"Reply A","max_output_tokens":1,"reasoning":{"effort":"none"}}')" [[ "$result" == *'"status":"completed"'* && "$result" == *'"text":"A"'* ]] || { echo "unexpected Responses result: $result" >&2 exit 1 } ''' def docker(*args, check=True): try: return subprocess.run(['docker', *map(str, args)], check=check, capture_output=True, text=True, timeout=30) except subprocess.CalledProcessError as error: detail = (error.stderr or '').strip()[-2048:] raise RuntimeError(f'docker command failed ({error.returncode}): {detail}') from None 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 fixture_container = 'strata-runtime-fixture-' + uuid.uuid4().hex fixture_volume = 'strata-runtime-fixture-' + uuid.uuid4().hex base = proof['base'] 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': '0.0.0.0:8081', 'model': 'embedded-fixture', 'engine_probe': True, 'engine': {'executable': '@embedded', 'args': ['/tmp/worker.pid'], 'tokenizer_dir': '/fixture', 'startup_timeout_s': 5, 'max_restarts': 1}})) (root/'probe.sh').write_text(PROBE_SCRIPT) for path in root.iterdir(): path.chmod(0o644) (root/'probe.sh').chmod(0o755) try: # The Docker daemon may be outside the job container. A job-local # /tmp bind source is therefore not guaranteed to exist on it. # `docker cp` streams this synthetic fixture through the CLI into # a named volume; the tested image then mounts that volume read-only. docker('volume', 'create', fixture_volume) docker('create', '--name', fixture_container, '--platform', 'linux/amd64', '--mount', f'type=volume,src={fixture_volume},dst=/fixture', '--entrypoint', '/bin/sleep', base, '30') docker('cp', f'{root}/.', f'{fixture_container}:/fixture/') docker('rm', fixture_container) 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', '--mount', f'type=volume,src={fixture_volume},dst=/fixture,readonly', image, 'serve', '--config', '/fixture/config.json') def probe(): # The Gitea job container and Docker daemon do not share the # daemon host's loopback. A short-lived client joins the # server's network namespace, so probes remain loopback-only # and don't publish a runtime port from the CI host. docker('run', '--rm', '--platform', 'linux/amd64', '--network', f'container:{name}', '--read-only', '--cap-drop', 'ALL', '--security-opt', 'no-new-privileges', '--user', '65532:65532', '-e', 'CUDA_VISIBLE_DEVICES=', '-e', 'NVIDIA_VISIBLE_DEVICES=void', '--mount', f'type=volume,src={fixture_volume},dst=/fixture,readonly', '--entrypoint', '/bin/bash', base, '/fixture/probe.sh') probe() 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()