Skip to content

Commit

Permalink
events: add python library tests
Browse files Browse the repository at this point in the history
Use tox to handle virtual env and pytest to as test runner.
Add a task in cirrus CI to run them.

Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz committed Sep 25, 2024
1 parent 6eb9b24 commit 9a11967
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
18 changes: 18 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ build_python_lib_task:
dist_artifacts:
path: "target/wheels/*"

test_python_lib_task:
container:
matrix:
- image: python:3.8
- image: python:3.9
- image: python:3.10
- image: python:3.11
- image: python:3.12
setup_script:
- apt-get update
- apt-get install -y curl
- curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh
- chmod +x rustup-init.sh && ./rustup-init.sh -y --profile minimal
build_script:
- source $HOME/.cargo/env
- python -m pip install tox
- make pytest

functional_task:
# Run for PRs with a specific label
required_pr_labels: run-functional-tests
Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ pylib:
$(call out_console,MATURIN,Building python bindings ...)
$(CONTAINER_RUNTIME) run --rm --name maturin -v $$PWD:/io:z ghcr.io/pyo3/maturin build -m retis-events/Cargo.toml -F python-lib

pytest-deps:
@which tox &> /dev/null || (echo "Please install tox ('pip install tox')."; exit 1)

pytest: pytest-deps
$(call out_console,TOX,Testing python bindings ...)
cd retis-events && tox

clean-ebpf:
$(call out_console,CLEAN,cleaning ebpf progs...)
for i in $(EBPF_PROBES) $(EBPF_HOOKS); do \
Expand All @@ -130,6 +137,7 @@ help:
$(PRINT) 'release -- Builds Retis with the release option.'
$(PRINT) 'test -- Builds and runs unit tests.'
$(PRINT) 'pylib -- Builds the python bindings.'
$(PRINT) 'pytest -- Tests the python bindings (requires "tox" installed).'
$(PRINT)
$(PRINT) 'Optional variables that can be used to override the default behavior:'
$(PRINT) 'V -- If set to 1 the verbose output will be printed.'
Expand All @@ -146,4 +154,4 @@ help:
$(PRINT) 'NOVENDOR -- Avoid to self detect and consume the vendored headers'
$(PRINT) ' shipped with libbpf-sys.'

.PHONY: all bench clean clean-ebpf ebpf $(EBPF_PROBES) $(EBPF_HOOKS) help install release test pylib
.PHONY: all bench clean clean-ebpf ebpf $(EBPF_PROBES) $(EBPF_HOOKS) help install release test pylib pytest-deps pytest
1 change: 1 addition & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ flavor coding style for the BPF parts.
1. `cargo clippy -- -D warnings`
1. `make test V=1`, or to include runtime tests,
`CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER=sudo CARGO_CMD_OPTS="--features=test_cap_bpf" make test V=1`
1. `make pytest V=1`
1. Make sure commits are
[signed off](https://www.kernel.org/doc/html/latest/process/submitting-patches.html?highlight=signed%20off#developer-s-certificate-of-origin-1-1).
1. Use a clear, concise and descriptive title.
Expand Down
6 changes: 0 additions & 6 deletions retis-events/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,3 @@ maintainers = [
{name = "Retis team", email = "[email protected]"}
]
repository = "https://github.com/retis-org/retis"

[project.optional-dependencies]
test = [
"pytest",
"tox",
]
35 changes: 35 additions & 0 deletions retis-events/pytests/test_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from retis import Event, EventReader, EventSeries

def verify_event(e):
"""Verify the event is valid"""
assert e.__class__ == Event
assert isinstance(e.raw(), dict)
assert isinstance(e.show(), str)
assert "userspace" in e or "kernel" in e

def test_event_reader() :
"""Test event reader is capable of reading valid events"""
r = EventReader("test_data/test_events.json")
assert not r.sorted()

for e in r:
assert e.__class__ == Event
assert isinstance(e.raw(), dict)
assert isinstance(e.show(), str)

def test_event_reader() :
"""Test event reader is capable of reading sorted events"""
r = EventReader("test_data/test_events_sorted.json")

assert r.sorted()

for s in r:
assert s.__class__ == EventSeries
length = len(s)
i = 0

for e in s:
verify_event(e)
i += 1

assert i == length
3 changes: 3 additions & 0 deletions retis-events/test_data/test_events_sorted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[{"common":{"smp_id":0,"task":{"comm":"napi/phy0-8197","pid":1360,"tgid":1360},"timestamp":23868643385999},"kernel":{"probe_type":"kprobe","symbol":"tcp_v4_rcv"},"skb":{},"skb-tracking":{"orig_head":18446616568293939200,"skb":18446616546229617920,"timestamp":23868643385999},"tracking":{"idx":0,"skb":{"orig_head":18446616568293939200,"skb":18446616546229617920,"timestamp":23868643385999}}}]
[{"common":{"smp_id":2,"task":{"comm":"napi/phy0-8197","pid":1360,"tgid":1360},"timestamp":23868955262984},"kernel":{"probe_type":"raw_tracepoint","symbol":"openvswitch:ovs_dp_upcall"},"ovs":{"cmd":1,"cpu":1,"event_type":"upcall","port":3366920467},"skb":{},"skb-tracking":{"orig_head":18446616576100907520,"skb":18446616546107689472,"timestamp":23868955262984},"tracking":{"idx":0,"skb":{"orig_head":18446616576100907520,"skb":18446616546107689472,"timestamp":23868955262984}}},{"common":{"smp_id":3,"task":{"comm":"napi/phy0-8197","pid":1360,"tgid":1360},"timestamp":23868955276361},"kernel":{"probe_type":"kretprobe","symbol":"ovs_dp_upcall"},"ovs":{"event_type":"upcall_return","ret":0,"upcall_cpu":1,"upcall_ts":23868955262984},"skb":{},"skb-tracking":{"orig_head":18446616576100907520,"skb":18446616546107689472,"timestamp":23868955262984},"tracking":{"idx":1,"skb":{"orig_head":18446616576100907520,"skb":18446616546107689472,"timestamp":23868955262984}}}]
[{"common":{"smp_id":0,"task":{"comm":"napi/phy0-8197","pid":1360,"tgid":1360},"timestamp":23868955449721},"kernel":{"probe_type":"raw_tracepoint","symbol":"skb:kfree_skb"},"skb":{},"skb-drop":{"drop_reason":"NO_SOCKET"},"skb-tracking":{"orig_head":18446616575285769216,"skb":18446616552502694400,"timestamp":23868955437572},"tracking":{"idx":0,"skb":{"orig_head":18446616575285769216,"skb":18446616552502694400,"timestamp":23868955437572}}}]
12 changes: 12 additions & 0 deletions retis-events/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tox]
requires =
tox>=4
env_list = py{38,39,310,311,312}

[testenv]
description = Run unit tests
deps =
pytest>=7
maturin>=1.7
commands =
pytest {posargs:pytests}

0 comments on commit 9a11967

Please sign in to comment.