aboutsummaryrefslogtreecommitdiff
path: root/tests/setup.sh
blob: 7e38cf9d05219c69b1aca9d6dcbf7d6aaf75e532 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
set -o pipefail

export LOGFILE_STASIS="stasis.log"
export LOGFILE_INDEXER="stasis_indexer.log"
export CHECK_OUTPUT_PATTERNS=()

unset STASIS_SYSCONFDIR
if [ -n "$GITHUB_TOKEN" ] && [ -z "$STASIS_GH_TOKEN"]; then
    export STASIS_GH_TOKEN="$GITHUB_TOKEN"
else
    export STASIS_GH_TOKEN="anonymous"
fi

if [[ -z "$PYTHON_VERSIONS" ]]; then
    PYTHON_VERSIONS=(
        3.10
        3.11
        3.12
    )
fi

setup_script_dir="$(dirname ${BASH_SOURCE[0]})"
export TOPDIR=$(pwd)
export TEST_DATA="$TOPDIR"/data

pushd() {
    command pushd "$@" 1>/dev/null
}

popd() {
    command popd 1>/dev/null
}

WS_DEFAULT="ws/_"
setup_workspace() {
    if [ -z "$1" ]; then
        echo "setup_workspace requires a name argument" >&2
        return 1
    fi
    WORKSPACE="${WS_DEFAULT}$1"
    rm -rf "$WORKSPACE"
    if ! mkdir -p "$WORKSPACE"; then
        echo "directory creation failed. cannot continue" >&2
        return 1;
    fi
    WORKSPACE="$(realpath $WORKSPACE)"
    
    export INSTALL_DIR="$WORKSPACE"/local
    if ! mkdir -p "$INSTALL_DIR"; then
        echo "directory creation failed. cannot continue" >&2
        return 1;
    fi

    export BUILD_DIR="$WORKSPACE"/build
    if ! mkdir -p "$BUILD_DIR"; then
        echo "directory creation failed. cannot continue" >&2
        return 1;
    fi

    pushd "$WORKSPACE"
    export LANG="C"
    export HOME="$WORKSPACE"
    . /etc/profile
}

teardown_workspace() {
    if [ -z "$1" ]; then
        echo "teardown_workspace requires a workspace path" >&2
        return 1
    elif ! [[ ${WS_DEFAULT}$1 =~ ${WS_DEFAULT}.* ]]; then
        echo "$1 is not a valid workspace" >&2
        return 1
    fi
    popd
    clean_up
}

install_stasis() {
    pushd "$BUILD_DIR"
    if ! cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" -DCMAKE_BUILD_TYPE=Debug "${TOPDIR}"/../..; then
        echo "cmake failed" >&2
        return 1
    fi

    if ! make install; then
        echo "make failed" >&2
        return 1
    fi

    export PATH="$INSTALL_DIR/bin:$PATH"
    hash -r
    if ! type -P stasis; then
        echo "stasis program not on PATH" >&2
        return 1
    fi

    if ! type -P stasis_indexer; then
        echo "stasis_indexer program not on PATH" >&2
        return 1
    fi
    popd
}


STASIS_TEST_RESULT_FAIL=0
STASIS_TEST_RESULT_PASS=0
STASIS_TEST_RESULT_SKIP=0
run_command() {
    local logfile="$(mktemp).log"
    local cmd="${@}"
    local lines_on_error=100
    /bin/echo "Testing: $cmd "

    $cmd &>"$logfile"
    code=$?
    if (( code )); then
        if (( code == 127 )); then
            echo "... SKIP"
            (( STASIS_TEST_RESULT_SKIP++ ))
        else
            echo "... FAIL"
            if [[ -s "$logfile" ]]; then
                echo "#"
                echo "# Last $lines_on_error line(s) follow:"
                echo "#"
                tail -n $lines_on_error "$logfile"
            fi
            (( STASIS_TEST_RESULT_FAIL++ ))
        fi
    else
        echo "... PASS"
        (( STASIS_TEST_RESULT_PASS++ ))
    fi
    rm -f "$logfile"
}

