diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5255e88707d..19e22458f63 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,7 @@ concurrency: env: DEBIAN_FRONTEND: noninteractive + RUNTEST_JOBS: 8 jobs: @@ -56,7 +57,7 @@ jobs: - name: Test run: | set -x - scripts/rip-environment runtests -p -d + scripts/rip-environment runtests -j $RUNTEST_JOBS -p -d - name: Upload UI smoke screenshots if: always() uses: actions/upload-artifact@v7 @@ -120,7 +121,7 @@ jobs: - name: Test run: | set -x - scripts/rip-environment runtests -p ./tests + scripts/rip-environment runtests -j $RUNTEST_JOBS -p ./tests - name: Verify no untracked or modified files after test run: | .github/scripts/verify-clean-repo.sh @@ -188,7 +189,7 @@ jobs: - name: Test run: | set -x - scripts/rip-environment runtests -p -d + scripts/rip-environment runtests -j $RUNTEST_JOBS -p -d - name: Upload UI smoke screenshots if: always() uses: actions/upload-artifact@v7 @@ -300,7 +301,7 @@ jobs: timeout-minutes: 75 strategy: matrix: - runner: ["ubuntu-24.04", "ubuntu-24.04-arm"] + runner: ["ubuntu-26.04", "ubuntu-26.04-arm"] image: ["debian:bookworm", "debian:trixie", "debian:sid"] include: - allow_fail: False @@ -313,7 +314,19 @@ jobs: image: ${{ matrix.image }} # IPC_OWNER is needed for shmget IPC_CREAT # SYS_ADMIN is needed for shmctl IPC_SET - options: --cap-add=IPC_OWNER --cap-add=SYS_ADMIN + # needed for bwrap parallel tests: + # --security-opt apparmor=unconfined + # --security-opt seccomp=unconfined + # --security-opt systempaths=unconfined + # needed for parallel tests due to an overlay is mounted in /tmp and overlay over overlay fails: + # --tmpfs /tmp:rw + options: >- + --cap-add=IPC_OWNER + --cap-add=SYS_ADMIN + --security-opt apparmor=unconfined + --security-opt seccomp=unconfined + --security-opt systempaths=unconfined + --tmpfs /tmp:rw continue-on-error: ${{ matrix.allow_fail }} steps: - name: Dump GitHub context @@ -359,8 +372,8 @@ jobs: run: | set -x adduser --disabled-password --gecos "" testrunner - chmod 0777 $(find tests/ -type d) # make test dirs world-writable for the testrunner - su -c "./scripts/runtests -p -d ./tests" testrunner + chown -R testrunner:testrunner tests # make test dirs owned by testrunner for parallel execution + su -c "./scripts/runtests -j $RUNTEST_JOBS -p -d ./tests" testrunner - name: Verify no untracked or modified files after test run: | .github/scripts/verify-clean-repo.sh ':(exclude)VERSION' ':(exclude)debian/changelog' @@ -388,7 +401,7 @@ jobs: if-no-files-found: error package-indep: - runs-on: ubuntu-24.04 + runs-on: ubuntu-26.04 timeout-minutes: 75 strategy: matrix: diff --git a/debian/control.top.in b/debian/control.top.in index f08b04739f4..894a747f90b 100644 --- a/debian/control.top.in +++ b/debian/control.top.in @@ -19,6 +19,8 @@ Build-Depends: autoconf, automake, bwidget (>= 1.7), + bubblewrap, + parallel, desktop-file-utils, intltool, libboost-python-dev, diff --git a/scripts/runtests.in b/scripts/runtests.in index 823f9c10401..f18529ce19e 100755 --- a/scripts/runtests.in +++ b/scripts/runtests.in @@ -202,9 +202,101 @@ test_and_remove_shmem() { return $ret } -TMPDIR=$(mktemp -d /tmp/runtest.XXXXXX) -trap 'rm -rf "$TMPDIR"' 0 1 2 3 15 +run_one_test() { + if [ "$#" -ne 1 ]; then + echo "ERROR: run_one_test testfile" + return 1 + fi + testname=$1 + testdir=$(dirname "$testname") + # check if there's a "musthave" file with prerequisites from config.h + if [ -e "$testdir/musthave" ] ; then + # one prerequisite per line + while IFS= read -r prereq ; do + if ! grep --quiet --regexp "^#define HAVE_$prereq.*" "$TOPDIR/src/config.h" ; then + echo "Skipping test for missing prerequisite \"$prereq\": $testdir" 1>&2 + SKIP=$((SKIP + 1)) + return 0 + fi + done < "$testdir/musthave" + fi + # skip test if there's a "skip" file + if [ -e "$testdir/skip" ]; then + if ! [ -x "$testdir/skip" ] || ! "$testdir/skip"; then + echo "Skipping disabled test: $testdir" 1>&2 + SKIP=$((SKIP + 1)) + return 0 + fi + fi + NUM=$((NUM + 1)) + TEST_DIR=$(readlink -f "$testdir") + export TEST_DIR + echo "Running test: $testdir" 1>&2 + if test -n "$SYSTEM_BUILD"; then + # Tell `halcompile` where to install comps + USER_MODULE_DIR=$(readlink -f "$testdir") \ + PATH=$(readlink -f "$testdir"):$PATH \ + run_test "$testname" + else + run_test "$testname" + fi + exitcode=$? + if [ "$exitcode" -ne 0 ]; then + reason="test run exited with $exitcode" + else + if [ -e "$testdir/checkresult" ]; then + "$testdir/checkresult" "$testdir/result" + exitcode=$? + reason="checkresult exited with $exitcode" + elif [ -f "$testdir/expected" ]; then + cmp -s "$testdir/expected" "$testdir/result" + exitcode=$? + reason="result differed from expected" + if [ "$exitcode" -ne 0 ]; then + diff -u "$testdir/expected" "$testdir/result" > "$TMPDIR/diff" + SIZE=$(wc -l < "$TMPDIR/diff") + if [ "$SIZE" -lt 40 ]; then + cat "$TMPDIR/diff" + else + OMIT=$((SIZE-40)) + head -40 "$TMPDIR/diff" + echo "($OMIT more lines omitted)" + fi + fi + else + exitcode=1 + reason="Neither expected nor checkresult existed" + fi + fi + if [ "$exitcode" -ne 0 ]; then + echo "*** $testdir: FAIL: $reason" + if test $PRINT = 1; then + echo "************** result:" + tail -500 "$testdir/result" | sed 's/^/ /' + echo "************** stderr:" + tail -500 "$testdir/stderr" | sed 's/^/ /' + echo "**************" + fi + FAIL=$((FAIL + 1)) + FAIL_NAMES="$FAIL_NAMES"$'\n'"$testdir" + if test $STOP = 1; then + return 0 + fi + else + if [ $NOCLEAN -eq 0 ]; then + rm -f "$testdir/stderr" "$testdir/result" \ + "$testdir"/*.var "$testdir"/*.var.bak + fi + fi + if ! test_and_remove_shmem; then + if [ $? -eq 2 ]; then + # Cannot remove attached segments. Fail hard. + exit 1 + fi + SHMERR=$((SHMERR + 1)) + fi +} run_tests () { if ! test_shmem; then @@ -214,104 +306,112 @@ run_tests () { find "$@" -name test.hal -or -name test.sh -or -name test \ | sort > "$TMPDIR/alltests" - while read -r testname; do - testdir=$(dirname "$testname") - # check if there's a "musthave" file with prerequisites from config.h - if [ -e "$testdir/musthave" ] ; then - # one prerequisite per line - while IFS= read -r prereq ; do - if ! grep --quiet --regexp "^#define HAVE_$prereq.*" "$TOPDIR/src/config.h" ; then - echo "Skipping test for missing prerequisite \"$prereq\": $testdir" 1>&2 - SKIP=$((SKIP + 1)) - continue 3 - fi - done < "$testdir/musthave" + if [ "$PARALLEL" -gt 0 ]; then + #convert relative to full path + while read -r testname; do + realpath -s "$testname" >> "$TMPDIR/alltests_realpath" + done < "$TMPDIR/alltests" + + #ro-bind: / (root) so if any files are written at an unknown location, an error is generated + #dev: add device files + #tmpfs: /tmp /var/tmp /run (No files from host are needed there) + #tmp-overlay: $HOME, $TOPDIR (Files from host are needed but writes are per instance and discarded) + # Withouth $HOME overlay, some tests fail. There are some files created and changed there. + #overlay: Use overlays to gather the results in different folders + # This needs the test path to be a full path + #Isolate: ipc, pid, net, proc + BWRAPOPTS=( + "--ro-bind" "/" "/" + "--dev" "/dev" + "--tmpfs" "/tmp" + "--tmpfs" "/var/tmp" + "--tmpfs" "/run" + "--overlay-src" "$HOME" "--tmp-overlay" "$HOME" + "--overlay-src" "$TOPDIR" "--tmp-overlay" "$TOPDIR" + "--overlay-src" "{//}" "--overlay" "$TMPDIR/result_{#}" "$TMPDIR/work_{#}" "{//}" + "--unshare-ipc" + "--unshare-pid" + "--unshare-net" + "--proc" "/proc" + "--die-with-parent" + ) + CMD="mkdir $TMPDIR/result_{#} ; mkdir $TMPDIR/work_{#} ; bwrap ${BWRAPOPTS[*]} -- scripts/runtests ${WORKER_OPT[*]} -w {}" + + #Disable rtapi_app cpu assignement to an isolated CPU if not explicitely set + #This otherwhise results in all rtapi_app processes running on the same CPU + if [[ ! -v RTAPI_CPU_NUMBER ]]; then + export RTAPI_CPU_NUMBER=-1 fi - # skip test if there's a "skip" file - if [ -e "$testdir/skip" ]; then - if ! [ -x "$testdir/skip" ] || ! "$testdir/skip"; then - echo "Skipping disabled test: $testdir" 1>&2 - SKIP=$((SKIP + 1)) - continue - fi - fi - NUM=$((NUM + 1)) - TEST_DIR=$(readlink -f "$testdir") - export TEST_DIR - echo "Running test: $testdir" 1>&2 - if test -n "$SYSTEM_BUILD"; then - # Tell `halcompile` where to install comps - USER_MODULE_DIR=$(readlink -f "$testdir") \ - PATH=$(readlink -f "$testdir"):$PATH \ - run_test "$testname" - else - run_test "$testname" - fi - exitcode=$? - if [ "$exitcode" -ne 0 ]; then - reason="test run exited with $exitcode" - else - if [ -e "$testdir/checkresult" ]; then - "$testdir/checkresult" "$testdir/result" - exitcode=$? - reason="checkresult exited with $exitcode" - elif [ -f "$testdir/expected" ]; then - cmp -s "$testdir/expected" "$testdir/result" - exitcode=$? - reason="result differed from expected" - if [ "$exitcode" -ne 0 ]; then - diff -u "$testdir/expected" "$testdir/result" > "$TMPDIR/diff" - SIZE=$(wc -l < "$TMPDIR/diff") - if [ "$SIZE" -lt 40 ]; then - cat "$TMPDIR/diff" - else - OMIT=$((SIZE-40)) - head -40 "$TMPDIR/diff" - echo "($OMIT more lines omitted)" - fi - fi - else - exitcode=1 - reason="Neither expected nor checkresult existed" - fi - fi - if [ "$exitcode" -ne 0 ]; then - echo "*** $testdir: FAIL: $reason" - if test $PRINT = 1; then - echo "************** result:" - tail -500 "$testdir/result" | sed 's/^/ /' - echo "************** stderr:" - tail -500 "$testdir/stderr" | sed 's/^/ /' - echo "**************" - fi - FAIL=$((FAIL + 1)) - FAIL_NAMES="$FAIL_NAMES"$'\n'"$testdir" - if test $STOP = 1; then - break - fi - else - if [ $NOCLEAN -eq 0 ]; then - rm -f "$testdir/stderr" "$testdir/result" \ - "$testdir"/*.var "$testdir"/*.var.bak - fi - fi - - if ! test_and_remove_shmem; then - if [ $? -eq 2 ]; then - # Cannot remove attached segments. Fail hard. - exit 1 + + parallel -j "$PARALLEL" "$CMD" < "$TMPDIR/alltests_realpath" 4> "$TMPDIR/testlog" + RET_PARALLEL=$? #Will be nonzero if any command failed + + #Create sumary based on testlog + ALL=$(wc -l < "$TMPDIR/alltests") + SHMERR=$(grep -c SHMERR < "$TMPDIR/testlog") + SUCC=$(grep -c SUCCESS < "$TMPDIR/testlog") + FAIL=$(grep -c FAILED < "$TMPDIR/testlog") + FAIL_NAMES=$(grep FAILED < "$TMPDIR/testlog" | sed 's/ FAILED$//') + SKIP=$(grep -c SKIPPED < "$TMPDIR/testlog") + NUM=$((SUCC + FAIL)) + + #Copy results back from overlay RWSRC + i=1 + while read -r testname; do + testdir=$(dirname "$testname") + if [ -n "$PARALLEL_OUTDIR" ]; then + #Assuming that tests are where there are suposed to be + #This will create long paths if tests are anywhere else + testoutdir="$PARALLEL_OUTDIR"/"${testdir//$TOPDIR\/tests\//}" + else + testoutdir=$testdir fi - SHMERR=$((SHMERR + 1)) + mkdir -p "$testoutdir" + cp -r "$TMPDIR/result_$i"/. "$testoutdir/" + + #Cleanup whiteouts + #A whiteout is a character device with 0/0 device number + #marking a deleted file in the overlay + #See: https://docs.kernel.org/filesystems/overlayfs.html + #These can be safely deleted - this happens when a test run + #creates a file and the next test deletes it in the overlay. + #The following command does not check for id/number but + #no character devices are expected in tests directory + #expect the whiteouts + find "$testoutdir/" -type c -delete + + ((i++)) + done < "$TMPDIR/alltests_realpath" + + echo "Runtest parallel: $NUM tests run, $SUCC successful, $FAIL failed, $SKIP skipped, $SHMERR shmem errors" + #Check consistency + if [ "$ALL" -ne $((SUCC + FAIL + SKIP)) ]; then + echo "Bug: $ALL tests but this does not match $SUCC successful + $FAIL failed + $SKIP skipped" + exit 1; + fi + #Check if failed + if [ "$FAIL" -ne 0 ]; then + echo "Failed:"$'\n'"$FAIL_NAMES" + exit 1; + elif [ "$RET_PARALLEL" -ne 0 ]; then + echo "parallel failed with: $RET_PARALLEL" + exit 1; + else + exit 0; fi - done < "$TMPDIR/alltests" - - SUCC=$((NUM-FAIL)) - echo "Runtest: $NUM tests run, $SUCC successful, $FAIL failed, $SKIP skipped, $SHMERR shmem errors" - if [ "$FAIL" -ne 0 ]; then - echo "Failed: $FAIL_NAMES" - exit 1; else - exit 0; + while read -r testname; do + run_one_test "$testname" + done < "$TMPDIR/alltests" + + SUCC=$((NUM-FAIL)) + echo "Runtest: $NUM tests run, $SUCC successful, $FAIL failed, $SKIP skipped, $SHMERR shmem errors" + if [ "$FAIL" -ne 0 ]; then + echo "Failed: $FAIL_NAMES" + exit 1; + else + exit 0; + fi fi } @@ -336,27 +436,78 @@ Usage: Enable crash dumps: on a crashing test, print a gdb backtrace of the core, collected from wherever the system put it (coredumpctl or a plain-file core_pattern). Off by default; CI passes it. + + $P -j jobs + Run parallel jobs. Not compatible with option -s. + Needs -u if not in "run_in_place" mode. Tests calling + sudo will fail. + + $P -o outdir + With parallel jobs: Define where the test results will be redirected. + To have this option but not running parallel, -j 1 can be used. + + $P -w test-script + Worker mode, used to run parallel jobs. The test result is returned on file descriptor 4. + Do not use this mode except to test the worker mode. 4>&1 can be used to show the result. EOF } +cleanup() { + if [ -d "$TMPDIR" ]; then + chmod -R u+rwx "$TMPDIR" #This is needed to delete the work directory's from overlayfs + rm -rf "$TMPDIR" + fi +} + +TMPDIR=$(mktemp -d /tmp/runtest.XXXXXX) +trap 'cleanup; echo Aborted; exit 1' 1 2 3 15 +trap 'cleanup' 0 + CLEAN_ONLY=0 NOCLEAN=0 STOP=0 PRINT=0 -while getopts cnvsphd opt; do +PARALLEL=0 +PARALLEL_OUTDIR="" +WORKER=0 +WORKER_OPT=() +while getopts cnvsphdj:o:w opt; do case "$opt" in - c) CLEAN_ONLY=1 ;; - n) NOCLEAN=1 ;; - v) VERBOSE=1 ;; - s) STOP=1 ;; - p) PRINT=1 ;; - d) export ENABLE_CRASHDUMPS=1 ;; + c) CLEAN_ONLY=1;; + n) NOCLEAN=1 ; WORKER_OPT+=(-n) ;; + v) VERBOSE=1; WORKER_OPT+=(-v) ;; + s) STOP=1;; + p) PRINT=1; WORKER_OPT+=(-p) ;; + d) export ENABLE_CRASHDUMPS=1; WORKER_OPT+=(-d) ;; + j) PARALLEL="$OPTARG" ;; + o) PARALLEL_OUTDIR="$OPTARG" ;; + w) WORKER=1 ;; h|?) usage; exit 0 ;; *) usage; exit 1 ;; esac done shift $((OPTIND-1)) +if [[ ! "$PARALLEL" =~ ^[0-9]+$ ]]; then + echo "ERROR: -j expects a positive integer, got $PARALLEL" + exit 1; +fi + +if [ "$PARALLEL" -gt 0 ]; then + #bwrap version needs to be >= 0.11.0 + s=$(bwrap --version) + version="${s##* }" + if [ "$(printf '%s\n' "$version" '0.11.0' | sort -V | head -n1)" != "0.11.0" ]; then + echo "WARNING: brwap older than 0.11.0, --overlay-src not supported, falling back to single threaded" + PARALLEL=0; + fi +else + if [ -n "$PARALLEL_OUTDIR" ]; then + echo "ERROR: -o needs -j > 0" + exit 1; + fi +fi + if [ $# -eq 0 ]; then if [ -f test.hal ] || [ -f test.sh ]; then set -- . @@ -367,6 +518,31 @@ fi if [ $CLEAN_ONLY -eq 1 ]; then clean "$@" +elif [ $WORKER -eq 1 ]; then + if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then + echo ERROR: worker mode expects one testfile as argument + exit 1; + fi + if [ "$PARALLEL" -ne 0 ]; then + echo ERROR: worker mode does not take -j argument + exit 1; + fi + run_one_test "$1" + #FD 4 is used in parallel to log test results, so stdout / stderr / fd 3 used above don't interfere + echo -n "$1" >&4 + if [ "$SHMERR" -ne 0 ]; then + echo " SHMERR" >&4 + fi + if [ "$FAIL" -ne 0 ]; then + echo " FAILED" >&4 + exit 1; + elif [ "$SKIP" -ne 0 ]; then + echo " SKIPPED" >&4 + exit 0; + else + echo " SUCCESS" >&4 + exit 0; + fi else run_tests "$@" fi diff --git a/scripts/runtests_flaky b/scripts/runtests_flaky new file mode 100755 index 00000000000..6907deb40bb --- /dev/null +++ b/scripts/runtests_flaky @@ -0,0 +1,135 @@ +#! /bin/bash + +# Copyright 2026 Hannes Diethelm +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of version 2 of the GNU General +# Public License as published by the Free Software Foundation. +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -o pipefail + +usage () { + P=${0##*/} + cat < 0, each runtests run gets cpus_per_job assigned + using taskset. cpus_per_job*parallel_jobs <= nproc + + $P -s sleep_max + Random sleep up to sleep_max before starting runtests + + $P -o testout_dir + Testresults will be stored in testout_dir/$JOB_ID + +Examples: + $P -r128 -j32 -p8 -c4 -s0 2>/dev/null + Good for a 16 core CPU with hyper-threading + 128 testruns + Running 8x parallel each using 32 parallel jobs + Each 32x parallel runtests run has 4 CPU's assigned + No sleep between start + 2>/dev/null: Run messages are not shown, only failures and summary + $P -r128 -j32 -p8 -s15 2>/dev/null + Good for a 16 core CPU with hyper-threading + 128 testruns + Running 8x parallel each using 32 parallel jobs + No cpu assignment + 0...15s random sleep to randomize spread the highest load part + at startup + 2>/dev/null: Run messages are not shown, only failures and summary +EOF +} + +#Default: Should run on a quad core +RUNS=4 +RUNTEST_JOBS=16 +PARALLEL_JOBS=2 +CPUS_PER_JOB=0 +SLEEP_MAX=16 +TESTOUT_DIR=testout +while getopts r:j:p:c:s:o:h opt; do + case "$opt" in + r) RUNS="$OPTARG" ;; + j) RUNTEST_JOBS="$OPTARG" ;; + p) PARALLEL_JOBS="$OPTARG" ;; + c) CPUS_PER_JOB="$OPTARG" ;; + s) SLEEP_MAX="$OPTARG" ;; + o) TESTOUT_DIR="$OPTARG" ;; + h|?) usage; exit 0 ;; + *) usage; exit 1 ;; + esac +done +shift $((OPTIND-1)) + +if [ $((CPUS_PER_JOB*PARALLEL_JOBS)) -gt "$(nproc --all)" ]; then + echo "ERROR: cpus_per_job*parallel_jobs <= nproc but is $((CPUS_PER_JOB*PARALLEL_JOBS)) > $(nproc)" + exit 1 +fi + +#Convert to full path before changing directory +TESTOUT_DIR=$(realpath -s "$TESTOUT_DIR") + +run_job(){ + local JOB_ID=$1 + local JOB_SLOT=$2 + local RUNTEST_JOBS=$3 + local CPUS_PER_JOB=$4 + local SLEEP_MAX=$5 + local TESTOUT_DIR=$6 + + local SLEEP=0 + if [ "$SLEEP_MAX" -gt 0 ]; then + SLEEP=$(shuf -i "0-$SLEEP_MAX" -n 1) + fi + + if [ "$CPUS_PER_JOB" -gt 0 ]; then + RT_CPU_ID=$((CPUS_PER_JOB*(JOB_SLOT-1))) + CPU_RANGE=$((CPUS_PER_JOB*(JOB_SLOT-1)))-$((CPUS_PER_JOB*JOB_SLOT-1)) + echo "Running $JOB_ID sleep $SLEEP RTAI_CPU_NUMBER $RT_CPU_ID taskset $CPU_RANGE" + sleep "$SLEEP" + export RTAPI_CPU_NUMBER=$RT_CPU_ID + taskset -c $CPU_RANGE scripts/runtests -n -o "$TESTOUT_DIR/$JOB_ID" -p -j "$RUNTEST_JOBS" + else + echo "Running $JOB_ID sleep $SLEEP" + sleep "$SLEEP" + unset RTAPI_CPU_NUMBER + scripts/runtests -n -o "$TESTOUT_DIR/$JOB_ID" -p -j "$RUNTEST_JOBS" + fi +} + +export -f run_job + +#Start from top directory +cd "$(dirname "$0")/.." || { echo "Could not change directory to '$(dirname "$0")/..'"; exit 1; } +seq "$RUNS" | parallel -j "$PARALLEL_JOBS" "run_job {} {%} $RUNTEST_JOBS $CPUS_PER_JOB $SLEEP_MAX $TESTOUT_DIR" +exitcode=$? +if [ "$exitcode" -ne 0 ]; then + echo "Failed with $exitcode" + exit "$exitcode" +fi