]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/test-functions
test(ISCSI): make test-30 use the test dracut modules
[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
af31df4b
HG
19test_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
9a52c3fd
HH
30command -v test_check &> /dev/null || test_check() {
31 :
32}
df42cd3d 33
ae7cd94b
HG
34command -v test_cleanup &> /dev/null || test_cleanup() {
35 :
36}
37
a0af318a
HH
38# terminal sequence to set color to a 'success' color (currently: green)
39function SETCOLOR_SUCCESS() { echo -en '\033[0;32m'; }
40# terminal sequence to set color to a 'failure' color (currently: red)
41function SETCOLOR_FAILURE() { echo -en '\033[0;31m'; }
42# terminal sequence to set color to a 'warning' color (currently: yellow)
43function SETCOLOR_WARNING() { echo -en '\033[0;33m'; }
44# terminal sequence to reset to the default color.
45function SETCOLOR_NORMAL() { echo -en '\033[0;39m'; }
46
82fe4ea0
HH
47COLOR_SUCCESS='\033[0;32m'
48COLOR_FAILURE='\033[0;31m'
49COLOR_WARNING='\033[0;33m'
50COLOR_NORMAL='\033[0;39m'
51
1f524c45 52check_root() {
75d758e8 53 if ((EUID != 0)); then
9a52c3fd
HH
54 SETCOLOR_FAILURE
55 echo "Tests must be run as root! Please use 'sudo'."
56 SETCOLOR_NORMAL
1f524c45
HH
57 exit 1
58 fi
59}
a0af318a 60
2cfd778a
HH
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
f1346763 69# size: optional file size in MiB (0 implies size is not set)
2cfd778a
HH
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
f1346763 81# qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.ext3 root 0 1
2cfd778a
HH
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# ```
87qemu_add_drive_args() {
88 local index=${!1}
89 local file=$3
90 local name=${4:-$index}
f1346763
HG
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
2cfd778a
HH
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
f1346763
HG
107test_marker_reset() {
108 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
109}
110
111test_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
c00f04f5
HH
119while (($# > 0)); do
120 case $1 in
3b403b32 121 --run)
1f524c45 122 check_root
09132c73
HH
123 echo "TEST RUN: $TEST_DESCRIPTION"
124 test_check && test_run
9a52c3fd
HH
125 exit $?
126 ;;
3b403b32 127 --setup)
1f524c45 128 check_root
09132c73
HH
129 echo "TEST SETUP: $TEST_DESCRIPTION"
130 test_check && test_setup
9a52c3fd
HH
131 exit $?
132 ;;
3b403b32 133 --clean)
09132c73
HH
134 echo "TEST CLEANUP: $TEST_DESCRIPTION"
135 test_cleanup
136 rm -fr -- "$TESTDIR"
137 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
9a52c3fd
HH
138 exit $?
139 ;;
3b403b32 140 --all)
1f524c45 141 check_root
9a52c3fd 142 if ! test_check 2 &> test${TEST_RUN_ID:+-$TEST_RUN_ID}.log; then
baa4acd4 143 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_WARNING" "[SKIPPED]" "$COLOR_NORMAL"
9a52c3fd 144 exit 0
82fe4ea0 145 else
baa4acd4 146 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[STARTED]" "$COLOR_NORMAL"
df42cd3d 147 fi
75d758e8 148 if [[ $V == "1" ]]; then
51d0a545
HH
149 set -o pipefail
150 (
09132c73
HH
151 test_setup && test_run
152 ret=$?
153 test_cleanup
9a52c3fd 154 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
31e18286 155 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
51d0a545 156 fi
09132c73
HH
157 rm -fr -- "$TESTDIR"
158 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
159 exit $ret
baa4acd4 160 ) < /dev/null 2>&1 | tee "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
75d758e8 161 elif [[ $V == "2" ]]; then
82fe4ea0 162 set -o pipefail
baa4acd4 163 # shellcheck disable=SC2154
82fe4ea0 164 (
09132c73
HH
165 test_setup && test_run
166 ret=$?
167 test_cleanup
9a52c3fd 168 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
712f471e 169 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
827a5b1a 170 fi
09132c73
HH
171 rm -fr -- "$TESTDIR"
172 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
173 exit $ret
baa4acd4 174 ) < /dev/null 2>&1 | "$basedir/logtee" "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
09132c73
HH
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
9a52c3fd 183 ) < /dev/null > test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
3e1d48fd 184 fi
09132c73 185 ret=$?
3e1d48fd 186 set +o pipefail
09132c73 187 if [ $ret -eq 0 ]; then
67f43d21 188 rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
baa4acd4 189 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[OK]" "$COLOR_NORMAL"
09132c73 190 else
baa4acd4 191 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
82fe4ea0 192 if [ "$V" == "2" ]; then
baa4acd4
HH
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"
82fe4ea0 195 else
09132c73 196 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
82fe4ea0 197 fi
09132c73 198 fi
9a52c3fd
HH
199 exit $ret
200 ;;
c00f04f5
HH
201 *) break ;;
202 esac
203 shift
204done