]> git.ipfire.org Git - thirdparty/dracut.git/blob - test/test-functions
fix(bluetooth): make bluetooth rules more strict
[thirdparty/dracut.git] / test / test-functions
1 #!/bin/bash
2 PATH=/usr/sbin:/usr/bin:/sbin:/bin
3 export PATH
4
5 # shellcheck disable=SC1090
6 [[ -e .testdir${TEST_RUN_ID:+-$TEST_RUN_ID} ]] && . .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
7 if [[ -z $TESTDIR ]] || [[ ! -d $TESTDIR ]]; then
8 TESTDIR=$(mktemp -d -p "/var/tmp" -t dracut-test.XXXXXX)
9 fi
10 echo "TESTDIR=\"$TESTDIR\"" > .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
11 export TESTDIR
12
13 KVERSION=${KVERSION-$(uname -r)}
14
15 [ -z "$USE_NETWORK" ] && USE_NETWORK="network-legacy"
16
17 if [[ -z $basedir ]]; then basedir="$(realpath ../..)"; fi
18
19 test_dracut() {
20 TEST_DRACUT_ARGS+=" --local --no-hostonly --no-early-microcode --add test --kver $KVERSION"
21
22 # shellcheck disable=SC2162
23 IFS=' ' read -a TEST_DRACUT_ARGS_ARRAY <<< "$TEST_DRACUT_ARGS"
24
25 "$basedir"/dracut.sh "$@" \
26 --kernel-cmdline "panic=1 oops=panic softlockup_panic=1 systemd.crash_reboot selinux=0 console=ttyS0,115200n81 $DEBUGFAIL" \
27 "${TEST_DRACUT_ARGS_ARRAY[@]}" || return 1
28 }
29
30 command -v test_check &> /dev/null || test_check() {
31 :
32 }
33
34 command -v test_cleanup &> /dev/null || test_cleanup() {
35 :
36 }
37
38 # terminal sequence to set color to a 'success' color (currently: green)
39 function SETCOLOR_SUCCESS() { echo -en '\033[0;32m'; }
40 # terminal sequence to set color to a 'failure' color (currently: red)
41 function SETCOLOR_FAILURE() { echo -en '\033[0;31m'; }
42 # terminal sequence to set color to a 'warning' color (currently: yellow)
43 function SETCOLOR_WARNING() { echo -en '\033[0;33m'; }
44 # terminal sequence to reset to the default color.
45 function SETCOLOR_NORMAL() { echo -en '\033[0;39m'; }
46
47 COLOR_SUCCESS='\033[0;32m'
48 COLOR_FAILURE='\033[0;31m'
49 COLOR_WARNING='\033[0;33m'
50 COLOR_NORMAL='\033[0;39m'
51
52 check_root() {
53 if ((EUID != 0)); then
54 SETCOLOR_FAILURE
55 echo "Tests must be run as root! Please use 'sudo'."
56 SETCOLOR_NORMAL
57 exit 1
58 fi
59 }
60
61 # generate qemu arguments for named raw disks
62 #
63 # qemu_add_drive_args <index> <args> <filename> <id-name> [<bootindex>]
64 #
65 # index: name of the index variable (set to 0 at start)
66 # args: name of the argument array variable (set to () at start)
67 # filename: filename of the raw disk image
68 # id-name: name of the disk in /dev/disk/by-id -> /dev/disk/by-id/ata-disk_$name
69 # size: optional file size in MiB (0 implies size is not set)
70 # bootindex: optional bootindex number
71 #
72 # to be used later with `qemu … "${args[@]}" …`
73 # The <index> variable will be incremented each time the function is called.
74 #
75 # can't be easier than this :-/
76 #
77 # # EXAMPLES
78 # ```
79 # declare -a disk_args=()
80 # declare -i disk_index=0
81 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.ext3 root 0 1
82 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/client.img client
83 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk2.img iscsidisk2
84 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk3.img iscsidisk3
85 # qemu "${disk_args[@]}"
86 # ```
87 qemu_add_drive_args() {
88 local index=${!1}
89 local file=$3
90 local name=${4:-$index}
91 local size=${5:-0}
92 local bootindex=$6
93
94 if [ "${size}" -ne 0 ]; then
95 dd if=/dev/zero of="${file}" bs=1MiB count="${size}"
96 fi
97
98 eval "${2}"'+=(' \
99 -drive "if=none,format=raw,file=${file},id=drive-sata${index}" \
100 -device "ide-hd,bus=ide.${index},drive=drive-sata${index},id=sata${index},${bootindex:+bootindex=$bootindex,}model=disk,serial=${name}" \
101 ')'
102
103 # shellcheck disable=SC2219
104 let "${1}++"
105 }
106
107 test_marker_reset() {
108 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
109 }
110
111 test_marker_check() {
112 local marker=${1:-dracut-root-block-success}
113 local file=${2:-marker.img}
114
115 grep -U --binary-files=binary -F -m 1 -q "$marker" "$TESTDIR/$file"
116 return $?
117 }
118
119 while (($# > 0)); do
120 case $1 in
121 --run)
122 check_root
123 echo "TEST RUN: $TEST_DESCRIPTION"
124 test_check && test_run
125 exit $?
126 ;;
127 --setup)
128 check_root
129 echo "TEST SETUP: $TEST_DESCRIPTION"
130 test_check && test_setup
131 exit $?
132 ;;
133 --clean)
134 echo "TEST CLEANUP: $TEST_DESCRIPTION"
135 test_cleanup
136 rm -fr -- "$TESTDIR"
137 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
138 exit $?
139 ;;
140 --all)
141 check_root
142 if ! test_check 2 &> test${TEST_RUN_ID:+-$TEST_RUN_ID}.log; then
143 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_WARNING" "[SKIPPED]" "$COLOR_NORMAL"
144 exit 0
145 else
146 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[STARTED]" "$COLOR_NORMAL"
147 fi
148 if [[ $V == "1" ]]; then
149 set -o pipefail
150 (
151 test_setup && test_run
152 ret=$?
153 test_cleanup
154 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
155 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
156 fi
157 rm -fr -- "$TESTDIR"
158 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
159 exit $ret
160 ) < /dev/null 2>&1 | tee "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
161 elif [[ $V == "2" ]]; then
162 set -o pipefail
163 # shellcheck disable=SC2154
164 (
165 test_setup && test_run
166 ret=$?
167 test_cleanup
168 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
169 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
170 fi
171 rm -fr -- "$TESTDIR"
172 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
173 exit $ret
174 ) < /dev/null 2>&1 | "$basedir/logtee" "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
175 else
176 (
177 test_setup && test_run
178 ret=$?
179 test_cleanup
180 rm -fr -- "$TESTDIR"
181 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
182 exit $ret
183 ) < /dev/null > test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
184 fi
185 ret=$?
186 set +o pipefail
187 if [ $ret -eq 0 ]; then
188 rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
189 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[OK]" "$COLOR_NORMAL"
190 else
191 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
192 if [ "$V" == "2" ]; then
193 tail -c 1048576 "$(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log" "$(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
194 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
195 else
196 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
197 fi
198 fi
199 exit $ret
200 ;;
201 *) break ;;
202 esac
203 shift
204 done