]> git.ipfire.org Git - thirdparty/dracut.git/blob - test/test-functions
b0c538d955964e41b77e3f78498666e16c1f09bc
[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 command -v test_check &> /dev/null || test_check() {
20 :
21 }
22
23 command -v test_cleanup &> /dev/null || test_cleanup() {
24 :
25 }
26
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'; }
35
36 COLOR_SUCCESS='\033[0;32m'
37 COLOR_FAILURE='\033[0;31m'
38 COLOR_WARNING='\033[0;33m'
39 COLOR_NORMAL='\033[0;39m'
40
41 check_root() {
42 if ((EUID != 0)); then
43 SETCOLOR_FAILURE
44 echo "Tests must be run as root! Please use 'sudo'."
45 SETCOLOR_NORMAL
46 exit 1
47 fi
48 }
49
50 # generate qemu arguments for named raw disks
51 #
52 # qemu_add_drive_args <index> <args> <filename> <id-name> [<bootindex>]
53 #
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
60 #
61 # to be used later with `qemu … "${args[@]}" …`
62 # The <index> variable will be incremented each time the function is called.
63 #
64 # can't be easier than this :-/
65 #
66 # # EXAMPLES
67 # ```
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[@]}"
75 # ```
76 qemu_add_drive_args() {
77 local index=${!1}
78 local file=$3
79 local name=${4:-$index}
80 local size=${5:-0}
81 local bootindex=$6
82
83 if [ "${size}" -ne 0 ]; then
84 dd if=/dev/zero of="${file}" bs=1MiB count="${size}"
85 fi
86
87 eval "${2}"'+=(' \
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}" \
90 ')'
91
92 # shellcheck disable=SC2219
93 let "${1}++"
94 }
95
96 test_marker_reset() {
97 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
98 }
99
100 test_marker_check() {
101 local marker=${1:-dracut-root-block-success}
102 local file=${2:-marker.img}
103
104 grep -U --binary-files=binary -F -m 1 -q "$marker" "$TESTDIR/$file"
105 return $?
106 }
107
108 while (($# > 0)); do
109 case $1 in
110 --run)
111 check_root
112 echo "TEST RUN: $TEST_DESCRIPTION"
113 test_check && test_run
114 exit $?
115 ;;
116 --setup)
117 check_root
118 echo "TEST SETUP: $TEST_DESCRIPTION"
119 test_check && test_setup
120 exit $?
121 ;;
122 --clean)
123 echo "TEST CLEANUP: $TEST_DESCRIPTION"
124 test_cleanup
125 rm -fr -- "$TESTDIR"
126 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
127 exit $?
128 ;;
129 --all)
130 check_root
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"
133 exit 0
134 else
135 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[STARTED]" "$COLOR_NORMAL"
136 fi
137 if [[ $V == "1" ]]; then
138 set -o pipefail
139 (
140 test_setup && test_run
141 ret=$?
142 test_cleanup
143 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
144 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
145 fi
146 rm -fr -- "$TESTDIR"
147 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
148 exit $ret
149 ) < /dev/null 2>&1 | tee "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
150 elif [[ $V == "2" ]]; then
151 set -o pipefail
152 # shellcheck disable=SC2154
153 (
154 test_setup && test_run
155 ret=$?
156 test_cleanup
157 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
158 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
159 fi
160 rm -fr -- "$TESTDIR"
161 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
162 exit $ret
163 ) < /dev/null 2>&1 | "$basedir/logtee" "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
164 else
165 (
166 test_setup && test_run
167 ret=$?
168 test_cleanup
169 rm -fr -- "$TESTDIR"
170 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
171 exit $ret
172 ) < /dev/null > test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
173 fi
174 ret=$?
175 set +o pipefail
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"
179 else
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"
184 else
185 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
186 fi
187 fi
188 exit $ret
189 ;;
190 *) break ;;
191 esac
192 shift
193 done