File: test-functions

package info (click to toggle)
dracut 051-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,952 kB
  • sloc: sh: 23,881; ansic: 4,006; makefile: 367; perl: 241; python: 166; lisp: 2
file content (117 lines) | stat: -rw-r--r-- 4,443 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

[[ -e .testdir${TEST_RUN_ID:+-$TEST_RUN_ID} ]] && . .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
    TESTDIR=$(mktemp -d -p "/var/tmp" -t dracut-test.XXXXXX)
fi
echo "TESTDIR=\"$TESTDIR\"" > .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
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'; }

COLOR_SUCCESS='\033[0;32m'
COLOR_FAILURE='\033[0;31m'
COLOR_WARNING='\033[0;33m'
COLOR_NORMAL='\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${TEST_RUN_ID:+-$TEST_RUN_ID}
            exit $?;;
        --all)
            check_root
            if ! test_check 2&>test${TEST_RUN_ID:+-$TEST_RUN_ID}.log ; then
                echo -e "TEST: $TEST_DESCRIPTION " $COLOR_WARNING "[SKIPPED]" $COLOR_NORMAL
                exit 0;
            else
                echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[STARTED]" $COLOR_NORMAL;
            fi
            if [[ "$V" == "1" ]]; then
                set -o pipefail
                (
                    test_setup && test_run
                    ret=$?
                    test_cleanup
                    if ((ret!=0)) && [[ -f "$TESTDIR"/server.log ]]; then
                        mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
                    fi
                    rm -fr -- "$TESTDIR"
                    rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
                    exit $ret
                ) </dev/null 2>&1 | tee test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
            elif [[ "$V" == "2" ]]; then
                set -o pipefail
                (
                    test_setup && test_run
                    ret=$?
                    test_cleanup
                    if ((ret!=0)) && [[ -f "$TESTDIR"/server.log ]]; then
                        mv "$TESTDIR"/server.log ./server${TEST_RUN_ID:+-$TEST_RUN_ID}.log
                    fi
                    rm -fr -- "$TESTDIR"
                    rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
                    exit $ret
                ) </dev/null 2>&1 | $basedir/logtee test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
            else
                (
                    test_setup && test_run
                    ret=$?
                    test_cleanup
                    rm -fr -- "$TESTDIR"
                    rm -f -- .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
                    exit $ret
                ) </dev/null >test${TEST_RUN_ID:+-$TEST_RUN_ID}.log 2>&1
            fi
            ret=$?
            set +o pipefail
            if [ $ret -eq 0 ]; then
                rm -- test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
                echo -e "TEST: $TEST_DESCRIPTION " $COLOR_SUCCESS "[OK]" $COLOR_NORMAL
            else
                echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
                if [ "$V" == "2" ]; then
                    tail -c 1048576 $(pwd)/server${TEST_RUN_ID:+-$TEST_RUN_ID}.log $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log
                    echo -e "TEST: $TEST_DESCRIPTION " $COLOR_FAILURE "[FAILED]" $COLOR_NORMAL
                else
                    echo "see $(pwd)/test${TEST_RUN_ID:+-$TEST_RUN_ID}.log"
                fi
            fi
            exit $ret;;
        *) break ;;
    esac
    shift
done