~landscape-charmers/landscape-client/fix-ceph-relation-error

« back to all changes in this revision

Viewing changes to tests/lib/test-helpers.sh

  • Committer: Marco Ceppi
  • Date: 2013-05-29 22:01:20 UTC
  • mfrom: (19.3.11 landscape-client)
  • Revision ID: marco@ceppi.net-20130529220120-5attgp6hl5e6i60j
add-landscape-relation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
set -eu
 
4
 
 
5
# Test setup
 
6
readonly TEST_NAME=$0
 
7
 
 
8
# Test counts. Neither skips or errors are necessarily fatal.
 
9
SKIPS=0
 
10
ERRORS=0
 
11
UNHANDLED=0
 
12
 
 
13
 
 
14
function info() {
 
15
    echo "INFO: $@"
 
16
}
 
17
 
 
18
function ok() {
 
19
    echo "PASS: $@"
 
20
}
 
21
 
 
22
function skip() {
 
23
    echo "SKIP: $@"
 
24
    (( SKIPS +=1 ))
 
25
}
 
26
 
 
27
function error() {
 
28
    echo "FAIL: $@"
 
29
    (( ERRORS += 1 ))
 
30
}
 
31
 
 
32
 
 
33
function start-test() {
 
34
    readonly DATADIR=$(mktemp -d --tmpdir "${TEST_NAME}.XXXXXXX")
 
35
    info "Executing ${TEST_NAME} with results in $DATADIR: $1"
 
36
    setup
 
37
    trap trapped-teardown EXIT TERM INT
 
38
}
 
39
 
 
40
function setup() {
 
41
    # Empty, override in actual test as needed
 
42
    true
 
43
}
 
44
 
 
45
 
 
46
function end-test() {
 
47
    info "Completed test $TEST_NAME"
 
48
    trap - EXIT
 
49
    test-teardown
 
50
}
 
51
 
 
52
 
 
53
function teardown() {
 
54
    # Empty, override in actual test as needed
 
55
    true
 
56
}
 
57
 
 
58
 
 
59
function test-teardown() {
 
60
    teardown
 
61
 
 
62
    if [[ "$ERRORS" -gt 0 ]] ; then
 
63
        TEST_RESULT=1
 
64
    elif [[ "$SKIPS" -gt 0 ]] ; then
 
65
        TEST_RESULT=100
 
66
        touch $DATADIR/passed
 
67
    else 
 
68
        TEST_RESULT=0
 
69
        touch $DATADIR/passed
 
70
    fi
 
71
 
 
72
    if [ -n "$DATADIR" ] ; then
 
73
        if [ -f $DATADIR/passed ]; then
 
74
            info "Passed test results for ${TEST_NAME} in ${DATADIR}, skips=${SKIPS}"
 
75
            # rm -r $DATADIR
 
76
        else
 
77
            info "Failed test results for ${TEST_NAME} in ${DATADIR}, errors=${ERRORS}, skips=${SKIPS}"
 
78
            if [ -f $DATADIR/wget.log ] ; then
 
79
                info "BEGIN wget.log"
 
80
                cat $DATADIR/wget.log
 
81
                info "END wget.log"
 
82
            fi
 
83
        fi
 
84
    fi
 
85
    exit $TEST_RESULT
 
86
}
 
87
 
 
88
 
 
89
function diagnose() {
 
90
    # Unpack the caller info to get some useful diagnostics for
 
91
    # tracing any assertion issues
 
92
    local data=( $(caller 1) )
 
93
    local lineno=${data[0]}
 
94
    local function_name=${data[1]}
 
95
    local script_name=${data[2]}
 
96
    echo "function $function_name at line $lineno ($script_name)"
 
97
}
 
98
 
 
99
function trapped-teardown() {
 
100
    (( UNHANDLED += 1 ))
 
101
    error "Untrapped error in $(diagnose)"
 
102
    test-teardown
 
103
}
 
104
 
 
105
function assert() {
 
106
    # The assert function takes two args, such as:
 
107
    # assert "what we are asserting" "non empty result passes"
 
108
    # If the second arg is empty or undefined, the assertion fails
 
109
    if [[ "$#" -lt 2 || -z "$2" ]] ; then
 
110
        error "$1, assertion failed in $(diagnose)"
 
111
    else
 
112
        ok "$1"
 
113
    fi
 
114
}