Add bpf_logging/tests/test_core.py

This commit is contained in:
Mika 2025-12-27 16:53:27 +00:00
parent 7f074a7ae6
commit fe1ed7e17c

View file

@ -0,0 +1,45 @@
import json
import os
import tempfile
import pytest
from pathlib import Path
from bpf_logging import core
def test_setup_bpf_logging_creates_environment(monkeypatch):
# Simuliert das Setup erwartet keine Exceptions
test_id = "test123"
core.setup_bpf_logging(test_id)
# Kein Rückgabewert, daher nur prüfbar, dass kein Fehler auftritt
def test_log_timekeeping_event_writes_json(monkeypatch):
tmpdir = tempfile.TemporaryDirectory()
log_path = Path(tmpdir.name) / "output" / "log_entries.json"
# Umleitung des globalen Logpfads innerhalb core
monkeypatch.setattr(core, "LOG_FILE", log_path)
correlation_id = "cid-42"
event_type = "first_tkread"
# Ausführen des Loggings
core.setup_bpf_logging(correlation_id)
core.log_timekeeping_event(event_type, correlation_id)
# Datei sollte existieren
assert log_path.exists()
# Inhalt prüfen
with open(log_path, 'r', encoding='utf-8') as f:
data = json.load(f)
assert isinstance(data, list)
assert len(data) == 1
entry = data[0]
assert entry["correlation_id"] == correlation_id
assert entry["event_type"] == event_type
assert isinstance(entry["timestamp"], float)
tmpdir.cleanup()