From 712f59a93a09a500bd07869acf3e576cbac404ab Mon Sep 17 00:00:00 2001
From: Andrii Nakryiko <andrii@kernel.org>
Date: Tue, 19 Nov 2024 11:46:37 -0800
Subject: [PATCH 1/2] ci: minimal CI testing setup

Add static and shared build and runtime testing.
---
 .github/workflows/test.yml  | 33 +++++++++++++++++++++++++++++++++
 tests/Makefile              |  7 ++++---
 tests/prepare-bt-script.awk |  2 +-
 tests/run_test.sh           | 14 ++++++++++----
 4 files changed, 48 insertions(+), 8 deletions(-)
 create mode 100644 .github/workflows/test.yml

diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000..7322108
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,33 @@
+name: USDT CI
+
+on:
+  push:
+    branches: [ "main" ]
+  pull_request:
+    branches: [ "main" ]
+
+jobs:
+  build:
+
+    runs-on: ubuntu-latest
+
+    steps:
+    - uses: actions/checkout@v4
+
+    - name: Install prerequisites
+      run: |
+        curl -L -o bpftrace https://github.com/bpftrace/bpftrace/releases/download/v0.21.2/bpftrace
+        chmod +x bpftrace
+        ./bpftrace --version
+
+    - name: Build (static)
+      run: make SHARED=0 -C tests -j$(nproc) build
+
+    - name: Build (shared)
+      run: make SHARED=1 -C tests -j$(nproc) build
+
+    - name: Test (static)
+      run: make V=1 BPFTRACE=$(realpath bpftrace) BPFTRACE_TIMEOUT=300 SHARED=0 -C tests test
+
+    - name: Test (shared)
+      run: make V=1 BPFTRACE=$(realpath bpftrace) BPFTRACE_TIMEOUT=300 SHARED=1 -C tests test
diff --git a/tests/Makefile b/tests/Makefile
index 4d27eef..b971d3c 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -34,13 +34,13 @@ endif
 NONTESTS = tester common
 
 TESTS := $(filter-out $(NONTESTS), 								\
-		$(shell ls *.{c,cpp} 2>/dev/null | grep -v '^lib' | 				\
-			${AWK} '{split($$0, p, /[^A-Za-z_]+/); print p[1]}' | 			\
+		$(shell ls *.c *.cpp 2>/dev/null | grep -v '^lib' | 				\
+			${AWK} '{split($$0, p, /[^A-Za-z_]+/); print p[1]}' |			\
 			sort | uniq 								\
 		)										\
 )
 LIBS := $(filter-out $(NONTESTS), 								\
-		$(shell ls lib*.{c,cpp} 2>/dev/null | 						\
+		$(shell ls lib*.c lib*.cpp 2>/dev/null | 					\
 			${AWK} '{split($$0, p, /[^A-Za-z_]+/); print substr(p[1], 4)}' |	\
 			sort | uniq 								\
 		)										\
@@ -59,6 +59,7 @@ clean:
 
 .PHONY: list
 list:
+	$(call msg,TESTS,$@)
 	$(Q)$(foreach test,$(TESTS), $(info $(test)))
 
 .PHONY: build
diff --git a/tests/prepare-bt-script.awk b/tests/prepare-bt-script.awk
index 55eaeb3..8be7af2 100644
--- a/tests/prepare-bt-script.awk
+++ b/tests/prepare-bt-script.awk
@@ -39,7 +39,7 @@
 
 	# Emit corresponding bpftrace probe spec:
 	# U:./test:group:name { printf("%s: some %s fmt %d spec %d\n", probe, str(arg0), (int)arg1, arg2 - 10); }
-	printf("U:%s:%s:%s { printf(\"%s%s:%s: %s\\n\"%s); }\n",
+	printf("usdt:%s:%s:%s { printf(\"%s%s:%s: %s\\n\"%s); }\n",
 	       path, group, name,
 	       probe[1] == "lib" ? "lib:" : "", group, name,
 	       fmt, args == "" ? "" : ", " args);
diff --git a/tests/run_test.sh b/tests/run_test.sh
index ea4389a..49c1d91 100755
--- a/tests/run_test.sh
+++ b/tests/run_test.sh
@@ -7,8 +7,7 @@ if [ "${V:-0}" -eq 1 ]; then
 	set -x
 fi
 
-TIMEOUT=10
-
+bpftrace_timeout=${BPFTRACE_TIMEOUT:-30}
 awk=${AWK:-awk}
 readelf=${READELF:-readelf}
 bpftrace=${BPFTRACE:-bpftrace}
@@ -75,7 +74,7 @@ if [ -s "$TEST_BTSCRIPT" ]; then
 		bt_elapsed=$(( $(date +%s) - bt_start ))
 		if grep -q "STARTED!" "$TEST_BTOUT_RAW"; then
 			break
-		elif [ "$bt_elapsed" -ge "$TIMEOUT" ]; then
+		elif [ "$bt_elapsed" -ge "$bpftrace_timeout" ]; then
 			sudo kill -KILL -$bt_pgid 2>/dev/null
 			echo "BPFTRACE STARTUP TIMEOUT!"
 			echo "BPFTRACE SCRIPT:"
@@ -91,15 +90,22 @@ if [ -s "$TEST_BTSCRIPT" ]; then
 			cat "$TEST_BTOUT_RAW"
 			exit 1
 		else
-			sleep 0.2
+			sleep 1
 		fi
+		echo "ITER"
 	done
 
+	echo "DONE 1"
+
 	# get test output while bpftrace is attached
 	$TEST_BIN &>"$TEST_OUT"
 
+	echo "DONE 2"
+
 	sudo kill -INT -$bt_pgid 2>/dev/null
 
+	echo "DONE 3"
+
 	$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
 		echo "BPFTRACE OUTPUT MISMTACH:"

From 54f5d4105cdb444c8fc9046c533364b225d68932 Mon Sep 17 00:00:00 2001
From: Andrii Nakryiko <andrii@kernel.org>
Date: Tue, 19 Nov 2024 17:13:39 -0800
Subject: [PATCH 2/2] t TMP

---
 .github/workflows/test.yml |  6 +++---
 tests/run_test.sh          | 13 +++++++++++--
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 7322108..423d8d6 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -26,8 +26,8 @@ jobs:
     - name: Build (shared)
       run: make SHARED=1 -C tests -j$(nproc) build
 
-    - name: Test (static)
-      run: make V=1 BPFTRACE=$(realpath bpftrace) BPFTRACE_TIMEOUT=300 SHARED=0 -C tests test
-
     - name: Test (shared)
       run: make V=1 BPFTRACE=$(realpath bpftrace) BPFTRACE_TIMEOUT=300 SHARED=1 -C tests test
+
+    - name: Test (static)
+      run: make V=1 BPFTRACE=$(realpath bpftrace) BPFTRACE_TIMEOUT=300 SHARED=0 -C tests test
diff --git a/tests/run_test.sh b/tests/run_test.sh
index 49c1d91..68da7d9 100755
--- a/tests/run_test.sh
+++ b/tests/run_test.sh
@@ -90,7 +90,8 @@ if [ -s "$TEST_BTSCRIPT" ]; then
 			cat "$TEST_BTOUT_RAW"
 			exit 1
 		else
-			sleep 1
+			sleep 0.2
+			break;
 		fi
 		echo "ITER"
 	done
@@ -98,10 +99,18 @@ if [ -s "$TEST_BTSCRIPT" ]; then
 	echo "DONE 1"
 
 	# get test output while bpftrace is attached
-	$TEST_BIN &>"$TEST_OUT"
+	file $TEST_BIN
+	echo OUTPUT START
+	$TEST_BIN
+	echo OUTPUT END
+	timeout 10s $TEST_BIN 2>&1 | tee "$TEST_OUT"
 
 	echo "DONE 2"
 
+	echo "BTOUT RAW START"
+	cat "$TEST_BTOUT_RAW"
+	echo "BTOUT RAW END"
+
 	sudo kill -INT -$bt_pgid 2>/dev/null
 
 	echo "DONE 3"