Skip to content

Commit f5d6b50

Browse files
committed
ci: minimal testing worklow
Add scripts to setup and run the tests on CI. === Note on bpftrace Using bpftrace -c doesn't 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. In run_test.sh bpftrace terminates at the end of target test program. After that test output is compared. Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
1 parent e09e184 commit f5d6b50

File tree

6 files changed

+101
-31
lines changed

6 files changed

+101
-31
lines changed

.github/scripts/setup.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
BPFTRACE_VERSION=${BPFTRACE_VERSION:-0.22.1}
6+
7+
# Assume sudo in this script
8+
9+
# Install dependencies
10+
apt update && apt install -y make file gawk libfuse2t64
11+
12+
# Download bpftrace release
13+
BIN_DIR=/usr/local/bin
14+
mkdir -p $BIN_DIR
15+
curl -L -o bpftrace https://github.com/bpftrace/bpftrace/releases/download/v${BPFTRACE_VERSION}/bpftrace
16+
chmod +x bpftrace
17+
mv bpftrace $BIN_DIR
18+
bpftrace --version
19+
20+
# mount tracefs to avoid warnings from bpftrace
21+
grep -q tracefs /proc/mounts || mount -t tracefs tracefs /sys/kernel/tracing

.github/workflows/test.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: USDT CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Install prerequisites
18+
run: sudo .github/scripts/setup.sh
19+
20+
- name: Build (static)
21+
run: make SHARED=0 -C tests -j$(nproc) build
22+
23+
- name: Build (shared)
24+
run: make SHARED=1 -C tests -j$(nproc) build
25+
26+
- name: Test (static)
27+
run: make SHARED=0 -C tests test
28+
29+
- name: Test (shared)
30+
run: make SHARED=1 -C tests test
31+

tests/DENYLIST

Whitespace-only changes.

tests/Makefile

+3-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ endif
3838
NONTESTS = tester common
3939

