mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Tests: Let a verifier-level xfail name the rejection it expects
Every xfail is strict with raises=Exception, so a verifier-level negative fixture counted any rejection as the expected one, including one caused by a codegen regression, or by bpftool failing. A verifier entry may now carry match = "..."; the harness attaches it as a verifier_match marker and test_kernel_verifier fails, through pytest.fail (not swallowed by raises=Exception), when the program is rejected for any other reason. xdp_devmap_helpers pins its documented "invalid bpf_context access". Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@ -63,6 +63,7 @@ python_files = ["test_*.py"]
|
||||
markers = [
|
||||
"verifier: requires sudo/root for kernel verifier tests (not run by default)",
|
||||
"vmlinux: requires vmlinux.py for current kernel",
|
||||
"verifier_match: substring a verifier-level xfail expects in the rejection",
|
||||
]
|
||||
log_cli = false
|
||||
|
||||
|
||||
@ -103,6 +103,10 @@ def pytest_collection_modifyitems(items):
|
||||
raises=Exception,
|
||||
)
|
||||
)
|
||||
# A verifier-level xfail may name the rejection it expects;
|
||||
# any other rejection is then a real failure, not an xfail.
|
||||
if item_level == "verifier" and case.xfail_match:
|
||||
item.add_marker(pytest.mark.verifier_match(case.xfail_match))
|
||||
|
||||
|
||||
# ── caplog level fixture: capture ERROR+ from pythonbpf ───────────────────
|
||||
|
||||
@ -23,6 +23,7 @@ class BpfTestCase:
|
||||
is_expected_fail: bool = False
|
||||
xfail_reason: str = ""
|
||||
xfail_level: str = "ir" # one of LEVELS
|
||||
xfail_match: str = "" # verifier level: substring the rejection must contain
|
||||
needs_vmlinux: bool = False
|
||||
skip_reason: str = ""
|
||||
|
||||
|
||||
@ -49,6 +49,7 @@ def collect_all_test_files() -> list[BpfTestCase]:
|
||||
is_expected_fail = xfail_entry is not None
|
||||
xfail_reason = xfail_entry.get("reason", "") if xfail_entry else ""
|
||||
xfail_level = xfail_entry.get("level", "ir") if xfail_entry else "ir"
|
||||
xfail_match = xfail_entry.get("match", "") if xfail_entry else ""
|
||||
|
||||
cases.append(
|
||||
BpfTestCase(
|
||||
@ -56,6 +57,7 @@ def collect_all_test_files() -> list[BpfTestCase]:
|
||||
rel_path=rel,
|
||||
is_expected_fail=is_expected_fail,
|
||||
xfail_reason=xfail_reason,
|
||||
xfail_match=xfail_match,
|
||||
xfail_level=xfail_level,
|
||||
needs_vmlinux=needs_vmlinux,
|
||||
)
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
# level "ir" = fails during pythonbpf IR generation (exception or ERROR log)
|
||||
# level "llc" = IR generates but llc rejects it
|
||||
# level "verifier" = IR and llc both succeed, but the kernel verifier rejects it
|
||||
# A verifier-level entry may add match = "..." : the rejection must contain
|
||||
# that text, otherwise the test fails instead of counting as expected.
|
||||
#
|
||||
# A failure at one level implies failure at every later one, so the declared
|
||||
# level marks that level and all later ones xfail.
|
||||
@ -71,4 +73,4 @@
|
||||
# prog_tests driver asserts that a plain load *fails*. The kernel rejects it
|
||||
# here with "invalid bpf_context access off=20 size=4", which is the pass
|
||||
# condition upstream. PythonBPF has no way to set expected_attach_type yet.
|
||||
"kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py" = {reason = "Negative fixture: egress_ifindex needs expected_attach_type=BPF_XDP_DEVMAP, which cannot be set yet; the verifier rejection is upstream's pass condition", level = "verifier"}
|
||||
"kernel_selftest_equivalent/vmlinux/xdp_devmap_helpers.py" = {reason = "Negative fixture: egress_ifindex needs expected_attach_type=BPF_XDP_DEVMAP, which cannot be set yet; the verifier rejection is upstream's pass condition", level = "verifier", match = "invalid bpf_context access"}
|
||||
|
||||
@ -54,7 +54,7 @@ def _get_rejection_reason(verifier_test_file: Path, output) -> str:
|
||||
_verifier_test_files(),
|
||||
ids=_verifier_test_ids(),
|
||||
)
|
||||
def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog):
|
||||
def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog, request):
|
||||
"""Compile the BPF test and verify it passes the kernel verifier."""
|
||||
ll_path = tmp_path / "output.ll"
|
||||
obj_path = tmp_path / "output.o"
|
||||
@ -70,4 +70,13 @@ def test_kernel_verifier(verifier_test_file: Path, tmp_path, caplog):
|
||||
assert obj_path.exists() and obj_path.stat().st_size > 0
|
||||
|
||||
ok, output = verify_object(obj_path)
|
||||
expected = request.node.get_closest_marker("verifier_match")
|
||||
if not ok and expected is not None and expected.args[0] not in output.stderr:
|
||||
# pytest.fail raises an OutcomeException, which the xfail marker's
|
||||
# raises=Exception does not swallow: a rejection for the wrong
|
||||
# reason is a failure, not the expected one.
|
||||
pytest.fail(
|
||||
f"{verifier_test_file.name}: rejected, but not for the expected reason "
|
||||
f"{expected.args[0]!r}:\n{output.stderr}"
|
||||
)
|
||||
assert ok, _get_rejection_reason(verifier_test_file, output)
|
||||
|
||||
Reference in New Issue
Block a user