summaryrefslogtreecommitdiff
path: root/test/web/host_fs.py
blob: ea39cc937a81986099816d8d510f576ea56942ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/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('<IB', 2, 0))
            data = finite.rpc(116, struct.pack('<IQI', 2, 0, 4096))[4:]
            assert b'mount-control' in data
            assert finite.rpc(116, struct.pack('<IQI', 2, len(data), 4096))[4:] == b''
            finite.s.close()
            assert http(p, mounted+'/scratch/upload', 'DELETE')[0] == 204
            # A blocking read through FUSE must not stall the shared backend.
            events = Nine(str(root/'cat.sock'))
            events.walk('mnt/9p/events/sites/fixture/events')
            events.rpc(112, struct.pack('<IB', 2, 0))
            assert json.loads(events.rpc(116, struct.pack('<IQI', 2, 0, 4096))[4:])['phase'] == 'idle'
            request = struct.pack('<IQI', 2, 0, 4096)
            events.s.sendall(struct.pack('<IBH', len(request)+7, 116, 2) + request)
            events.rpc(124, struct.pack('<I', 1), 3)
            # Also wake/retry this parked request from an unrelated catalogue change.
            assert http(p, '/fs/_9web/sites/fixture/ctl', 'PUT', 'build')[0] == 200
            assert http(p, '/fs/mnt/9p/events/sites/fixture/ctl', 'PUT', 'build')[0] == 200
            size, typ, tag = struct.unpack('<IBH', events.recv(7))
            assert (typ, tag) == (117, 2)
            assert json.loads(events.recv(size-7)[4:])['revision'] > 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('<IB', 2, 0))
                cancel.rpc(116, struct.pack('<IQI', 2, 0, 4096))
                cancel.s.sendall(struct.pack('<IBH', len(request)+7, 116, 2) + request)
                cancel.s.sendall(struct.pack('<IBHH', 9, 108, 3, 2))
                # An interrupted old read may be answered before Rflush.
                while True:
                    n, kind, tag = struct.unpack('<IBH', cancel.recv(7))
                    cancel.recv(n-7)
                    if (kind, tag) == (109, 3):
                        break
                    assert tag == 2 and kind in (107, 117), (kind, tag)
                cancel.rpc(120, struct.pack('<I', 2), 4)
            cancel.s.close()
            # Directory and upload/edit/append/delete use the same HTTP/9P path.
            target = '/fs'+tmp+'/created'
            assert http(p, target+'/', 'PUT')[0] == 201
            assert http(p, target+'/file', 'PUT', 'abcdef')[0] == 201
            assert http(p, target+'/file', 'PUT', 'xy')[0] == 200
            assert (root/'created/file').read_bytes() == b'xy'
            assert http(p, target+'/file?append=1', 'PUT', 'z')[0] == 200
            assert http(p, target+'/file')[2] == b'xyz'
            assert http(p, target+'/file', headers={'Range': 'bytes=1-2'})[2] == b'yz'
            assert http(p, target+'/file', 'HEAD')[1]['content-length'] == '3'
            # The local namespace is also accessible through raw 9P.
            nine = Nine(str(root/'cat.sock'))
            rel = tmp.lstrip('/')
            assert nine.read(rel+'/source') == b'original data'
            nine.write(rel+'/created/file', 'short')
            assert (root/'created/file').read_text() == 'short'
            # Twstat rename on the backing filesystem (not a UI copy/delete).
            nine.walk(rel+'/created/file')
            stat = struct.pack('<HI', 0xffff, 0xffffffff) + b'\xff'*13 + struct.pack('<IIIQ', 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffffffffffff) + string('renamed') + string('')*3
            stat = struct.pack('<H', len(stat)) + stat
            nine.rpc(126, struct.pack('<IH', 2, len(stat)) + stat)
            nine.rpc(120, struct.pack('<I', 2))
            assert (root/'created/renamed').read_text() == 'short'
            # Virtual tree stays read-only apart from ctl.
            assert http(p, '/fs/_9web/new', 'PUT', 'bad')[0] == 403
            assert http(p, '/fs/_9web/sites/fixture/public/index.html', 'PUT', 'bad')[0] == 403
            assert http(p, '/fs/_9web/sites/fixture/ctl', 'PUT', 'build')[0] == 200
            eventually(lambda: b'host-build' in http(p, '/fs/_9web/sites/fixture/log')[2])
            assert http(config['sites'][0]['port'])[2] == b'site still works'
            assert http(p, '/_cloud9/catalog.json')[0] == 200
            assert json.loads(http(p, '/_cloud9/catalog.json')[2])['namespace'] == '/mnt/9p/9web'
            assert http(p, '/fs/mnt/9p/9web/sites/fixture/public/index.html')[2] == b'site still works'
            assert http(p, '/sites/fixture/')[2] == b'site still works'
            pure = Nine(str(registry/'9web'))
            assert pure.list('sites') == ['fixture']
            assert pure.read('sites/fixture/public/index.html') == b'site still works'
            pure.s.close()
            assert http(p, target+'/renamed', 'DELETE')[0] == 204
            assert http(p, target, 'DELETE')[0] == 204
            nine.s.close()
            print('host_fs: root, symlinks, special-file refusal, /mnt/9p, upload/edit/append/rename/delete, raw 9P and catalogue controls passed')
        finally:
            proc.terminate()
            try:
                output = proc.communicate(timeout=5)[0]
            except subprocess.TimeoutExpired:
                proc.kill()
                output = proc.communicate()[0]
            if sys.exc_info()[0]:
                print(output.decode(errors='replace'), file=sys.stderr)
            demo.terminate()
            demo.wait(timeout=5)
            event_server.terminate()
            event_server.wait(timeout=5)


if __name__ == '__main__':
    main()