4040
TESTS := $(filter-out $(NONTESTS), \
41-
$(shell ls *.{c,cpp} 2>/dev/null | grep -v '^lib' | \
42-
${AWK} '{split($$0, p, /[^A-Za-z_]+/); print p[1]}' | \
41+
$(shell ls *.c *.cpp 2>/dev/null | grep -v '^lib' | \
42+
${AWK} '{split($$0, p, /[^A-Za-z_]+/); print p[1]}' | \
4343
sort | uniq \
4444
) \
4545
)
4646
LIBS := $(filter-out $(NONTESTS), \
47-
$(shell ls lib*.{c,cpp} 2>/dev/null | \
47+
$(shell ls lib*.c lib*.cpp 2>/dev/null | \
4848
${AWK} '{split($$0, p, /[^A-Za-z_]+/); print substr(p[1], 4)}' | \
4949
sort | uniq \
5050
) \
@@ -128,5 +128,4 @@ $(foreach lib, $(LIBS), $(eval $(call BUILD_RULE_LIB,$(lib))))
128128
$(BUILD_TARGETS): build-%: $(OBJDIR)/%
129129

130130
$(TEST_TARGETS): test-%: build-%
131-
$(call msg,TESTING,$(if $(filter 1,$(SHARED)),shared/,static/)$(patsubst test-%,%,$@))
132131
$(Q)./run_test.sh $(OBJDIR) $(patsubst test-%,%,$@)

tests/prepare-bt-script.awk

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@
3939

4040
# Emit corresponding bpftrace probe spec:
4141
# U:./test:group:name { printf("%s: some %s fmt %d spec %d\n", probe, str(arg0), (int)arg1, arg2 - 10); }
42-
printf("U:%s:%s:%s { printf(\"%s%s:%s: %s\\n\"%s); }\n",
42+
printf("usdt:%s:%s:%s { printf(\"%s%s:%s: %s\\n\"%s); }\n",
4343
path, group, name,
4444
probe[1] == "lib" ? "lib:" : "", group, name,
4545
fmt, args == "" ? "" : ", " args);
4646
}
4747

4848
END {
49-
if (has_contents)
49+
if (has_contents) {
50+
printf("uretprobe:%s:main { exit(); }\n", OUTPUT "/" TEST);
5051
printf("END { printf(\"DONE!\\n\"); }\n");
52+
}
5153
}

tests/run_test.sh

+42-25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ TEST=$2
1818
TEST_BIN="$1/$2"
1919
TEST_LIB="$1/lib$2.so"
2020

21+
TEST_ID=$(realpath --relative-to="$OUTPUT/.." "$TEST_BIN")
22+
23+
if grep -q "$TEST_ID" DENYLIST; then
24+
echo "run_test.sh: SKIPPING $TEST_ID"
25+
exit 0
26+
else
27+
echo "run_test.sh: TESTING $TEST_ID"
28+
fi
29+
2130
TEST_USDTS_SPEC=$TEST_BIN.exp_usdts.txt
2231
TEST_USDTS_OUT=$TEST_BIN.act_usdts.txt
2332
TEST_PRE_SPEC=$TEST_BIN.exp_pre_out.txt
@@ -69,36 +78,44 @@ if [ -s "$TEST_BTSCRIPT" ]; then
6978
bt_pid=$!
7079
bt_pgid="$(ps -opgid= "$bt_pid" | tr -d ' ')"
7180

81+
dump_bt_output() {
82+
echo "BPFTRACE SCRIPT:"
83+
cat "$TEST_BTSCRIPT"
84+
echo "BPFTRACE OUTPUT:"
85+
cat "$TEST_BTOUT_RAW"
86+
}
87+
88+
wait_for_bpftrace() {
89+
local bt_start=$(date +%s)
90+
local stop_word="$1"
91+
local msg="$2"
92+
while true; do
93+
local bt_elapsed=$(( $(date +%s) - bt_start ))
94+
if grep -q "$stop_word" "$TEST_BTOUT_RAW"; then
95+
break
96+
elif [ "$bt_elapsed" -ge "$TIMEOUT" ]; then
97+
sudo kill -KILL -$bt_pgid 2>/dev/null
98+
echo "BPFTRACE ${msg} TIMEOUT!"
99+
dump_bt_output
100+
exit 1
101+
elif ! kill -s 0 "$bt_pid"; then
102+
echo "BPFTRACE ${msg} FAILURE!"
103+
dump_bt_output
104+
exit 1
105+
else
106+
sleep 0.2
107+
fi
108+
done
109+
}
110+
72111
# wait for bpftrace to finish attachment
73-
bt_start=$(date +%s)
74-
while true; do
75-
bt_elapsed=$(( $(date +%s) - bt_start ))
76-
if grep -q "STARTED!" "$TEST_BTOUT_RAW"; then
77-
break
78-
elif [ "$bt_elapsed" -ge "$TIMEOUT" ]; then
79-
sudo kill -KILL -$bt_pgid 2>/dev/null
80-
echo "BPFTRACE STARTUP TIMEOUT!"
81-
echo "BPFTRACE SCRIPT:"
82-
cat "$TEST_BTSCRIPT"
83-
echo "BPFTRACE OUTPUT:"
84-
cat "$TEST_BTOUT_RAW"
85-
exit 1
86-
elif ! kill -s 0 "$bt_pid"; then
87-
echo "BPFTRACE STARTUP FAILURE!"
88-
echo "BPFTRACE SCRIPT:"
89-
cat "$TEST_BTSCRIPT"
90-
echo "BPFTRACE OUTPUT:"
91-
cat "$TEST_BTOUT_RAW"
92-
exit 1
93-
else
94-
sleep 0.2
95-
fi
96-
done
112+
wait_for_bpftrace "STARTED!" "STARTUP"
97113

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

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

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

0 commit comments

Comments
 (0)