]>
git.ipfire.org Git - thirdparty/dracut.git/blob - test/test-functions
b0c538d955964e41b77e3f78498666e16c1f09bc
2 PATH
=/usr
/sbin
:/usr
/bin
:/sbin
:/bin
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
)
10 echo "TESTDIR=\"$TESTDIR\"" > .testdir
${TEST_RUN_ID:+-$TEST_RUN_ID}
13 KVERSION
=${KVERSION-$(uname -r)}
15 [ -z "$USE_NETWORK" ] && USE_NETWORK
="network-legacy"
17 if [[ -z $basedir ]]; then basedir
="$(realpath ../..)"; fi
19 command -v test_check
&> /dev
/null || test_check
() {
23 command -v test_cleanup
&> /dev
/null || test_cleanup
() {
27 # terminal sequence to set color to a 'success' color (currently: green)
28 function SETCOLOR_SUCCESS
() { echo -en '\033[0;32m'; }
29 # terminal sequence to set color to a 'failure' color (currently: red)
30 function SETCOLOR_FAILURE
() { echo -en '\033[0;31m'; }
31 # terminal sequence to set color to a 'warning' color (currently: yellow)
32 function SETCOLOR_WARNING
() { echo -en '\033[0;33m'; }
33 # terminal sequence to reset to the default color.
34 function SETCOLOR_NORMAL
() { echo -en '\033[0;39m'; }
36 COLOR_SUCCESS
='\033[0;32m'
37 COLOR_FAILURE
='\033[0;31m'
38 COLOR_WARNING
='\033[0;33m'
39 COLOR_NORMAL
='\033[0;39m'
42 if ((EUID
!= 0)); then
44 echo "Tests must be run as root! Please use 'sudo'."
50 # generate qemu arguments for named raw disks
52 # qemu_add_drive_args <index> <args> <filename> <id-name> [<bootindex>]
54 # index: name of the index variable (set to 0 at start)
55 # args: name of the argument array variable (set to () at start)
56 # filename: filename of the raw disk image
57 # id-name: name of the disk in /dev/disk/by-id -> /dev/disk/by-id/ata-disk_$name
58 # size: optional file size in MiB (0 implies size is not set)
59 # bootindex: optional bootindex number
61 # to be used later with `qemu … "${args[@]}" …`
62 # The <index> variable will be incremented each time the function is called.
64 # can't be easier than this :-/
68 # declare -a disk_args=()
69 # declare -i disk_index=0
70 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.ext3 root 0 1
71 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/client.img client
72 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk2.img iscsidisk2
73 # qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk3.img iscsidisk3
74 # qemu "${disk_args[@]}"
76 qemu_add_drive_args
() {
79 local name
=${4:-$index}
83 if [ "${size}" -ne 0 ]; then
84 dd if=/dev
/zero of
="${file}" bs
=1MiB count
="${size}"
88 -drive "if=none,format=raw,file=${file},id=drive-sata${index}" \
89 -device "ide-hd,bus=ide.${index},drive=drive-sata${index},id=sata${index},${bootindex:+bootindex=$bootindex,}model=disk,serial=${name}" \
92 # shellcheck disable=SC2219
97 dd if=/dev
/zero of
="$TESTDIR"/marker.img bs
=1MiB count
=1
100 test_marker_check
() {
101 local marker
=${1:-dracut-root-block-success}
102 local file=${2:-marker.img}
104 grep -U --binary-files=binary
-F -m 1 -q "$marker" "$TESTDIR/$file"
112 echo "TEST RUN: $TEST_DESCRIPTION"
113 test_check
&& test_run
118 echo "TEST SETUP: $TEST_DESCRIPTION"
119 test_check
&& test_setup
123 echo "TEST CLEANUP: $TEST_DESCRIPTION"
126 rm -f -- .testdir
${TEST_RUN_ID:+-$TEST_RUN_ID}
131 if ! test_check
2 &> test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
; then
132 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_WARNING" "[SKIPPED]" "$COLOR_NORMAL"
135 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[STARTED]" "$COLOR_NORMAL"
137 if [[ $V == "1" ]]; then
140 test_setup
&& test_run
143 if ((ret
!= 0)) && [[ -f "$TESTDIR"/server.log
]]; then
144 mv "$TESTDIR"/server.log .
/server
${TEST_RUN_ID:+-$TEST_RUN_ID}.log
147 rm -f -- .testdir
${TEST_RUN_ID:+-$TEST_RUN_ID}
149 ) < /dev
/null
2>&1 |
tee "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
150 elif [[ $V == "2" ]]; then
152 # shellcheck disable=SC2154
154 test_setup
&& test_run
157 if ((ret
!= 0)) && [[ -f "$TESTDIR"/server.log
]]; then
158 mv "$TESTDIR"/server.log .
/server
${TEST_RUN_ID:+-$TEST_RUN_ID}.log
161 rm -f -- .testdir
${TEST_RUN_ID:+-$TEST_RUN_ID}
163 ) < /dev
/null
2>&1 |
"$basedir/logtee" "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
166 test_setup
&& test_run
170 rm -f -- .testdir
${TEST_RUN_ID:+-$TEST_RUN_ID}
172 ) < /dev
/null
> test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
2>&1
176 if [ $ret -eq 0 ]; then
177 rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
178 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[OK]" "$COLOR_NORMAL"
180 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
181 if [ "$V" == "2" ]; then
182 tail -c 1048576 "$(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log" "$(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
183 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
185 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"