summaryrefslogtreecommitdiff
path: root/test/test-functions
blob: 7bdfed28d6f7d6326558ad2d38752dda58d7afe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

[[ -e .testdir ]] && . .testdir
if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
   TESTDIR=$(mktemp -d --tmpdir="/var/tmp" -t dracut-test.XXXXXX)
fi
echo "TESTDIR=\"$TESTDIR\"" > .testdir
export TESTDIR

command -v test_check &>/dev/null || test_check() {
    :
}

# terminal sequence to set color to a 'success' color (currently: green)
function SETCOLOR_SUCCESS() { echo -en '\033[0;32m'; }
# terminal sequence to set color to a 'failure' color (currently: red)
function SETCOLOR_FAILURE() { echo -en '\033[0;31m'; }
# terminal sequence to set color to a 'warning' color (currently: yellow)
function SETCOLOR_WARNING() { echo -en '\033[0;33m'; }
# terminal sequence to reset to the default color.
function SETCOLOR_NORMAL() { echo -en '\033[0;39m'; }

check_root() {
    if (( $EUID != 0 )); then
        SETCOLOR_FAILURE; echo "Tests must be run as root! Please use 'sudo'."; SETCOLOR_NORMAL
        exit 1
    fi
}

while (($# > 0)); do
    case $1 in
        --run)
            check_root
	    echo "TEST RUN: $TEST_DESCRIPTION"
	    test_check && test_run
	    exit $?;;
        --setup)
            check_root
	    echo "TEST SETUP: $TEST_DESCRIPTION"
	    test_check && test_setup
	    exit $?;;
        --clean)
	    echo "TEST CLEANUP: $TEST_DESCRIPTION"
	    test_cleanup
	    rm -fr -- "$TESTDIR"
	    rm -f -- .testdir
	    exit $?;;
        --all)
            check_root
	    echo -n "TEST: $TEST_DESCRIPTION ";
            if ! test_check 2&>test.log ; then
                SETCOLOR_WARNING
                echo "[SKIPPED]"
                SETCOLOR_NORMAL
		exit 0;
            fi
            if [ "$V" != "1" ]; then
	    (
		test_setup && test_run
		ret=$?
		test_cleanup
		rm -fr -- "$TESTDIR"
		rm -f -- .testdir
		exit $ret
	    ) </dev/null >test.log 2>&1
            else
            set -o pipefail
            (
		test_setup && test_run
		ret=$?
		test_cleanup
		rm -fr -- "$TESTDIR"
		rm -f -- .testdir
		exit $ret
	    ) </dev/null 2>&1 | tee test.log
            fi
	    ret=$?
            set +o pipefail
	    if [ $ret -eq 0 ]; then
               rm -- test.log
               SETCOLOR_SUCCESS
	       echo "[OK]"
               SETCOLOR_NORMAL
	    else
               SETCOLOR_FAILURE
	       echo "[FAILED]"
               SETCOLOR_NORMAL
	       echo "see $(pwd)/test.log"
	    fi
	    exit $ret;;
        *) break ;;
    esac
    shift
done