peppa/packages/testkit/bin/mock-docker

127 lines
8.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# mock-docker — stands in for the docker CLI in .gitea/peppa-deploy.sh tests (first on PATH).
# Same calls and exit codes as the script uses from real docker; state lives in
# $MOCK_DOCKER_STATE: images (one tag per line), containers/<name> (its image), calls.log.
# MOCK_DOCKER_BUILD_FAIL=1 makes `docker build` fail; MOCK_DOCKER_SCREENSHOT_FAIL=1 — `docker run` (screenshot).
# PEP-17: `docker run … peppa/runner…` (the runner image) runs the command of the "container" right
# here, as real docker would inside it: env only from -e (NAME takes the client's value), PATH of
# the image = infra/runner/bin (+ $MOCK_DOCKER_PATH, then node and the system), -w as cwd; a mount
# whose target differs from its source (/peppa/run, /peppa/home) is mapped back in argv and env.
# Each run is logged to $MOCK_DOCKER_STATE/runs.jsonl (name, image, mounts, env names, argv).
# `docker rm -f <name>` drops containers/<name>; MOCK_DOCKER_RUNNER_FAIL=1 fails a runner run.
# PEP-189: `docker ps` prints $st/ps, `docker stop` removes its row and appends to $st/stopped.
# PEP-287: `docker builder prune` — MOCK_DOCKER_OLD_CLIENT=1 rejects --reserved-space like client 20.10.
set -uo pipefail
# Called through symlinks (a `docker` on PATH): resolve to this file to find the repo.
_src="$0"; while [[ -L "$_src" ]]; do _l="$(readlink "$_src")"; [[ "$_l" == /* ]] && _src="$_l" || _src="$(dirname "$_src")/$_l"; done
ROOT="$(cd "$(dirname "$_src")/../../.." && pwd)"
st="${MOCK_DOCKER_STATE:?MOCK_DOCKER_STATE is required}"
mkdir -p "$st/containers"
touch "$st/images"
echo "docker $*" >> "$st/calls.log"
runner_run() {
local name="" workdir="" user="" image="" ; local -a mounts=() envs=() cmd=()
while [[ $# -gt 0 ]]; do
case "$1" in
--rm|-it|-i|-t|--init|-d) shift ;;
--name) name="$2"; shift 2 ;;
--label|--add-host|--memory|--cpus|--network|--pids-limit) shift 2 ;;
-w) workdir="$2"; shift 2 ;;
-v) mounts+=("$2"); shift 2 ;;
-e) envs+=("$2"); shift 2 ;;
--user) user="$2"; shift 2 ;;
-*) echo "mock-docker: unsupported run flag $1" >&2; return 125 ;;
*) image="$1"; shift; cmd=("$@"); break ;;
esac
done
[[ -n "${MOCK_DOCKER_RUNNER_FAIL:-}" ]] && { echo "docker: Error response from daemon: mock runner failure" >&2; return 125; }
local -a map_src=() map_dst=()
for m in "${mounts[@]+"${mounts[@]}"}"; do
local src="${m%%:*}" rest="${m#*:}"; local dst="${rest%%:*}"
[[ -e "$src" ]] || { echo "docker: Error response from daemon: invalid mount config: bind source path does not exist: $src" >&2; return 125; }
[[ "$src" != "$dst" ]] && { map_src+=("$src"); map_dst+=("$dst"); }
done
remap() { local v="$1" i; for i in "${!map_dst[@]}"; do v="${v//${map_dst[$i]}/${map_src[$i]}}"; done; printf '%s' "$v"; }
local -a envv=() names=()
for e in "${envs[@]+"${envs[@]}"}"; do
if [[ "$e" == *=* ]]; then envv+=("$(remap "$e")"); names+=("${e%%=*}");
elif [[ -n "${!e+x}" ]]; then envv+=("$e=$(remap "${!e}")"); names+=("$e"); fi
done
local -a argv=(); for a in "${cmd[@]+"${cmd[@]}"}"; do argv+=("$(remap "$a")"); done
node -e '
const a = process.argv.slice(1); const f = a.shift(); const take = () => a.splice(0, Number(a.shift()));
const [name, image, user, workdir] = a.splice(0, 4);
const mounts = take(), env = take(), argv = a;
require("fs").appendFileSync(f, JSON.stringify({ name, image, user, workdir, mounts, env, argv }) + "\n");
' "$st/runs.jsonl" "$name" "$image" "$user" "$workdir" "${#mounts[@]}" "${mounts[@]+"${mounts[@]}"}" "${#names[@]}" "${names[@]+"${names[@]}"}" "${cmd[@]+"${cmd[@]}"}"
[[ -n "$name" ]] && echo "$image" > "$st/containers/$name"
# node alone (its own dir may hold Peppa's real bins, e.g. ~/.local/bin): the image has node.
local nodedir="$st/image-node"; mkdir -p "$nodedir"; ln -sf "$(command -v node)" "$nodedir/node"
local path="$ROOT/infra/runner/bin${MOCK_DOCKER_PATH:+:$MOCK_DOCKER_PATH}:$nodedir:/usr/bin:/bin:/usr/sbin:/sbin"
( cd "${workdir:-/}" && exec env -i "PATH=$path" "${envv[@]+"${envv[@]}"}" "${argv[@]}" )
local code=$?
[[ -n "$name" ]] && rm -f "$st/containers/$name"
return $code
}
case "$1" in
inspect) # docker inspect -f '{{.Config.Image}}' <container>
c="${@: -1}"
[[ -f "$st/containers/$c" ]] || { echo "Error: No such object: $c" >&2; exit 1; }
cat "$st/containers/$c" ;;
restart) # PEP-205: docker restart <container> — logs to $st/restarted, the container runs again
c="${@: -1}"
[[ -f "$st/containers/$c" ]] || { echo "Error response from daemon: No such container: $c" >&2; exit 1; }
echo "$c" >> "$st/restarted"; echo running > "$st/containers/$c"; echo "$c" ;;
version) echo "27.3.1-mock" ;;
builder) # PEP-287: docker builder prune -f (--reserved-space|--keep-storage) <N>GB
[[ "$2" == prune ]] || { echo "mock-docker: unsupported: $*" >&2; exit 1; }
for a in "$@"; do [[ "$a" == -a || "$a" == --all ]] && { echo "mock-docker: prune -a is not allowed" >&2; exit 1; }; done
if [[ -n "${MOCK_DOCKER_OLD_CLIENT:-}" && " $* " == *" --reserved-space "* ]]; then echo "unknown flag: --reserved-space" >&2; exit 125; fi
echo "Total reclaimed space: 12.5GB" ;;
ps) # PEP-189: docker ps … --format '{{.ID}}\t{{.Names}}\t{{.Ports}}\t{{.Labels}}' — rows of $st/ps (that format)
[[ -n "${MOCK_DOCKER_PS_FAIL:-}" ]] && { echo "Cannot connect to the Docker daemon" >&2; exit 1; }
[[ -f "$st/ps" ]] && cat "$st/ps"; exit 0 ;;
stop) # docker stop [-t N] <id|name> — drops the row from $st/ps, logs to $st/stopped
c="${@: -1}"
[[ -f "$st/ps" ]] && grep -q -E "^$c | $c " "$st/ps" || { echo "Error response from daemon: No such container: $c" >&2; exit 1; }
grep -v -E "^$c | $c " "$st/ps" > "$st/ps.tmp"; mv "$st/ps.tmp" "$st/ps"; echo "$c" >> "$st/stopped"; echo "$c" ;;
rm) # docker rm -f <name> — PEP-244: also drops its row from $st/ps and logs to $st/removed
c="${@: -1}"
inps=""; [[ -f "$st/ps" ]] && grep -q -E "^$c | $c " "$st/ps" && inps=1
[[ -f "$st/containers/$c" || -n "$inps" ]] || { echo "Error response from daemon: No such container: $c" >&2; exit 1; }
rm -f "$st/containers/$c"
[[ -n "$inps" ]] && { grep -v -E "^$c | $c " "$st/ps" > "$st/ps.tmp"; mv "$st/ps.tmp" "$st/ps"; }
echo "$c" >> "$st/removed"; echo "$c" ;;
image)
case "$2" in
inspect) img="${@: -1}"; grep -qxF "$img" "$st/images" || { echo "Error: No such image: $img" >&2; exit 1; }
if [[ "$3" == -f ]]; then echo "sha256:mock0123456789abcdef"; else echo "[{}]"; fi ;;
prune) exit 0 ;;
esac ;;
build) # docker build -t <image> ...
[[ -n "${MOCK_DOCKER_BUILD_FAIL:-}" ]] && { echo "mock build failed" >&2; exit 1; }
shift; while [[ $# -gt 0 ]]; do [[ $1 == -t ]] && { echo "$2" >> "$st/images"; break; }; shift; done
echo "Successfully built" ;;
compose) # docker compose -p <project> -f <file> [-f <override>] <up|down|run> ...
# PEP-112: the override with the project's secrets is kept as env-<project>.json.
shift; project=""; while [[ $# -gt 0 ]]; do case "$1" in -p) project="$2"; shift 2 ;; -f) [[ "$2" == *peppa-env.* ]] && cp "$2" "$st/env-$project.json"; shift 2 ;; *) break ;; esac; done
svc="${MOCK_DOCKER_SERVICE:-web}"
case "$1" in
up) grep -qxF "$IMAGE" "$st/images" || { echo "pull access denied for $IMAGE" >&2; exit 1; }
echo "$IMAGE" > "$st/containers/$project-$svc-1"; echo "Container $project-$svc-1 Started" ;;
down) rm -f "$st/containers/$project-"*; echo "Removed $project" ;;
run) echo "ran in $IMAGE: ${*: -1}" ;;
esac ;;
run) # PEP-17: the runner image (see the header) — anything else is the screenshot run below.
for a in "$@"; do [[ "$a" == peppa/runner* || ( -n "${PEPPA_RUNNER_IMAGE:-}" && "$a" == "$PEPPA_RUNNER_IMAGE" ) ]] && { shift; runner_run "$@"; exit $?; }; done
# PEP-44: docker run … <chrome image> -c '<chromium --screenshot …; base64>' _ <url> → base64 PNG on stdout
url="${@: -1}"
echo "$url" >> "$st/screenshots.log"
[[ -n "${MOCK_DOCKER_SCREENSHOT_FAIL:-}" ]] && { echo "mock screenshot failed" >&2; exit 1; }
# 1x1 transparent PNG
printf '%s' 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' ;;
*) echo "mock-docker: unsupported: $*" >&2; exit 1 ;;
esac