Skip to content

Commit d1bb019

Browse files
authored
Merge pull request #84 from pythonbpf/testing-framework
Automated testing framework which will give us regression and coverage reports, meant to run in a GH Action and locally
2 parents 0498885 + 01ddffb commit d1bb019

14 files changed

Lines changed: 506 additions & 2 deletions

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,29 @@ docs = [
4141
"sphinx-rtd-theme>=2.0",
4242
"sphinx-copybutton",
4343
]
44+
test = [
45+
"pytest>=8.0",
46+
"pytest-cov>=5.0",
47+
]
4448

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

tests/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# PythonBPF Test Suite
2+
3+
## Quick start
4+
5+
```bash
6+
# Activate the venv and install test deps (once)
7+
source .venv/bin/activate
8+
uv pip install -e ".[test]"
9+
10+
# Run the full suite (IR + LLC levels, no sudo required)
11+
make test
12+
13+
# Run with coverage report
14+
make test-cov
15+
```
16+
17+
## Test levels
18+
19+
Tests are split into three levels, each in a separate file:
20+
21+
| Level | File | What it checks | Needs sudo? |
22+
|---|---|---|---|
23+
| 1 — IR generation | `test_ir_generation.py` | `compile_to_ir()` completes without exception or `logging.ERROR` | No |
24+
| 2 — LLC compilation | `test_llc_compilation.py` | Level 1 + `llc` produces a non-empty `.o` file | No |
25+
| 3 — Kernel verifier | `test_verifier.py` | `bpftool prog load -d` exits 0 | Yes |
26+
27+
Levels 1 and 2 run together with `make test`. Level 3 is opt-in:
28+
29+
```bash
30+
make test-verifier # requires bpftool and sudo
31+
```
32+
33+
## Running a single test
34+
35+
Tests are parametrized by file path. Use `-k` to filter:
36+
37+
```bash
38+
# By file name
39+
pytest tests/ -v -k "and.py" -m "not verifier"
40+
41+
# By category
42+
pytest tests/ -v -k "conditionals" -m "not verifier"
43+
44+
# One specific level only
45+
pytest tests/test_ir_generation.py -v -k "hash_map.py"
46+
```
47+
48+
## Coverage report
49+
50+
```bash
51+
make test-cov
52+
```
53+
54+
- **Terminal**: shows per-file coverage with missing lines after the test run.
55+
- **HTML**: written to `htmlcov/index.html` — open in a browser for line-by-line detail.
56+
57+
```bash
58+
xdg-open htmlcov/index.html
59+
```
60+
61+
`htmlcov/` and `.coverage` are excluded from git (listed in `.gitignore` if not already).
62+
63+
## Expected failures (`test_config.toml`)
64+
65+
Known-broken tests are declared in `tests/test_config.toml`:
66+
67+
```toml
68+
[xfail]
69+
"failing_tests/my_test.py" = {reason = "...", level = "ir"}
70+
```
71+
72+
- `level = "ir"` — fails during IR generation; both IR and LLC tests are marked xfail.
73+
- `level = "llc"` — IR generates fine but `llc` rejects it; only the LLC test is marked xfail.
74+
75+
All xfails use `strict = True`: if a test starts **passing** it shows up as **XPASS** and is treated as a test failure. This is intentional — it means the bug was fixed and the test should be promoted to `passing_tests/`.
76+
77+
## Adding a new test
78+
79+
1. Create a `.py` file in `tests/passing_tests/<category>/` with the usual `@bpf` decorators and a `compile()` call at the bottom.
80+
2. Run `make test` — the file is discovered and tested automatically at all levels.
81+
3. If the test is expected to fail, add it to `tests/test_config.toml` instead of `passing_tests/`.
82+
83+
## Directory structure
84+
85+
```
86+
tests/
87+
├── README.md ← you are here
88+
├── conftest.py ← pytest config: discovery, xfail/skip injection, fixtures
89+
├── test_config.toml ← expected-failure list
90+
├── test_ir_generation.py ← Level 1
91+
├── test_llc_compilation.py ← Level 2
92+
├── test_verifier.py ← Level 3 (opt-in, sudo)
93+
├── framework/
94+
│ ├── bpf_test_case.py ← BpfTestCase dataclass
95+
│ ├── collector.py ← discovers test files, reads test_config.toml
96+
│ ├── compiler.py ← wrappers around compile_to_ir() + _run_llc()
97+
│ └── verifier.py ← bpftool subprocess wrapper
98+
├── passing_tests/ ← programs that should compile and verify cleanly
99+
└── failing_tests/ ← programs with known issues (declared in test_config.toml)
100+
```

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
# ── pytest_generate_tests: parametrize on bpf_test_file ───────────────────
35+
36+
37+
def pytest_generate_tests(metafunc):
38+
if "bpf_test_file" in metafunc.fixturenames:
39+
cases = collect_all_test_files()
40+
metafunc.parametrize(
41+
"bpf_test_file",
42+
[c.path for c in cases],
43+
ids=[c.rel_path for c in cases],
44+
)
45+
46+
47+
# ── pytest_collection_modifyitems: apply xfail / skip markers ─────────────
48+
49+
50+
def pytest_collection_modifyitems(items):
51+
case_map = {c.rel_path: c for c in collect_all_test_files()}
52+
53+
for item in items:
54+
# Resolve the test case from the parametrize ID embedded in the node id.
55+
# Node id format: tests/test_foo.py::test_bar[passing_tests/helpers/pid.py]
56+
case = None
57+
for bracket in (item.callspec.id,) if hasattr(item, "callspec") else ():
58+
case = case_map.get(bracket)
59+
break
60+
61+
if case is None:
62+
continue
63+
64+
# vmlinux skip
65+
if case.needs_vmlinux and not VMLINUX_AVAILABLE:
66+
item.add_marker(
67+
pytest.mark.skip(reason="vmlinux.py not available for current kernel")
68+
)
69+
continue
70+
71+
# xfail (strict: XPASS counts as a test failure, alerting us to fixed bugs)
72+
if case.is_expected_fail:
73+
# Level "ir" → fails at IR generation: xfail both IR and LLC tests
74+
# Level "llc" → IR succeeds but LLC fails: only xfail the LLC test
75+
is_llc_test = item.nodeid.startswith("tests/test_llc_compilation.py")
76+
77+
apply_xfail = (case.xfail_level == "ir") or (
78+
case.xfail_level == "llc" and is_llc_test
79+
)
80+
if apply_xfail:
81+
item.add_marker(
82+
pytest.mark.xfail(
83+
reason=case.xfail_reason,
84+
strict=True,
85+
raises=Exception,
86+
)
87+
)
88+
89+
90+
# ── caplog level fixture: capture ERROR+ from pythonbpf ───────────────────
91+
92+
93+
@pytest.fixture(autouse=True)
94+
def set_log_level(caplog):
95+
with caplog.at_level(logging.ERROR, logger="pythonbpf"):
96+
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from pathlib import Path
2+
3+
import tomllib
4+
5+
from tests.framework.bpf_test_case import BpfTestCase
6+
7+
TESTS_DIR = Path(__file__).parent.parent
8+
CONFIG_FILE = TESTS_DIR / "test_config.toml"
9+
10+
VMLINUX_TEST_DIRS_PASSING = {"passing_tests/vmlinux"}
11+
VMLINUX_TEST_DIRS_FAILING = {
12+
"failing_tests/vmlinux",
13+
"failing_tests/xdp",
14+
}
15+
16+
17+
def _is_vmlinux_test(rel_path: str) -> bool:
18+
for prefix in VMLINUX_TEST_DIRS_PASSING | VMLINUX_TEST_DIRS_FAILING:
19+
if rel_path.startswith(prefix):
20+
return True
21+
return False
22+
23+
24+
def _load_config() -> dict:
25+
if not CONFIG_FILE.exists():
26+
return {}
27+
with open(CONFIG_FILE, "rb") as f:
28+
return tomllib.load(f)
29+
30+
31+
def collect_all_test_files() -> list[BpfTestCase]:
32+
config = _load_config()
33+
xfail_map: dict = config.get("xfail", {})
34+
35+
cases = []
36+
for subdir in ("passing_tests", "failing_tests"):
37+
for py_file in sorted((TESTS_DIR / subdir).rglob("*.py")):
38+
rel = str(py_file.relative_to(TESTS_DIR))
39+
needs_vmlinux = _is_vmlinux_test(rel)
40+
41+
xfail_entry = xfail_map.get(rel)
42+
is_expected_fail = xfail_entry is not None
43+
xfail_reason = xfail_entry.get("reason", "") if xfail_entry else ""
44+
xfail_level = xfail_entry.get("level", "ir") if xfail_entry else "ir"
45+
46+
cases.append(
47+
BpfTestCase(
48+
path=py_file,
49+
rel_path=rel,
50+
is_expected_fail=is_expected_fail,
51+
xfail_reason=xfail_reason,
52+
xfail_level=xfail_level,
53+
needs_vmlinux=needs_vmlinux,
54+
)
55+
)
56+
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import subprocess
2+
import uuid
3+
from collections import namedtuple
4+
from pathlib import Path
5+
6+
7+
def verify_object(obj_path: Path) -> tuple[bool, str]:
8+
"""Run bpftool prog load -d to verify a BPF object file against the kernel verifier.
9+
10+
Pins the program temporarily at /sys/fs/bpf/bpf_prog_test_<uuid>, then removes it.
11+
Returns (success, combined_output). Requires sudo / root.
12+
"""
13+
pin_path = f"/sys/fs/bpf/bpf_prog_test_{uuid.uuid4().hex[:8]}"
14+
try:
15+
result = subprocess.run(
16+
["sudo", "bpftool", "prog", "load", "-d", str(obj_path), pin_path],
17+
capture_output=True,
18+
text=True,
19+
timeout=30,
20+
)
21+
Output = namedtuple("Output", ["stdout", "stderr"])
22+
output = Output(stdout=result.stdout, stderr=result.stderr)
23+
return result.returncode == 0, output
24+
except subprocess.TimeoutExpired:
25+
return False, "bpftool timed out after 30s"
26+
finally:
27+
subprocess.run(["sudo", "rm", "-f", pin_path], check=False, capture_output=True)

0 commit comments

Comments
 (0)