Skip to content

Commit

Permalink
tests: avoid sending signals in run_test.sh
Browse files Browse the repository at this point in the history
Change run_test.sh and .bt script such that bpftrace terminates at the
end of target test program. Compare the test output after bpftrace has
terminated.

Using kill -INT to stop bpftrace may interfere with CI environment,
because this signal may be interpreted as a job failure.

I tried bpftrace -c [1] to work around this, however it turned out to
not work for these tests when there are tracepoints refering to
dynamic libraries. This is because the .so is not loaded yet when
bpftrace attempts to attach to the child process.

[1] https://docs.bpftrace.org/latest#-c-command

Signed-off-by: Ihor Solodrai <[email protected]>
  • Loading branch information
theihor authored and anakryiko committed Jan 23, 2025
1 parent 7bd06f6 commit 0e39201
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
4 changes: 3 additions & 1 deletion tests/prepare-bt-script.awk
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
}

END {
if (has_contents)
if (has_contents) {
printf("uretprobe:%s:main { exit(); }\n", OUTPUT "/" TEST);
printf("END { printf(\"DONE!\\n\"); }\n");
}
}
58 changes: 33 additions & 25 deletions tests/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,44 @@ if [ -s "$TEST_BTSCRIPT" ]; then
bt_pid=$!
bt_pgid="$(ps -opgid= "$bt_pid" | tr -d ' ')"

dump_bt_output() {
echo "BPFTRACE SCRIPT:"
cat "$TEST_BTSCRIPT"
echo "BPFTRACE OUTPUT:"
cat "$TEST_BTOUT_RAW"
}

wait_for_bpftrace() {
local bt_start=$(date +%s)
local stop_word="$1"
local msg="$2"
while true; do
local bt_elapsed=$(( $(date +%s) - bt_start ))
if grep -q "$stop_word" "$TEST_BTOUT_RAW"; then
break
elif [ "$bt_elapsed" -ge "$TIMEOUT" ]; then
sudo kill -KILL -$bt_pgid 2>/dev/null
echo "BPFTRACE ${msg} TIMEOUT!"
dump_bt_output
exit 1
elif ! kill -s 0 "$bt_pid"; then
echo "BPFTRACE ${msg} FAILURE!"
dump_bt_output
exit 1
else
sleep 0.2
fi
done
}

# wait for bpftrace to finish attachment
bt_start=$(date +%s)
while true; do
bt_elapsed=$(( $(date +%s) - bt_start ))
if grep -q "STARTED!" "$TEST_BTOUT_RAW"; then
break
elif [ "$bt_elapsed" -ge "$TIMEOUT" ]; then
sudo kill -KILL -$bt_pgid 2>/dev/null
echo "BPFTRACE STARTUP TIMEOUT!"
echo "BPFTRACE SCRIPT:"
cat "$TEST_BTSCRIPT"
echo "BPFTRACE OUTPUT:"
cat "$TEST_BTOUT_RAW"
exit 1
elif ! kill -s 0 "$bt_pid"; then
echo "BPFTRACE STARTUP FAILURE!"
echo "BPFTRACE SCRIPT:"
cat "$TEST_BTSCRIPT"
echo "BPFTRACE OUTPUT:"
cat "$TEST_BTOUT_RAW"
exit 1
else
sleep 0.2
fi
done
wait_for_bpftrace "STARTED!" "STARTUP"

# get test output while bpftrace is attached
$TEST_BIN &>"$TEST_OUT"

sudo kill -INT -$bt_pgid 2>/dev/null
# wait for bpftrace to terminate
wait_for_bpftrace "DONE!" "RUNNING"

$awk '/STARTED!/ {flag=1; next} /DONE!/ {flag=0} flag' $TEST_BTOUT_RAW > $TEST_BTOUT
if ! $awk -f check-match.awk $TEST_BTOUT_SPEC $TEST_BTOUT; then
Expand Down

0 comments on commit 0e39201

Please sign in to comment.