I know this is a weird one, And I’m probably looking for help in a bleak topic since after days of scouring the net to no avail I decided to reach out to this community...
so I need to write unit tests for eBPF programs written in python with BCC
Any advice will be tremendously appreciated, even pointing me in the general direction of where i can further look for insights into solving the issue i am currently faced with
Here is a trivial example:
from bcc import BPF
# define BPF program
prog = """
int hello(void *ctx) {
bpf_trace_printk("Hello, World!\\n");
return 0;
}
"""
# load BPF program
b = BPF(text=prog)
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello")
# header
print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE"))
# format output
while 1:
try:
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
except ValueError:
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
I need to unit test the C code and the python code independently to see that their both behaving as expected
You can unit test the Python logic in the usual way you would for Python. For the BPF part, there are two approaches:
BPF_PROG_TEST_RUN
or you can attach it as planned and trigger the attach point (e.g., call clone
if the program is attached to the clone(2) syscall). Admittedly, this approach looks almost more like end-to-end tests than unit tests.