Merge branch '20276-fix-unexpected-return' into 'stable'
[tails.git] / run_test_suite
blob3219919993a9e0247a5d35cac5ca62be4fab119b
1 #!/bin/bash
3 set -e
4 set -u
5 set -o pipefail
7 NAME=$(basename "${0}")
9 GENERAL_DEPENDENCIES="
10 cucumber
11 devscripts
12 dnsmasq-base
13 ffmpeg
14 gawk
15 git
16 imagemagick
17 libcap2-bin
18 libvirt-clients
19 libvirt-daemon-system
20 libvirt-dev
21 libvirt0
22 obfs4proxy
23 openssh-server
24 ovmf
25 pry
26 python3-opencv
27 python3-pil
28 python3-slixmpp
29 qemu-system-x86
30 qrencode
31 ruby-guestfs
32 ruby-json
33 ruby-libvirt
34 ruby-net-dns
35 ruby-packetfu
36 ruby-rb-inotify
37 ruby-rspec
38 ruby-test-unit
39 seabios
40 tcpdump
41 tcplay
42 tor
43 unclutter
44 virt-viewer
45 xdotool
46 xvfb
49 # Recent tor client require a Chutney network with tor >= 0.4.4.6 (#18190)
50 MINIMUM_TOR_VERSION=0.4.4.6
52 usage() {
53 echo "Usage: $NAME [OPTION]... [--] [CUCUMBER_ARGS]...
54 Sets up an appropriate environment and invokes cucumber. Note that this script
55 must be run from the Tails source directory root.
57 Options for '@product' features:
58 --allow-non-root Normally the test suite must be run as root, but if you
59 really know what you are doing this option allows any
60 user to run it.
61 --artifacts-base-uri URI
62 Pretend that the artifact is located at URI when printing
63 its location during a scenario failure. This is useful if
64 you intend to serve the artifacts via the web, for
65 instance.
66 --capture Captures failed scenarios into videos stored in the
67 temporary directory (see --tmpdir below) using x264
68 encoding. Requires x264.
69 --capture-all Keep videos for all scenarios, including those that
70 succeed (implies --capture).
71 --interactive-debugging
72 On failure, pause test suite until pressing Enter. Also
73 offer the option to open an interactive Ruby shell (pry)
74 in the Cucumber world's context.
75 --image-bumping-mode
76 When any image matching fails, enter an interactive mode
77 that allows to update the image. If run from a graphical
78 environment, any found candidate image will be displayed
79 in a pop-up.
80 --keep-chutney Don't ever clean Chutney data directory.
81 This can be a big time saver when debugging steps
82 when --keep-snapshots is not an option.
83 --keep-snapshots Don't ever delete any snapshots (including ones marked as
84 temporary). This can be a big time saver when debugging new
85 features. Implies --keep-chutney.
86 --disable-chutney EXPERIMENTAL: All tests will be run using the real Tor
87 network, not a simulated one.
88 Expect this to break many test cases.
89 --late-patch
90 --late-patch=FILE
91 Copies files into the VM before running the test suite,
92 which often can avoid a rebuild.
93 FILE is a text file where each line contains tab-separated
94 source and destination. If only a source is given, it is
95 assumed to be a file inside config/chroot_local-includes,
96 and the corresponding destination is inferred. Lines with
97 a leading \"#\" are ignored.
98 FILE is optional, and if not given we copy in all files in
99 config/chroot_local-includes that have changed since the
100 commit the system under testing was built from, including
101 untracked files.
102 --early-patch Boots the system with the \"early_patch=umount\" cmdline option.
103 See wiki/src/contribute/build/early-patch.mdwn for details
104 This is useful when you need something to be patched early
105 in the boot process; or if you need to run arbitrary
106 commands, not just copying files
107 --all-tests Don't skip tests which have a @fragile or @skip_by_default tag.
108 --tmpdir Directory where various temporary files are written
109 during a test, e.g. VM snapshots and memory dumps,
110 failure screenshots, pcap files and disk images
111 (default is TMPDIR in the environment, and if unset,
112 /tmp/TailsToaster).
113 --view Shows the test session in a windows. Requires x11vnc
114 and tigervnc-viewer.
115 --view-interact The test session can be \"touched\". For debugging purposes
116 only.
117 --vnc-server-only Starts a VNC server for the test session. Requires x11vnc.
118 --iso IMAGE Test '@product' features using IMAGE.
119 --old-iso IMAGE For some '@product' features (e.g. usb_install) we need
120 an older version of Tails, which this options sets to
121 IMAGE. If none is given, it defaults to the same IMAGE
122 given by --iso, which will be good enough for most testing
123 purposes.
125 Note that '@source' features has no relevant options.
127 CUCUMBER_ARGS can be used to specify which features to be run, but also any
128 cucumber option, although then you must pass \`--\` first to let this wrapper
129 script know that we're done with *its* options. For debugging purposes, a
130 'debug' formatter has been added so pretty debugging can be enabled with
131 \`--format debug\`. You could even combine the default (pretty) formatter with
132 pretty debugging printed to a file with \`--format pretty --format debug
133 --out debug.log\`.
137 error() {
138 echo "${NAME}: error: ${*}" >&2
139 exit 1
142 package_installed() {
143 local ret
144 set +o pipefail
145 if dpkg -s "${1}" 2>/dev/null | grep -q "^Status:.*installed"; then
146 ret=0
147 else
148 ret=1
150 set -o pipefail
151 return ${ret}
154 check_dependencies() {
155 while [ -n "${1:-}" ]; do
156 if ! command -v "${1}" >/dev/null && ! package_installed "${1}" ; then
157 error "'${1}' is missing, please install it and run again."
159 shift
160 done
163 check_tor_version() {
164 local tor_version
165 tor_version=$(tor --version | grep -Po '^Tor version \K.*' | sed --regexp-extended 's,[.]$,,')
166 echo "tor version: $tor_version"
167 if dpkg --compare-versions "$tor_version" lt "$MINIMUM_TOR_VERSION"; then
168 error "Please upgrade to tor ${MINIMUM_TOR_VERSION} or newer."
172 check_virt_viewer_version() {
173 local version
174 version="$(dpkg-query --show --showformat='${VERSION}' virt-viewer)"
175 if dpkg --compare-versions "${version}" ge 10.0; then
176 if ! echo "${version}" | grep -q 'tails$'; then
177 error 'Please install virt-viewer from the isotester-bookworm APT suite' \
178 '(instructions: https://tails.net/contribute/release_process/test/setup/' \
179 'details: https://gitlab.tails.boum.org/tails/tails/-/issues/19064)'
184 display_in_use() {
185 [ -e "/tmp/.X${1#:}-lock" ] || [ -e "/tmp/.X11-unix/X${1#:}" ]
188 next_free_display() {
189 display_nr=0
190 while display_in_use ":${display_nr}"; do
191 display_nr=$((display_nr+1))
192 done
193 echo ":${display_nr}"
196 test_suite_cleanup() {
197 if [ -n "${XVFB_PID:-}" ]; then
198 (kill -0 "${XVFB_PID}" 2>/dev/null && kill "${XVFB_PID}") || /bin/true
200 return $?
203 start_xvfb() {
204 Xvfb "$TARGET_DISPLAY" -screen 0 1024x768x24+32 -noreset >/dev/null 2>&1 &
205 XVFB_PID=$!
206 # Wait for Xvfb to run on TARGET_DISPLAY
207 until display_in_use "$TARGET_DISPLAY"; do
208 sleep 1
209 done
210 echo "Virtual X framebuffer started on display ${TARGET_DISPLAY}"
211 # Hide the mouse cursor so it won't be in the way when we are
212 # trying to match images.
213 unclutter -display "$TARGET_DISPLAY" -root -idle 0.1 >/dev/null 2>&1 &
216 start_vnc_server() {
217 check_dependencies x11vnc
218 # shellcheck disable=SC2086
219 VNC_SERVER_PORT="$(env -u WAYLAND_DISPLAY x11vnc \
220 -listen localhost -display "${TARGET_DISPLAY}" \
221 -bg -nopw -forever ${TAILS_X11VNC_OPTS:-} 2>&1 | \
222 grep -m 1 "^PORT=[0-9]\+" | sed 's/^PORT=//')"
223 echo "VNC server running on: localhost:${VNC_SERVER_PORT}"
226 start_vnc_viewer() {
227 opts=
228 if [[ "${VNC_VIEWER_VIEWONLY}" = yes ]]; then
229 opts=-viewonly
231 check_dependencies tigervnc-viewer
232 xtigervncviewer \
233 -nojpeg $opts \
234 -RemoteResize=0 \
235 -AcceptClipboard=0 \
236 -SendClipboard=0 \
237 -SetPrimary=0 \
238 -SendPrimary=0 \
239 "localhost:${VNC_SERVER_PORT}" 1>/dev/null 2>&1 &
242 # main script
244 # Set default values of environment variables used by this script to
245 # pass options to cucumber (unless they are already set).
246 ALLOW_NON_ROOT=${ALLOW_NON_ROOT-}
247 ARTIFACTS_BASE_URI=${ARTIFACTS_BASE_URI-}
248 CAPTURE=${CAPTURE-}
249 CAPTURE_ALL=${CAPTURE_ALL-}
250 VNC_VIEWER=${VNC_VIEWER-}
251 VNC_VIEWER_VIEWONLY=${VNC_VIEWER_VIEWONLY-yes}
252 VNC_SERVER=${VNC_SERVER-}
253 INTERACTIVE_DEBUGGING=${INTERACTIVE_DEBUGGING-}
254 IMAGE_BUMPING_MODE=${IMAGE_BUMPING_MODE-}
255 KEEP_CHUTNEY=${KEEP_CHUTNEY-}
256 KEEP_SNAPSHOTS=${KEEP_SNAPSHOTS-}
257 TAILS_ISO=${TAILS_ISO-}
258 OLD_TAILS_ISO=${OLD_TAILS_ISO-}
259 EARLY_PATCH=${EARLY_PATCH-}
260 EXTRA_BOOT_OPTIONS=${EXTRA_BOOT_OPTIONS-}
261 ALL_TESTS=${ALL_TESTS-}
263 LONGOPTS="allow-non-root,artifacts-base-uri:,view,view-interact,vnc-server-only,capture,capture-all,help,tmpdir:,keep-chutney,keep-snapshots,disable-chutney,late-patch::,early-patch,extra-boot-options:,all-tests,iso:,old-iso:,interactive-debugging,image-bumping-mode"
264 OPTS=$(getopt -o "" --longoptions $LONGOPTS -n "${NAME}" -- "$@")
265 eval set -- "$OPTS"
266 while [ $# -gt 0 ]; do
267 case $1 in
268 --allow-non-root)
269 if ! /sbin/getcap /usr/bin/tcpdump | grep -q 'cap_net_raw=eip'; then
270 error "/usr/bin/tcpdump lacks cap_net_raw=eip"
272 ALLOW_NON_ROOT="yes"
274 --artifacts-base-uri)
275 shift
276 export ARTIFACTS_BASE_URI="${1}"
278 --view)
279 VNC_VIEWER=yes
280 VNC_VIEWER_VIEWONLY=yes
281 VNC_SERVER=yes
283 --view-interact)
284 VNC_VIEWER=yes
285 VNC_VIEWER_VIEWONLY=
286 VNC_SERVER=yes
288 --vnc-server-only)
289 VNC_VIEWER=
290 VNC_SERVER=yes
292 --capture)
293 check_dependencies x264
294 export CAPTURE="yes"
296 --capture-all)
297 check_dependencies x264
298 export CAPTURE="yes"
299 export CAPTURE_ALL="yes"
301 --interactive-debugging)
302 export INTERACTIVE_DEBUGGING="yes"
304 --image-bumping-mode)
305 export IMAGE_BUMPING_MODE="yes"
307 --keep-chutney)
308 export KEEP_CHUTNEY="yes"
310 --keep-snapshots)
311 export KEEP_CHUTNEY="yes"
312 export KEEP_SNAPSHOTS="yes"
314 --disable-chutney)
315 export DISABLE_CHUTNEY="yes"
317 --late-patch)
318 shift
319 if [ -n "${1:-}" ]; then
320 LATE_PATCH="$(realpath -s "$1")"
321 if [ ! -e "$LATE_PATCH" ]; then
322 error "--late-patch: FILE doesn't exist"
324 else
325 LATE_PATCH=''
327 export LATE_PATCH
329 --early-patch)
330 export EARLY_PATCH="yes"
332 --extra-boot-options)
333 shift
334 export EXTRA_BOOT_OPTIONS="$1"
336 --all-tests)
337 ALL_TESTS="yes"
339 --tmpdir)
340 shift
341 TMPDIR="$(readlink -f "$1")"
342 export TMPDIR
344 --iso)
345 shift
346 TAILS_ISO="$(realpath -s "$1")"
347 export TAILS_ISO
349 --old-iso)
350 shift
351 OLD_TAILS_ISO="$(realpath -s "$1")"
352 export OLD_TAILS_ISO
354 --help)
355 usage
356 exit 0
359 shift
360 break
362 esac
363 shift
364 done
366 trap "test_suite_cleanup" EXIT HUP INT QUIT TERM
368 if [ "${EUID}" -ne 0 ] && [ -z "${ALLOW_NON_ROOT}" ]; then
369 error "you are not running as root; if you really know what you are" \
370 "doing, see the --allow-non-root option"
373 # shellcheck disable=SC2086
374 check_dependencies ${GENERAL_DEPENDENCIES}
375 check_tor_version
376 check_virt_viewer_version
378 TARGET_DISPLAY=$(next_free_display)
380 start_xvfb
382 if [ -n "${VNC_SERVER:-}" ]; then
383 start_vnc_server
385 if [ -n "${VNC_VIEWER:-}" ]; then
386 start_vnc_viewer
389 TAGS_ARGS=""
390 if [ -n "${JENKINS_URL:-}" ]; then
391 . auto/scripts/utils.sh
393 if echo "${GIT_BRANCH}" | grep -q -E '[+-]real-Tor$'; then
394 export DISABLE_CHUTNEY="yes"
395 TAGS_ARGS="--tag @supports_real_tor"
396 # The current Git state may not reflect the state at the time the
397 # upstream job was started (e.g. since then we git fetch + git
398 # reset --hard) so we trust the Git state described in Jenkins'
399 # environment variables instead.
400 elif echo "${GIT_BRANCH}" | grep -q -E '[+-]force-all-tests$' \
401 || git_on_a_tag \
402 || [ "${GIT_BRANCH#origin/}" = feature/bookworm ] \
403 || [ "${GIT_BRANCH#origin/}" = testing ] \
404 || [ "${GIT_BRANCH#origin/}" = devel ] \
405 || [ -n "${ALL_TESTS:-}" ]; then
406 TAGS_ARGS=""
407 else
408 TAGS_ARGS="${TAGS_ARGS} --tag ~@fragile --tag ~@skip_by_default"
410 if [ "${UPSTREAMJOB_GIT_COMMIT}" != "${UPSTREAMJOB_GIT_BASE_BRANCH_HEAD}" ] && \
411 git_only_doc_changes_since "${UPSTREAMJOB_GIT_BASE_BRANCH_HEAD}"; then
412 TAGS_ARGS="${TAGS_ARGS} --tag @doc"
416 export USER_DISPLAY="${DISPLAY:-}"
417 export DISPLAY=${TARGET_DISPLAY}
419 # shellcheck disable=SC2086
420 cucumber --expand ${TAGS_ARGS} "${@}"