File: test-functionality.sh

package info (click to toggle)
squid 7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,440 kB
  • sloc: cpp: 184,513; ansic: 12,442; sh: 5,688; makefile: 5,247; perl: 2,560; sql: 326; python: 240; awk: 141; sed: 1
file content (235 lines) | stat: -rwxr-xr-x 6,737 bytes parent folder | download
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
#!/bin/sh
#
## Copyright (C) 1996-2025 The Squid Software Foundation and contributors
##
## Squid software is distributed under GPLv2+ license and includes
## contributions from numerous individuals and organizations.
## Please see the COPYING and CONTRIBUTORS files for details.
##

# This script tests a few Squid functionality requirements.
# It is suitable for automated CI and manual testing.

# Default-set and report used environment variables:
# * the root directory for storing test tools and test artifacts.
echo "TMPDIR=${TMPDIR:=${RUNNER_TEMP:-/tmp}}"
# * directory for cloning git repositories containing various test tools
echo "CLONES_DIR=${CLONES_DIR:=$TMPDIR/clones}"
# * directories of cloned repositories
echo "DAFT_DIR=${DAFT_DIR:=$CLONES_DIR/daft}"
echo "SQUID_DAFTS_DIR=${SQUID_DAFTS_DIR:=$CLONES_DIR/squid-dafts}"
echo "SQUID_OVERLORD_DIR=${SQUID_OVERLORD_DIR:=$CLONES_DIR/squid-overlord}"

# print an error message (with special markers recognized by GitHub Actions)
echo_error() {
    echo "::error ::" "$@"
}

# print a warning message (with special markers recognized by GitHub Actions)
echo_warning() {
    echo "::warning ::" "$@"
}

run() {
    echo "running: $@"
    "$@"
}

# Whether the branch has a commit with a message matching the given regex.
# The first argument is the SHA of the primary commit with the desired change.
# We want to find up/backported commits that will have different SHAs and that
# may have minor commit message variations, so that SHA is for reference only.
has_commit_by_message() {
    local commit="$1"
    shift

    if git log --grep "$@" | grep -q ^commit
    then
        return 0;
    fi

    echo "Was looking for branch commit with a message matching the following"
    echo "    regex: " "$@"
    echo "    This code lacks commit $commit or equivalent."
    return 1;
}

clone_repo() {
    local repo_url="$1"
    local destination_dir="$2"

    if test -e $destination_dir
    then
        echo "Skipping already fetched $destination_dir"
    elif run git clone --no-tags --quiet --depth=1 --branch production -- "$repo_url" "$destination_dir"
    then
        if test -e "$destination_dir/package.json"
        then
            ( cd $destination_dir && run npm install --no-audit --no-save ) || return
        fi
    else
        echo_error "Failed to fetch $repo_url into $destination_dir"
        return 1
    fi

    (cd $destination_dir && echo "Using commit `git rev-parse HEAD`")
}

start_overlord() {
    local url=http://localhost:13128/
    local log=$TMPDIR/squid-overlord.log
    if test -e $log && curl -H 'Pop-Version: 4' --no-progress-meter $url/check > /dev/null
    then
        echo "Will reuse squid-overlord service running at $url"
        return 0;
    fi

    # Do not be tempted to simply run `sudo ... overlord.pl`: User nobody will
    # lack read permissions, and sudo will ask for a password.
    sudo -n --background -u nobody perl < $SQUID_OVERLORD_DIR/overlord.pl > $log 2>&1 || return
    echo "Started squid-overlord service at $url"
}

setup_test_tools() {
    echo "::group::Setup test tools"

    clone_repo https://github.com/measurement-factory/daft $DAFT_DIR || return
    clone_repo https://github.com/measurement-factory/squid-dafts $SQUID_DAFTS_DIR || return
    clone_repo https://github.com/measurement-factory/squid-overlord $SQUID_OVERLORD_DIR || return

    if ! test -e $SQUID_DAFTS_DIR/src
    then
        run ln -s `realpath $DAFT_DIR/src` $SQUID_DAFTS_DIR/src || return
    fi
    if ! test -e $SQUID_DAFTS_DIR/node_modules
    then
        run ln -s `realpath $DAFT_DIR/node_modules` $SQUID_DAFTS_DIR/node_modules || return
    fi

    start_overlord || return

    # TODO: Find a good way to end group on (currently fatal) errors as well.
    echo "::endgroup::"
}

# executes a single test after the caller confirms that the test is applicable
run_confirmed_test() {
    local testId="$1"

    local testRunner="$DAFT_DIR/src/cli/daft.js"
    if ! test -e $testRunner
    then
        echo_error "Missing Daft test execution script"
        echo "Expected to find it at $testRunner"
        exit 1;
    fi

    local testsDir="$SQUID_DAFTS_DIR/tests"
    if ! test -d $testsDir
    then
        echo_error "Missing collection of Squid-specific Daft tests"
        echo "Expected to find them in $testsDir/"
        exit 1;
    fi

    local testScript=$testsDir/$testId.js
    if ! test -e $testScript
    then
        echo_error "Unknown test requested: $testId"
        echo "Expected to find it at $testScript"
        return 1;
    fi

    local log="$TMPDIR/$testId.log"

    echo "Running test: $testId"
    local result=undefined
    if $testRunner run $testScript > $log 2>&1
    then
        echo "Test $testId: OK"
        return 0;
    else
        result=$?
    fi

    # TODO: Report skipped tests and ignored failures more prominently. See
    # test-sources.sh for CHECK_OUTCOME_PHRASE tricks (but avoid duplication).
    echo
    echo_error "Test $testId: Failed with exit code $result"
    echo "::group::Test log tail:"
    tail -n 100 $log
    echo "::endgroup::"

    # TODO: Link to the artifact
    echo "See the test log (tailed above) for failure details: $log"
    return $result
}

# executes a single test named by the parameter
run_one_test() {
    local testName=$1

    # convert the given foo-bar test name into a check_foo_bar() function name
    checkFunction=`echo "check_$testName" | sed s/-/_/g`

    if type $checkFunction 1> /dev/null 2> /dev/null
    then
        # a custom test wrapper exists
        $checkFunction
    else
        run_confirmed_test $testName
    fi
}

# executes all of the given tests, providing a summary of their failures
run_tests() {
    local result=0
    local failed_tests=""
    for testName in "$@"
    do
        if run_one_test $testName
        then
            continue;
        else
            result=$?
            failed_tests="$failed_tests $testName"
        fi
    done

    if test -n "$failed_tests"
    then
        echo_error "Failed test(s):$failed_tests"
    fi
    return $result
}

# run the tests named on the command line (if any) or the default tests (otherwise)
main() {

    setup_test_tools || return

    local tests="$@"

    if test -z "$tests"
    then
        local default_tests="
            pconn
            dead-peer
            proxy-update-headers-after-304
            accumulate-headers-after-304
            upgrade-protocols
            cache-response
            proxy-collapsed-forwarding
            hit-revalidation
            busy-restart
            truncated-responses
            malformed-request
        "
        tests="$default_tests"
    fi

    run_tests $tests
}

main "$@"
exit $?