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_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) |
| 33 | set -u |
| 34 | HERE="$(cd "$(dirname "$0")/.." && pwd)" |
| 35 | IMAGE="${TEST_VERSIONS_IMAGE:-ci-py}" |
| 36 | COMMIT=HEAD |
| 37 | SHARDS="" |
| 38 | |
| 39 | usage() { sed -n '2,/^set -u/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//' >&2; exit 2; } |
| 40 | while getopts "c:j:h" opt; do |
| 41 | case "$opt" in |
| 42 | c) COMMIT=$OPTARG;; |
| 43 | j) SHARDS=$OPTARG;; |
| 44 | *) usage;; |
| 45 | esac |
| 46 | done |
| 47 | shift $((OPTIND - 1)) |
| 48 | [ $# -eq 0 ] && set -- 3.10 3.13 |
| 49 | for 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 |
| 54 | done |
| 55 | |
| 56 | command -v docker >/dev/null || { echo "docker is not installed" >&2; exit 2; } |
| 57 | docker info >/dev/null 2>&1 || { echo "docker is not running" >&2; exit 2; } |
| 58 | if [ -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 |
| 61 | fi |
| 62 | case "$SHARDS" in ''|*[!0-9]*|0) echo "-j takes a positive integer" >&2; exit 2;; esac |
| 63 | |
| 64 | SHA=$(git -C "$HERE" rev-parse --verify --quiet "$COMMIT^{commit}") \ |
| 65 | || { echo "not a commit in this repository: $COMMIT" >&2; exit 2; } |
| 66 | SHORT=$(git -C "$HERE" rev-parse --short "$SHA") |
| 67 | if [ "$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 |
| 69 | fi |
| 70 | |
| 71 | LOGS="${TEST_VERSIONS_LOGS:-$HERE/build/test-versions}" |
| 72 | mkdir -p "$LOGS"; LOGS="$(cd "$LOGS" && pwd)" |
| 73 | WORK="$(mktemp -d "${TMPDIR:-/tmp}/test-versions.XXXXXX")" |
| 74 | RUN_ID="test-versions-$$" |
| 75 | cleanup() { |
| 76 | docker rm -f $(docker ps -aq --filter "name=^${RUN_ID}-") >/dev/null 2>&1 |
| 77 | rm -rf "$WORK" |
| 78 | } |
| 79 | trap cleanup EXIT |
| 80 | trap 'exit 130' INT TERM |
| 81 | |
| 82 | # A clean clone of exactly that commit; the container gets it read-only. |
| 83 | git clone -q --no-hardlinks "$HERE" "$WORK/src" && git -C "$WORK/src" checkout -q --detach "$SHA" \ |
| 84 | || { echo "could not clone $SHORT" >&2; exit 1; } |
| 85 | chmod -R a+rX "$WORK" |
| 86 | |
| 87 | ensure_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 | |
| 94 | FAILED=0 |
| 95 | for 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 |
| 131 | done |
| 132 | exit "$FAILED" |