Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions tools/operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ See *Find every endpoint* in the cookbook
| `URBEX_OPERATOR_IMAGE` | a release's `urbex-op`: `ghcr.io/urbforge/urbex-op:<its version>`; 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.
Expand Down
70 changes: 70 additions & 0 deletions tools/operator/test-urbex-op.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
#!/bin/bash
echo "\$*" >>"$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; }
13 changes: 9 additions & 4 deletions tools/operator/urbex-op
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading