Files
ExecuteR/scripts/tests/Test-DeployDatabase.sh
Alphaeus Mote 2936b08c6c fix(deploy): restart NATS when nats.conf changed [skip ci]
compose up -d leaves NATS on its old config when only the bind-mounted
nats.conf changed, so the 8 MB max_payload never applied on upgrades.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 05:47:27 -05:00

210 lines
14 KiB
Bash
Executable File

#!/usr/bin/env bash
#=============================================================================
# Checks deploy.sh's database decisions without Docker: the functions alone,
# then configure_database on fixtures in a temp folder (a .env and the stack's
# data folders) for new installs, existing SQLite installs, re-runs, a lost
# password, your own Postgres, and the move to Postgres. Plain asserts; exits
# 1 when any fails.
#
# bash scripts/tests/Test-DeployDatabase.sh
#=============================================================================
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO="$(cd "$HERE/../.." && pwd)"
# shellcheck disable=SC1091
# (carriage returns dropped: a Windows checkout may have them.)
EXECUTER_DEPLOY_FUNCTIONS_ONLY=1 source <(tr -d $'\r' < "$REPO/scripts/deploy.sh")
set +eE
trap - ERR
FAILED=0
PASSED=0
pass() { PASSED=$((PASSED + 1)); }
fail() { FAILED=$((FAILED + 1)); printf 'FAIL: %s\n' "$*" >&2; }
equal() { [[ "$2" == "$3" ]] && pass || fail "$1: expected [$2], got [$3]"; }
true_() { if eval "$2"; then pass; else fail "$1"; fi; }
false_() { if eval "$2"; then fail "$1"; else pass; fi; }
# ── The functions ────────────────────────────────────────────────────────────
equal "a new .env is unset, nothing on disk" "postgres new-install" "$(database_provider 1 postgres /nonexistent/app.db)"
equal "no provider, nothing on disk" "postgres new-install" "$(database_provider 0 '' /nonexistent/app.db)"
equal "the operator's sqlite" "sqlite configured" "$(database_provider 0 sqlite /nonexistent/app.db)"
equal "the backend's spellings: PostgreSQL" "postgres configured" "$(database_provider 0 ' PostgreSQL ' /nonexistent/app.db)"
equal "the backend's spellings: mssql" "sqlserver configured" "$(database_provider 0 mssql /nonexistent/app.db)"
equal "the backend reads anything else as sqlite" "sqlite configured" "$(database_provider 0 sqllite /nonexistent/app.db)"
true_ "no string is the bundled one" "postgres_bundled postgres ''"
true_ "the bundled string" "postgres_bundled postgres '$BUNDLED_POSTGRES_CONNECTION'"
true_ "Server= and spacing" "postgres_bundled postgres 'Server = POSTGRES ; Database=x'"
false_ "your own server" "postgres_bundled postgres 'Host=db.example.com;Database=executer'"
false_ "a host that only starts with postgres" "postgres_bundled postgres 'Host=postgres.example.com'"
false_ "sqlite" "postgres_bundled sqlite ''"
true_ "the sample public URL" "is_sample_address 'https://control.example.com'"
true_ "a sample host list" "is_sample_address 'control.example.com, *.example.org'"
true_ "a sample with a port" "is_sample_address 'https://Control.Example.com:8443/'"
false_ "a real URL" "is_sample_address 'https://executer.contoso.com'"
false_ "a real host beside a sample" "is_sample_address 'control.example.com,executer.contoso.com'"
false_ "a name that only contains example" "is_sample_address 'https://example.com.contoso.net'"
false_ "empty" "is_sample_address ''"
SAMPLES="$(mktemp -d)"
printf 'PUBLIC_SERVER_URL=https://control.example.com\nTRUSTED_HOSTS=control.example.com\nPROXY_PUBLIC_HOSTNAME=executer.contoso.com\n' > "$SAMPLES/.env"
(cd "$SAMPLES" && set -a && source .env && set +a && clear_sample_addresses >/dev/null)
equal "a sample URL is cleared" "PUBLIC_SERVER_URL=" "$(grep '^PUBLIC_SERVER_URL=' "$SAMPLES/.env")"
equal "sample hosts are cleared" "TRUSTED_HOSTS=" "$(grep '^TRUSTED_HOSTS=' "$SAMPLES/.env")"
equal "a real hostname is kept" "PROXY_PUBLIC_HOSTNAME=executer.contoso.com" "$(grep '^PROXY_PUBLIC_HOSTNAME=' "$SAMPLES/.env")"
rm -rf "$SAMPLES"
CONF="$(mktemp -d)"
printf 'max_payload: 1MB\n' > "$CONF/nats.conf"
true_ "no stamp: changed" "config_changed '$CONF/nats.conf' '$CONF/stamp'"
config_applied "$CONF/nats.conf" "$CONF/stamp"
false_ "applied: unchanged" "config_changed '$CONF/nats.conf' '$CONF/stamp'"
printf 'max_payload: 8MB\n' > "$CONF/nats.conf"
true_ "edited after: changed" "config_changed '$CONF/nats.conf' '$CONF/stamp'"
rm -rf "$CONF"
EMPTY="$(mktemp -d)"; FULL="$(mktemp -d)"; touch "$FULL/PG_VERSION"
equal "a password is kept" keep "$(database_password_action secret "$FULL")"
equal "none and no database: generate" generate "$(database_password_action '' "$EMPTY")"
equal "none and no folder: generate" generate "$(database_password_action '' "$EMPTY/missing")"
equal "none over a database: lost" lost "$(database_password_action '' "$FULL")"
rm -rf "$EMPTY" "$FULL"
equal "profile added" "postgres" "$(compose_profiles_with '' add postgres)"
equal "profile added once" "tools,postgres" "$(compose_profiles_with 'postgres,tools' add postgres)"
equal "profile removed, others kept" "tools,debug" "$(compose_profiles_with 'tools, postgres,debug' remove postgres)"
equal "nothing left" "" "$(compose_profiles_with 'postgres' remove postgres)"
true_ "2.20.2 is enough" "version_at_least 2.20.2 2.20.2"
true_ "2.29.7 is enough" "version_at_least 2.29.7 2.20.2"
true_ "numeric, not text: 2.100.0" "version_at_least 2.100.0 2.20.2"
false_ "2.20.1 is not" "version_at_least 2.20.1 2.20.2"
false_ "2.9.0 is not" "version_at_least 2.9.0 2.20.2"
false_ "an unknown version is not" "version_at_least '' 2.20.2"
PASSWORD="$(new_password)"
true_ "a password of 32 letters and digits" "[[ '$PASSWORD' =~ ^[A-Za-z0-9]{32}$ ]]"
[[ "$(new_password)" != "$PASSWORD" ]] && pass || fail "two passwords differ"
# ── set_env: written, quoted when needed, read back by a shell, exported ─────
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
(
cd "$WORK" || exit 1
printf 'A=1\nKEEP=x\nA=2\n' > .env
set_env A 'Host=postgres;Port=5432&x|y'
set_env NEW plain_value-1.2
set_env EMPTY ''
equal "every line for the key replaced" 0 "$(grep -c '^A=[12]$' .env)"
equal "quoted" 'A="Host=postgres;Port=5432&x|y"' "$(grep '^A=' .env | sort -u)"
equal "plain" 'NEW=plain_value-1.2' "$(grep '^NEW=' .env)"
equal "empty" 'EMPTY=' "$(grep '^EMPTY=' .env)"
equal "others untouched" 'KEEP=x' "$(grep '^KEEP=' .env)"
equal "exported" 'Host=postgres;Port=5432&x|y' "$(bash -c 'printf %s "$A"')"
equal "a shell reads it back" 'Host=postgres;Port=5432&x|y' "$(set -a; source ./.env; printf %s "$A")"
exit $FAILED
) || FAILED=$((FAILED + $?))
# ── configure_database on fixtures ───────────────────────────────────────────
# One scenario: a stack folder with the given .env and data, configure_database
# run in a fresh shell (as deploy.sh: .env sourced and exported first), then
# the resulting .env read back as deploy.sh and compose would.
scenario() {
local name="$1" new_env="$2" env_text="$3" sqlite="$4" pgdata="$5" dir
dir="$WORK/$name"
mkdir -p "$dir/API/Data" "$dir/Postgres/Data"
printf '%s\n' "$env_text" > "$dir/.env"
[[ $sqlite == yes ]] && printf 'SQLite format 3\0' > "$dir/API/Data/app.db"
[[ $pgdata == yes ]] && printf '18\n' > "$dir/Postgres/Data/PG_VERSION"
(
cd "$dir" || exit 99
set -a; source ./.env; set +a
configure_database "$new_env" "$dir/API/Data" "$dir/Postgres/Data"
printf '%s %s %s\n' "$DB_PROVIDER" "$DB_REASON" "$BUNDLED_POSTGRES" > result
) > "$dir/out" 2>&1
echo $? > "$dir/code"
}
env_of() { (set -a; source "$WORK/$1/.env"; printf %s "${!2:-}"); }
result_of() { cat "$WORK/$1/result" 2>/dev/null; }
OLD_ENV="$(printf 'STACK_NAME=stk\nDATABASE_PROVIDER=sqlite\nDATABASE_CONNECTION_STRING=\nDATABASE_BACKUP_PATH=/data/backups')"
TEMPLATE="$(tr -d $'\r' < "$REPO/.env.example")"
# A new install from the template: the bundled Postgres with a generated password.
scenario new 1 "$TEMPLATE" no no
equal "new: decision" "postgres new-install 1" "$(result_of new)"
equal "new: provider" postgres "$(env_of new DATABASE_PROVIDER)"
equal "new: connection string" "$BUNDLED_POSTGRES_CONNECTION" "$(env_of new DATABASE_CONNECTION_STRING)"
true_ "new: password generated" "[[ '$(env_of new DATABASE_PASSWORD)' =~ ^[A-Za-z0-9]{32}$ ]]"
equal "new: profile on" postgres "$(env_of new COMPOSE_PROFILES)"
equal "new: one password line" 1 "$(grep -c '^DATABASE_PASSWORD=' "$WORK/new/.env")"
# The live server today: an older .env that says sqlite, and its database.
scenario live 0 "$OLD_ENV" yes no
equal "live: kept on sqlite" "sqlite configured 0" "$(result_of live)"
equal "live: provider" sqlite "$(env_of live DATABASE_PROVIDER)"
equal "live: no string" "" "$(env_of live DATABASE_CONNECTION_STRING)"
equal "live: no password" "" "$(env_of live DATABASE_PASSWORD)"
equal "live: profile off" "" "$(env_of live COMPOSE_PROFILES)"
equal "live: database untouched" "$(printf 'SQLite format 3\0' | wc -c)" "$(wc -c < "$WORK/live/API/Data/app.db")"
# An existing install whose .env names no provider: the SQLite file decides, and says so.
scenario unset 0 "$(printf 'STACK_NAME=stk\nDATABASE_BACKUP_PATH=/data/backups')" yes no
equal "unset: kept on sqlite" "sqlite existing-sqlite 0" "$(result_of unset)"
equal "unset: written to .env" "DATABASE_PROVIDER=sqlite" "$(grep '^DATABASE_PROVIDER=' "$WORK/unset/.env")"
true_ "unset: the note" "grep -q 'keeping SQLite' '$WORK/unset/out' && grep -q 'migrate-database' '$WORK/unset/out'"
equal "unset: profile off" "" "$(env_of unset COMPOSE_PROFILES)"
# .env lost, the template copied again, the SQLite database still there.
scenario relost 1 "$TEMPLATE" yes no
equal "relost: kept on sqlite" "sqlite existing-sqlite 0" "$(result_of relost)"
equal "relost: string cleared" "" "$(env_of relost DATABASE_CONNECTION_STRING)"
equal "relost: profile off" "" "$(env_of relost COMPOSE_PROFILES)"
equal "relost: no password" "" "$(env_of relost DATABASE_PASSWORD)"
# An existing .env without a provider and no database yet: Postgres.
scenario fresh 0 "$(printf 'STACK_NAME=stk')" no no
equal "fresh: postgres" "postgres new-install 1" "$(result_of fresh)"
true_ "fresh: password" "[[ -n '$(env_of fresh DATABASE_PASSWORD)' ]]"
# Deploying again: the password, string and profile stay as they are.
cp -r "$WORK/new" "$WORK/again"
before="$(env_of new DATABASE_PASSWORD)"
printf '18\n' > "$WORK/again/Postgres/Data/PG_VERSION"
scenario again 0 "$(cat "$WORK/new/.env")" no yes
equal "again: same password" "$before" "$(env_of again DATABASE_PASSWORD)"
equal "again: profile once" postgres "$(env_of again COMPOSE_PROFILES)"
equal "again: decision" "postgres configured 1" "$(result_of again)"
# A database on disk, its password gone from .env: stop, never a new password over it.
scenario lost 0 "$(printf 'DATABASE_PROVIDER=postgres\nDATABASE_PASSWORD=\nCOMPOSE_PROFILES=postgres')" no yes
false_ "lost: refused" "[[ \$(cat '$WORK/lost/code') == 0 ]]"
true_ "lost: says why" "grep -q 'DATABASE_PASSWORD in .env is empty' '$WORK/lost/out'"
equal "lost: nothing generated" "" "$(env_of lost DATABASE_PASSWORD)"
# Your own Postgres: not bundled, the profile taken out, others kept, no password made up.
scenario own 0 "$(printf 'DATABASE_PROVIDER=postgres\nDATABASE_CONNECTION_STRING="Host=db.example.com;Database=executer;Username=executer"\nCOMPOSE_PROFILES=postgres,tools')" no no
equal "own: not bundled" "postgres configured 0" "$(result_of own)"
equal "own: profile out" tools "$(env_of own COMPOSE_PROFILES)"
equal "own: string kept" "Host=db.example.com;Database=executer;Username=executer" "$(env_of own DATABASE_CONNECTION_STRING)"
equal "own: no password" "" "$(env_of own DATABASE_PASSWORD)"
# The move: the operator sets postgres on a SQLite install; the SQLite file stays for migrate-database.
scenario move 0 "$(printf 'STACK_NAME=stk\nDATABASE_PROVIDER=postgres\nDATABASE_CONNECTION_STRING=\nDATABASE_BACKUP_PATH=/data/backups')" yes no
equal "move: bundled" "postgres configured 1" "$(result_of move)"
equal "move: string" "$BUNDLED_POSTGRES_CONNECTION" "$(env_of move DATABASE_CONNECTION_STRING)"
true_ "move: password" "[[ -n '$(env_of move DATABASE_PASSWORD)' ]]"
equal "move: profile on" postgres "$(env_of move COMPOSE_PROFILES)"
true_ "move: sqlite file kept" "[[ -s '$WORK/move/API/Data/app.db' ]]"
# Back to SQLite: the profile goes, so the bundled database is not started.
scenario back 0 "$(printf 'DATABASE_PROVIDER=sqlite\nDATABASE_CONNECTION_STRING=\nDATABASE_PASSWORD=kept\nCOMPOSE_PROFILES=postgres')" yes yes
equal "back: sqlite" "sqlite configured 0" "$(result_of back)"
equal "back: profile off" "" "$(env_of back COMPOSE_PROFILES)"
equal "back: password kept" kept "$(env_of back DATABASE_PASSWORD)"
echo "$PASSED passed, $FAILED failed"
[[ $FAILED -eq 0 ]]