Skip to content

Commit da57911

Browse files
r41k0uCopilot
andcommitted
Tests: Add automated testing framework with coverage support
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0498885 commit da57911

File tree

12 files changed

+425
-2
lines changed

12 files changed

+425
-2
lines changed

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
install:
2-
pip install -e .
2+
uv pip install -e ".[test]"
33

44
clean:
55
rm -rf build dist *.egg-info
66
rm -rf examples/*.ll examples/*.o
7+
rm -rf htmlcov .coverage
8+
9+
test:
10+
pytest tests/ -v --tb=short -m "not verifier"
11+
12+
test-cov:
13+
pytest tests/ -v --tb=short -m "not verifier" \
14+
--cov=pythonbpf --cov-report=term-missing --cov-report=html
15+
16+
test-verifier:
17+
@echo "NOTE: verifier tests require sudo and bpftool. Uses sudo .venv/bin/python3."
18+
pytest tests/test_verifier.py -v --tb=short -m verifier
719

820
all: clean install
921

10-
.PHONY: all clean
22+
.PHONY: all clean install test test-cov test-verifier

pyproject.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,31 @@ docs = [
4141
"sphinx-rtd-theme>=2.0",
4242
"sphinx-copybutton",
4343
]
44+
test = [
45+
"pytest>=8.0",
46+
"pytest-cov>=5.0",
47+
"tomli>=2.0; python_version < '3.11'",
48+
]
4449

4550
[tool.setuptools.packages.find]
4651
where = ["."]
4752
include = ["pythonbpf*"]
53+
54+
[tool.pytest.ini_options]
55+
testpaths = ["tests"]
56+
python_files = ["test_*.py"]
57+
python_classes = ["Test*"]
58+
python_functions = ["test_*"]
59+
markers = [
60+
"verifier: requires sudo/root for kernel verifier tests (not run by default)",
61+
"vmlinux: requires vmlinux.py for current kernel",
62+
]
63+
log_cli = false
64+
65+
[tool.coverage.run]
66+
source = ["pythonbpf"]
67+
omit = ["*/vmlinux*", "*/__pycache__/*"]
68+
69+
[tool.coverage.report]
70+
show_missing = true
71+
skip_covered = false

tests/conftest.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
pytest configuration for the PythonBPF test suite.
3+
4+
Test discovery:
5+
All .py files under tests/passing_tests/ and tests/failing_tests/ are
6+
collected as parametrized BPF test cases.
7+
8+
Markers applied automatically from test_config.toml:
9+
- xfail (strict=True): failing_tests/ entries that are expected to fail
10+
- skip: vmlinux tests when vmlinux.py is not importable
11+
12+
Run the suite:
13+
pytest tests/ -v -m "not verifier" # IR + LLC only (no sudo)
14+
pytest tests/ -v --cov=pythonbpf # with coverage
15+
pytest tests/test_verifier.py -m verifier # kernel verifier (sudo required)
16+
"""
17+
18+
import logging
19+
20+
import pytest
21+
22+
from tests.framework.collector import collect_all_test_files
23+
24+
# ── vmlinux availability ────────────────────────────────────────────────────
25+
26+
try:
27+
import vmlinux # noqa: F401
28+
29+
VMLINUX_AVAILABLE = True
30+
except ImportError:
31+
VMLINUX_AVAILABLE = False
32+
33+
34+
# ── shared fixture: collected test cases ───────────────────────────────────
35+
36+
37+
def _all_cases():
38+
return collect_all_test_files()
39+
40+
41+
# ── pytest_generate_tests: parametrize on bpf_test_file ───────────────────
42+
43+
44+
def pytest_generate_tests(metafunc):
45+
if "bpf_test_file" in metafunc.fixturenames:
46+
cases = _all_cases()
47+
metafunc.parametrize(
48+
"bpf_test_file",
49+
[c.path for c in cases],
50+
ids=[c.rel_path for c in cases],
51+
)
52+
53+
54+
# ── pytest_collection_modifyitems: apply xfail / skip markers ─────────────
55+
56+
57+
def pytest_collection_modifyitems(items):
58+
case_map = {c.rel_path: c for c in _all_cases()}
59+
60+
for item in items:
61+
# Resolve the test case from the parametrize ID embedded in the node id.
62+
# Node id format: tests/test_foo.py::test_bar[passing_tests/helpers/pid.py]
63+
case = None
64+
for bracket in (item.callspec.id,) if hasattr(item, "callspec") else ():
65+
case = case_map.get(bracket)
66+
break
67+
68+
if case is None:
69+
continue
70+
71+
# vmlinux skip
72+
if case.needs_vmlinux and not VMLINUX_AVAILABLE:
73+
item.add_marker(
74+
pytest.mark.skip(reason="vmlinux.py not available for current kernel")
75+
)
76+
continue
77+
78+
# xfail (strict: XPASS counts as a test failure, alerting us to fixed bugs)
79+
if case.is_expected_fail:
80+
# Level "ir" → fails at IR generation: xfail both IR and LLC tests
81+
# Level "llc" → IR succeeds but LLC fails: only xfail the LLC test
82+
is_llc_test = item.nodeid.startswith("tests/test_llc_compilation.py")
83+
84+
apply_xfail = (case.xfail_level == "ir") or (
85+
case.xfail_level == "llc" and is_llc_test
86+
)
87+
if apply_xfail:
88+
item.add_marker(
89+
pytest.mark.xfail(
90+
reason=case.xfail_reason,
91+
strict=True,
92+
raises=Exception,
93+
)
94+
)
95+
96+
97+
# ── caplog level fixture: capture ERROR+ from pythonbpf ───────────────────
98+
99+
100+
@pytest.fixture(autouse=True)
101+
def set_log_level(caplog):
102+
with caplog.at_level(logging.ERROR, logger="pythonbpf"):
103+
yield

tests/framework/__init__.py

Whitespace-only changes.

tests/framework/bpf_test_case.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from dataclasses import dataclass
2+
from pathlib import Path
3+
4+
5+
@dataclass
6+
class BpfTestCase:
7+
path: Path
8+
rel_path: str
9+
is_expected_fail: bool = False
10+
xfail_reason: str = ""
11+
xfail_level: str = "ir" # "ir" or "llc"
12+
needs_vmlinux: bool = False
13+
skip_reason: str = ""
14+
15+
@property
16+
def test_id(self) -> str:
17+
return self.rel_path.replace("/", "::")

tests/framework/collector.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import sys
2+
from pathlib import Path
3+
4+
if sys.version_info >= (3, 11):
5+
import tomllib
6+
else:
7+
import tomli as tomllib
8+
9+
from .bpf_test_case import BpfTestCase
10+
11+
TESTS_DIR = Path(__file__).parent.parent
12+
CONFIG_FILE = TESTS_DIR / "test_config.toml"
13+
14+
VMLINUX_TEST_DIRS = {"passing_tests/vmlinux"}
15+
VMLINUX_TEST_PREFIXES = {
16+
"failing_tests/vmlinux",
17+
"failing_tests/xdp",
18+
}
19+
20+
21+
def _is_vmlinux_test(rel_path: str) -> bool:
22+
for prefix in VMLINUX_TEST_DIRS | VMLINUX_TEST_PREFIXES:
23+
if rel_path.startswith(prefix):
24+
return True
25+
return False
26+
27+
28+
def _load_config() -> dict:
29+
if not CONFIG_FILE.exists():
30+
return {}
31+
with open(CONFIG_FILE, "rb") as f:
32+
return tomllib.load(f)
33+
34+
35+
def collect_all_test_files() -> list[BpfTestCase]:
36+
config = _load_config()
37+
xfail_map: dict = config.get("xfail", {})
38+
39+
cases = []
40+
for subdir in ("passing_tests", "failing_tests"):
41+
for py_file in sorted((TESTS_DIR / subdir).rglob("*.py")):
42+
rel = str(py_file.relative_to(TESTS_DIR))
43+
needs_vmlinux = _is_vmlinux_test(rel)
44+
45+
xfail_entry = xfail_map.get(rel)
46+
is_expected_fail = xfail_entry is not None
47+
xfail_reason = xfail_entry.get("reason", "") if xfail_entry else ""
48+
xfail_level = xfail_entry.get("level", "ir") if xfail_entry else "ir"
49+
50+
cases.append(
51+
BpfTestCase(
52+
path=py_file,
53+
rel_path=rel,
54+
is_expected_fail=is_expected_fail,
55+
xfail_reason=xfail_reason,
56+
xfail_level=xfail_level,
57+
needs_vmlinux=needs_vmlinux,
58+
)
59+
)
60+
return cases

tests/framework/compiler.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import logging
2+
from pathlib import Path
3+
4+
from pythonbpf.codegen import compile_to_ir, _run_llc
5+
6+
7+
def run_ir_generation(test_path: Path, output_ll: Path):
8+
"""Run compile_to_ir on a BPF test file.
9+
10+
Returns the (output, structs_sym_tab, maps_sym_tab) tuple from compile_to_ir.
11+
Raises on exception. Any logging.ERROR records captured by pytest caplog
12+
indicate a compile failure even when no exception is raised.
13+
"""
14+
return compile_to_ir(str(test_path), str(output_ll), loglevel=logging.WARNING)
15+
16+
17+
def run_llc(ll_path: Path, obj_path: Path) -> bool:
18+
"""Compile a .ll file to a BPF .o using llc.
19+
20+
Raises subprocess.CalledProcessError on failure (llc uses check=True).
21+
Returns True on success.
22+
"""
23+
return _run_llc(str(ll_path), str(obj_path))

tests/framework/verifier.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import subprocess
2+
import uuid
3+
from pathlib import Path
4+
5+
6+
def verify_object(obj_path: Path) -> tuple[bool, str]:
7+
"""Run bpftool prog load -d to verify a BPF object file against the kernel verifier.
8+
9+
Pins the program temporarily at /sys/fs/bpf/bpf_prog_test_<uuid>, then removes it.
10+
Returns (success, combined_output). Requires sudo / root.
11+
"""
12+
pin_path = f"/sys/fs/bpf/bpf_prog_test_{uuid.uuid4().hex[:8]}"
13+
try:
14+
result = subprocess.run(
15+
["sudo", "bpftool", "prog", "load", "-d", str(obj_path), pin_path],
16+
capture_output=True,
17+
text=True,
18+
timeout=30,
19+
)
20+
output = result.stdout + result.stderr
21+
return result.returncode == 0, output
22+
except subprocess.TimeoutExpired:
23+
return False, "bpftool timed out after 30s"
24+
finally:
25+
subprocess.run(["sudo", "rm", "-f", pin_path], check=False, capture_output=True)

tests/test_config.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# test_config.toml
2+
#
3+
# [xfail] — tests expected to fail.
4+
# key = path relative to tests/
5+
# value = {reason = "...", level = "ir" | "llc"}
6+
# level "ir" = fails during pythonbpf IR generation (exception or ERROR log)
7+
# level "llc" = IR generates but llc rejects it
8+
#
9+
# Tests removed from this list because they now pass (fixed by compilation-context PR):
10+
# failing_tests/assign/retype.py
11+
# failing_tests/conditionals/helper_cond.py
12+
# failing_tests/conditionals/oneline.py
13+
# failing_tests/direct_assign.py
14+
# failing_tests/globals.py
15+
# failing_tests/if.py
16+
# failing_tests/license.py
17+
# failing_tests/named_arg.py
18+
# failing_tests/xdp/xdp_test_1.py
19+
# These files can be moved to passing_tests/ when convenient.
20+
21+
[xfail]
22+
23+
"failing_tests/conditionals/struct_ptr.py" = {reason = "Struct pointer used directly as boolean condition not supported", level = "ir"}
24+
25+
"failing_tests/license.py" = {reason = "Missing LICENSE global produces IR that llc rejects — should be caught earlier with a clear error message", level = "llc"}
26+
27+
"failing_tests/undeclared_values.py" = {reason = "Undeclared variable used in f-string — should raise SyntaxError (correct behaviour, test documents it)", level = "ir"}
28+
29+
"failing_tests/vmlinux/args_test.py" = {reason = "struct_trace_event_raw_sys_enter args field access not supported", level = "ir"}
30+
31+
"failing_tests/vmlinux/assignment_handling.py" = {reason = "Assigning vmlinux enum value (XDP_PASS) to a local variable not yet supported", level = "ir"}
32+
33+
"failing_tests/xdp_pass.py" = {reason = "XDP program using vmlinux structs (struct_xdp_md) and complex map/struct interaction not yet supported", level = "ir"}

tests/test_ir_generation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Level 1 — IR Generation tests.
3+
4+
For every BPF test file, calls compile_to_ir() and asserts:
5+
1. No exception is raised by the pythonbpf compiler.
6+
2. No logging.ERROR records are emitted during compilation.
7+
3. A .ll file is produced.
8+
9+
Tests in failing_tests/ are marked xfail (strict=True) by conftest.py —
10+
they must raise an exception or produce an ERROR log to pass the suite.
11+
"""
12+
13+
import logging
14+
from pathlib import Path
15+
16+
17+
from tests.framework.compiler import run_ir_generation
18+
19+
20+
def test_ir_generation(bpf_test_file: Path, tmp_path, caplog):
21+
ll_path = tmp_path / "output.ll"
22+
23+
run_ir_generation(bpf_test_file, ll_path)
24+
25+
error_records = [r for r in caplog.records if r.levelno >= logging.ERROR]
26+
assert not error_records, "IR generation produced ERROR log(s):\n" + "\n".join(
27+
f" [{r.name}] {r.getMessage()}" for r in error_records
28+
)
29+
assert ll_path.exists(), "compile_to_ir() returned without writing a .ll file"

0 commit comments

Comments
 (0)