]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/test-functions
fix(test/run-qemu): shellcheck for test/run-qemu
[thirdparty/dracut.git] / test / test-functions
CommitLineData
df42cd3d 1#!/bin/bash
f5b01e3c
HH
2PATH=/sbin:/bin:/usr/sbin:/usr/bin
3export PATH
4
a2dbecfc 5[[ -e .testdir${TEST_RUN_ID:+-$TEST_RUN_ID} ]] && . .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
75d758e8 6if [[ -z $TESTDIR ]] || [[ ! -d $TESTDIR ]]; then
82fe4ea0 7 TESTDIR=$(mktemp -d -p "/var/tmp" -t dracut-test.XXXXXX)
0be1785a 8fi
a2dbecfc 9echo "TESTDIR=\"$TESTDIR\"" > .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
0be1785a 10export TESTDIR
f5b01e3c 11
9a52c3fd
HH
12command -v test_check &> /dev/null || test_check() {
13 :
14}
df42cd3d 15
a0af318a
HH
16# terminal sequence to set color to a 'success' color (currently: green)
17function SETCOLOR_SUCCESS() { echo -en '\033[0;32m'; }
18# terminal sequence to set color to a 'failure' color (currently: red)
19function SETCOLOR_FAILURE() { echo -en '\033[0;31m'; }
20# terminal sequence to set color to a 'warning' color (currently: yellow)
21function SETCOLOR_WARNING() { echo -en '\033[0;33m'; }
22# terminal sequence to reset to the default color.
23function SETCOLOR_NORMAL() { echo -en '\033[0;39m'; }
24
82fe4ea0
HH
25COLOR_SUCCESS='\033[0;32m'
26COLOR_FAILURE='\033[0;31m'
27COLOR_WARNING='\033[0;33m'
28COLOR_NORMAL='\033[0;39m'
29
1f524c45 30check_root() {
75d758e8 31 if ((EUID != 0)); then
9a52c3fd
HH
32 SETCOLOR_FAILURE
33 echo "Tests must be run as root! Please use 'sudo'."
34 SETCOLOR_NORMAL
1f524c45
HH
35 exit 1
36 fi
37}
a0af318a 38
c00f04f5
HH
39while (($# > 0)); do
40 case $1 in
3b403b32 41 --run)
1f524c45 42 check_root
09132c73
HH
43 echo "TEST RUN: $TEST_DESCRIPTION"
44 test_check && test_run
9a52c3fd
HH
45 exit $?
46 ;;
3b403b32 47 --setup)
1f524c45 48 check_root
09132c73
HH
49 echo "TEST SETUP: $TEST_DESCRIPTION"
50 test_check && test_setup
9a52c3fd
HH
51 exit $?
52 ;;
3b403b32 53 --clean)
09132c73
HH
54 echo "TEST CLEANUP: $TEST_DESCRIPTION"
55 test_cleanup
56 rm -fr -- "$TESTDIR"
57 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
9a52c3fd
HH
58 exit $?
59 ;;
3b403b32 60 --all)
1f524c45 61 check_root
9a52c3fd 62 if ! test_check 2 &> test${TEST_RUN_ID:+-$TEST_RUN_ID}.log; then
09132c73 63 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_WARNING "[SKIPPED]" $COLOR_NORMAL
9a52c3fd 64 exit 0
82fe4ea0 65 else
9a52c3fd 66 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[STARTED]" $COLOR_NORMAL
df42cd3d 67 fi
75d758e8 68 if [[ $V == "1" ]]; then
51d0a545
HH
69 set -o pipefail
70 (
09132c73
HH
71 test_setup && test_run
72 ret=$?
73 test_cleanup
9a52c3fd 74 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
31e18286 75 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
51d0a545 76 fi
09132c73
HH
77 rm -fr -- "$TESTDIR"
78 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
79 exit $ret
9a52c3fd 80 ) < /dev/null 2>&1 | tee test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
75d758e8 81 elif [[ $V == "2" ]]; then
82fe4ea0
HH
82 set -o pipefail
83 (
09132c73
HH
84 test_setup && test_run
85 ret=$?
86 test_cleanup
9a52c3fd 87 if ((ret != 0)) && [[ -f "$TESTDIR"/server.log ]]; then
712f471e 88 mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
827a5b1a 89 fi
09132c73
HH
90 rm -fr -- "$TESTDIR"
91 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
92 exit $ret
9a52c3fd 93 ) < /dev/null 2>&1 | $basedir/logtee test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
09132c73
HH
94 else
95 (
96 test_setup && test_run
97 ret=$?
98 test_cleanup
99 rm -fr -- "$TESTDIR"
100 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
101 exit $ret
9a52c3fd 102 ) < /dev/null > test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
3e1d48fd 103 fi
09132c73 104 ret=$?
3e1d48fd 105 set +o pipefail
09132c73 106 if [ $ret -eq 0 ]; then
67f43d21 107 rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
09132c73
HH
108 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[OK]" $COLOR_NORMAL
109 else
110 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
82fe4ea0 111 if [ "$V" == "2" ]; then
09132c73
HH
112 tail -c 1048576 $(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
113 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
82fe4ea0 114 else
09132c73 115 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
82fe4ea0 116 fi
09132c73 117 fi
9a52c3fd
HH
118 exit $ret
119 ;;
c00f04f5
HH
120 *) break ;;
121 esac
122 shift
123done