Add bpf_logging/tests/test_core.py
This commit is contained in:
parent
7f074a7ae6
commit
fe1ed7e17c
1 changed files with 45 additions and 0 deletions
45
bpf_logging/tests/test_core.py
Normal file
45
bpf_logging/tests/test_core.py
Normal 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()
|
||||||
Loading…
Reference in a new issue