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