Groups every flat test_*.py under the module it exercises, matching the
existing tests/{profiler,sniffer,prober,collector,correlation,cli,web,
topology,swarm,bus,updater,api,docker,geoip,...} layout. New folders:
services/, fleet/, config/, logging/, db/ (+ db/mysql/), telemetry/,
mutator/, core/.
Path-dependent __file__ references bumped an extra .parent in three
files that moved one level deeper:
- tests/sniffer/test_sniffer_ja3.py (template path)
- tests/services/test_ssh_capture_emit.py (template path)
- tests/cli/test_mode_gating.py (REPO root)
- tests/web/test_env_lazy_jwt.py (repo var)
Also drops two SQLite runtime artifacts (test_decnet.db-{shm,wal}) that
were leaking into the repo from a previous test run.
Fixes two test_service_isolation cases that patched asyncio.sleep (no
longer on the profiler main-loop hot path — same pre-existing bug I
fixed earlier in test_attacker_worker.py) by patching asyncio.wait_for
and passing interval=0.
72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
"""Tests for the syslog file handler."""
|
|
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import decnet.logging.file_handler as fh
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_handler(tmp_path, monkeypatch):
|
|
"""Reset the module-level logger between tests."""
|
|
monkeypatch.setattr(fh, "_handler", None)
|
|
monkeypatch.setattr(fh, "_logger", None)
|
|
monkeypatch.setenv(fh._LOG_FILE_ENV, str(tmp_path / "test.log"))
|
|
yield
|
|
# Remove handlers to avoid file lock issues on next test
|
|
if fh._logger is not None:
|
|
for h in list(fh._logger.handlers):
|
|
h.close()
|
|
fh._logger.removeHandler(h)
|
|
fh._handler = None
|
|
fh._logger = None
|
|
|
|
|
|
def test_write_creates_log_file(tmp_path):
|
|
log_path = tmp_path / "decnet.log"
|
|
os.environ[fh._LOG_FILE_ENV] = str(log_path)
|
|
fh.write_syslog("<134>1 2026-04-04T12:00:00+00:00 h svc - e - test message")
|
|
assert log_path.exists()
|
|
assert "test message" in log_path.read_text()
|
|
|
|
|
|
def test_write_appends_multiple_lines(tmp_path):
|
|
log_path = tmp_path / "decnet.log"
|
|
os.environ[fh._LOG_FILE_ENV] = str(log_path)
|
|
for i in range(3):
|
|
fh.write_syslog(f"<134>1 ts host svc - event{i} -")
|
|
lines = log_path.read_text().splitlines()
|
|
assert len(lines) == 3
|
|
assert "event0" in lines[0]
|
|
assert "event2" in lines[2]
|
|
|
|
|
|
def test_get_log_path_default(monkeypatch):
|
|
monkeypatch.delenv(fh._LOG_FILE_ENV, raising=False)
|
|
assert fh.get_log_path() == Path(fh._DEFAULT_LOG_FILE)
|
|
|
|
|
|
def test_get_log_path_custom(monkeypatch, tmp_path):
|
|
custom = str(tmp_path / "custom.log")
|
|
monkeypatch.setenv(fh._LOG_FILE_ENV, custom)
|
|
assert fh.get_log_path() == Path(custom)
|
|
|
|
|
|
def test_rotating_handler_configured(tmp_path):
|
|
log_path = tmp_path / "r.log"
|
|
os.environ[fh._LOG_FILE_ENV] = str(log_path)
|
|
logger = fh._get_logger()
|
|
handler = logger.handlers[0]
|
|
assert isinstance(handler, logging.handlers.RotatingFileHandler)
|
|
assert handler.maxBytes == fh._MAX_BYTES
|
|
assert handler.backupCount == fh._BACKUP_COUNT
|
|
|
|
|
|
def test_write_syslog_does_not_raise_on_bad_path(monkeypatch):
|
|
monkeypatch.setenv(fh._LOG_FILE_ENV, "/no/such/dir/that/exists/decnet.log")
|
|
# Should not raise — falls back to StreamHandler
|
|
fh.write_syslog("<134>1 ts h svc - e -")
|