#!/bin/bash # A felhom-samba VERSION NUMBER IS PUSHED ONCE — the test for build-samba-image.sh's registry and tree checks # (R-972, 1.2.0). Same method as test_build_push_once.sh: a fake `docker` first on PATH drives the REAL script; # the fake answers `build`/`push` with a marker, so a run that got past a refusal is caught, and nothing is pushed. # Run from controller/: bash scripts/test_samba_build_push_once.sh set -u HERE="$(cd "$(dirname "$0")" && pwd)" SH="$HERE/build-samba-image.sh" fails=0; ran=0 make_fake_docker() { mkdir -p "$1" cat > "$1/docker" <<'FAKE' #!/bin/bash case "$1 $2" in "manifest inspect") case "${FAKE_MODE}" in present) echo '{"schemaVersion":2}'; exit 0 ;; absent) echo "manifest unknown: manifest unknown" >&2; exit 1 ;; down) echo "dial tcp: connection refused" >&2; exit 1 ;; esac ;; "build "*|"push "*) echo "FAKE-DOCKER-REACHED: $*"; exit 0 ;; esac exit 0 FAKE chmod +x "$1/docker" } run() { # $1 mode, $2 repo dir ("" = default), rest = args local mode="$1" repo="$2"; shift 2 local tmp; tmp="$(mktemp -d)"; make_fake_docker "$tmp/bin" if [[ -n "$repo" ]]; then FAKE_MODE="$mode" FELHOM_REPO_DIR="$repo" PATH="$tmp/bin:$PATH" bash "$SH" "$@" >"$tmp/out" 2>&1 else FAKE_MODE="$mode" PATH="$tmp/bin:$PATH" bash "$SH" "$@" >"$tmp/out" 2>&1; fi rc=$?; out="$(cat "$tmp/out")"; rm -rf "$tmp" } check() { # name, want_rc, want_text, forbid_text ran=$((ran+1)) if [[ "$rc" != "$2" ]] || { [[ -n "$3" ]] && ! grep -qi -- "$3" <<<"$out"; } || { [[ -n "${4:-}" ]] && grep -qi -- "$4" <<<"$out"; }; then fails=$((fails+1)); echo " FAIL $1 (exit $rc)"; echo "$out" | tail -4 | sed 's/^/ /'; return fi echo " ok $1" } scratch_repo() { local d; d="$(mktemp -d)"; git -C "$d" init -q; git -C "$d" config user.email t@t; git -C "$d" config user.name t mkdir -p "$d/controller/infra-images/samba"; echo x > "$d/controller/infra-images/samba/Dockerfile" git -C "$d" add -A >/dev/null; git -C "$d" commit -qm one >/dev/null; git -C "$d" update-ref refs/remotes/origin/main HEAD; echo "$d" } echo "build-samba-image.sh — a version number is pushed once, from a clean tree" R="$(scratch_repo)" run present "$R" 9.9.9 --push; check "tag present -> refuses" 1 "already exists in the registry" "FAKE-DOCKER-REACHED" run down "$R" 9.9.9 --push; check "registry unreachable -> refuses" 1 "Could not establish whether" "FAKE-DOCKER-REACHED" run absent "$R" 9.9.9 --push; check "tag absent, clean tree -> builds and pushes" 0 "FAKE-DOCKER-REACHED: push" "refusing" echo dirt > "$R/controller/infra-images/samba/extra" run absent "$R" 9.9.9 --push; check "dirty tree -> refuses" 1 "uncommitted changes" "FAKE-DOCKER-REACHED" rm -f "$R/controller/infra-images/samba/extra"; echo y > "$R/controller/f2"; git -C "$R" add -A >/dev/null; git -C "$R" commit -qm two >/dev/null run absent "$R" 9.9.9 --push; check "ahead of origin/main -> refuses" 1 "not at origin/main" "FAKE-DOCKER-REACHED" rm -rf "$R" run present "" 9.9.9; check "local build, tag present -> not refused" 0 "FAKE-DOCKER-REACHED: build" "already exists" echo [[ $fails -eq 0 ]] && { echo "all $ran cases behaved"; exit 0; } echo "$fails of $ran cases FAILED"; exit 1