ARTEX/scripts/test-docker-sock.sh
00xcyd 0fd8ff579f sandbox: detect Podman via platform name and accept symlinked sockets
docker version has no .Server.Engine field, so the previous check never
matched. Read .Server.Platform.Name instead and run detection even when
DOCKER_HOST is explicit, so the unverified-Podman warning also fires for
the common compat-socket setup.

Docker Desktop publishes /var/run/docker.sock as a symlink, so stop
refusing symlinked sockets; test -S follows them and find -L checks the
target's mode.
2026-12-02 21:15:03 +00:00

98 lines
3.8 KiB
Bash
Executable file

#!/usr/bin/env bash
# Tests for the Docker API socket resolver in install.sh/update.sh (issue #742).
# It sources only the resolver functions, so it does not touch a real engine.
set -uo pipefail
cd "$(dirname "$0")/.." || exit 1
eval "$(sed -n '/^resolve_docker_sock(){/,/^# ── 1)/p' install.sh | sed '$d')"
pass=0 fail=0
ok_case(){ pass=$((pass+1)); printf ' ok %s\n' "$1"; }
bad_case(){ fail=$((fail+1)); printf ' FAIL %s\n' "$1"; }
die(){ printf '%s\n' "$*" >&2; exit 1; }
ok(){ :; }
warnings=""
warn(){ warnings="$warnings $*"; }
set_env(){ printf 'SET_ENV %s=%s\n' "$1" "$2"; }
unset DOCKER_HOST
env_backup=""
if [ -f .env ]; then env_backup="$(mktemp)"; cp .env "$env_backup"; else : > .env; fi
sock_dir="$(mktemp -d)"
cleanup(){
[ -n "$env_backup" ] && cp "$env_backup" .env && rm -f "$env_backup" || rm -f .env
rm -rf "$sock_dir"
}
trap cleanup EXIT
sock_path="$sock_dir/engine.sock"
python3 - "$sock_path" <<'PY' &
import socket, sys, time
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.bind(sys.argv[1]); s.listen(1); time.sleep(30)
PY
sleep 0.5
chmod 660 "$sock_path"
# 1. rootless Podman is detected and its socket resolved.
docker(){ echo podman; }
podman(){ echo "$sock_path"; }
resolve_docker_sock && [ "$DOCKER_SOCK_RESOLVED" = "$sock_path" ] \
&& ok_case "rootless podman socket resolved" || bad_case "rootless podman socket resolved"
# 2. A non-default socket is persisted; the default is left implicit.
out="$(persist_docker_sock)"
[ "$out" = "SET_ENV ARTEX_DOCKER_SOCK=$sock_path" ] \
&& ok_case "non-default socket persisted to .env" || bad_case "non-default socket persisted (got: $out)"
DOCKER_HOST=""
docker(){ echo docker; }
podman(){ return 1; }
DOCKER_SOCK_RESOLVED=/var/run/docker.sock
out="$(persist_docker_sock)"
[ -z "$out" ] && ok_case "default socket not written to .env" || bad_case "default socket not written (got: $out)"
# 3. Unsafe or unusable sockets are refused before any image is built.
chmod 666 "$sock_path"
docker(){ echo podman; }
podman(){ echo "$sock_path"; }
if ( resolve_docker_sock ) 2>/dev/null; then bad_case "world-writable socket refused"; else ok_case "world-writable socket refused"; fi
chmod 660 "$sock_path"
podman(){ echo "$sock_dir/missing.sock"; }
if ( resolve_docker_sock ) 2>/dev/null; then bad_case "missing socket refused"; else ok_case "missing socket refused"; fi
podman(){ return 1; }
DOCKER_HOST=tcp://10.0.0.5:2375
if ( resolve_docker_sock ) 2>/dev/null; then bad_case "remote DOCKER_HOST refused"; else ok_case "remote DOCKER_HOST refused"; fi
# 4. An explicit Unix DOCKER_HOST wins over engine detection.
export DOCKER_HOST="unix://$sock_path"
docker(){ echo docker; }
podman(){ return 1; }
resolve_docker_sock
[ "$DOCKER_SOCK_RESOLVED" = "$sock_path" ] \
&& ok_case "unix DOCKER_HOST honoured" || bad_case "unix DOCKER_HOST honoured (got: $DOCKER_SOCK_RESOLVED)"
# 5. A symlinked socket is accepted (Docker Desktop publishes one on macOS).
sock_link="$sock_dir/docker.sock"
ln -s "$sock_path" "$sock_link"
export DOCKER_HOST="unix://$sock_link"
resolve_docker_sock
[ "$DOCKER_SOCK_RESOLVED" = "$sock_link" ] \
&& ok_case "symlinked socket accepted" || bad_case "symlinked socket accepted (got: $DOCKER_SOCK_RESOLVED)"
# 6. An explicit DOCKER_HOST served by Podman still warns that it is unverified.
export DOCKER_HOST="unix://$sock_path"
warnings=""
docker(){ echo "Podman Engine"; }
podman(){ return 1; }
resolve_docker_sock
[ "$DOCKER_ENGINE_PODMAN" = 1 ] && [ "$DOCKER_SOCK_RESOLVED" = "$sock_path" ] \
&& ok_case "explicit Podman DOCKER_HOST detected" || bad_case "explicit Podman DOCKER_HOST detected"
case "$warnings" in
*"not verified"*) ok_case "explicit Podman DOCKER_HOST warns" ;;
*) bad_case "explicit Podman DOCKER_HOST warns (got: $warnings)" ;;
esac
printf '\n%d passed, %d failed\n' "$pass" "$fail"
[ "$fail" -eq 0 ]