Release v117 with persistent logs and WAN tunnel recovery
This commit is contained in:
1 parent
5f65a3d811
commit
b62db40daf
61 files changed
+3080
-438
No files matched your search
+12
-90
@@ -1,4 +1,4 @@
|
||||
"""Exercise the real RAM journal and authorized router CGI with temporary files."""
|
||||
"""Exercise the bounded persistent journal and authorized router CGI with temporary files."""
|
||||
import concurrent.futures
|
||||
import base64
|
||||
import json
|
||||
@@ -22,12 +22,14 @@ class GlobalLogTests(unittest.TestCase):
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
self.log_dir = self.root / 'journal'
|
||||
self.run_dir = self.root / 'runtime'
|
||||
self.helper = self.root / 'logs.sh'
|
||||
source = (ROOT / 'files/usr/lib/owrt-remote/logs.sh').read_text(encoding='utf-8')
|
||||
source = source.replace('log_clock() { cut -d. -f1 /proc/uptime; }',
|
||||
'log_clock() { echo "${TEST_UPTIME:-1000}"; }')
|
||||
self.helper_source = source
|
||||
source = source.replace('log_ram_storage() {', 'log_ram_storage() { return 0;', 1)
|
||||
source = source.replace('log_flash_storage() {', 'log_flash_storage() { return 0;', 1)
|
||||
self.helper.write_text(source, encoding='utf-8', newline='\n')
|
||||
self.cgi = self.root / 'cgi'
|
||||
self.cgi.write_text((ROOT / 'files/www/cgi-bin/owrt-remote').read_text(encoding='utf-8'),
|
||||
@@ -35,12 +37,15 @@ class GlobalLogTests(unittest.TestCase):
|
||||
self.key = self.root / 'key'
|
||||
self.key.write_text('test-key', encoding='utf-8')
|
||||
self.env = {**os.environ, 'OWRT_REMOTE_LOG_DIR': self.log_dir.as_posix(),
|
||||
'OWRT_REMOTE_LOG_RUNTIME': self.run_dir.as_posix(),
|
||||
'OWRT_REMOTE_LOG_HELPER': self.helper.as_posix(), 'TMPDIR': self.root.as_posix(),
|
||||
'KEY_FILE': self.key.as_posix(), 'TEST_UPTIME': '1000'}
|
||||
if os.name == 'nt':
|
||||
self.env['PATH'] = str(GIT_BIN) + os.pathsep + self.env['PATH']
|
||||
|
||||
def helper_call(self, command, text='', *args, env=None):
|
||||
if command == 'export':
|
||||
self.helper_call('flush', '', 'force', env=env)
|
||||
result = subprocess.run([SH, self.helper.as_posix(), command, *args], input=text,
|
||||
encoding='utf-8', capture_output=True, env=env or self.env, timeout=20)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
@@ -61,8 +66,9 @@ class GlobalLogTests(unittest.TestCase):
|
||||
self.assertIn('Xray connected', downloaded)
|
||||
self.assertIn('Cache-Control: no-store', downloaded)
|
||||
# The bounded read is independent of append filtering; supply an existing large journal.
|
||||
with (self.log_dir / 'current.log').open('a', encoding='utf-8') as journal:
|
||||
journal.write(''.join(f'{i}: ' + 'y' * 1024 + '\n' for i in range(70)))
|
||||
state = (self.log_dir / 'state').read_text().split()
|
||||
for i in range(5):
|
||||
(self.log_dir / f'part.{state[0]}.{i:018d}.{i+1:018d}.1').write_text('y' * 15000 + '\n')
|
||||
live = self.cgi_call('logs_live').split('\n\n', 1)[1]
|
||||
# Git for Windows can translate CRLF inside native pipes.
|
||||
self.assertLessEqual(len(live.encode()), 65536)
|
||||
@@ -141,32 +147,6 @@ class GlobalLogTests(unittest.TestCase):
|
||||
lines = self.helper_call('read').splitlines()
|
||||
self.assertEqual([line.split(']', 1)[0] for line in lines], ['[ERROR', '[ERROR', '[ERROR', '[WARN'])
|
||||
|
||||
def test_persistent_storage_is_rejected_before_any_file_is_written(self):
|
||||
mounts = self.root / 'mounts'
|
||||
mounts.write_text('/dev/root / overlay rw 0 0\n', encoding='utf-8')
|
||||
source = self.helper_source.replace('/proc/mounts', mounts.as_posix())
|
||||
self.helper.write_text(source, encoding='utf-8', newline='\n')
|
||||
env = self.env
|
||||
result = subprocess.run([SH, self.helper.as_posix(), 'append'], input='must not be written\n',
|
||||
encoding='utf-8', capture_output=True, env=env, timeout=20)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertIn('refusing persistent storage', result.stderr)
|
||||
self.assertFalse(self.log_dir.exists())
|
||||
self.env = env
|
||||
self.assertIn('503 Service Unavailable', self.cgi_call('logs_live'))
|
||||
self.assertFalse(self.log_dir.exists())
|
||||
# The same location is permitted only when its containing mount is tmpfs.
|
||||
mounts.write_text('/dev/root / overlay rw 0 0\ntmpfs ' + self.root.as_posix() + ' tmpfs rw 0 0\n', encoding='utf-8')
|
||||
self.helper_call('append', 'RAM fact\n')
|
||||
self.assertEqual(self.helper_call('read'), '[OK] RAM fact\n')
|
||||
mounts.write_text('/dev/root / overlay rw 0 0\ntmpfs ' + self.root.as_posix()
|
||||
+ ' tmpfs rw 0 0\n/dev/flash ' + self.log_dir.as_posix()
|
||||
+ ' overlay rw 0 0\n', encoding='utf-8')
|
||||
result = subprocess.run([SH, self.helper.as_posix(), 'append'], input='must not enter nested Flash mount\n',
|
||||
encoding='utf-8', capture_output=True, env=env, timeout=20)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertEqual((self.log_dir / 'current.log').read_text(), '[OK] RAM fact\n')
|
||||
|
||||
def test_clear_requires_post_and_correct_key(self):
|
||||
self.helper_call('append', 'retain this event\n')
|
||||
self.assertIn('Доступ запрещен', self.cgi_call('logs_download', key='wrong'))
|
||||
@@ -178,13 +158,6 @@ class GlobalLogTests(unittest.TestCase):
|
||||
self.helper_call('append', 'new event\n')
|
||||
self.assertEqual(self.helper_call('read'), '[OK] new event\n')
|
||||
|
||||
def test_retention_uses_uptime_and_clears_on_reboot(self):
|
||||
self.helper_call('append', 'old event\n')
|
||||
self.assertIn('old event', self.helper_call('read', env={**self.env, 'TEST_UPTIME': '87399'}))
|
||||
self.assertEqual(self.helper_call('read', env={**self.env, 'TEST_UPTIME': '87400'}), '')
|
||||
self.helper_call('append', 'after daily clear\n', env={**self.env, 'TEST_UPTIME': '87401'})
|
||||
self.assertEqual(self.helper_call('read', env={**self.env, 'TEST_UPTIME': '20'}), '')
|
||||
|
||||
def test_export_acknowledges_only_received_bytes_and_remote_clear_is_idempotent(self):
|
||||
self.helper_call('append', 'First event\n')
|
||||
first = json.loads(self.helper_call('export'))
|
||||
@@ -207,39 +180,6 @@ class GlobalLogTests(unittest.TestCase):
|
||||
self.helper_call('ack', '', second['journal_id'], str(second['to']), 'clear-123')
|
||||
self.assertIn('After clear', self.helper_call('read'), 'retrying clear must not clear new events')
|
||||
|
||||
def test_export_catches_up_in_bounded_batches_after_rotation(self):
|
||||
self.helper_call('clear')
|
||||
raw = b'[OK] ' + b'x' * 100000 + b'\n'
|
||||
(self.log_dir / 'current.log').write_bytes(raw)
|
||||
first = json.loads(self.helper_call('export'))
|
||||
first_raw = base64.b64decode(first['data'])
|
||||
self.assertLessEqual(len(first_raw), 65536)
|
||||
self.helper_call('ack', '', first['journal_id'], str(first['to']))
|
||||
second = json.loads(self.helper_call('export'))
|
||||
self.assertEqual(first_raw + base64.b64decode(second['data']), raw)
|
||||
# Bytes dropped by local rotation are detected by the server from the gap.
|
||||
(self.log_dir / 'current.log').write_bytes(b'[OK] Latest\n')
|
||||
(self.log_dir / 'total').write_text('200000')
|
||||
late = json.loads(self.helper_call('export'))
|
||||
self.assertEqual(late['from'], 200000 - len(b'[OK] Latest\n'))
|
||||
self.assertEqual(base64.b64decode(late['data']), b'[OK] Latest\n')
|
||||
|
||||
def test_rotation_retains_recent_log_and_clear_removes_both(self):
|
||||
self.helper_call('clear')
|
||||
current = self.log_dir / 'current.log'
|
||||
current.write_text('A' * 1048576, encoding='utf-8')
|
||||
self.helper_call('append', 'second file\n')
|
||||
self.assertTrue((self.log_dir / 'previous.log').exists())
|
||||
self.assertEqual(current.read_text(), '[OK] second file\n')
|
||||
current.write_text('B' * 1048576, encoding='utf-8')
|
||||
self.helper_call('append', 'third file\n')
|
||||
output = self.helper_call('read')
|
||||
self.assertNotIn('A', output)
|
||||
self.assertTrue(output.endswith('third file\n'))
|
||||
self.helper_call('clear')
|
||||
self.assertFalse((self.log_dir / 'previous.log').exists())
|
||||
self.assertEqual(current.read_text(), '')
|
||||
|
||||
def test_secrets_are_redacted_before_storage_and_html_is_literal(self):
|
||||
text = ('Heartbeat OK\nuci set owrtremote.main.hub_token=SECRET_TOKEN\n'
|
||||
'Authorization: Bearer SECRET_AUTH\nhttps://hub.test/?key=SECRET_KEY\n'
|
||||
@@ -256,11 +196,11 @@ class GlobalLogTests(unittest.TestCase):
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
|
||||
list(pool.map(lambda i: self.helper_call('append', f'event-{i}\n'), range(12)))
|
||||
self.assertEqual(set(self.helper_call('read').splitlines()), {f'[OK] event-{i}' for i in range(12)})
|
||||
self.assertFalse((self.log_dir / 'lock').exists())
|
||||
self.assertFalse((self.run_dir / 'lock').exists())
|
||||
|
||||
def test_stale_lock_recovers(self):
|
||||
self.helper_call('clear')
|
||||
lock = self.log_dir / 'lock'
|
||||
lock = self.run_dir / 'lock'
|
||||
lock.mkdir()
|
||||
(lock / 'pid').write_text('99999999')
|
||||
self.helper_call('append', 'recovered\n')
|
||||
@@ -272,28 +212,10 @@ class GlobalLogTests(unittest.TestCase):
|
||||
|
||||
def test_empty_lock_left_by_killed_writer_recovers(self):
|
||||
self.helper_call('clear')
|
||||
(self.log_dir / 'lock').mkdir()
|
||||
(self.run_dir / 'lock').mkdir()
|
||||
self.helper_call('append', 'after empty lock\n')
|
||||
self.assertEqual(self.helper_call('read'), '[OK] after empty lock\n')
|
||||
|
||||
def test_collector_failure_cleans_both_children_and_fifo(self):
|
||||
source = self.helper.read_text(encoding='utf-8')
|
||||
mocks = '''
|
||||
log_init() { return 0; }
|
||||
mkfifo() { return 0; }
|
||||
logread() { return 0; }
|
||||
log_lock() { return 1; }
|
||||
kill() { printf 'KILL=%s\\n' "$*"; }
|
||||
wait() { return 0; }
|
||||
rm() { printf 'REMOVE=%s\\n' "$*"; }
|
||||
'''
|
||||
self.helper.write_text(source.replace('case "${1:-}" in', mocks + '\ncase "${1:-}" in', 1),
|
||||
encoding='utf-8', newline='\n')
|
||||
result = subprocess.run([SH, self.helper.as_posix(), 'collect'], capture_output=True,
|
||||
encoding='utf-8', env=self.env, timeout=10)
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertEqual(result.stdout.count('KILL='), 2)
|
||||
self.assertIn('REMOVE=-f ' + self.log_dir.as_posix() + '/stream.', result.stdout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user