aboutsummaryrefslogtreecommitdiffstats
path: root/.gitlab-ci/tracie/tests/test.sh
blob: cd0f08e4efdb79ce88ead3be20a2a04fb33cfea5 (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
#!/bin/sh

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

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.
    python3 $TEST_DIR/tracie.py --file $TEST_DIR/tests/traces.yml --device-name gl-test-device && \
    python3 $TEST_DIR/tracie.py --file $TEST_DIR/tests/traces.yml --device-name vk-test-device
}

cleanup() {
    [ "$TEST_DIR" = "/tmp/*" ] && rm -rf "$TEST_DIR"
}

prepare_for_run() {
    TEST_DIR="$(mktemp -d -t tracie.test.XXXXXXXXXX)"
    # Copy all the tracie scripts to the test dir for the run-tests.sh script.
    # This avoids polluting the normal working dir with test result artifacts.
    cp -R "$TRACIE_DIR"/. "$TEST_DIR"
    cd "$TEST_DIR"
    mkdir traces-db
    mv tests/test-data/* traces-db/.
    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() {
    run_tracie
    assert "[ $? = 0 ]"
}

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

    run_tracie
    assert "[ $? != 0 ]"
}

tracie_skips_traces_without_checksum() {
    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" > traces-db/trace1/red.testtrace

    run_tracie
    assert "[ $? = 0 ]"
}

tracie_fails_on_dump_image_error() {
    # "invalid" should fail to parse as rgba and
    # cause an error
    echo "invalid" > traces-db/trace1/magenta.testtrace

    run_tracie
    assert "[ $? != 0 ]"
}

tracie_stores_only_logs_on_checksum_match() {
    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"
}

tracie_stores_images_on_checksum_mismatch() {
    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" ]"
}

tracie_stores_images_on_request() {
    (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"
}

run_test tracie_succeeds_if_all_images_match
run_test tracie_fails_on_image_mismatch
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