]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/test-functions
test: for V=2 tail only the last MB of logs
[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}
0be1785a 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
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 52 rm -fr -- "$TESTDIR"
a2dbecfc 53 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
c00f04f5 54 exit $?;;
3b403b32 55 --all)
1f524c45 56 check_root
67f43d21 57 if ! test_check 2&>test${TEST_RUN_ID:+-$TEST_RUN_ID}.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
51d0a545 63 if [[ "$V" == "1" ]]; then
82fe4ea0
HH
64 (
65 test_setup && test_run
66 ret=$?
67 test_cleanup
68 rm -fr -- "$TESTDIR"
a2dbecfc 69 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
82fe4ea0 70 exit $ret
67f43d21 71 ) </dev/null >test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
51d0a545
HH
72 elif [[ "$V" == "2" ]]; then
73 set -o pipefail
74 (
75 test_setup && test_run
76 ret=$?
77 test_cleanup
78 if ((ret!=0)) && [[ -f "$TESTDIR"/server.log ]]; then
79 mv [[ -f "$TESTDIR"/server.log ]] ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
80 fi
81 rm -fr -- "$TESTDIR"
82 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
83 exit $ret
67f43d21 84 ) </dev/null 2>&1 | $basedir/logtee test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
3e1d48fd 85 else
82fe4ea0
HH
86 set -o pipefail
87 (
88 test_setup && test_run
89 ret=$?
90 test_cleanup
827a5b1a 91 if ((ret!=0)) && [[ -f "$TESTDIR"/server.log ]]; then
a2dbecfc 92 mv [[ -f "$TESTDIR"/server.log ]] ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
827a5b1a 93 fi
82fe4ea0 94 rm -fr -- "$TESTDIR"
a2dbecfc 95 rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
82fe4ea0 96 exit $ret
67f43d21 97 ) </dev/null 2>&1 | tee test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
3e1d48fd 98 fi
261d0296 99 ret=$?
3e1d48fd 100 set +o pipefail
c00f04f5 101 if [ $ret -eq 0 ]; then
67f43d21 102 rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
82fe4ea0 103 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[OK]" $COLOR_NORMAL
c00f04f5 104 else
82fe4ea0
HH
105 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
106 if [ "$V" == "2" ]; then
f8cad009 107 tail -c 1048576 $(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
827a5b1a 108 echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
82fe4ea0 109 else
67f43d21 110 echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
82fe4ea0 111 fi
261d0296 112 fi
3273c0a5 113 exit $ret;;
c00f04f5
HH
114 *) break ;;
115 esac
116 shift
117done