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_versions.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_versions.sh · 5.8 KB · 132 lines
1#!/usr/bin/env bash
2# Run the default test suite on other Python versions, in containers, sharded.
3#
4# scripts/test_versions.sh [-c COMMIT] [-j SHARDS] [VERSION...]
5#
6# VERSION a Python version from the CI matrix: 3.10 ... 3.14, or 3.14t
7# (free-threaded). Default: 3.10 3.13
8# -c the commit to test (default: HEAD). Only committed work is
9# tested: the script makes its own clean clone of that commit, so
10# nothing uncommitted in the working tree can leak in.
11# -j pytest processes per version (default: Docker's CPU count minus
12# two, at least 1, at most 12)
13#
14# For each version, one container does what .github/workflows/tests.yml does
15# (new venv, `pip install -e ".[test]"`, then the `exchange` extra if it
16# installs) and then runs that commit's scripts/test_fast.sh, which splits the
17# suite into SHARDS pytest processes and checks that together they ran exactly
18# the tests a plain `pytest` selects. Versions run one after another, so each
19# has the CPUs to itself.
20#
21# Prints one line per version, test_fast.sh's own summary line with the
22# version and wall time added ("OK: N passed, ..." or "FAIL: ..."), and exits
23# non-zero if any version failed.
24#
25# Needs docker and git, and the network (base image the first time, pip every
26# time). Images are named ci-py:VERSION and are built from scripts/ci-image
27# when missing.
28#
29# Environment:
30# TEST_VERSIONS_LOGS where logs go (default: build/test-versions);
31# per version: run.log and shards/shard-*.log
32# TEST_VERSIONS_IMAGE image name without the tag (default: ci-py)
33set -u
34HERE="$(cd "$(dirname "$0")/.." && pwd)"
35IMAGE="${TEST_VERSIONS_IMAGE:-ci-py}"
36COMMIT=HEAD
37SHARDS=""
38
39usage() { sed -n '2,/^set -u/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//' >&2; exit 2; }
40while getopts "c:j:h" opt; do
41 case "$opt" in
42 c) COMMIT=$OPTARG;;
43 j) SHARDS=$OPTARG;;
44 *) usage;;
45 esac
46done
47shift $((OPTIND - 1))
48[ $# -eq 0 ] && set -- 3.10 3.13
49for v in "$@"; do
50 case "$v" in
51 3.[0-9]|3.[0-9][0-9]|3.[0-9]t|3.[0-9][0-9]t) ;;
52 *) echo "not a Python version: $v (expected like 3.10 or 3.14t)" >&2; exit 2;;
53 esac
54done
55
56command -v docker >/dev/null || { echo "docker is not installed" >&2; exit 2; }
57docker info >/dev/null 2>&1 || { echo "docker is not running" >&2; exit 2; }
58if [ -z "$SHARDS" ]; then
59 n=$(docker info --format '{{.NCPU}}' 2>/dev/null); case "$n" in ''|*[!0-9]*) n=4;; esac
60 SHARDS=$((n - 2)); [ "$SHARDS" -lt 1 ] && SHARDS=1; [ "$SHARDS" -gt 12 ] && SHARDS=12
61fi
62case "$SHARDS" in ''|*[!0-9]*|0) echo "-j takes a positive integer" >&2; exit 2;; esac
63
64SHA=$(git -C "$HERE" rev-parse --verify --quiet "$COMMIT^{commit}") \
65 || { echo "not a commit in this repository: $COMMIT" >&2; exit 2; }
66SHORT=$(git -C "$HERE" rev-parse --short "$SHA")
67if [ "$COMMIT" = HEAD ] && [ -n "$(git -C "$HERE" status --porcelain 2>/dev/null)" ]; then
68 echo "note: the working tree has uncommitted changes; testing commit $SHORT without them" >&2
69fi
70
71LOGS="${TEST_VERSIONS_LOGS:-$HERE/build/test-versions}"
72mkdir -p "$LOGS"; LOGS="$(cd "$LOGS" && pwd)"
73WORK="$(mktemp -d "${TMPDIR:-/tmp}/test-versions.XXXXXX")"
74RUN_ID="test-versions-$$"
75cleanup() {
76 docker rm -f $(docker ps -aq --filter "name=^${RUN_ID}-") >/dev/null 2>&1
77 rm -rf "$WORK"
78}
79trap cleanup EXIT
80trap 'exit 130' INT TERM
81
82# A clean clone of exactly that commit; the container gets it read-only.
83git clone -q --no-hardlinks "$HERE" "$WORK/src" && git -C "$WORK/src" checkout -q --detach "$SHA" \
84 || { echo "could not clone $SHORT" >&2; exit 1; }
85chmod -R a+rX "$WORK"
86
87ensure_image() { # $1 = version
88 docker image inspect "$IMAGE:$1" >/dev/null 2>&1 && return 0
89 echo "building $IMAGE:$1 (first use) ..." >&2
90 docker build -q -t "$IMAGE:$1" --build-arg "PY=$1" --build-arg "BASE=${1%t}" "$HERE/scripts/ci-image" \
91 > "$LOGS/build-$1.log" 2>&1
92}
93
94FAILED=0
95for v in "$@"; do
96 out="$LOGS/$v"
97 rm -rf "$out"; mkdir -p "$out"; chmod 777 "$out"
98 start=$(date +%s)
99 if ! ensure_image "$v"; then
100 echo "FAIL: could not build $IMAGE:$v (see $LOGS/build-$v.log) [Python $v, commit $SHORT]"
101 FAILED=1; continue
102 fi
103 # The log directory is mounted at the same path inside the container, so
104 # the paths test_fast.sh prints are right on the host too.
105 docker run --rm --name "$RUN_ID-$v" -e TZ=UTC \
106 -v "$WORK/src:/src:ro" -v "$out:$out" \
107 -e "TEST_FAST_LOGS=$out/shards" -e "SHARDS=$SHARDS" \
108 "$IMAGE:$v" bash -c '
109 set -u
110 cp -a /src "$HOME/work" && cd "$HOME/work" || exit 1
111 python -m venv "$HOME/venv" && . "$HOME/venv/bin/activate" || exit 1
112 python -c "import sys, sysconfig; print(sys.version); print(\"free-threaded build:\", bool(sysconfig.get_config_var(\"Py_GIL_DISABLED\")))"
113 git --version; node --version
114 # The same three installs as .github/workflows/tests.yml.
115 python -m pip install -q --upgrade pip || exit 1
116 python -m pip install -q -e ".[test]" || exit 1
117 python -m pip install -q -e ".[test,exchange]" \
118 || echo "WARNING: exchange extra did not install; exchange tests will skip"
119 [ -f scripts/test_fast.sh ] || { echo "FAIL: this commit has no scripts/test_fast.sh"; exit 1; }
120 PYTHON=python bash scripts/test_fast.sh "$SHARDS"
121 ' > "$out/run.log" 2>&1
122 rc=$?
123 line=$(grep -E '^(OK|FAIL): ' "$out/run.log" | tail -1)
124 [ -n "$line" ] || line="FAIL: no test result (container exit $rc; see $out/run.log)"
125 case "$line" in OK:*) [ "$rc" -eq 0 ] || line="FAIL: container exit $rc after: $line";; esac
126 case "$line" in OK:*) ;; *) FAILED=1;; esac
127 pyver=$(grep -m1 -E '^3\.[0-9]+\.[0-9]+' "$out/run.log" | cut -d' ' -f1)
128 kind=""; case "$v" in *t) kind=" free-threaded";; esac
129 echo "$line [Python ${pyver:-$v}$kind, commit $SHORT, $(( $(date +%s) - start ))s in all]"
130 grep -E '^WARNING: |^ (shard|ran) ' "$out/run.log" | sed 's/^ */ /' >&2
131done
132exit "$FAILED"