#!/usr/bin/env bash # Tests for scripts/deploy-enabled.sh # # cd.yml gates build and deploy on this script's answer (#3). It exists because # main was red on every push: deploy patches k3s/apps/spatial-agent-app/deployment.yaml in # mathias/infra, which does not exist, and build kept pushing images for a # service that is not deployed. # # The failure that matters is answering "true" when deployment is not meant to # be on, so the cases below are about what must NOT enable it: a directory, a # symlink, an absent marker. # # Usage: ./scripts/tests/deploy-enabled_test.sh set -uo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" SCRIPT="$ROOT/scripts/deploy-enabled.sh" pass=0; fail=0 ok() { printf ' ok %s\n' "$1"; pass=$((pass+1)); } bad() { printf ' FAIL %s\n %s\n' "$1" "${2:-}"; fail=$((fail+1)); } tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT echo "deploy-enabled tests" if [ -f "$SCRIPT" ]; then ok "script exists" else bad "script exists" "$SCRIPT missing"; echo; echo "$pass passed, $fail failed"; exit 1; fi # want_answer : stdout must equal and exit must be 0. want_answer() { local name="$1" dir="$2" want="$3" got rc got="$(bash "$SCRIPT" "$dir" 2>/dev/null)"; rc=$? if [ "$got" = "$want" ] && [ "$rc" -eq 0 ]; then ok "$name" else bad "$name" "got '$got' (exit $rc), want '$want' (exit 0)"; fi } mkdir "$tmp/regular"; echo "enabled" > "$tmp/regular/.deploy-enabled" mkdir "$tmp/absent" mkdir "$tmp/empty"; : > "$tmp/empty/.deploy-enabled" mkdir "$tmp/isdir"; mkdir "$tmp/isdir/.deploy-enabled" mkdir "$tmp/symlink"; echo "enabled" > "$tmp/symlink/target"; ln -s target "$tmp/symlink/.deploy-enabled" mkdir "$tmp/dangling"; ln -s nowhere "$tmp/dangling/.deploy-enabled" want_answer "a regular marker file enables deployment" "$tmp/regular" true want_answer "no marker means deployment stays off" "$tmp/absent" false want_answer "an empty marker file still enables (content is ignored)" "$tmp/empty" true want_answer "a marker that is a directory does not enable" "$tmp/isdir" false want_answer "a symlink to a regular file does not enable" "$tmp/symlink" false want_answer "a dangling symlink does not enable" "$tmp/dangling" false # A missing directory is not "off": answering false would let a typo in the # workflow look like a deliberate opt-out. Exit 2 and print nothing on stdout. got="$(bash "$SCRIPT" "$tmp/nope" 2>/dev/null)"; rc=$? if [ -z "$got" ] && [ "$rc" -eq 2 ]; then ok "a missing directory exits 2 with empty stdout" else bad "a missing directory exits 2 with empty stdout" "got '$got' (exit $rc)"; fi # Default directory is the current one. got="$(cd "$tmp/regular" && bash "$SCRIPT" 2>/dev/null)" [ "$got" = "true" ] && ok "with no argument it inspects the current directory" \ || bad "with no argument it inspects the current directory" "got '$got'" echo echo "$pass passed, $fail failed" [ "$fail" -eq 0 ]