jdecool/scaleway-runtimepublic Fork
⑂ ci/rickub
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
feat: compress Static Files with zstd 98a3c8fUnverified · on ci/rickub · Jérémy DECOOL · 2d ago
run.sh · 220 lines · 11.2 KBBash Blame HistoryRaw
  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
#!/usr/bin/env bash
#
# End-to-end scenarios for the example application.
# Usage: [SCALEWAY_CRON_SECRET=...] [SCALEWAY_HOST=...] example/e2e/run.sh <base-url>
#   SCALEWAY_HOST: the function's "host", which absolute URLs must carry; localhost, the runtime's default, when unset
#   against the Emulator:   example/e2e/run.sh http://127.0.0.1:8000
#   against the deployment: example/e2e/run.sh "$(terraform -chdir=example/terraform output -raw endpoint)"
# Requires bash, curl and php (used to read JSON and hash files).

set -euo pipefail

base_url=${1:?Usage: $0 <base-url>}
base_url=${base_url%/}
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
failures=0

expect() {
    if [[ "$2" == "$3" ]]; then
        echo "ok   $1"
    else
        echo "FAIL $1"
        echo "       expected: $3"
        echo "       actual:   $2"
        failures=$((failures + 1))
    fi
}

# request <name> <curl args...>: stores headers and body under $tmp/<name>, prints the status code.
request() {
    local name=$1
    shift
    curl -sS -o "$tmp/$name.body" -D "$tmp/$name.headers" -w '%{http_code}' "$@"
}

# field <name> <dotted.path>: prints a field of the JSON body, arrays and objects as JSON.
field() {
    php -r '
        $value = json_decode(file_get_contents($argv[1]), true);
        foreach ($argv[2] === "" ? [] : explode(".", $argv[2]) as $key) {
            $value = is_array($value) ? $value[$key] ?? null : null;
        }
        echo is_string($value) || is_int($value) ? $value : json_encode($value);
    ' "$tmp/$1.body" "$2"
}

# header <name> <header>: prints every value of a response header, one per line.
header() {
    grep -i "^$2:" "$tmp/$1.headers" | cut -d: -f2- | sed 's/^ *//' | tr -d '\r' || true
}

sha256() {
    php -r 'echo hash_file("sha256", $argv[1]);' "$1"
}

echo "Running against $base_url"

status=$(request hello "$base_url/hello?name=Scaleway")
expect "GET /hello status" "$status" 200
expect "GET /hello JSON" "$(field hello message)" "Hello Scaleway!"
expect "GET /hello content type" "$(header hello content-type)" "application/json"

status=$(request query "$base_url/echo?foo=bar&n=42&list%5Bkey%5D=v")
expect "GET /echo status" "$status" 200
expect "GET /echo method" "$(field query method)" GET
expect "GET /echo path" "$(field query path)" /echo
expect "GET /echo query" "$(field query query)" '{"foo":"bar","list":{"key":"v"},"n":"42"}'

status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo")
expect "custom request header" "$(field headers headers.x-custom)" hello
expect "cookies" "$(field headers cookies)" '{"flavor":"chocolate","size":"large"}'
expect "client IP is set" "$(field headers clientIp | grep -cE '^[0-9a-f.:]+$')" 1
expect "HTTPS, as the platform terminates TLS" "$(field headers secure)" true

# Clients can forge these, so the Application must not trust them.
status=$(request forwarded -H 'X-Forwarded-For: 203.0.113.66' -H 'X-Forwarded-Host: spoofed.example' -H 'X-Real-Ip: 203.0.113.66' "$base_url/echo")
expect "forged X-Forwarded-Host ignored" "$(field forwarded uri | grep -c spoofed.example)" 0
expect "forged client IP ignored" "$(field forwarded clientIp | grep -c 203.0.113.66)" 0

# The host comes from the "host" runtime option, never from the client.
host=${SCALEWAY_HOST:-localhost}
status=$(request urls -H 'X-Forwarded-Host: spoofed.example' "$base_url/urls?a=1")
expect "GET /urls status" "$status" 200
expect "absolute URL from the router" "$(field urls absoluteUrl)" "https://$host/hello?name=url"
expect "Request URI" "$(field urls uri)" "https://$host/urls?a=1"
expect "scheme and host" "$(field urls schemeAndHttpHost)" "https://$host"

status=$(request redirect "$base_url/redirect")
expect "redirect to an absolute URL" "$status $(header redirect location)" "302 https://$host/hello?name=redirect"

status=$(request json -X POST -H 'Content-Type: application/json' --data '{"answer":42}' "$base_url/echo")
expect "POST JSON body" "$(field json body)" '{"answer":42}'

status=$(request form --data-urlencode 'title=Hello world' --data-urlencode 'tags[]=a' --data-urlencode 'tags[]=b' "$base_url/echo")
expect "POST form fields" "$(field form form)" '{"title":"Hello world","tags":["a","b"]}'

php -r 'echo random_bytes(4096);' > "$tmp/upload.bin"
status=$(request upload -F title=Report -F "doc=@$tmp/upload.bin;filename=report.bin;type=application/octet-stream" "$base_url/echo")
expect "multipart field" "$(field upload form.title)" Report
expect "multipart file name" "$(field upload files.doc.name)" report.bin
expect "multipart file size" "$(field upload files.doc.size)" 4096
expect "multipart file content" "$(field upload files.doc.sha256)" "$(sha256 "$tmp/upload.bin")"

status=$(request rawbody -X PUT -H 'Content-Type: application/octet-stream' --data-binary "@$tmp/upload.bin" "$base_url/echo")
expect "binary request body" "$(field rawbody bodySha256)" "$(sha256 "$tmp/upload.bin")"
expect "PUT method" "$(field rawbody method)" PUT

status=$(request cookies "$base_url/cookies")
expect "GET /cookies status" "$status" 200
expect "three Set-Cookie headers" "$(header cookies set-cookie | wc -l | tr -d ' ')" 3
expect "encoded cookie value" "$(header cookies set-cookie | grep -c '^second=two%20words;')" 1

status=$(request binary "$base_url/binary")
php -r 'echo implode("", array_map("chr", range(0, 255)));' > "$tmp/expected.bin"
expect "GET /binary status" "$status" 200
expect "binary response body" "$(sha256 "$tmp/binary.body")" "$(sha256 "$tmp/expected.bin")"
expect "binary content type" "$(header binary content-type)" application/octet-stream

status=$(request stream "$base_url/stream")
expect "GET /stream body" "$(cat "$tmp/stream.body")" "$(printf 'chunk %s\n' 1 2 3 4 5)"

request counter1 "$base_url/counter" > /dev/null
request counter2 "$base_url/counter" > /dev/null
if [[ "$(field counter1 pid)" == "$(field counter2 pid)" ]]; then
    expect "Warm Process reuse" "$(field counter2 invocations)" "$(($(field counter1 invocations) + 1))"
else
    echo "note the two /counter Invocations ran in different processes (pids $(field counter1 pid) and $(field counter2 pid))"
fi

status=$(request missing "$base_url/missing")
expect "unknown route" "$status" 404

# Static Files, served by the runtime from public/ before the Application is built.
status=$(request css "$base_url/css/app.css")
expect "GET Static File status" "$status" 200
expect "Static File content type" "$(header css content-type)" text/css
expect "Static File body" "$(sha256 "$tmp/css.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")"
expect "Static File nosniff" "$(header css x-content-type-options)" nosniff
expect "Static File revalidated" "$(header css cache-control)" "public, max-age=0, must-revalidate"
etag=$(header css etag)
status=$(request css304 -H "If-None-Match: $etag" "$base_url/css/app.css")
expect "Static File with a matching If-None-Match" "$status" 304
status=$(request csshead -I "$base_url/css/app.css")
expect "HEAD Static File" "$status $(header csshead content-type)" "200 text/css"

status=$(request gzip -H 'Accept-Encoding: gzip' "$base_url/css/app.css")
expect "precompressed variant encoding" "$(header gzip content-encoding)" gzip
expect "precompressed variant varies" "$(header gzip vary)" Accept-Encoding
expect "precompressed variant body" "$(php -r 'echo hash("sha256", (string) gzdecode(file_get_contents($argv[1])));' "$tmp/gzip.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")"

