~bjornt/charms/precise/landscape-client/relation-depart-no-register

« back to all changes in this revision

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

  • Committer: David Britton
  • Date: 2013-03-06 21:29:16 UTC
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: dpb@canonical.com-20130306212916-ygm79bya103fz1en
Simple install test, runnable with jitsu test.

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
    if [ -z "$JITSU_LOGDIR" ] ; then
 
35
    readonly DATADIR=$(mktemp -d --tmpdir "${TEST_NAME}.XXXXXXX")
 
36
    else
 
37
    readonly DATADIR=$JITSU_LOGDIR
 
38
    fi
 
39
    info "Executing ${TEST_NAME} with results in $DATADIR: $1"
 
40
    setup
 
41
    trap trapped-teardown EXIT TERM INT
 
42
}
 
43
 
 
44
function setup() {
 
45
    # Empty, override in actual test as needed
 
46
    true
 
47
}
 
48
 
 
49
 
 
50
function end-test() {
 
51
    info "Completed test $TEST_NAME"
 
52
    trap - EXIT
 
53
    test-teardown
 
54
}
 
55
 
 
56
 
 
57
function teardown() {
 
58
    # Empty, override in actual test as needed
 
59
    true
 
60
}
 
61
 
 
62
 
 
63
function test-teardown() {
 
64
    teardown
 
65
 
 
66
    if [[ "$ERRORS" -gt 0 ]] ; then
 
67
        TEST_RESULT=1
 
68
    elif [[ "$SKIPS" -gt 0 ]] ; then
 
69
        TEST_RESULT=100
 
70
        touch $DATADIR/passed
 
71
    else 
 
72
        TEST_RESULT=0
 
73
        touch $DATADIR/passed
 
74
    fi
 
75
 
 
76
    if [ -n "$DATADIR" ] ; then
 
77
        if [ -f $DATADIR/passed ]; then
 
78
            info "Passed test results for ${TEST_NAME} in ${DATADIR}, skips=${SKIPS}"
 
79
            # rm -r $DATADIR
 
80
        else
 
81
            info "Failed test results for ${TEST_NAME} in ${DATADIR}, errors=${ERRORS}, skips=${SKIPS}"
 
82
            if [ -f $DATADIR/wget.log ] ; then
 
83
                info "BEGIN wget.log"
 
84
                cat $DATADIR/wget.log
 
85
                info "END wget.log"
 
86
            fi
 
87
        fi
 
88
    fi
 
89
    exit $TEST_RESULT
 
90
}
 
91
 
 
92
 
 
93
function diagnose() {
 
94
    # Unpack the caller info to get some useful diagnostics for
 
95
    # tracing any assertion issues
 
96
    local data=( $(caller 1) )
 
97
    local lineno=${data[0]}
 
98
    local function_name=${data[1]}
 
99
    local script_name=${data[2]}
 
100
    echo "function $function_name at line $lineno ($script_name)"
 
101
}
 
102
 
 
103
function trapped-teardown() {
 
104
    (( UNHANDLED += 1 ))
 
105
    error "Untrapped error in $(diagnose)"
 
106
    test-teardown
 
107
}
 
108
 
 
109
function assert() {
 
110
    # The assert function takes two args, such as:
 
111
    # assert "what we are asserting" "non empty result passes"
 
112
    # If the second arg is empty or undefined, the assertion fails
 
113
    if [[ "$#" -lt 2 || -z "$2" ]] ; then
 
114
        error "$1, assertion failed in $(diagnose)"
 
115
    else
 
116
        ok "$1"
 
117
    fi
 
118
}