import os from pathlib import Path import signal import subprocess import sys import tempfile import time import unittest from unittest.mock import patch import nao_log_stream as stream import log_retention class NativeLogTests(unittest.TestCase): def test_log_write_failure_does_not_interrupt_child_work(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory).resolve(); result = root / 'completed.txt' command = [sys.executable, '-c', 'import sys,pathlib;sys.stdout.write("x"*300000);pathlib.Path(sys.argv[1]).write_text("done")', str(result)] with patch.object(stream.PrivateHandler, 'emit', side_effect=OSError('Disk full')): self.assertEqual(stream.capture(root / 'logs/server.log', command), 0) self.assertEqual(result.read_text(), 'done') def test_real_child_output_rolls_and_stays_bounded(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory).resolve() / 'logs/server.log' command = [sys.executable, '-c', 'import sys;sys.stdout.write("old line\\n"*6000+"LAST_MARKER\\n");sys.stderr.write("stderr marker\\n")'] self.assertEqual(stream.capture(path, command, segment_bytes=1024, total_bytes=9000), 0) files = list(path.parent.glob('server.log*')) self.assertGreater(len(files), 1); self.assertLessEqual(sum(p.stat().st_size for p in files), 9000) contents = ''.join(p.read_text() for p in files) self.assertIn('LAST_MARKER', contents); self.assertIn('stderr marker', contents) for p in files: self.assertEqual(p.stat().st_mode & 0o077, 0) def test_oversized_legacy_log_and_expired_archives_are_bounded_on_start(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory).resolve() / 'logs/server.log'; path.parent.mkdir() path.write_bytes(b'old' * 10000); old = path.with_name('server.log.8'); old.write_bytes(b'expired') os.utime(old, (time.time() - 9 * 86400,) * 2) stream.capture(path, [sys.executable, '-c', 'print("new")'], segment_bytes=1024, total_bytes=9000) self.assertIn('new', path.read_text()); self.assertFalse(old.exists()) self.assertLessEqual(sum(p.stat().st_size for p in path.parent.glob('server.log*')), 9000) def test_symlink_log_or_archive_is_rejected_before_child_launch(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory).resolve(); path = root / 'logs/server.log'; path.parent.mkdir() outside = root / 'data.xlsx'; outside.write_bytes(b'business data') path.with_name('server.log.1').symlink_to(outside) with self.assertRaises(ValueError): stream.capture(path, [sys.executable, '-c', 'print("no")']) self.assertEqual(outside.read_bytes(), b'business data') @unittest.skipIf(os.name == 'nt', 'POSIX process-group shutdown contract') def test_stop_keeps_wrapper_until_term_ignoring_child_can_be_killed_as_a_group(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory).resolve(); path = root / 'logs/fastapi.log' command = [sys.executable, str(Path(stream.__file__).resolve()), '--log', str(path), '--', sys.executable, '-u', '-c', 'import signal,time;signal.signal(signal.SIGTERM,signal.SIG_IGN);print("READY",flush=True);time.sleep(60)'] process = subprocess.Popen(command, start_new_session=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) try: deadline = time.monotonic() + 5 while not path.exists() or 'READY' not in path.read_text(): if time.monotonic() > deadline: self.fail('Native wrapper did not start') time.sleep(.02) import nao_quality_runtime as runtime with patch.object(runtime, 'PYTHON', Path(sys.executable)), patch.object(runtime, 'FASTAPI_MAIN', Path('-c')): record=runtime._process_record(process.pid,'fastapi',5283) self.assertTrue(runtime._process_matches(record,root)) self.assertFalse(runtime._process_matches(record,root/'another-session')) os.killpg(process.pid, signal.SIGTERM); time.sleep(.1) self.assertIsNone(process.poll()) os.killpg(process.pid, signal.SIGKILL); process.wait(timeout=5) finally: if process.poll() is None: os.killpg(process.pid, signal.SIGKILL); process.wait(timeout=5) def test_native_actor_and_session_logs_are_cleaned_but_business_files_and_open_logs_are_kept(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory).resolve(); support = root / 'support' actor = root / '.runtime/nao-users/actor' paths = [actor / 'logs/server.log', actor / 'sessions/session/logs/fastapi.log.1'] for path in paths: path.parent.mkdir(parents=True); path.write_bytes(b'old diagnostics\n'); os.utime(path, (time.time() - 9 * 86400,) * 2) private = actor / 'sessions/session/private/nao.sqlite'; private.parent.mkdir(); private.write_bytes(b'keep') self.assertTrue(set(paths).issubset(log_retention.candidates(root, support))) result = log_retention.retain(root, support, open_paths={str(paths[0])}) self.assertEqual(result['logsRemoved'], 1); self.assertTrue(paths[0].exists()); self.assertTrue(private.exists()) if __name__ == '__main__': unittest.main()