Repository navigation
Expand file tree
/
Copy pathrun-web.sh
More file actions
executable file
·154 lines (142 loc) · 6.75 KB
/
Copy pathrun-web.sh
File metadata and controls
executable file
·154 lines (142 loc) · 6.75 KB
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
#!/bin/sh
# Build fpp at a given ref, serve its web UI from Docker, and run the
# Playwright suite in web/ against it from this machine.
#
# ./run-web.sh [--fpp DIR] [--ref REF] [--flavor asan|tsan|debug|optimized|coverage] [--nopch] [--port N] [--artifacts DIR] [playwright args...]
#
# The options are run.sh's; --port is where the UI is published on 127.0.0.1
# (default 8080), and --artifacts where the results go (default
# artifacts/web/), emptied first. Anything else goes to `playwright test`, e.g.
# ./run-web.sh tests/fpp.spec.ts -g "Unsaved"
#
# --serve only serves the UI, until Ctrl-C, for writing a test against it:
# ./run-web.sh --serve then, in web/: npx playwright test --ui
#
# fppd behind the UI is the same build the scenarios use (ASan by default), and
# is held to the same standard: the run fails if fppd died, wedged, exited
# non-zero on SIGTERM or wrote a sanitizer report while the pages drove it.
#
# Needs Node.js 20+ here; the first run installs the suite's packages and its
# Chromium. Results go to the artifacts: junit.xml, the HTML report, failed
# tests' screenshots and traces, fppd.json (fppd's verdict) and, when fppd
# failed, its media directory and Apache's logs. --flavor coverage also writes
# coverage/ (scripts/coverage-report.sh): the fppd code the pages reached.
#
# --www-coverage writes coverage/www/ (scripts/www-coverage-report.py), with any
# flavor: the PHP and the JS under www/ the suite ran. Only tests that take the
# page from fixtures.ts count towards the JS.
set -e
HERE=$(cd "$(dirname "$0")" && pwd)
FPP="$HERE/../fpp"
REF=HEAD
FLAVOR=asan
PORT=${FPP_TESTS_WEB_PORT:-8080}
SERVE=
WWW_COVERAGE=
ARTIFACTS="$HERE/artifacts/web"
while [ $# -gt 0 ]; do
case "$1" in
--fpp) FPP="$2"; shift 2 ;;
--ref) REF="$2"; shift 2 ;;
--flavor) FLAVOR="$2"; shift 2 ;;
--port) PORT="$2"; shift 2 ;;
--nopch) export FPP_TESTS_NOPCH=require; shift ;;
--serve) SERVE=1; shift ;;
--www-coverage) WWW_COVERAGE=1; shift ;;
--artifacts) ARTIFACTS="$2"; shift 2 ;;
*) break ;;
esac
done
# Absolute, as Docker mounts it: a relative --artifacts is from where this ran.
mkdir -p "$ARTIFACTS"
ARTIFACTS=$(cd "$ARTIFACTS" && pwd)
. "$HERE/scripts/prepare-tree.sh"
docker build -q -t fpp-tests-web "$HERE/docker/web" > /dev/null
# Emptied through a container: the web container runs as root, and a run that
# stopped before handing its files back (below) leaves them owned by root,
# e.g. apache2/, which the next run (and verify-fix.sh's second) can't delete.
# As in run.sh, a failure here is another run sharing the directory, and
# doesn't stop this one.
$DOCKER -v "$ARTIFACTS:/artifacts" fpp-tests-env find /artifacts -mindepth 1 -delete ||
echo "warning: could not empty $ARTIFACTS; is another run using it? (--artifacts DIR)" >&2
# Node 20 or later (the header's "Needs Node.js 20+"); a spec written on a newer
# Node can lean on what 20 lacks (global WebSocket arrived in 22), so the
# version goes in the log.
NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)
if [ "$NODE_MAJOR" -lt 20 ]; then
echo "error: run-web.sh needs Node.js 20 or later (found: $(node --version 2>/dev/null || echo none))" >&2
exit 1
fi
# Packages first (a checkout from before typescript was added installs again),
# then the specs' type check, so a broken spec fails before the fpp build.
if [ ! -d "$HERE/web/node_modules" ] || [ ! -d "$HERE/web/node_modules/typescript" ]; then
(cd "$HERE/web" && npm ci --no-audit --no-fund && npx playwright install chromium)
fi
echo "Type-checking the specs (node $(node --version))"
(cd "$HERE/web" && npx tsc --noEmit -p .) || { echo "error: the specs do not type-check" >&2; exit 1; }
"$HERE/scripts/lint-web-specs.sh" || { echo "error: the spec lint failed" >&2; exit 1; }
$DOCKER -v "$HERE:/tests:ro" fpp-tests-env /tests/scripts/build-fpp.sh "$TREE" "$FLAVOR"
WWW_ENV=
if [ -n "$WWW_COVERAGE" ]; then
# Written by the container's root and the host's Playwright.
mkdir -p "$ARTIFACTS/coverage/www/raw/php" "$ARTIFACTS/coverage/www/raw/js" "$ARTIFACTS/coverage/www/raw/js-src"
WWW_ENV="-e FPP_TESTS_WWW_COVERAGE=1"
export FPP_TESTS_WWW_COVERAGE_DIR="$ARTIFACTS/coverage/www/raw"
fi
# Not --rm: the container's exit code is fppd's verdict, read after it stops.
NAME="fpp-tests-web-$$"
make_test_network
trap 'docker rm -f "$NAME" > /dev/null 2>&1 || true; remove_test_network' EXIT
${DOCKER%%--rm*}${DOCKER#*--rm} -d --init --name "$NAME" --network "$NETWORK" -p "127.0.0.1:$PORT:80" \
-v "$HERE:/tests:ro" -v "$ARTIFACTS:/artifacts" \
-e FPP_TREE="$TREE" -e FPP_TESTS_ARTIFACTS=/artifacts \
--cap-add SYS_PTRACE --cap-add SYS_NICE \
--tmpfs /tmp:rw,exec,nosuid,size=4g \
$WWW_ENV \
fpp-tests-web python3 /tests/scripts/web_serve.py > /dev/null
echo "waiting for the web UI on 127.0.0.1:$PORT"
i=0
until docker logs "$NAME" 2>&1 | grep -q '^READY$'; do
if [ "$(docker inspect -f '{{.State.Running}}' "$NAME")" != true ]; then
docker logs "$NAME" 2>&1 | tail -40
echo "the web container exited before it was ready" >&2
exit 1
fi
i=$((i + 1))
if [ $i -gt 300 ]; then
docker logs "$NAME" 2>&1 | tail -40
echo "the web UI was not ready within 5 minutes" >&2
exit 1
fi
sleep 1
done
if [ -n "$SERVE" ]; then
echo "serving fpp $DESCRIBE at http://127.0.0.1:$PORT/ until Ctrl-C"
echo "(container $NAME: FPP_TESTS_WEB_CONTAINER=$NAME for the tests that need it)"
trap 'docker stop -t 120 "$NAME" > /dev/null; docker rm -f "$NAME" > /dev/null 2>&1; remove_test_network; exit 0' INT TERM
docker wait "$NAME" > /dev/null
exit 1
fi
rc=0
# FPP_TESTS_WEB_CONTAINER: for the tests that act from inside the player.
(cd "$HERE/web" && PLAYWRIGHT_BASE_URL="http://127.0.0.1:$PORT" FPP_TESTS_WEB_ARTIFACTS="$ARTIFACTS" \
FPP_TESTS_WEB_CONTAINER="$NAME" npx playwright test "$@") || rc=$?
# SIGTERM: web_serve.py stops fppd and judges it; give an ASan fppd time.
docker stop -t 120 "$NAME" > /dev/null
fppd_rc=$(docker wait "$NAME")
if [ "$FLAVOR" = coverage ]; then
$DOCKER -v "$HERE:/tests:ro" -v "$ARTIFACTS:/artifacts" fpp-tests-env \
/tests/scripts/coverage-report.sh "$TREE" /artifacts/coverage || rc=1
fi
if [ -n "$WWW_COVERAGE" ]; then
$DOCKER -v "$HERE:/tests:ro" -v "$ARTIFACTS:/artifacts" fpp-tests-env \
python3 /tests/scripts/www-coverage-report.py "$TREE" /artifacts/coverage/www /artifacts/coverage/www/raw || rc=1
fi
# Hand what the container wrote (fppd.json, apache2/, media/) back to the caller.
$DOCKER -v "$ARTIFACTS:/artifacts" fpp-tests-env chown -hR "$(id -u):$(id -g)" /artifacts
if [ "$fppd_rc" != 0 ]; then
docker logs "$NAME" 2>&1 | tail -60
echo "fppd failed under the web suite (exit $fppd_rc); see $ARTIFACTS/fppd.json" >&2
[ $rc -ne 0 ] || rc=1
fi
exit $rc