Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/scripts/test_linux_sandbox.sh
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_linux_sandbox.sh · 7.1 KB · 163 lines
1#!/usr/bin/env bash
2# Run the tests that need the real sandbox: Linux, Landlock, the launcher as
3# root, the tests as the server user, user code as the sandbox user.
4#
5# scripts/test_linux_sandbox.sh [sandbox|full|all] default: sandbox
6#
7# sandbox the sandbox, launcher, fork-server and worker-pool test files
8# (under a minute once the images are built)
9# full the whole default suite, through scripts/test_fast.sh
10# all both, one after the other
11#
12# macOS has no Landlock and CI runners have no root launcher, so there the
13# tests that matter most for the sandbox skip. This builds the project's own
14# image from one commit, adds a test layer (tests/linux_sandbox/Dockerfile:
15# git, pytest, a root helper that lets a test kill a sandbox-user worker),
16# and runs the tests in a throwaway container through the image's real
17# entrypoint. Nothing is installed on the host; it needs docker, git and
18# python3.
19#
20# The image and the tests both come from a clean checkout of the commit, not
21# from the working tree: uncommitted changes are not tested. The test layer comes
22# from this checkout, so the lane can be pointed at an older commit.
23#
24# Each part prints one summary line and the script exits non-zero if any part
25# failed. A sandbox test that skips fails the sandbox part: there, a skip
26# means the container is not the real setup.
27#
28# Environment:
29# LINUX_SANDBOX_COMMIT commit to test (default: HEAD)
30# LINUX_SANDBOX_SHARDS shards for `full` (default: 4)
31# LINUX_SANDBOX_LOGS where the logs go (default: build/test-linux-sandbox)
32# LINUX_SANDBOX_ALLOW_SKIPS=1 let a sandbox test skip (a kernel without a
33# Landlock feature); the skips are still listed
34# LINUX_SANDBOX_KEEP=1 keep the two images afterwards (faster next run)
35# DOCKER the docker command (default: docker)
36set -u
37cd "$(dirname "$0")/.."
38REPO="$PWD"
39DOCKER="${DOCKER:-docker}"
40PART="${1:-sandbox}"
41case "$PART" in sandbox|full|all) ;; *) echo "usage: $0 [sandbox|full|all]" >&2; exit 2;; esac
42SHARDS="${LINUX_SANDBOX_SHARDS:-4}"
43case "$SHARDS" in ''|*[!0-9]*|0) echo "LINUX_SANDBOX_SHARDS must be a positive integer" >&2; exit 2;; esac
44
45# The files of the `sandbox` part. A file the commit does not have is an error.
46SANDBOX_FILES="
47tests/test_object_sandbox.py
48tests/test_object_sandbox_launcher.py
49tests/test_object_sandbox_fork_server.py
50tests/test_object_sandbox_worker_log.py
51tests/test_object_schedule_sandbox.py
52tests/test_object_worker_pool.py
53tests/test_object_server_worker_pool.py
54tests/test_worker_pool_real_pages.py
55tests/test_linux_sandbox_signal_helper.py
56"
57
58COMMIT="$(git rev-parse --verify --quiet "${LINUX_SANDBOX_COMMIT:-HEAD}^{commit}")" || {
59 echo "not a commit: ${LINUX_SANDBOX_COMMIT:-HEAD}" >&2; exit 2; }
60SHORT="$(git rev-parse --short=12 "$COMMIT")"
61GIT_COMMON="$(git rev-parse --path-format=absolute --git-common-dir)"
62LOGS="${LINUX_SANDBOX_LOGS:-build/test-linux-sandbox}"
63rm -rf "$LOGS"; mkdir -p "$LOGS"; LOGS="$(cd "$LOGS" && pwd)"
64BASE_IMAGE="dbbasic-linux-sandbox:base-$SHORT"
65TEST_IMAGE="dbbasic-linux-sandbox:test-$SHORT"
66NAME="dbbasic-linux-sandbox-$SHORT-$$"
67WORK="$(mktemp -d "${TMPDIR:-/tmp}/test-linux-sandbox.XXXXXX")"
68
69cleanup() {
70 "$DOCKER" rm -f "$NAME" >/dev/null 2>&1
71 if [ "${LINUX_SANDBOX_KEEP:-0}" != 1 ]; then
72 "$DOCKER" rmi "$TEST_IMAGE" "$BASE_IMAGE" >/dev/null 2>&1
73 fi
74 rm -rf "$WORK"
75}
76trap cleanup EXIT
77trap 'exit 130' INT TERM
78
79# A checkout of exactly that commit, with its own small git directory that
80# borrows this repository's objects (nothing is copied, nothing here is
81# written). The tests get a real checkout whose HEAD is the commit: a few of
82# them read an earlier release out of git history.
83git clone -q --shared --no-checkout "$REPO" "$WORK/src" \
84 && git -C "$WORK/src" -c advice.detachedHead=false checkout -q --detach "$COMMIT" \
85 || { echo "could not check out $SHORT" >&2; exit 1; }
86
87echo "building the image of $SHORT and the test layer (log: $LOGS/build.log)"
88{
89 "$DOCKER" build -t "$BASE_IMAGE" --build-arg BUILD_COMMIT="$COMMIT" "$WORK/src" \
90 && "$DOCKER" build -t "$TEST_IMAGE" --build-arg BASE="$BASE_IMAGE" "$REPO/tests/linux_sandbox"
91} > "$LOGS/build.log" 2>&1 || { echo "FAIL: the image build failed; see $LOGS/build.log" >&2; tail -20 "$LOGS/build.log" >&2; exit 1; }
92
93# In the container the checkout is mounted read-only at /src and the borrowed
94# objects, read-only, at /gitobjects.
95printf '/gitobjects\n' > "$WORK/src/.git/objects/info/alternates"
96
97in_container() { # in_container <log> <command...>: the image's real entrypoint runs the command
98 local log="$1"; shift
99 "$DOCKER" run --rm --name "$NAME" -e TZ=UTC \
100 -v "$WORK/src":/src:ro -v "$GIT_COMMON/objects":/gitobjects:ro -v "$LOGS":/logs \
101 -w /src "$TEST_IMAGE" "$@" > "$log" 2>&1
102}
103
104FAILED=0
105
106run_sandbox() {
107 local start rc
108 start=$(date +%s)
109 # shellcheck disable=SC2086
110 in_container "$LOGS/sandbox.log" python -m pytest -p no:cacheprovider -rs -v $SANDBOX_FILES
111 rc=$?
112 python3 - "$LOGS/sandbox.log" "$rc" "$start" "$SHORT" "${LINUX_SANDBOX_ALLOW_SKIPS:-0}" <<'PYEOF'
113import re, sys, time
114log, rc, start, short = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), sys.argv[4]
115text = open(log, errors="replace").read().splitlines()
116last = next((l for l in reversed(text) if re.search(r" in [\d.]+s", l)), "")
117tot = dict(passed=0, failed=0, skipped=0, error=0)
118for num, word in re.findall(r"(\d+) (passed|failed|skipped|errors?)", last):
119 tot["error" if word.startswith("error") else word] += int(num)
120real = any("workers run as dbbasic-sandbox" in l for l in text)
121bad = []
122if rc != 0 or not last:
123 bad.append(f"pytest exit {rc}")
124if not real:
125 bad.append("the root launcher did not start")
126if tot["skipped"] and sys.argv[5] != "1":
127 bad.append("a sandbox test skipped: this is not the real setup")
128if not tot["passed"]:
129 bad.append("nothing ran")
130status = "FAIL" if bad or tot["failed"] or tot["error"] else "OK"
131print(f"{status}: sandbox files at {short}: {tot['passed']} passed, {tot['failed']} failed, "
132 f"{tot['skipped']} skipped, {tot['error']} errors in {int(time.time()) - start}s (log: {log})")
133for b in bad:
134 print(" " + b, file=sys.stderr)
135for l in text:
136 if l.startswith(("SKIPPED", "FAILED", "ERROR")):
137 print(" " + l, file=sys.stderr)
138sys.exit(0 if status == "OK" else 1)
139PYEOF
140}
141
142run_full() {
143 local rc line
144 in_container "$LOGS/full.log" sh -c \
145 "TEST_FAST_LOGS=/logs/test-fast PYTHON=python bash scripts/test_fast.sh $SHARDS"
146 rc=$?
147 line="$(grep -E '^(OK|FAIL): [0-9]+ passed' "$LOGS/full.log" | tail -1)"
148 if [ -z "$line" ]; then
149 echo "FAIL: whole suite at $SHORT: no summary line (exit $rc); see $LOGS/full.log"
150 return 1
151 fi
152 # test_fast.sh names its logs by their path inside the container.
153 echo "${line%% (logs:*}; whole suite at $SHORT (logs: $LOGS/full.log, $LOGS/test-fast/shard-*.log)"
154 if [ "$rc" -ne 0 ]; then
155 grep -h -E '^(FAILED|ERROR) ' "$LOGS"/test-fast/shard-*.log 2>/dev/null | sort -u | sed 's/^/ /' >&2
156 return 1
157 fi
158 case "$line" in OK:*) return 0;; *) return 1;; esac
159}
160
161case "$PART" in sandbox|all) run_sandbox || FAILED=1;; esac
162case "$PART" in full|all) run_full || FAILED=1;; esac
163exit "$FAILED"