]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-24.sh
1ff1c3347b2c04c7b78591a3fd58b76bf7733151
[thirdparty/systemd.git] / test / units / testsuite-24.sh
1 #!/usr/bin/env bash
2 #set -ex
3 #set -o pipefail
4
5 NPROC=$(nproc)
6 MAX_QUEUE_SIZE=${NPROC:-2}
7 IFS=$'\n' TEST_LIST=($(ls /usr/lib/systemd/tests/test-*))
8
9 # reset state
10 rm /failed-tests /skipped-tests /skipped
11
12 # Check & report test results
13 # Arguments:
14 # $1: test path
15 # $2: test exit code
16 function report_result() {
17 if [[ $# -ne 2 ]]; then
18 echo >&2 "check_result: missing arguments"
19 exit 1
20 fi
21
22 local name="${1##*/}"
23 local ret=$2
24
25 if [[ $ret -ne 0 && $ret != 77 ]]; then
26 echo "$name failed with $ret"
27 echo "$name" >>/failed-tests
28 {
29 echo "--- $name begin ---"
30 cat "/$name.log"
31 echo "--- $name end ---"
32 } >>/failed
33 elif [[ $ret == 77 ]]; then
34 echo "$name skipped"
35 echo "$name" >>/skipped-tests
36 {
37 echo "--- $name begin ---"
38 cat "/$name.log"
39 echo "--- $name end ---"
40 } >>/skipped
41 else
42 echo "$name OK"
43 echo "$name" >>/testok
44 fi
45
46 systemd-cat echo "--- $name ---"
47 systemd-cat cat "/$name.log"
48 }
49
50 # Associative array for running tasks, where running[test-path]=PID
51 declare -A running=()
52 for task in "${TEST_LIST[@]}"; do
53 # If there's MAX_QUEUE_SIZE running tasks, keep checking the running queue
54 # until one of the tasks finishes, so we can replace it.
55 while [[ ${#running[@]} -ge $MAX_QUEUE_SIZE ]]; do
56 for key in "${!running[@]}"; do
57 if ! kill -0 ${running[$key]} &>/dev/null; then
58 # Task has finished, report its result and drop it from the queue
59 wait ${running[$key]}
60 ec=$?
61 report_result "$key" $ec
62 unset running["$key"]
63 # Break from inner for loop and outer while loop to skip
64 # the sleep below when we find a free slot in the queue
65 break 2
66 fi
67 done
68
69 # Precisely* calculated constant to keep the spinlock from burning the CPU(s)
70 sleep 0.01
71 done
72
73 if [[ -x $task ]]; then
74 log_file="/${task##*/}.log"
75 $task &>"$log_file" &
76 running[$task]=$!
77 fi
78 done
79
80 # Wait for remaining running tasks
81 for key in "${!running[@]}"; do
82 wait ${running[$key]}
83 ec=$?
84 report_result "$key" $ec
85 unset running["$key"]
86 done
87
88 exit 0