Merge pull request #68 from dracut-mailing-devs/1431698021-16626-3-git-send-email...
[dracut.git] / test / test-functions
blob1f5eed59b64d84d525d3bdf309ee755bcc9bcd5e
1 #!/bin/bash
2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
3 export PATH
5 [[ -e .testdir ]] && . .testdir
6 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
7 TESTDIR=$(mktemp -d --tmpdir="/var/tmp" -t dracut-test.XXXXXX)
8 fi
9 echo "TESTDIR=\"$TESTDIR\"" > .testdir
10 export TESTDIR
12 command -v test_check &>/dev/null || test_check() {
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'; }
25 check_root() {
26 if (( $EUID != 0 )); then
27 SETCOLOR_FAILURE; echo "Tests must be run as root! Please use 'sudo'."; SETCOLOR_NORMAL
28 exit 1
32 while (($# > 0)); do
33 case $1 in
34 --run)
35 check_root
36 echo "TEST RUN: $TEST_DESCRIPTION"
37 test_check && test_run
38 exit $?;;
39 --setup)
40 check_root
41 echo "TEST SETUP: $TEST_DESCRIPTION"
42 test_check && test_setup
43 exit $?;;
44 --clean)
45 echo "TEST CLEANUP: $TEST_DESCRIPTION"
46 test_cleanup
47 rm -fr -- "$TESTDIR"
48 rm -f -- .testdir
49 exit $?;;
50 --all)
51 check_root
52 echo -n "TEST: $TEST_DESCRIPTION ";
53 if ! test_check 2&>test.log ; then
54 SETCOLOR_WARNING
55 echo "[SKIPPED]"
56 SETCOLOR_NORMAL
57 exit 0;
59 if [ "$V" != "1" ]; then
61 test_setup && test_run
62 ret=$?
63 test_cleanup
64 rm -fr -- "$TESTDIR"
65 rm -f -- .testdir
66 exit $ret
67 ) </dev/null >test.log 2>&1
68 else
69 set -o pipefail
71 test_setup && test_run
72 ret=$?
73 test_cleanup
74 rm -fr -- "$TESTDIR"
75 rm -f -- .testdir
76 exit $ret
77 ) </dev/null 2>&1 | tee test.log
79 ret=$?
80 set +o pipefail
81 if [ $ret -eq 0 ]; then
82 rm -- test.log
83 SETCOLOR_SUCCESS
84 echo "[OK]"
85 SETCOLOR_NORMAL
86 else
87 SETCOLOR_FAILURE
88 echo "[FAILED]"
89 SETCOLOR_NORMAL
90 echo "see $(pwd)/test.log"
92 exit $ret;;
93 *) break ;;
94 esac
95 shift
96 done