#!/usr/bin/env python3 """Exercise host export; mutations are confined to our temporary directory.""" from catalog import Nine, eventually, http, port, string import json import os from pathlib import Path import socket import struct import subprocess import sys import tempfile def main(): binary = str(Path(sys.argv[1]).resolve()) ns = Path('zig-out/bin/9ns').resolve() with tempfile.TemporaryDirectory(prefix='9web-host-') as tmp: root = Path(tmp) p = port() public = root/'public' public.mkdir() (public/'index.html').write_text('site still works') (root/'source').write_text('original data') (root/'alias').symlink_to(root, target_is_directory=True) (root/'dangling').symlink_to(root/'missing') os.mkfifo(root/'fifo') large = root/'large' large.mkdir() for i in range(1500): (large/f'entry-{i:04}').touch() config = {'filesystem': '/', 'sites': [{'name': 'fixture', 'title': 'Fixture', 'root': str(public), 'cwd': tmp, 'port': port(), 'build': ['sh', '-c', 'echo host-build']}]} (root/'config.json').write_text(json.dumps(config)) runtime = root/'runtime' registry = runtime/'9p' registry.mkdir(parents=True) demo = subprocess.Popen([str(Path('zig-out/bin/9proc-demo').resolve()), '--unix', str(registry/'fixture')], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) eventually(lambda: (registry/'fixture').exists()) event_config = {'sites': [{**config['sites'][0], 'port': port()}]} (root/'events.json').write_text(json.dumps(event_config)) event_server = subprocess.Popen([binary, '--listen', f'127.0.0.1:{port()}', '--catalog', str(root/'events.json'), '--catalog-socket', str(registry/'events'), '--state-dir', str(root/'events-state')], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) eventually(lambda: (registry/'events').exists()) args = [binary, '--listen', f'127.0.0.1:{p}', '--catalog', str(root/'config.json'), '--catalog-socket', str(root/'cat.sock'), '--state-dir', str(root/'state')] # Match the deployed wrapper. No host mount is changed. if not ns.exists(): raise RuntimeError('build 9ns first: zig build 9ns') proc = subprocess.Popen([str(ns), '--mntgen', '--', *args], env={**os.environ, 'XDG_RUNTIME_DIR': str(runtime)}, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) try: eventually(lambda: http(p)[0] == 200) entries = json.loads(http(p, '/fs/')[2]) assert {'home', 'etc', 'usr', 'mnt', '_9web'} <= {x['name'] for x in entries}, entries assert http(p, '/fs/etc/hostname')[2] == Path('/etc/hostname').read_bytes() assert http(p, '/fs'+tmp+'/alias/source')[2] == b'original data' assert 'dangling' in {x['name'] for x in json.loads(http(p, '/fs'+tmp)[2])} assert http(p, '/fs'+tmp+'/fifo')[0] >= 400 listing = http(p, '/fs'+tmp+'/large') assert listing[0] == 200 and len(json.loads(listing[2])) == 1500 assert http(p, '/fs/dev/null')[0] >= 400 assert http(p, '/fs/mnt/9p')[0] == 200 assert 'fixture' in {x['name'] for x in json.loads(http(p, '/fs/mnt/9p')[2])} assert http(p, '/fs/mnt/9p/fixture/build/zig_version')[0] == 200 assert b'0.17' in http(p, '/fs/mnt/9p/fixture/build/zig_version')[2] mounted = '/fs/mnt/9p/fixture' assert http(p, mounted+'/scratch/upload', 'PUT', 'via FUSE')[0] == 201 assert http(p, mounted+'/scratch/upload')[2] == b'via FUSE' control = http(p, mounted+'/runtime/ctl', 'PUT', 'echo mount-control') assert control[0] == 200, control assert b'mount-control' in http(p, mounted+'/runtime/ctl')[2] finite = Nine(str(root/'cat.sock')) finite.walk('mnt/9p/fixture/runtime/ctl') finite.rpc(112, struct.pack(' 1 events.s.close() eventually(lambda: json.loads(http(p, '/fs/mnt/9p/events/sites/fixture/status')[2])['generation'] == 1) # Repeated flush+clunk must release the underlying blocking reads, # rather than exhaust the bounded task table or steal later events. cancel = Nine(str(root/'cat.sock')) for _ in range(40): cancel.walk('mnt/9p/events/sites/fixture/events') cancel.rpc(112, struct.pack('