summaryrefslogtreecommitdiffstats
path: root/.gitlab-ci/tracie/tests/test.sh
blob: 4d52578c47e2b615e921228fe1e3efb6b7c2aa4c (plain)
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
#!/bin/sh

TRACIE_DIR="$(dirname "$(readlink -f "$0")")/.."
TEST_DIR=""
TEST_EXIT=0

create_repo() {
    repo="$(mktemp -d $TEST_DIR/repo.XXXXXXXXXX)"
    cp -R "$TEST_DIR"/tests/test-data/* "$repo"
    (
     cd "$repo";
     git init -q .;
     git config user.email "me@example.com"
     git config user.name "Me me"
     git lfs track '*.testtrace' > /dev/null;
     git add .;
     git commit -q -a -m 'initial';
    )
    echo $repo
}

destroy_repo() {
    [ -d "$1"/.git ] && rm -rf "$1"
}

assert() {
    if ! $1; then
        echo "Assertion failed:  \"$1\""
        exit 1
    fi
}

run_tracie() {
    # Run tests for the .testtrace types, using the "gl-test-device" and "vk-test-device" device names.
    DEVICE_NAME=gl-test-device CI_PROJECT_DIR="$TEST_DIR" \
        "$TEST_DIR/tracie.sh" "$TEST_DIR/tests/traces.yml" testtrace && \
    DEVICE_NAME=vk-test-device CI_PROJECT_DIR="$TEST_DIR" \
        "$TEST_DIR/tracie.sh" "$TEST_DIR/tests/traces.yml" testtrace
}

cleanup() {
    rm -rf "$TEST_DIR"
}

prepare_for_run() {
    TEST_DIR="$(mktemp -d -t tracie.test.XXXXXXXXXX)"
    # Copy all the tracie scripts to the test dir and later make that the
    # CI_PROJECT_DIR for the run-tests.sh script. This avoids polluting the
    # normal working dir with test result artifacts.
    cp -R "$TRACIE_DIR"/. "$TEST_DIR"
    trap cleanup EXIT
    # Ensure we have a clean environment.
    unset TRACIE_STORE_IMAGES
}

run_test() {
    prepare_for_run
    log=$(mktemp)
    if ($1 > "$log" 2>&1 ;); then
        if [ -t 1 ]; then
            echo "$1: \e[0;32mSuccess\e[0m"
        else
            echo "$1: Success"
        fi
    else
        if [ -t 1 ]; then
            echo "$1: \e[0;31mFail\e[0m"
        else
            echo "$1: Fail"
        fi
        cat "$log"
        TEST_EXIT=1
    fi
    rm "$log"
    cleanup
}

tracie_succeeds_if_all_images_match() {
    repo="$(create_repo)"
    cd "$repo"

    run_tracie
    assert "[ $? = 0 ]"

    destroy_repo "$repo"
}

tracie_fails_on_image_mismatch() {
    repo="$(create_repo)"
    cd "$repo"

    sed -i 's/5efda83854befe0155ff8517a58d5b51/8e0a801367e1714463475a824dab363b/g' \
        "$TEST_DIR/tests/traces.yml"

    run_tracie
    assert "[ $? != 0 ]"

    destroy_repo "$repo"
}

tracie_ignores_unspecified_trace_types() {
    repo="$(create_repo)"
    cd "$repo"

    echo "  - path: trace1/empty.trace" >> "$TEST_DIR/tests/traces.yml"
    echo "    expectations:" >> "$TEST_DIR/tests/traces.yml"
    echo "    - device: gl-test-device" >> "$TEST_DIR/tests/traces.yml"
    echo "      checksum: 000000000000000" >> "$TEST_DIR/tests/traces.yml"
    # For the tests we only scan for the .testtrace type,
    # so the .trace file added below should be ignored.
    echo "empty" > trace1/empty.trace
    git lfs track '*.trace'
    git add trace1
    git commit -a -m 'break'

    run_tracie
    assert "[ $? = 0 ]"

    destroy_repo "$repo"
}

tracie_skips_traces_without_checksum() {
    repo="$(create_repo)"
    cd "$repo"

    echo "  - path: trace1/red.testtrace" >> "$TEST_DIR/tests/traces.yml"
    echo "    expectations:" >> "$TEST_DIR/tests/traces.yml"
    echo "    - device: bla" >> "$TEST_DIR/tests/traces.yml"
    echo "      checksum: 000000000000000" >> "$TEST_DIR/tests/traces.yml"
    # red.testtrace should be skipped, since it doesn't
    # have any checksums for our device
    echo "ff0000ff" > trace1/red.testtrace
    git add trace1
    git commit -a -m 'red'

    run_tracie
    assert "[ $? = 0 ]"

    destroy_repo "$repo"
}

tracie_fails_on_dump_image_error() {
    repo="$(create_repo)"
    cd "$repo"

    # "invalid" should fail to parse as rgba and
    # cause an error
    echo "invalid" > trace1/magenta.testtrace
    git add trace1
    git commit -a -m 'invalid'

    run_tracie
    assert "[ $? != 0 ]"

    destroy_repo "$repo"
}

tracie_stores_only_logs_on_checksum_match() {
    repo="$(create_repo)"
    cd "$repo"

    run_tracie
    assert "[ $? = 0 ]"

    assert "[ -f "$TEST_DIR/results/trace1/test/gl-test-device/magenta.testtrace.log" ]"
    assert "[ -f "$TEST_DIR/results/trace2/test/vk-test-device/olive.testtrace.log" ]"

    assert "[ ! -f "$TEST_DIR/results/trace1/test/gl-test-device/magenta.testtrace-0.png" ]"
    assert "[ ! -f "$TEST_DIR/results/trace2/test/vk-test-device/olive.testtrace-0.png" ]"

    ls -lR "$TEST_DIR"

    destroy_repo "$repo"
}

tracie_stores_images_on_checksum_mismatch() {
    repo="$(create_repo)"
    cd "$repo"

    sed -i 's/5efda83854befe0155ff8517a58d5b51/8e0a801367e1714463475a824dab363b/g' \
        "$TEST_DIR/tests/traces.yml"

    run_tracie
    assert "[ $? != 0 ]"

    assert "[ ! -f "$TEST_DIR/results/trace1/test/gl-test-device/magenta.testtrace-0.png" ]"
    assert "[ -f "$TEST_DIR/results/trace2/test/vk-test-device/olive.testtrace-0.png" ]"

    destroy_repo "$repo"
}

tracie_stores_images_on_request() {
    repo="$(create_repo)"
    cd "$repo"

    (export TRACIE_STORE_IMAGES=1; run_tracie)
    assert "[ $? = 0 ]"

    assert "[ -f "$TEST_DIR/results/trace1/test/gl-test-device/magenta.testtrace-0.png" ]"
    assert "[ -f "$TEST_DIR/results/trace2/test/vk-test-device/olive.testtrace-0.png" ]"

    ls -lR "$TEST_DIR"

    destroy_repo "$repo"
}

run_test tracie_succeeds_if_all_images_match
run_test tracie_fails_on_image_mismatch
run_test tracie_ignores_unspecified_trace_types
run_test tracie_skips_traces_without_checksum
run_test tracie_fails_on_dump_image_error
run_test tracie_stores_only_logs_on_checksum_match
run_test tracie_stores_images_on_checksum_mismatch
run_test tracie_stores_images_on_request

exit $TEST_EXIT