askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
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) |
| 36 | set -u |
| 37 | cd "$(dirname "$0")/.." |
| 38 | REPO="$PWD" |
| 39 | DOCKER="${DOCKER:-docker}" |
| 40 | PART="${1:-sandbox}" |
| 41 | case "$PART" in sandbox|full|all) ;; *) echo "usage: $0 [sandbox|full|all]" >&2; exit 2;; esac |
| 42 | SHARDS="${LINUX_SANDBOX_SHARDS:-4}" |
| 43 | case "$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. |
| 46 | SANDBOX_FILES=" |
| 47 | tests/test_object_sandbox.py |
| 48 | tests/test_object_sandbox_launcher.py |
| 49 | tests/test_object_sandbox_fork_server.py |
| 50 | tests/test_object_sandbox_worker_log.py |
| 51 | tests/test_object_schedule_sandbox.py |
| 52 | tests/test_object_worker_pool.py |
| 53 | tests/test_object_server_worker_pool.py |
| 54 | tests/test_worker_pool_real_pages.py |
| 55 | tests/test_linux_sandbox_signal_helper.py |
| 56 | " |
| 57 | |
| 58 | COMMIT="$(git rev-parse --verify --quiet "${LINUX_SANDBOX_COMMIT:-HEAD}^{commit}")" || { |
| 59 | echo "not a commit: ${LINUX_SANDBOX_COMMIT:-HEAD}" >&2; exit 2; } |
| 60 | SHORT="$(git rev-parse --short=12 "$COMMIT")" |
| 61 | GIT_COMMON="$(git rev-parse --path-format=absolute --git-common-dir)" |
| 62 | LOGS="${LINUX_SANDBOX_LOGS:-build/test-linux-sandbox}" |
| 63 | rm -rf "$LOGS"; mkdir -p "$LOGS"; LOGS="$(cd "$LOGS" && pwd)" |
| 64 | BASE_IMAGE="dbbasic-linux-sandbox:base-$SHORT" |
| 65 | TEST_IMAGE="dbbasic-linux-sandbox:test-$SHORT" |
| 66 | NAME="dbbasic-linux-sandbox-$SHORT-$$" |
| 67 | WORK="$(mktemp -d "${TMPDIR:-/tmp}/test-linux-sandbox.XXXXXX")" |
| 68 | |
| 69 | cleanup() { |
| 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 | } |
| 76 | trap cleanup EXIT |
| 77 | trap '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. |
| 83 | git 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 | |
| 87 | echo "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. |
| 95 | printf '/gitobjects\n' > "$WORK/src/.git/objects/info/alternates" |
| 96 | |
| 97 | in_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 | |
| 104 | FAILED=0 |
| 105 | |
| 106 | run_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' |
| 113 | import re, sys, time |
| 114 | log, rc, start, short = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), sys.argv[4] |
| 115 | text = open(log, errors="replace").read().splitlines() |
| 116 | last = next((l for l in reversed(text) if re.search(r" in [\d.]+s", l)), "") |
| 117 | tot = dict(passed=0, failed=0, skipped=0, error=0) |
| 118 | for num, word in re.findall(r"(\d+) (passed|failed|skipped|errors?)", last): |
| 119 | tot["error" if word.startswith("error") else word] += int(num) |
| 120 | real = any("workers run as dbbasic-sandbox" in l for l in text) |
| 121 | bad = [] |
| 122 | if rc != 0 or not last: |
| 123 | bad.append(f"pytest exit {rc}") |
| 124 | if not real: |
| 125 | bad.append("the root launcher did not start") |
| 126 | if tot["skipped"] and sys.argv[5] != "1": |
| 127 | bad.append("a sandbox test skipped: this is not the real setup") |
| 128 | if not tot["passed"]: |
| 129 | bad.append("nothing ran") |
| 130 | status = "FAIL" if bad or tot["failed"] or tot["error"] else "OK" |
| 131 | print(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})") |
| 133 | for b in bad: |
| 134 | print(" " + b, file=sys.stderr) |
| 135 | for l in text: |
| 136 | if l.startswith(("SKIPPED", "FAILED", "ERROR")): |
| 137 | print(" " + l, file=sys.stderr) |
| 138 | sys.exit(0 if status == "OK" else 1) |
| 139 | PYEOF |
| 140 | } |
| 141 | |
| 142 | run_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 | |
| 161 | case "$PART" in sandbox|all) run_sandbox || FAILED=1;; esac |
| 162 | case "$PART" in full|all) run_full || FAILED=1;; esac |
| 163 | exit "$FAILED" |