Something went wrong. Try again.
Backend environment for match hosting for lance.blue
Something went wrong. Try again.
Shell
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230#!/usr/bin/env bash# scripts/image.sh decides which locally built image is in play, so what is# worth testing is the deciding: which commit a tag names, which of several tags# wins, and what it says when the answer is none of them.## No daemon is needed and none is available - this runs in the image's test# stage. A stub `docker` on PATH answers the two read-only queries the script# makes from a fixture table of tags, which is also the only way to test it# against a machine holding a known set of images. The git side is real: a# throwaway repository, the same trick tests/shell/test-deploy.sh uses, because# choosing between builds made in the same second is a git question.## Not covered: `diff` and `--deep`. Those read layers and files out of real# images, which needs a daemon; ./scripts/test.sh is where that would live.set -uo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"# Not TMP: this file sources image.sh, which has a TMP of its own.WORK="$(mktemp -d)"trap 'rm -rf "$WORK"' EXIT
pass=0; fail=0check() { if "${@:2}"; then echo "ok $1"; pass=$((pass+1)); else echo "FAIL $1"; fail=$((fail+1)); fi; }
eq() { [ "$1" = "$2" ] || { echo " want '$2', got '$1'" >&2; return 1; }; }has() { case "$2" in *"$1"*) return 0 ;; esac; echo " no '$1' in the output" >&2; return 1; }nope() { ! "$@"; }
# --- a fake repo with the script in it ---------------------------------------
mkdir -p "$WORK/arena/scripts" "$WORK/bin"cp "$ROOT/scripts/image.sh" "$WORK/arena/scripts/image.sh"cp "$ROOT/versions.env" "$WORK/arena/versions.env"
git -C "$WORK/arena" init --quiet -b maingit -C "$WORK/arena" config user.email t@tgit -C "$WORK/arena" config user.name tgit -C "$WORK/arena" add -Agit -C "$WORK/arena" commit --quiet -m "first"C1="$(git -C "$WORK/arena" rev-parse --short=12 HEAD)"git -C "$WORK/arena" commit --quiet --allow-empty -m "second"C2="$(git -C "$WORK/arena" rev-parse --short=12 HEAD)"git -C "$WORK/arena" commit --quiet --allow-empty -m "third and newest"C3="$(git -C "$WORK/arena" rev-parse --short=12 HEAD)"
# Stands in for the daemon. Reads tag|id|size|created|revision rows out of# $FIXTURE and answers in the shapes image.sh asks for. Deliberately bash and# coreutils only: the test stage is the build environment plus a few packages,# and a stub needing something it does not have fails for the wrong reason.cat > "$WORK/bin/docker" <<'STUB'#!/usr/bin/env bashrow() { local t id size created rev while IFS='|' read -r t id size created rev; do if [ "$t" = "$1" ]; then printf '%s|%s|%s|%s\n' "$id" "$size" "$created" "$rev"; return 0; fi done < "$FIXTURE" return 1}
[ "$1" = image ] || exit 99shift
case "$1" in ls) shift # `image ls -f dangling=true`: this machine has none. if [ "$1" = "-f" ]; then exit 0; fi cut -d'|' -f1 "$FIXTURE" ;; inspect) shift fmt="" if [ "$1" = "--format" ]; then fmt="$2"; shift 2; fi rc=0 for ref in "$@"; do if ! out="$(row "${ref##*:}")"; then echo "Error response from daemon: No such image: $ref" >&2 rc=1 continue fi case "$fmt" in "") ;; # an existence check *Id*) printf 'sha256:%s\n' "$out" ;; *) echo "stub: unhandled --format $fmt" >&2; exit 98 ;; esac done exit "$rc" ;; *) exit 99 ;;esacSTUBchmod +x "$WORK/bin/docker"
img() { ( cd "$WORK/arena" && PATH="$WORK/bin:$PATH" ./scripts/image.sh "$@" )}
# Three builds one second apart on the clock but not in git, which is the case# that made this script sort for itself: docker gives them one Created value and# lists them in a different order every time. The rows are out of order here on# purpose - nothing may depend on what the daemon happened to print first.SAME="2026-08-07T17:44:54.895042045-04:00"cat > "$WORK/fixture" <<FIXmm0.51.0-sur26.4.7-main-$C1|aa11aa11aa11|400000000|$SAME|main-$C1mm0.51.0-sur26.4.7-main-$C3|cc33cc33cc33|400002000|$SAME|main-$C3latest|cc33cc33cc33|400002000|$SAME|main-$C3mm0.51.0-sur26.4.7-main-$C2|bb22bb22bb22|400001000|$SAME|main-$C2mm0.51.0-sur26.4.7-claude-gone-deadbeef1234|dd44dd44dd44|300000000|2026-08-06T10:00:00.0-04:00|main-deadbeef1234mm0.51.0-ws26.4.5|ee55ee55ee55|790931751|2026-08-03T11:15:15.9-04:00|${C1:0:7}FIXexport FIXTURE="$WORK/fixture"
# --- the parsing, with nothing stubbed ---------------------------------------
# Sourcing runs nothing. It does bring `set -e` with it, which would end the run# on the first case that is meant to fail, so it is turned back off.# shellcheck source=../../scripts/image.sh. "$WORK/arena/scripts/image.sh"set +e
check "the commit is the last field, not the third" \ eq "$(tag_commit mm0.51.0-sur26.4.7-claude-local-image-select-c836f26bc6a8)" c836f26bc6a8check "a dirty tag still names its commit" \ eq "$(tag_commit mm0.51.0-sur26.4.7-main-c836f26bc6a8-dirty)" c836f26bc6a8check "a branch full of dashes and dots does not confuse it" \ eq "$(tag_commit mm0.51.0-ws26.4.5-pr-permadeath.com-3msha3pbglf24-r1-5187da90a652)" 5187da90a652check "a tag from before the ref was in the name has no commit" \ eq "$(tag_commit mm0.51.0-ws26.4.5)" ""check "a short hex field is not a commit" \ eq "$(tag_commit mm0.51.0-ws26.4.5-abc)" ""check "latest names no commit" eq "$(tag_commit latest)" ""
check "the revision label answers when the tag cannot" \ eq "$(image_commit mm0.51.0-ws26.4.5 66b928d)" 66b928dcheck "the tag wins over the label when both have one" \ eq "$(image_commit mm0.51.0-sur26.4.7-main-c836f26bc6a8 main-4e5403faaa6a)" c836f26bc6a8
check "a seven-character sha matches a twelve-character tag" \ sha_matches c836f26 c836f26bc6a8check "a forty-character sha matches it too" \ sha_matches c836f26bc6a8ffffffffffffffffffffffffffff c836f26bc6a8check "a different commit does not" nope sha_matches deadbee c836f26bc6a8check "one character is too little to mean a commit" nope sha_matches c c836f26bc6a8
check "sizes under a megabyte read as KiB" eq "$(human_bytes 2263)" "2.2 KiB"check "image sizes read as MiB" eq "$(human_bytes 430211158)" "410.3 MiB"check "a disk total reads as GiB" eq "$(human_bytes 18897856102)" "17.6 GiB"
# --- selectors, against the fixture ------------------------------------------
check "no selector means the tag versions.env names" \ eq "$(IMAGE_TAG=mm0.51.0-ws26.4.5 img resolve)" "lance-blue/arena:mm0.51.0-ws26.4.5"
check "an exact tag resolves to itself" \ eq "$(img resolve "mm0.51.0-sur26.4.7-main-$C2")" "lance-blue/arena:mm0.51.0-sur26.4.7-main-$C2"
check "a short sha finds the build made from it" \ eq "$(img resolve "${C2:0:7}")" "lance-blue/arena:mm0.51.0-sur26.4.7-main-$C2"
check "a commit this repo does not have is still resolvable" \ eq "$(img resolve deadbeef)" "lance-blue/arena:mm0.51.0-sur26.4.7-claude-gone-deadbeef1234"
check "a full reference is taken as given" \ eq "$(img resolve "lance-blue/arena:mm0.51.0-ws26.4.5")" "lance-blue/arena:mm0.51.0-ws26.4.5"
check "latest resolves, because it is a tag on this machine" \ eq "$(img resolve latest)" "lance-blue/arena:latest"
# The three timestamps are identical, so only git can say which is newest - and# the answer has to be the same every time it is asked.check "newest is the tip of the three built in the same second" \ eq "$(img resolve newest)" "lance-blue/arena:mm0.51.0-sur26.4.7-main-$C3"
# The fake repo is clean and on main at C3, so versions.env names C3's tag - the# ordinary case, where the build for the checked-out commit is the one in play.check " which is also what no selector at all resolves to here" \ eq "$(img resolve)" "lance-blue/arena:mm0.51.0-sur26.4.7-main-$C3"
# --- what it refuses ----------------------------------------------------------
out="$(IMAGE_TAG=mm0.51.0-sur26.4.7-main-000000000000 img resolve 2>&1)"check "refuses when versions.env names a tag nothing has built" test $? -ne 0check " and says which script would build it" has "build.sh" "$out"
out="$(img resolve nosuchthing 2>&1)"check "refuses a selector that matches nothing" test $? -ne 0
out="$(img diff onlyone 2>&1)"check "refuses a diff with one argument" test $? -ne 0
out="$(img frobnicate 2>&1)"check "refuses a command it does not have" test $? -ne 0
# Two builds of one commit under different branch names is the shape that makes# a sha ambiguous. Guessing between them would be worse than stopping.cat > "$WORK/fixture-ambiguous" <<FIXmm0.51.0-sur26.4.7-claude-one-deadbeef1234|dd44dd44dd44|300000000|2026-08-06T10:00:00.0-04:00|main-deadbeef1234mm0.51.0-sur26.4.7-claude-two-deadbeef1234|ff66ff66ff66|300000001|2026-08-06T09:00:00.0-04:00|main-deadbeef1234FIXout="$(FIXTURE="$WORK/fixture-ambiguous" img resolve deadbeef 2>&1)"check "refuses a sha that names two builds" test $? -ne 0check " and names the first of them" has "claude-one-deadbeef1234" "$out"check " and the second" has "claude-two-deadbeef1234" "$out"
# --- list ---------------------------------------------------------------------
out="$(img list 2>&1)"check "six tags over five images are counted as five" has "5 image(s)" "$out"check " with the moving tag named as an alias, not a row" has "also tagged: latest" "$out"check "a commit this repo does not have is called out" \ has "1 image(s) name a commit this repo does not have" "$out"check "an old tag gets its commit from the revision label" has " ${C1:0:7} " "$out"check "marks the row versions.env names" has "* mm0.51.0-sur26.4.7-main-$C3" "$out"
out="$(IMAGE_TAG=mm0.51.0-sur26.4.7-main-000000000000 img list 2>&1)"check "says so when versions.env names nothing that is built" has "nothing here is" "$out"
# --- verify -------------------------------------------------------------------
out="$(img verify newest 2>&1)"check "verify names the commit's subject" has "third and newest" "$out"check " and lists the other tags on that image" has "also latest" "$out"
out="$(img verify deadbeef 2>&1)"check "verify says when nothing can rebuild an image" has "NOT in this repo" "$out"
echoecho "$pass passed, $fail failed"[ "$fail" -eq 0 ]