#!/usr/bin/env bash # Tests for prepare-apps.sh, using a fake `carlos` on PATH. Run directly: # scripts/prepare-apps_test.sh set -uo pipefail here=$(cd -- "$(dirname -- "$0")" && pwd) script=$here/prepare-apps.sh work=$(mktemp -d) trap 'rm -rf "$work"' EXIT fails=0 # The fake logs every call to $CALLS. Its behaviour comes from the environment: # FAKE_APPS what `apps list` prints (default: nothing) # FAKE_LIST_FAIL 1: `apps list` fails # FAKE_CREATE_FAIL 1: `apps create` fails # FAKE_RELEASES what `releases` prints # FAKE_RELEASES_FAIL 1: `releases` fails # FAKE_PROMOTE_FAIL 1: `promote` fails # FAKE_SHIP_BUSY n: `ship` says "too many uploads" n times, then works mkdir "$work/bin" cat >"$work/bin/carlos" <<'FAKE' #!/usr/bin/env bash echo "$*" >>"$CALLS" case "$1 $2" in "apps list") [ "${FAKE_LIST_FAIL:-}" = 1 ] && { echo "list failed" >&2; exit 1; } printf '%s\n' "${FAKE_APPS:-}" ;; "apps create") [ "${FAKE_CREATE_FAIL:-}" = 1 ] && { echo "app name is taken" >&2; exit 1; } ;; esac case "$1" in releases) [ "${FAKE_RELEASES_FAIL:-}" = 1 ] && { echo "console unreachable" >&2; exit 1; } printf 'VERSION LABEL KIND BUILT TAG\n%s\n' "${FAKE_RELEASES:-}" ;; ship) if [ "${FAKE_SHIP_BUSY:-0}" -gt 0 ]; then n=$(cat "$CALLS.busy" 2>/dev/null || echo 0) if [ "$n" -lt "$FAKE_SHIP_BUSY" ]; then echo $((n + 1)) >"$CALLS.busy" echo "too many uploads — try again in 1s" >&2; exit 1 fi fi ;; promote) [ "${FAKE_PROMOTE_FAIL:-}" = 1 ] && { echo "promote refused" >&2; exit 1; } ;; esac exit 0 FAKE chmod +x "$work/bin/carlos" # The catalogue's app ids, read the way the script reads them, so a new # catalogue entry (Contra's binary comes from another repo) needs no edit here. catalog_ids=$(grep -o '"id": *"[^"]*"' "$here/../internal/catalog/catalog.json" | sed 's/.*: *"\(.*\)"/\1/') # What `apps list` prints when the account already has every catalogue app. all_apps=$(for a in $catalog_ids; do echo "$a x"; done) # newdir makes a release dir with one build of each catalogue app. newdir() { local d d=$(mktemp -d "$work/rel.XXXXXX") for a in $catalog_ids; do : >"$d/$a-linux-arm64-aaa111" : >"$d/$a-linux-arm64-aaa111.COMMIT" done echo "$d" } # check -- script args... check() { local name=$1 want=$2 must=$3 mustnot=$4 shift 5 : >"$work/calls" CALLS=$work/calls PATH=$work/bin:$PATH "$script" "$@" >"$work/out" 2>&1 local rc=$? ok=1 if [ "$want" = zero ] && [ $rc -ne 0 ]; then ok=0; fi if [ "$want" = nonzero ] && [ $rc -eq 0 ]; then ok=0; fi if [ -n "$must" ] && ! grep -qE "$must" "$work/calls"; then ok=0; fi if [ -n "$mustnot" ] && grep -qE "$mustnot" "$work/calls"; then ok=0; fi if [ $ok -eq 1 ]; then echo "ok $name" else echo "FAIL $name (exit $rc)"; sed 's/^/ out: /' "$work/out"; sed 's/^/ call: /' "$work/calls" fails=$((fails + 1)) fi } d=$(newdir) export PREPARE_APPS_RETRY_SLEEP=0 # Deploy creates a stray instance, so it must never be called, in any case below. # A failed releases lookup must stop the script before it ships. FAKE_APPS="$all_apps" FAKE_RELEASES_FAIL=1 \ check "releases lookup failure stops before ship" nonzero '^releases ' '^(deploy|ship|promote) ' -- acme "$d" # An app that is not in this account's listing, and cannot be created, stops it. FAKE_APPS='' FAKE_CREATE_FAIL=1 \ check "app owned elsewhere stops the script" nonzero '^apps create ' '^(deploy|ship|instances) ' -- acme "$d" # A failed apps listing stops it too, without trying to create. FAKE_LIST_FAIL=1 \ check "apps listing failure stops before create" nonzero '^apps list ' '^(apps create|deploy|ship) ' -- acme "$d" # Several builds of one app and no explicit version is ambiguous. d2=$(newdir) for a in $catalog_ids; do : >"$d2/$a-linux-arm64-bbb222"; done check "ambiguous release dir is refused" nonzero '' '^(apps|deploy|instances|releases) ' -- acme "$d2" grep -qi 'version' "$work/out" || { echo "FAIL ambiguity message should mention version"; fails=$((fails + 1)); } # An explicit version picks the right build out of several. FAKE_APPS="$all_apps" \ check "explicit version resolves the ambiguity" zero 'ship .*--version bbb222 .*welcome-linux-arm64-bbb222' '^deploy ' -- acme "$d2" bbb222 # Version match is exact: aaa111 listed as a prefix of the shipped version is not shipped. FAKE_APPS="$all_apps" FAKE_RELEASES='aaa1112 - binary now -' \ check "releases match is exact, not substring" zero '^ship ' '^deploy ' -- acme "$d" # Already shipped: promote to edge, don't ship or deploy. Existing apps: no create. FAKE_APPS="$all_apps" FAKE_RELEASES='aaa111 - binary now -' \ check "shipped version is only promoted, existing app not created" zero '^promote .* aaa111 edge$' '^(deploy|ship|apps create) ' -- acme "$d" # Fresh account: create, ship, promote to edge, enable, and never deploy or touch stable. check "fresh account creates, ships, promotes to edge, enables" zero 'apps create .*scratchpad' '^deploy |stable' -- acme "$d" grep -qE '^promote --app scratchpad --account acme --no-prompt aaa111 edge$' "$work/calls" || { echo "FAIL promote should target edge"; fails=$((fails + 1)); } grep -qE '^instances enable --app scratchpad' "$work/calls" || { echo "FAIL instances should be enabled"; fails=$((fails + 1)); } # A failing promote stops the script before instances are enabled. FAKE_APPS="$all_apps" FAKE_PROMOTE_FAIL=1 \ check "failing promote stops the script" nonzero '^promote ' '^(deploy|instances) ' -- acme "$d" # "too many uploads" is retried until ship works. rm -f "$work/calls.busy" FAKE_APPS="$all_apps" FAKE_SHIP_BUSY=2 \ check "ship is retried while uploads are rate limited" zero '^promote ' '^deploy ' -- acme "$d" [ "$(grep -c '^ship .*welcome' "$work/calls")" -eq 3 ] || { echo "FAIL ship should be tried 3 times"; fails=$((fails + 1)); } [ $fails -eq 0 ] && echo "all passed" || { echo "$fails failed"; exit 1; }