2020-03-04 12:35:06 +03:00
#!/usr/bin/env bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2021-04-09 20:39:41 +03:00
set -eux
set -o pipefail
2018-09-19 08:30:29 +03:00
2019-03-05 15:50:28 +03:00
NPROC = $( nproc)
MAX_QUEUE_SIZE = ${ NPROC :- 2 }
2021-06-21 21:46:44 +03:00
TESTS_GLOB = ${ TESTS_GLOB :- test -* }
mapfile -t TEST_LIST < <( find /usr/lib/systemd/tests/ -maxdepth 1 -type f -name " ${ TESTS_GLOB } " )
2019-03-05 15:50:28 +03:00
2019-12-13 14:34:41 +03:00
# reset state
2021-04-09 20:56:12 +03:00
rm -fv /failed-tests /skipped-tests /skipped
2019-12-13 14:34:41 +03:00
2019-03-05 15:50:28 +03:00
# Check & report test results
# Arguments:
# $1: test path
# $2: test exit code
function report_result( ) {
if [ [ $# -ne 2 ] ] ; then
echo >& 2 "check_result: missing arguments"
exit 1
fi
local name = " ${ 1 ##*/ } "
local ret = $2
if [ [ $ret -ne 0 && $ret != 77 ] ] ; then
echo " $name failed with $ret "
2019-12-10 17:31:09 +03:00
echo " $name " >>/failed-tests
2019-03-05 15:50:28 +03:00
{
echo " --- $name begin --- "
cat " / $name .log "
echo " --- $name end --- "
2019-12-10 17:31:09 +03:00
} >>/failed
2019-03-05 15:50:28 +03:00
elif [ [ $ret = = 77 ] ] ; then
echo " $name skipped "
2019-12-10 17:31:09 +03:00
echo " $name " >>/skipped-tests
2019-03-05 15:50:28 +03:00
{
echo " --- $name begin --- "
cat " / $name .log "
echo " --- $name end --- "
2019-12-10 17:31:09 +03:00
} >>/skipped
2018-09-19 08:30:29 +03:00
else
2019-03-05 15:50:28 +03:00
echo " $name OK "
2019-12-10 17:31:09 +03:00
echo " $name " >>/testok
2019-03-05 15:50:28 +03:00
fi
systemd-cat echo " --- $name --- "
systemd-cat cat " / $name .log "
}
2022-06-17 15:44:49 +03:00
set +x
2019-03-05 15:50:28 +03:00
# Associative array for running tasks, where running[test-path]=PID
declare -A running = ( )
for task in " ${ TEST_LIST [@] } " ; do
# If there's MAX_QUEUE_SIZE running tasks, keep checking the running queue
# until one of the tasks finishes, so we can replace it.
while [ [ ${# running [@] } -ge $MAX_QUEUE_SIZE ] ] ; do
for key in " ${ !running[@] } " ; do
2021-04-09 20:49:32 +03:00
if ! kill -0 " ${ running [ $key ] } " & >/dev/null; then
2019-03-05 15:50:28 +03:00
# Task has finished, report its result and drop it from the queue
2021-04-09 20:56:12 +03:00
wait " ${ running [ $key ] } " && ec = 0 || ec = $?
2019-03-05 15:50:28 +03:00
report_result " $key " $ec
2022-06-17 16:28:17 +03:00
unset " running[ $key ] "
2019-03-05 15:50:28 +03:00
# Break from inner for loop and outer while loop to skip
# the sleep below when we find a free slot in the queue
break 2
fi
done
# Precisely* calculated constant to keep the spinlock from burning the CPU(s)
sleep 0.01
done
if [ [ -x $task ] ] ; then
2022-06-17 15:44:49 +03:00
echo " Executing test ' $task ' "
2019-03-05 15:50:28 +03:00
log_file = " / ${ task ##*/ } .log "
2019-12-10 17:31:09 +03:00
$task & >" $log_file " &
2019-03-05 15:50:28 +03:00
running[ $task ] = $!
2018-09-19 08:30:29 +03:00
fi
2019-03-05 15:50:28 +03:00
done
2018-09-19 08:30:29 +03:00
2019-03-05 15:50:28 +03:00
# Wait for remaining running tasks
for key in " ${ !running[@] } " ; do
2022-06-17 15:44:49 +03:00
echo " Waiting for test ' $key ' to finish "
2021-06-21 14:34:07 +03:00
wait ${ running [ $key ] } && ec = 0 || ec = $?
2019-03-05 15:50:28 +03:00
report_result " $key " $ec
2022-06-17 16:28:17 +03:00
unset " running[ $key ] "
2018-09-19 08:30:29 +03:00
done
2022-06-17 15:44:49 +03:00
set -x
2021-06-21 14:34:07 +03:00
# Test logs are sometimes lost, as the system shuts down immediately after
journalctl --sync
2018-09-19 08:30:29 +03:00
exit 0