aboutsummaryrefslogtreecommitdiff
path: root/test.sh
blob: 99a78deb59b56265ad29b9b8eea84bfbde07797e (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env bash
set -o pipefail

RTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

source "${RTDIR}"/test_config.sh

get_data_exists() {
    local url
    local response

    url="${test_data_remote}/$1"
    if [[ -z $url ]]; then
        echo "get_data_exists: requires a path relative to $test_data_remote"
        exit 1
    fi

    response=$(curl -s --head "$url" | head -n 1 | awk '{print $2}')
    if (( $response != 200 )); then
        return 1;
    fi
    return 0;
}

get_data() {
    local url
    local filename

    if [[ ! -d "${test_data}" ]]; then
        mkdir -p "${test_data}"
    fi

    url="${test_data_remote}/$1"
    if [[ -z $url ]]; then
        echo "get_data: requires a path relative to $test_data_remote"
        exit 1
    fi

    filename=$(basename $url)
    if ! get_data_exists "$1"; then
        echo "${url}: not found on remote server" >&2
        return 1
    fi

    if ! (cd $test_data && curl -L -O "$url"); then
        echo "Could not download data" >&2
        return 1
    fi

    echo "$test_data/$filename";
}

put_data() {
    local url
    local filename

    url="${test_data_remote}/$2"
    filename="$1"

    if [[ -z $url ]]; then
        echo "put_data: requires a path relative to $test_data_remote"
        return 1
    fi

    if ! curl -s --user "${test_data_remote_auth}" --upload-file "${filename}" "${url}/${filename}"; then
        echo "Could not upload '${filename}' to '${url}'" >&2
        return 1
    fi

    return 0
}

put_result() {
    local src;
    local dest;
    src="$1"
    dest="$2"

    if [[ ! -d "$src" ]]; then
        echo "push_result: source directory does not exist: ${src}" >&2
        return 1
    fi

    if [[ -z "$dest" ]]; then
        echo "push_result: requires a destination relative to ${test_data_remote}" >&2
        return 1
    fi

    for f in $(find "${src}" -type f); do
        echo "Uploading '$f' to '$dest'"
        if ! put_data "$f" "${dest}"; then
            echo "Failed uploading '$src' to '$dest'" >&2
            return 1
        fi
    done;
    return 0
}

read_tests() {
    for f in "${RTDIR}"/tests/test_*; do
        source "$f" || return 0
    done
    return 1
}

export_tests() {
    tests=(
        $(declare -f | cut -d ' ' -f 1 | grep -E '^test_')
    )
    total_passed=0
    total_failed=0
    total_tests=${#tests[@]}
}

run_tests() {
    for (( i=0; i < ${total_tests}; i++ )); do
        just_failed=0
        test_case="${tests[$i]}"
        rm -rf "${test_case}"
        mkdir -p "${test_case}"
        pushd "${test_case}" &>/dev/null
            log_file="output_${test_case}.log"

            /bin/echo -n -e "[$i] ${test_case}... "
            "$test_case" 2>&1 | tee ${log_file} &>/dev/null
            retval=$?

            if [[ "$retval" -ne "0" ]]; then
                echo "FAILED ($retval)"
                (( just_failed++ ))
                (( total_failed++ ))
            else
                echo "PASSED"
                (( total_passed++ ))
            fi

            if [[ ${just_failed} -ne 0 ]] && [[ -s ${log_file} ]]; then
                echo "# OUTPUT:"
                cat "${log_file}"
            fi
        popd &>/dev/null
    done

    # Upload all artifacts in all test directories
    if [[ -n "$test_data_remote_auth" ]]; then
        echo
        for (( i=0; i < ${total_tests}; i++ )); do
            test_case="${tests[i]}"
            put_result ${test_case} ${test_data_upload}
        done
    fi
}

check_runtime() {
    if [[ ! -f "${test_program}" ]]; then
        echo "Tests cannot run without: ${test_program}" >&2
        exit 1
    fi
}

show_stats() {
    printf "\n%d test(s): %d passed, %d failed\n" ${total_tests} ${total_passed} ${total_failed}
}

# main program
check_runtime

if read_tests; then
    echo "Failed to aggregate tests." >&2
    exit 1
fi

export_tests
run_tests
show_stats

if (( total_failed )); then
    exit 1
fi

exit 0