]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/test-functions
fix(resolve-deps): check the existing file—not the source
[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
2d7508dc
LG
13if [[ -z $basedir ]]; then basedir="$(realpath ../..)"; fi
14
9a52c3fd
HH
15command -v test_check &> /dev/null || test_check() {
16 :
17}
df42cd3d 18
a0af318a
HH
19# terminal sequence to set color to a 'success' color (currently: green)
20function SETCOLOR_SUCCESS() { echo -en '\033[0;32m'; }
21# terminal sequence to set color to a 'failure' color (currently: red)
22function SETCOLOR_FAILURE() { echo -en '\033[0;31m'; }
23# terminal sequence to set color to a 'warning' color (currently: yellow)
24function SETCOLOR_WARNING() { echo -en '\033[0;33m'; }
25# terminal sequence to reset to the default color.
26function SETCOLOR_NORMAL() { echo -en '\033[0;39m'; }
27
82fe4ea0
HH
28COLOR_SUCCESS='\033[0;32m'
29COLOR_FAILURE='\033[0;31m'
30COLOR_WARNING='\033[0;33m'
31COLOR_NORMAL='\033[0;39m'
32
1f524c45 33check_root() {
75d758e8 34 if ((EUID != 0)); then
9a52c3fd
HH
35 SETCOLOR_FAILURE
36 echo "Tests must be run as root! Please use 'sudo'."
37 SETCOLOR_NORMAL
1f524c45
HH
38 exit 1
39 fi
40}
a0af318a 41
2cfd778a
HH
42# generate qemu arguments for named raw disks
43#
44# qemu_add_drive_args <index> <args> <filename> <id-name> [<bootindex>]
45#
46# index: name of the index variable (set to 0 at start)
47# args: name of the argument array variable (set to () at start)
48# filename: filename of the raw disk image
49# id-name: name of the disk in /dev/disk/by-id -> /dev/disk/by-id/ata-disk_$name
50# bootindex: optional bootindex number
51#
52# to be used later with `qemu … "${args[@]}" …`
53# The <index> variable will be incremented each time the function is called.
54#
55# can't be easier than this :-/
56#
57# # EXAMPLES
58# ```
59# declare -a disk_args=()
60# declare -i disk_index=0
61# qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.ext3 root 1
62# qemu_add_drive_args disk_index disk_args "$TESTDIR"/client.img client
63# qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk2.img iscsidisk2
64# qemu_add_drive_args disk_index disk_args "$TESTDIR"/iscsidisk3.img iscsidisk3
65# qemu "${disk_args[@]}"
66# ```
67qemu_add_drive_args() {
68 local index=${!1}
69 local file=$3
70 local name=${4:-$index}
71 local bootindex=$5
72
73 eval "${2}"'+=(' \
74 -drive "if=none,format=raw,file=${file},id=drive-sata${index}" \
75 -device "ide-hd,bus=ide.${index},drive=drive-sata${index},id=sata${index},${bootindex:+bootindex=$bootindex,}model=disk,serial=${name}" \
76 ')'
77
78 # shellcheck disable=SC2219
79 let "${1}++"
80}
81
c00f04f5
HH
82while (($# > 0)); do
83 case $1 in
3b403b32 84 --run)
1f524c45 85 check_root
09132c73
HH
86 echo "TEST RUN: $TEST_DESCRIPTION"
87 test_check && test_run
9a52c3fd
HH
88 exit $?
89 ;;
3b403b32 90 --setup)
1f524c45 91 check_root
09132c73
HH
92 echo "TEST SETUP: $TEST_DESCRIPTION"
93 test_check && test_setup
9a52c3fd
HH
94 exit $?
95 ;;
3b403b32 96 --clean)
09132c73
HH
97 echo "TEST CLEANUP: $TEST_DESCRIPTION"
98 test_cleanup
99 rm -fr -- "$TESTDIR"
100 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
9a52c3fd
HH
101 exit $?
102 ;;
3b403b32 103 --all)
1f524c45 104 check_root
9a52c3fd 105 if ! test_check 2 &> test${TEST_RUN_ID:+-$TEST_RUN_ID}.log; then
baa4acd4 106 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_WARNING" "[SKIPPED]" "$COLOR_NORMAL"
9a52c3fd 107 exit 0
82fe4ea0 108 else
baa4acd4 109 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[STARTED]" "$COLOR_NORMAL"
df42cd3d 110 fi
75d758e8 111 if [[ $V == "1" ]]; then
51d0a545
HH
112 set -o pipefail
113 (
09132c73
HH
114 test_setup && test_run
115 ret=$?
116 test_cleanup
9a52c3fd 117 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
31e18286 118 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
51d0a545 119 fi
09132c73
HH
120 rm -fr -- "$TESTDIR"
121 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
122 exit $ret
baa4acd4 123 ) < /dev/null 2>&1 | tee "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
75d758e8 124 elif [[ $V == "2" ]]; then
82fe4ea0 125 set -o pipefail
baa4acd4 126 # shellcheck disable=SC2154
82fe4ea0 127 (
09132c73
HH
128 test_setup && test_run
129 ret=$?
130 test_cleanup
9a52c3fd 131 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
712f471e 132 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
827a5b1a 133 fi
09132c73
HH
134 rm -fr -- "$TESTDIR"
135 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
136 exit $ret
baa4acd4 137 ) < /dev/null 2>&1 | "$basedir/logtee" "test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
09132c73
HH
138 else
139 (
140 test_setup && test_run
141 ret=$?
142 test_cleanup
143 rm -fr -- "$TESTDIR"
144 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
145 exit $ret
9a52c3fd 146 ) < /dev/null > test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
3e1d48fd 147 fi
09132c73 148 ret=$?
3e1d48fd 149 set +o pipefail
09132c73 150 if [ $ret -eq 0 ]; then
67f43d21 151 rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
baa4acd4 152 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_SUCCESS" "[OK]" "$COLOR_NORMAL"
09132c73 153 else
baa4acd4 154 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
82fe4ea0 155 if [ "$V" == "2" ]; then
baa4acd4
HH
156 tail -c 1048576 "$(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log" "$(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
157 echo -e "TEST: $TEST_DESCRIPTION " "$COLOR_FAILURE" "[FAILED]" "$COLOR_NORMAL"
82fe4ea0 158 else
09132c73 159 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
82fe4ea0 160 fi
09132c73 161 fi
9a52c3fd
HH
162 exit $ret
163 ;;
c00f04f5
HH
164 *) break ;;
165 esac
166 shift
167done