]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/test-functions
test: also output server.log on failure
[thirdparty/dracut.git] / test / test-functions
CommitLineData
df42cd3d 1#!/bin/bash
f5b01e3c
HH
2PATH=/sbin:/bin:/usr/sbin:/usr/bin
3export PATH
4
0be1785a
HH
5[[ -e .testdir ]] && . .testdir
6if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
82fe4ea0 7 TESTDIR=$(mktemp -d -p "/var/tmp" -t dracut-test.XXXXXX)
0be1785a
HH
8fi
9echo "TESTDIR=\"$TESTDIR\"" > .testdir
10export TESTDIR
f5b01e3c 11
df42cd3d 12command -v test_check &>/dev/null || test_check() {
82fe4ea0
HH
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
HH
30check_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}
a0af318a 36
c00f04f5
HH
37while (($# > 0)); do
38 case $1 in
3b403b32 39 --run)
1f524c45 40 check_root
261d0296 41 echo "TEST RUN: $TEST_DESCRIPTION"
df42cd3d 42 test_check && test_run
c00f04f5 43 exit $?;;
3b403b32 44 --setup)
1f524c45 45 check_root
261d0296 46 echo "TEST SETUP: $TEST_DESCRIPTION"
df42cd3d 47 test_check && test_setup
c00f04f5 48 exit $?;;
3b403b32 49 --clean)
261d0296
DD
50 echo "TEST CLEANUP: $TEST_DESCRIPTION"
51 test_cleanup
32bd2fbb
HH
52 rm -fr -- "$TESTDIR"
53 rm -f -- .testdir
c00f04f5 54 exit $?;;
3b403b32 55 --all)
1f524c45 56 check_root
df42cd3d 57 if ! test_check 2&>test.log ; then
82fe4ea0 58 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_WARNING "[SKIPPED]" $COLOR_NORMAL
df42cd3d 59 exit 0;
82fe4ea0
HH
60 else
61 echo "TEST: $TEST_DESCRIPTION [STARTED]";
df42cd3d 62 fi
3e1d48fd 63 if [ "$V" != "1" ]; then
82fe4ea0
HH
64 (
65 test_setup && test_run
66 ret=$?
67 test_cleanup
68 rm -fr -- "$TESTDIR"
69 rm -f -- .testdir
70 exit $ret
71 ) </dev/null >test.log 2>&1
3e1d48fd 72 else
82fe4ea0
HH
73 set -o pipefail
74 (
75 test_setup && test_run
76 ret=$?
77 test_cleanup
827a5b1a
HH
78 if ((ret!=0)) && [[ -f "$TESTDIR"/server.log ]]; then
79 mv [[ -f "$TESTDIR"/server.log ]] ./
80 fi
82fe4ea0
HH
81 rm -fr -- "$TESTDIR"
82 rm -f -- .testdir
83 exit $ret
84 ) </dev/null 2>&1 | tee test.log
3e1d48fd 85 fi
261d0296 86 ret=$?
3e1d48fd 87 set +o pipefail
c00f04f5 88 if [ $ret -eq 0 ]; then
82fe4ea0
HH
89 rm -- test.log
90 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[OK]" $COLOR_NORMAL
c00f04f5 91 else
82fe4ea0
HH
92 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
93 if [ "$V" == "2" ]; then
827a5b1a
HH
94 cat $(pwd)/server.log $(pwd)/test.log
95 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
82fe4ea0
HH
96 else
97 echo "see $(pwd)/test.log"
98 fi
261d0296 99 fi
3273c0a5 100 exit $ret;;
c00f04f5
HH
101 *) break ;;
102 esac
103 shift
104done