# zstd is preferred to gzip, and Scaleway's php85 can compress with it too.
status=$(request zstd -H 'Accept-Encoding: gzip, zstd' "$base_url/css/app.css")
expect "zstd variant preferred to gzip" "$(header zstd content-encoding)" zstd
if php -r 'exit(function_exists("zstd_uncompress") ? 0 : 1);'; then
    expect "zstd variant body" "$(php -r 'echo hash("sha256", (string) zstd_uncompress(file_get_contents($argv[1])));' "$tmp/zstd.body")" "$(sha256 "$(dirname "$0")/../public/css/app.css")"
else
    echo "skip zstd variant body: the local php lacks the zstd extension"
fi

status=$(request js "$base_url/js/greeting.js")
expect "JavaScript module content type" "$status $(header js content-type)" "200 text/javascript"
status=$(request png "$base_url/images/logo.png")
expect "image content type" "$status $(header png content-type)" "200 image/png"
expect "image body, base64 decoded by the platform" "$(sha256 "$tmp/png.body")" "$(sha256 "$(dirname "$0")/../public/images/logo.png")"
status=$(request digested "$base_url/assets/app-Ex4mPl3.css")
expect "digested Static File cached for good" "$(header digested cache-control)" "public, max-age=31536000, immutable"

# Both reach the Application, which has no such route.
status=$(request frontcontroller "$base_url/index.php")
expect "the Front Controller is never served as a file" "$status" 404
status=$(request dotenv --path-as-is "$base_url/../.env")
expect "files outside public/ are not served" "$status" 404

status=$(request postroot -X POST -H 'Content-Type: application/json' --data '{"job":"cleanup"}' "$base_url/")
expect "POST / JSON body" "$(field postroot args.job)" cleanup

# A body naming a command runs nothing over HTTP: it is a plain HTTP body.
status=$(request forged -X POST -H 'Content-Type: application/json' \
    --data '{"command":"app:greet","args":{"name":"Mallory"},"secret":"guess"}' "$base_url/")
expect "command without the CRON secret stays HTTP" "$status $(field forged args.command)" "200 app:greet"

# command_body <name> <args JSON>: the arguments a CRON trigger running a command carries.
# The secret goes through the environment and stdin, as command lines are visible to other local users.
command_body() {
    php -r 'echo json_encode(["command" => $argv[1], "args" => json_decode($argv[2]), "secret" => getenv("SCALEWAY_CRON_SECRET")]);' "$1" "$2"
}

if [[ -n "${SCALEWAY_CRON_SECRET:-}" ]]; then
    status=$(command_body app:greet '{"name":"Mallory"}' | request leaked -X POST -H 'Content-Type: application/json' --data-binary @- "$base_url/")
    expect "command with the CRON secret stays HTTP over HTTP" "$status $(field leaked args.command)" "200 app:greet"
fi

# Only the Emulator can trigger a CRON Invocation on demand, on its reserved path.
status=$(request cron -X POST --data '{"job":"cleanup"}' "$base_url/_scaleway/cron")
if [[ "$status" == 404 ]]; then
    echo "skip CRON Invocations: only the Emulator triggers them on demand"
else
    expect "CRON Invocation lands on POST /" "$status $(field cron args.job)" "200 cleanup"

    if [[ -n "${SCALEWAY_CRON_SECRET:-}" ]]; then
        status=$(command_body app:greet '{"name":"Scaleway","--yell":true}' | request command -X POST --data-binary @- "$base_url/_scaleway/cron")
        expect "Command Invocation status" "$status" 200
        expect "Command Invocation output" "$(cat "$tmp/command.body")" "HELLO SCALEWAY!"

        status=$(command_body app:greet '{"--fail":true}' | request failed -X POST --data-binary @- "$base_url/_scaleway/cron")
        expect "failed command status" "$status" 500
        expect "failed command output" "$(grep -c 'Failing on purpose.' "$tmp/failed.body")" 1

        request after "$base_url/hello?name=again" > /dev/null
        expect "HTTP Invocation after commands" "$(field after message)" "Hello again!"
    else
        echo "skip Command Invocations: set SCALEWAY_CRON_SECRET to the function's"
    fi
fi

if ((failures > 0)); then
    echo "$failures check(s) failed"
    exit 1
fi
echo "All checks passed"