Behaviour tests
A plugin reads OPNsense's files, calls its classes and runs its commands, and a check can follow every one of those names into a new release. What it cannot follow is behaviour: the direction a counter counts in, which end of a connection a pf state calls its own, what a pf table holds after a reload, what Unbound does with a blocklist entry, how the captive portal accounts a session. A release can change any of these with every file still where it was, and a plugin built on the old behaviour then counts wrong, blocks the wrong host, or does nothing, with no error anywhere.
A behaviour test is a short shell script, kept in the plugin beside its code, that makes the state on an appliance, measures what OPNsense does with it, says whether that is what the plugin relies on, and puts the appliance back. tools/behaviour/run.py runs a plugin's tests on one appliance over ssh. None of this runs on a GitHub runner: there is no OPNsense there. What the runner itself does is checked there, against a stand-in for ssh (tools/test-behaviour.py).
Adding a test to a plugin
One file per behaviour, tests/behaviour/<name>.sh in the plugin's repository, <name> in lower-case
letters, digits and dashes. Start from tools/behaviour/template.sh.
The file is sent to the appliance, never installed there: the package does not carry tests/.
The header, after the shebang and the licence lines, says what the test is about, one # key: value
line each, a long value carried on to lines indented by two spaces or more after the #:
| key | says | |
|---|---|---|
behaviour |
what OPNsense does that the plugin relies on, in one sentence | required |
needs |
what the appliance must have for the test to run | required |
restore |
what the test changes, and how it is put back | required |
relies |
where the plugin relies on it: the file, and the function or the line | optional |
The body is one case on STEP, which the runner sets, with BEHAVIOUR set to the test's name:
| step | does | exits |
|---|---|---|
state |
reads the appliance; keeps, in a directory of its own under /tmp, what restore will need |
0 when the appliance has what the test needs; 2 when it does not, and the test is skipped |
test |
makes the state, measures, judges; its last line of output is the result's line | 0 when the behaviour is the one relied on; 1 when it is not; anything else when it could not be measured |
restore |
puts back what state kept and takes away what test made |
0 when the appliance is as state found it |
The runner sends the steps in the order state, test, restore, each with the file on standard
input: what the appliance has and what will be needed to put it back is kept before anything is
changed. Over ssh a step is the line
env STEP=<step> BEHAVIOUR=<name> lockf -k -t <wait> /tmp/behaviour.lock timeout -k 5 <seconds> sh -s
The root shell on an appliance is csh, and that line is all it is asked to read: the script itself is
always run by sh. timeout cuts the step on the appliance at --timeout, whatever happens to the
ssh session, and the lock makes each step wait there until the one before it has ended: a restore
sent after a session that was cut does not run beside a test still going. The file is sent as one
{ ... } < /dev/null group, so sh has read the whole of it before any of it runs, and a command in
it that reads its standard input - nc with nothing piped in, read, cat with no file - gets
nothing rather than the rest of the script. restore is sent after every test that started, whatever
the test answered, a test cut at --timeout included, so it must hold wherever the test stopped and
when it is sent twice. A test the appliance lacks something for gets state only.
What makes a good one:
- Its own state, not the appliance's. A test that needs a connection makes one it controls, in
jails or on interfaces it creates, rather than reading the traffic that happens to be there. A test
that has to change the configuration keeps
/conf/config.xmlinstateand puts it back inrestore, reloading what it changed. - Everything back. The runner checks it: before
stateand afterrestoreit readsconfig.xml's checksum, the list of/conf/backup, pf's rule and NAT counts, anchors and tables, ipfw's rules and dummynet's pipes and queues where they are loaded, the kernel modules, the interfaces, the jails, and the daemons - the processes whose parent is init. A test after which any of these is not as it was fails, even when the behaviour held:configforconfig.xml,leftoverfor the rest, with what changed. The runner also keeps its own copy ofconfig.xmland a tar of/conf/backupbeforestate, in/root/behaviour-keep(--keep), and puts back what the test did not:config.xmlby a copy and a rename, with nothing reloaded, and the backups as they were - a backup made by a save the test put back is not a failure. The copy is taken away once the appliance is back, and after the last test the empty/root/behaviour-keepand the lock file/tmp/behaviour.lockgo too, from a step that holds the lock (until 2026-12-02 they stayed); a copy the runner could not put back stays, with its directory. - Short. A step that takes longer than
--timeout(300 seconds unless it is given) is cut on the appliance, or by the runner itself with--local. - Nothing left running.
timeouton the appliance waits for every process the step started, and the runner with--localfor every one that holds the step's output: a job a step leaves in the background - a listener, a connection held open - holds the step to--timeout, and what it measured is lost as cut. Start such a job with its input and output redirected (< /dev/null > /dev/null 2>&1 &), and stop and wait for it in atrap ... EXITset before it starts, so it ends however the step does. os-quota'spf-table-block.shdoes both. - FreeBSD's
sh.--checkruns thesh -nof the machine it runs on, which may not be FreeBSD's: FreeBSD's refuses a function named as one of its special built-ins (times,trap,exit,set), and a step it cannot read exits 2 -stateread as skipped. Run a new test once on a FreeBSD machine. - Judged in the test. The figures go in the output, and the verdict is the exit: the runner does not read numbers.
- Plain shell. FreeBSD's base system and what OPNsense installs are all there is:
sh,awk,nc,jail,pfctl,ipfwwhere it is loaded,configctl. Python is there too, as OPNsense's own.
Check a plugin's tests without an appliance - the header, the three steps, sh -n:
python3 tools/behaviour/run.py --check <plugin checkout>
Running them
From a checkout of os-xgs-npu, with the plugin's checkout beside it and an appliance ssh reaches as root by key:
python3 tools/behaviour/run.py <plugin checkout> <appliance>
python3 tools/behaviour/run.py <plugin checkout> <appliance> --only <name>
python3 tools/behaviour/run.py <plugin checkout> <appliance> --json result.json --log logs/
<appliance> is what ssh is given: a name from ~/.ssh/config, or root@<address>. --ssh replaces
the ssh command when it needs options of its own, and --local runs every step on this machine with
sh, for a run on the appliance itself. Before state and after restore the runner reads the
appliance's OPNsense version, the plugin's installed.json and what is listed under "Everything
back" above. An appliance it cannot read before a test gets nothing sent, and the test fails as
look; one it cannot read after leaves the runner's copy of config.xml where --keep says, and the
test fails as look too.
It prints one line per test - PASS, FAIL or SKIP, the plugin and the test, and the test's last
line or why it failed - and then the summary in JSON: each test's header, its status, why it failed
(differs, error, timeout, restore, config, leftover, look, keep or header), each
step's exit, what changed and what the runner put back, and what the appliance said before it. It
exits 0 when every test passed, 1 when one failed, 3 when none failed and one was skipped, and 2 when
it was called wrongly.
The canary
One lab appliance takes each OPNsense release before anyone else does, once
upstream.yml has seen it and every plugin's contract.json holds
against it - the issue it raises says "N of N plugins hold". tools/behaviour/canary.py
is that appliance's own run, prepared and not switched on: it updates the appliance it runs on, which is
the owner's word to give. Until its settings file says ENABLED=yes, and a cron line and a start hook are
put in place by hand, it only says what it would do (canary.py status).
Switched on, each run looks for upstream.yml's open issue of a release newer than the one the appliance
runs. Where every plugin holds, it keeps the appliance's record before the update
(tools/collect-update-record.sh) and starts System: Firmware's own
update (configctl firmware update), which reboots where the release needs it. After the boot - from a
start hook of its own, detached with daemon -f, as the boot waits for each start hook - and once no
firmware run, pkg or boot is going on, it keeps the record after, runs every installed plugin's behaviour
tests on itself, one plugin at a time -
python3 tools/behaviour/run.py <plugin checkout> canary --local --json <plugin>.json
- and tools/record-update.py
--dry-runon the two records. A release that changes packages alone does not reboot: the next daily run finds the update in and nothing going on, and makes the same check. The result is kept on the appliance. With a token file it may use, it is a comment on the release's issue, and the issue is closed where every test passed and the record holds; without one it waits there for whoever reads it. A comment GitHub does not take is said in the result, and the check is not run again. ASKIPis in the result too: a test the canary cannot run is a behaviour nobody has checked. The record itself - compat.json and the "verified on" lines - is a signed commit, which the appliance cannot make: it is written by hand from the comment. tools/test-canary.py holds it to this with stand-ins for the appliance, git and GitHub.
The discussion of this page is held in the project's GitHub Discussions. Showing it here loads giscus from giscus.app and GitHub, and writing in it needs a GitHub account - what that means.