~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zttp

mirror of https://tangled.org/zat.dev/zttp

0
0
main
81 lines · 5.6 KB Raw History
"""Run the HTTP qualification gate with no external network or host credentials."""
import argparse
import hashlib
from pathlib import Path
import subprocess
import tempfile

ZIO_HASH = 'zio-0.19.0-xHbVVDjiLgBKwVXGZoPcq9hhFA5nnpb9w6VM9h_cVr_T'
ZIO_DEPLOYED_HASH = 'zio-0.19.0-xHbVVMFBLACx1T0028HttNTseV6bDeL0AI4id51AHx57'


def snapshot(root, destination, zio_hash=ZIO_HASH):
    listed = subprocess.check_output(['git', 'ls-files', '-z', '--cached', '--others', '--exclude-standard'], cwd=root)
    digest = hashlib.sha256()
    for name in sorted(set(listed.decode().split('\0')) - {''}):
        relative = Path(name)
        if relative.is_absolute() or '..' in relative.parts:
            raise ValueError('invalid source path')
        if not (name.startswith(('src/', 'examples/', 'tests/conformance/')) or name in ('build.zig', 'build.zig.zon', 'scripts/test_tls_clock.py')):
            continue
        if relative.suffix not in ('.zig', '.zon', '.json', '.py', '.der') and relative.name != 'HTTNOPE-LICENSE':
            continue
        source = root / relative
        if source.is_symlink() or not source.is_file():
            raise ValueError(f'source must be a regular file: {name}')
        payload = source.read_bytes()
        if name == 'tests/conformance/build.zig.zon' and zio_hash == ZIO_DEPLOYED_HASH:
            payload = payload.replace(b'git+https://tangled.org/zzstoatzz.io/zio-lalinsky#7d3a2e398bcca44afdc2e1fabd714b5922a230c0', b'https://github.com/lalinsky/zio/archive/3bbd74d68a889e5d4da7274afdc7c7617ec614d6.tar.gz').replace(ZIO_HASH.encode(), zio_hash.encode())
        target = destination / relative
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_bytes(payload)
        digest.update(name.encode() + b'\0' + payload + b'\0')
    (destination / 'tests/conformance/zig-pkg').symlink_to('/opt/zig-pkg')
    return digest.hexdigest()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--image', default='zttp-conformance-tools:0.17.0')
    parser.add_argument('--zio-source', type=Path)
    parser.add_argument('--optimize', choices=['Debug', 'ReleaseSafe'], default='Debug')
    parser.add_argument('--suite', choices=['full', 'shutdown'], default='full')
    args = parser.parse_args()
    zio_hash = args.zio_source.name if args.zio_source else ZIO_HASH
    if zio_hash not in (ZIO_HASH, ZIO_DEPLOYED_HASH):
        raise ValueError('expected a qualified zio package directory')
    root = Path(__file__).resolve().parents[1]
    image = subprocess.check_output(['docker', 'image', 'inspect', args.image, '--format', '{{.Id}}'], text=True).strip()
    with tempfile.TemporaryDirectory(prefix='zttp-conformance-') as temporary:
        source = Path(temporary) / 'source'
        source.mkdir()
        fingerprint = snapshot(root, source, zio_hash)
        command = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--cap-drop', 'ALL', '--security-opt', 'no-new-privileges', '--memory', '4g', '--cpus', '2', '--cpuset-cpus', '0-1', '--pids-limit', '256', '--ulimit', 'fsize=1073741824:1073741824', '--ulimit', 'nofile=2048:2048', '--tmpfs', '/scratch:rw,exec,size=4g,mode=1777', '--mount', f'type=bind,src={source},dst=/source,readonly', '--workdir', '/scratch']
        if args.zio_source:
            dependency = args.zio_source.resolve(strict=True)
            if dependency.name != zio_hash or not (dependency / 'src/zio.zig').is_file():
                raise ValueError('expected the pinned zio package directory')
            command += ['--mount', f'type=bind,src={dependency},dst=/opt/zig-pkg/{zio_hash},readonly']
        command += [image, 'env', '-i', 'PATH=/opt/zig:/usr/local/bin:/usr/bin:/bin', 'HOME=/scratch/home', 'TMPDIR=/scratch/tmp', 'ZIG_GLOBAL_CACHE_DIR=/scratch/global', 'UV_CACHE_DIR=/scratch/uv', 'UV_PYTHON_DOWNLOADS=never', 'UV_PYTHON_PREFERENCE=only-system', '/bin/sh', '-ec']
        optimize = args.optimize
        script = f'''mkdir -p /scratch/home /scratch/tmp
timeout -k 5 60 zig fmt --check /source/build.zig /source/build.zig.zon /source/src /source/tests/conformance /source/examples
timeout -k 5 600 zig build --build-file /source/build.zig test --cache-dir /scratch/local -Doptimize={optimize} --summary all
timeout -k 5 180 uv run --no-project --offline --python python3 /source/scripts/test_tls_clock.py
timeout -k 5 600 zig build --build-file /source/tests/conformance/build.zig test --cache-dir /scratch/contract -Doptimize={optimize} --summary all
timeout -k 5 600 zig build --build-file /source/tests/conformance/build.zig --cache-dir /scratch/contract --prefix /scratch/probe -Doptimize={optimize}
timeout -k 5 240 uv run --no-project --offline --python python3 /source/tests/conformance/run.py --probe /scratch/probe/bin/zttp-probe --output /scratch/report.json
timeout -k 5 180 uv run --no-project --offline --python python3 /source/tests/conformance/shutdown_wire.py --protocol http --probe /scratch/probe/bin/zttp-shutdown-probe
'''
        if args.suite == 'shutdown':
            script = f'''mkdir -p /scratch/home /scratch/tmp
timeout -k 5 60 zig fmt --check /source/build.zig /source/build.zig.zon /source/src /source/tests/conformance
timeout -k 5 600 zig build --build-file /source/tests/conformance/build.zig --cache-dir /scratch/contract --prefix /scratch/probe -Doptimize={optimize}
timeout -k 5 180 uv run --no-project --offline --python python3 /source/tests/conformance/shutdown_wire.py --protocol http --probe /scratch/probe/bin/zttp-shutdown-probe
'''
        print(f'source_sha256={fingerprint} tool_image={image} optimize={optimize} suite={args.suite} zio_package={zio_hash}', flush=True)
        subprocess.run(command + [script], check=True, timeout=2600)


if __name__ == '__main__':
    main()