run_summary() {
    local total=$(( STASIS_TEST_RESULT_PASS + STASIS_TEST_RESULT_FAIL + STASIS_TEST_RESULT_SKIP))
    echo
    echo "[RT] ${STASIS_TEST_RESULT_PASS} tests passed, ${STASIS_TEST_RESULT_FAIL} failed, ${STASIS_TEST_RESULT_SKIP} skipped out of ${total}"
    echo
}

run_stasis() {
    local logfile="$LOGFILE_STASIS"
    $(type -P stasis) --unbuffered -v $@ 2>&1 | tee "$logfile"
}

run_stasis_indexer() {
    local logfile="$LOGFILE_INDEXER"
    local root="$1"
    if [ -z "$root" ]; then
        echo "run_stasis_indexer root directory cannot be empty" >&2
        exit 1
    fi
    $(type -P stasis_indexer) --web --unbuffered -v "$root"/* 2>&1 | tee "$logfile"
}

check_output_add() {
    local pattern="$1"
    CHECK_OUTPUT_PATTERNS+=("$pattern")
}

check_output_reset() {
    CHECK_OUTPUT_PATTERNS=()
}

check_output_stasis_dir() {
    local retcode=0
    local startdir="$1"
    local logfile="$LOGFILE_STASIS"

    echo "#### Files ####"
    find $startdir | sort
    echo

    echo "#### Contents ####"
    files=$(find $startdir -type f \( -name "$logfile" -o -name '*.yml' -o -name '*.md' -o -name '*.stasis' -o -name '*.ini' \) | sort)
    for x in $files; do
        echo
        echo "FILENAME: $x"
        echo
        if [ "$x" == "$logfile" ]; then
            # do not print thousands of lines of output we _just_ sat through
            echo "Output omitted"
        else
            cat "$x"
            echo "[EOF]"
        fi
        echo

        for cond in "${CHECK_OUTPUT_PATTERNS[@]}"; do
            if grep --color -H -n "$cond" "$x" >&2; then
                echo "ERROR DETECTED IN $x!" >&2
                retcode+=1
            fi
        done
    done

    if (( retcode )); then
        return 1
    else
        return 0
    fi
}

check_output_indexed_dir() {
    local retcode=0
    local startdir="$1"
    local logfile="$2"

    echo "#### Files ####"
    find $startdir | sort

    for cond in "${CHECK_OUTPUT_PATTERNS[@]}"; do
        if grep --color -H -n "$cond" "$logfile" >&2; then
            echo "ERROR DETECTED IN INDEX OPERATION!" >&2
            retcode+=1
        fi
    done

    echo "#### Contents ####"
    files=$(find $startdir -type f \( -name '*.html' \) | sort)
    for x in $files; do
        echo
        echo "FILENAME: $x"
        echo
        cat "$x"
        echo "[EOF]"
        echo
    done
    if (( retcode )); then
        return 1
    else
        return 0
    fi
}

assert_eq() {
    local a="$1"
    local b="$2"
    local msg="$3"
    if [[ "$a" == "$b" ]]; then
        return 0
    else
        [[ -n "$msg" ]] && echo "'$a' != '$b' :: $msg" >&2
        return 1
    fi
}

assert_file_contains() {
    local file="$1"
    local str="$2"
    local msg="$3"
    if grep -E "$str" "$file" &>/dev/null; then
        return 0
    else
        [[ -n "$msg" ]] && echo "'$str' not in file '$file' :: $msg" >&2
        return 1
    fi
}

clean_up_docker() {
    CONTAINERS_DIR="$WORKSPACE/.local/share/containers"
    # HOME points to the WORKSPACE. The only reason we'd have this directory is if docker/podman was executed
    # Fair to assume if the directory exists, docker/podman is functional.
    if [ -d "$CONTAINERS_DIR" ]; then
        docker run --rm -it -v $CONTAINERS_DIR:/data alpine sh -c '/bin/rm -r -f /data/*'
    fi
}

clean_up() {
    if ([ -z "$RT_KEEP_WORKSPACE" ] || [ -z "$KEEP_WORKSPACE" ]) && [ -d "$WORKSPACE" ]; then
        clean_up_docker
        rm -rf "$WORKSPACE"
    fi

    run_summary
    if (( STASIS_TEST_RESULT_FAIL )); then
        exit 1
    else
        exit 0
    fi
}