diff mbox series

[meta-oe,v2,2/3] ptest-pgxs.bbclass: add class for PGXS extension tests

Message ID 20261202012557.7-3-bbnpreetsingh@gmail.com
State Under Review
Headers show
Series ptest for PGXS extensions | expand

Commit Message

Babanpreet Singh Dec. 2, 2026, 1:25 a.m. UTC
Run a PGXS extension's own pg_regress tests as its ptest, with the
tests and options taken from the extension's Makefile.

AI-Generated: Uses Claude (claude-opus-5-5)
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
v2:
- write the scratch makefile to ${T}
- install a --schedule file under its basename and pass that name to
  pg_regress, so a schedule in a subdirectory is found
- document the fixed database and PTEST_PGXS_CONF in the class header,
  and the whitespace split of REGRESS_OPTS
- pg_regress path kept as ${libdir}/postgresql: in this class ${BPN}
  is the extension's name, not postgresql

 meta-oe/classes/ptest-pgxs.bbclass | 108 +++++++++++++++++++++++++++++
 1 file changed, 108 insertions(+)
 create mode 100644 meta-oe/classes/ptest-pgxs.bbclass
diff mbox series

Patch

diff --git a/meta-oe/classes/ptest-pgxs.bbclass b/meta-oe/classes/ptest-pgxs.bbclass
new file mode 100644
index 0000000000..8e1eda3602
--- /dev/null
+++ b/meta-oe/classes/ptest-pgxs.bbclass
@@ -0,0 +1,108 @@ 
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+
+# Class for running the pg_regress tests of PostgreSQL extensions built
+# with PGXS as ptest
+#
+# The tests run against a temporary server, always in the database
+# contrib_regression: --temp-instance, --temp-config and --dbname in the
+# extension's REGRESS_OPTS are replaced. An extension whose expected
+# output names its own database, or needs other server settings, sets
+# them in PTEST_PGXS_CONF (pg-cron sets cron.database_name this way).
+
+inherit pgxs ptest
+
+PTEST_PGXS_CONF ?= ""
+PTEST_PGXS_CONF[doc] = "Extra postgresql.conf lines for the test server, separated by \\n."
+
+do_install_ptest() {
+	cat > ${T}/ptest-pgxs.mk << 'EOF'
+ptest-pgxs-vars:
+	@echo "REGRESS=$(REGRESS)"
+	@echo "REGRESS_OPTS=$(REGRESS_OPTS)"
+EOF
+	vars=$(oe_runmake -s -C ${B} -f Makefile -f ${T}/ptest-pgxs.mk ptest-pgxs-vars)
+	regress=$(echo "$vars" | sed -n 's/^REGRESS=//p')
+	conf=""
+	opts=""
+	inputdir=.
+	schedules=""
+	# REGRESS_OPTS is split on whitespace, so an option whose value
+	# contains spaces or quotes is not supported
+	for o in $(echo "$vars" | sed -n 's/^REGRESS_OPTS=//p'); do
+		case "$o" in
+		--temp-config=*) conf=$(basename "${o#--temp-config=}") ;;
+		--temp-instance=*|--dbname=*) ;;
+		--inputdir=*) inputdir="${o#--inputdir=}"; opts="$opts $o" ;;
+		--schedule=*)
+			schedules="$schedules ${o#--schedule=}"
+			opts="$opts --schedule=$(basename "${o#--schedule=}")"
+			;;
+		*) opts="$opts $o" ;;
+		esac
+	done
+
+	install -d ${D}${PTEST_PATH}/tests/$inputdir
+	for d in sql expected input output data; do
+		if [ -d ${S}/$inputdir/$d ]; then
+			cp -r ${S}/$inputdir/$d ${D}${PTEST_PATH}/tests/$inputdir/
+		fi
+	done
+	for s in $schedules; do
+		install -m 0644 ${S}/$s ${D}${PTEST_PATH}/tests/
+	done
+
+	: > ${D}${PTEST_PATH}/tests/ptest.conf
+	if [ -n "$conf" ] && [ -f ${S}/$conf ]; then
+		cat ${S}/$conf >> ${D}${PTEST_PATH}/tests/ptest.conf
+	fi
+	if [ -n "${PTEST_PGXS_CONF}" ]; then
+		printf '%b\n' "${PTEST_PGXS_CONF}" >> ${D}${PTEST_PATH}/tests/ptest.conf
+	fi
+
+	printf 'REGRESS="%s"\nREGRESS_OPTS="%s"\nPGBIN="%s"\nPG_REGRESS="%s"\n' \
+		"$regress" "$opts" "${bindir}" \
+		"${libdir}/postgresql/pgxs/src/test/regress/pg_regress" \
+		> ${D}${PTEST_PATH}/ptest-pgxs.vars
+
+	if [ ! -f "${UNPACKDIR}/run-ptest" ]; then
+		cat > ${D}${PTEST_PATH}/run-ptest << 'EOF'
+#!/bin/sh
+
+PTEST_PATH=$(dirname "$(readlink -f "$0")")
+. "$PTEST_PATH/ptest-pgxs.vars"
+NAME=$(basename "$(dirname "$PTEST_PATH")")
+
+WORK=$(mktemp -d /tmp/ptest-pgxs.XXXXXX)
+cp -r "$PTEST_PATH/tests/." "$WORK/"
+chown -R postgres:postgres "$WORK"
+
+LOG="$WORK/pg_regress.log"
+{ su postgres -c "cd $WORK && $PG_REGRESS \
+      --temp-instance=$WORK/tmp_check --temp-config=$WORK/ptest.conf \
+      --bindir=$PGBIN --dbname=contrib_regression \
+      $REGRESS_OPTS $REGRESS" 2>&1; echo $? > "$WORK/rc"; } |
+    tee "$LOG" |
+    sed -n -e 's/^ok [0-9]* *[-+] *\([^ ]*\).*/PASS: \1/p' \
+           -e 's/^not ok [0-9]* *[-+] *\([^ ]*\).*/FAIL: \1/p'
+RC=$(cat "$WORK/rc" 2>/dev/null || echo 2)
+
+if [ "$RC" -ne 0 ]; then
+    if [ "$RC" -ne 1 ] || ! grep -q '^\(not \)\{0,1\}ok [0-9]' "$LOG"; then
+        echo "FAIL: $NAME"
+        cat "$LOG"
+        echo
+    fi
+    [ -f "$WORK/regression.diffs" ] && cat "$WORK/regression.diffs"
+fi
+rm -rf "$WORK"
+[ "$RC" -eq 0 ]
+EOF
+		chmod 0755 ${D}${PTEST_PATH}/run-ptest
+	fi
+}
+
+RDEPENDS:${PN}-ptest += "postgresql postgresql-client postgresql-regress"