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