diff --git a/mise.toml b/mise.toml index c2cd57f..97153c0 100644 --- a/mise.toml +++ b/mise.toml @@ -35,6 +35,7 @@ description = "Run the unit tests" run = [ "go test ./...", "deno test --no-prompt platform/komodo", + "bash tools/operator/test-urbex-op.sh", ] [tasks.lint] diff --git a/tools/operator/README.md b/tools/operator/README.md index 3ef41a6..9762731 100644 --- a/tools/operator/README.md +++ b/tools/operator/README.md @@ -133,6 +133,11 @@ See *Find every endpoint* in the cookbook | `URBEX_OPERATOR_IMAGE` | a release's `urbex-op`: `ghcr.io/urbforge/urbex-op:`; from a checkout: `urbex-operator` | The image runs use (and `build` creates). | | `URBEX_PROXMOX_TOKEN`, `URBEX_CLOUDFLARE_TOKEN`, `URBEX_AGE_KEY` | - | Passed to the container when set; otherwise the credentials file is used. | +`urbex-op` pulls an image the first time only if it names its registry +(`ghcr.io/...`): one built here - `localhost/...` with podman, a bare +name such as `urbex-operator` with docker - is never pulled; when it is +missing, `urbex-op` says to build it. + From a checkout, rebuild the image (`urbex-op build`, or `mise run operator-image`) after updating it: the image contains the `urbex` binary built from it, and the tools at the versions `mise.toml` pins. diff --git a/tools/operator/test-urbex-op.sh b/tools/operator/test-urbex-op.sh new file mode 100755 index 0000000..bc244c7 --- /dev/null +++ b/tools/operator/test-urbex-op.sh @@ -0,0 +1,70 @@ +#!/bin/bash +# Tests of urbex-op's choice of image (urbforge/urbex-cli#114), with fake +# podman and docker that log their calls: a published image is pulled when +# it is missing, one built here (podman's localhost/..., docker's bare name) +# never is. Run by 'mise run test'. +set -u +here=$(cd "$(dirname "$0")" && pwd) +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +fails=0 + +# A PATH with the tools urbex-op uses and one fake engine, so that the real +# podman or docker on this machine is never found. +tools=$tmp/tools +mkdir -p "$tools" +for t in bash sh sed id cat grep mkdir mv dirname rm; do + ln -s "$(command -v "$t")" "$tools/$t" +done + +# fake ENGINE PRESENT: an engine that logs its calls and has only PRESENT. +fake() { + local dir=$tmp/$1-bin + mkdir -p "$dir" + cat >"$dir/$1" <>"$tmp/calls" +case "\$1 \$2" in + "image inspect") [ "\$3" = "$2" ] || [ "\$5" = "$2" ] ;; + *) exit 0 ;; +esac +EOF + chmod +x "$dir/$1" + echo "$dir" +} + +# check NAME ENGINE IMAGE PRESENT WANT: run urbex-op with IMAGE; WANT is +# "pull" (it pulled IMAGE), "local" (it ran it, no pull) or "refuse". +check() { + local name=$1 engine=$2 image=$3 present=$4 want=$5 bin rc got + bin=$(fake "$engine" "$present") + : >"$tmp/calls" + mkdir -p "$tmp/ws/home" + PATH=$bin:$tools URBEX_OPERATOR_IMAGE=$image HOME=$tmp \ + "$here/urbex-op" -w "$tmp/ws" true >"$tmp/out" 2>&1 + rc=$? + if grep -q "^pull $image\$" "$tmp/calls"; then got=pull + elif [ $rc -ne 0 ] && grep -q "build it with" "$tmp/out"; then got=refuse + elif grep -q "^run .* $image " "$tmp/calls"; then got=local + else got="unexpected (rc $rc)" + fi + if [ "$got" = "$want" ]; then + echo "ok $name" + else + echo "FAIL $name: $got, want $want" + sed 's/^/ /' "$tmp/out" "$tmp/calls" + fails=$((fails + 1)) + fi +} + +published=ghcr.io/urbforge/urbex-op:0.1.0 +check "podman: a published image, missing, is pulled" podman $published none pull +check "podman: a published image, present, is run" podman $published $published local +check "podman: localhost/..., missing, is never pulled" podman localhost/urbex-operator none refuse +check "podman: localhost:5000/..., missing, is never pulled" podman localhost:5000/urbex-op none refuse +check "podman: localhost/..., present, is run" podman localhost/urbex-operator-forgejo localhost/urbex-operator-forgejo local +check "docker: a bare name, missing, is never pulled" docker urbex-operator none refuse +check "docker: a bare name, present, is run" docker urbex-operator urbex-operator local +check "docker: a published image, missing, is pulled" docker $published none pull + +[ $fails -eq 0 ] || { echo "$fails failed"; exit 1; } diff --git a/tools/operator/urbex-op b/tools/operator/urbex-op index e6daa2a..69a6a30 100755 --- a/tools/operator/urbex-op +++ b/tools/operator/urbex-op @@ -34,13 +34,18 @@ ENGINE=$(command -v podman || command -v docker || { echo "urbex-op: podman or d # macOS bash 3.2 calls an empty "${a[@]}" unbound under set -u: expand arrays as ${a[@]+"${a[@]}"}. USERNS=(); [[ "$ENGINE" == *podman ]] && USERNS=(--userns keep-id) -# The image, pulled the first time when it is a published one. +# The image, pulled the first time when it is a published one: one that +# names its registry (ghcr.io/urbforge/urbex-op:0.1.0). One built here is +# never pulled - docker names it bare (urbex-operator), podman localhost/... need_image() { "$ENGINE" image inspect "$IMAGE" >/dev/null 2>&1 && return - case "$IMAGE" in - */*) echo "urbex-op: pulling $IMAGE" >&2; "$ENGINE" pull "$IMAGE" >&2 ;; - *) echo "urbex-op: no image $IMAGE - run 'urbex-op build' in a checkout of urbforge/urbex-cli" >&2; exit 1 ;; + case "${IMAGE%%/*}" in + "$IMAGE" | localhost | localhost:*) + echo "urbex-op: no image $IMAGE - build it with 'urbex-op build' (or 'mise run operator-image') in a checkout of urbforge/urbex-cli" >&2 + exit 1 ;; esac + echo "urbex-op: pulling $IMAGE" >&2 + "$ENGINE" pull "$IMAGE" >&2 } case "${1:-}" in