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