| 1 | #!/usr/bin/env bash |
| 2 | # Tests for prepare-apps.sh, using a fake `carlos` on PATH. Run directly: |
| 3 | # scripts/prepare-apps_test.sh |
| 4 | set -uo pipefail |
| 5 | |
| 6 | here=$(cd -- "$(dirname -- "$0")" && pwd) |
| 7 | script=$here/prepare-apps.sh |
| 8 | work=$(mktemp -d) |
| 9 | trap 'rm -rf "$work"' EXIT |
| 10 | fails=0 |
| 11 | |
| 12 | # The fake logs every call to $CALLS. Its behaviour comes from the environment: |
| 13 | # FAKE_APPS what `apps list` prints (default: nothing) |
| 14 | # FAKE_LIST_FAIL 1: `apps list` fails |
| 15 | # FAKE_CREATE_FAIL 1: `apps create` fails |
| 16 | # FAKE_RELEASES what `releases` prints |
| 17 | # FAKE_RELEASES_FAIL 1: `releases` fails |
| 18 | # FAKE_PROMOTE_FAIL 1: `promote` fails |
| 19 | # FAKE_SHIP_BUSY n: `ship` says "too many uploads" n times, then works |
| 20 | mkdir "$work/bin" |
| 21 | cat >"$work/bin/carlos" <<'FAKE' |
| 22 | #!/usr/bin/env bash |
| 23 | echo "$*" >>"$CALLS" |
| 24 | case "$1 $2" in |
| 25 | "apps list") |
| 26 | [ "${FAKE_LIST_FAIL:-}" = 1 ] && { echo "list failed" >&2; exit 1; } |
| 27 | printf '%s\n' "${FAKE_APPS:-}" ;; |
| 28 | "apps create") |
| 29 | [ "${FAKE_CREATE_FAIL:-}" = 1 ] && { echo "app name is taken" >&2; exit 1; } ;; |
| 30 | esac |
| 31 | case "$1" in |
| 32 | releases) |
| 33 | [ "${FAKE_RELEASES_FAIL:-}" = 1 ] && { echo "console unreachable" >&2; exit 1; } |
| 34 | printf 'VERSION LABEL KIND BUILT TAG\n%s\n' "${FAKE_RELEASES:-}" ;; |
| 35 | ship) |
| 36 | if [ "${FAKE_SHIP_BUSY:-0}" -gt 0 ]; then |
| 37 | n=$(cat "$CALLS.busy" 2>/dev/null || echo 0) |
| 38 | if [ "$n" -lt "$FAKE_SHIP_BUSY" ]; then |
| 39 | echo $((n + 1)) >"$CALLS.busy" |
| 40 | echo "too many uploads — try again in 1s" >&2; exit 1 |
| 41 | fi |
| 42 | fi ;; |
| 43 | promote) |
| 44 | [ "${FAKE_PROMOTE_FAIL:-}" = 1 ] && { echo "promote refused" >&2; exit 1; } ;; |
| 45 | esac |
| 46 | exit 0 |
| 47 | FAKE |
| 48 | chmod +x "$work/bin/carlos" |
| 49 | |
| 50 | # The catalogue's app ids, read the way the script reads them, so a new |
| 51 | # catalogue entry (Contra's binary comes from another repo) needs no edit here. |
| 52 | catalog_ids=$(grep -o '"id": *"[^"]*"' "$here/../internal/catalog/catalog.json" | sed 's/.*: *"\(.*\)"/\1/') |
| 53 | |
| 54 | # What `apps list` prints when the account already has every catalogue app. |
| 55 | all_apps=$(for a in $catalog_ids; do echo "$a x"; done) |
| 56 | |
| 57 | # newdir makes a release dir with one build of each catalogue app. |
| 58 | newdir() { |
| 59 | local d |
| 60 | d=$(mktemp -d "$work/rel.XXXXXX") |
| 61 | for a in $catalog_ids; do |
| 62 | : >"$d/$a-linux-arm64-aaa111" |
| 63 | : >"$d/$a-linux-arm64-aaa111.COMMIT" |
| 64 | done |
| 65 | echo "$d" |
| 66 | } |
| 67 | |
| 68 | # check <name> <want-exit: zero|nonzero> <want-calls-regex or ''> <not-calls-regex or ''> -- script args... |
| 69 | check() { |
| 70 | local name=$1 want=$2 must=$3 mustnot=$4 |
| 71 | shift 5 |
| 72 | : >"$work/calls" |
| 73 | CALLS=$work/calls PATH=$work/bin:$PATH "$script" "$@" >"$work/out" 2>&1 |
| 74 | local rc=$? ok=1 |
| 75 | if [ "$want" = zero ] && [ $rc -ne 0 ]; then ok=0; fi |
| 76 | if [ "$want" = nonzero ] && [ $rc -eq 0 ]; then ok=0; fi |
| 77 | if [ -n "$must" ] && ! grep -qE "$must" "$work/calls"; then ok=0; fi |
| 78 | if [ -n "$mustnot" ] && grep -qE "$mustnot" "$work/calls"; then ok=0; fi |
| 79 | if [ $ok -eq 1 ]; then |
| 80 | echo "ok $name" |
| 81 | else |
| 82 | echo "FAIL $name (exit $rc)"; sed 's/^/ out: /' "$work/out"; sed 's/^/ call: /' "$work/calls" |
| 83 | fails=$((fails + 1)) |
| 84 | fi |
| 85 | } |
| 86 | |
| 87 | d=$(newdir) |
| 88 | export PREPARE_APPS_RETRY_SLEEP=0 |
| 89 | |
| 90 | # Deploy creates a stray instance, so it must never be called, in any case below. |
| 91 | # A failed releases lookup must stop the script before it ships. |
| 92 | FAKE_APPS="$all_apps" FAKE_RELEASES_FAIL=1 \ |
| 93 | check "releases lookup failure stops before ship" nonzero '^releases ' '^(deploy|ship|promote) ' -- acme "$d" |
| 94 | |
| 95 | # An app that is not in this account's listing, and cannot be created, stops it. |
| 96 | FAKE_APPS='' FAKE_CREATE_FAIL=1 \ |
| 97 | check "app owned elsewhere stops the script" nonzero '^apps create ' '^(deploy|ship|instances) ' -- acme "$d" |
| 98 | |
| 99 | # A failed apps listing stops it too, without trying to create. |
| 100 | FAKE_LIST_FAIL=1 \ |
| 101 | check "apps listing failure stops before create" nonzero '^apps list ' '^(apps create|deploy|ship) ' -- acme "$d" |
| 102 | |
| 103 | # Several builds of one app and no explicit version is ambiguous. |
| 104 | d2=$(newdir) |
| 105 | for a in $catalog_ids; do : >"$d2/$a-linux-arm64-bbb222"; done |
| 106 | check "ambiguous release dir is refused" nonzero '' '^(apps|deploy|instances|releases) ' -- acme "$d2" |
| 107 | grep -qi 'version' "$work/out" || { echo "FAIL ambiguity message should mention version"; fails=$((fails + 1)); } |
| 108 | |
| 109 | # An explicit version picks the right build out of several. |
| 110 | FAKE_APPS="$all_apps" \ |
| 111 | check "explicit version resolves the ambiguity" zero 'ship .*--version bbb222 .*welcome-linux-arm64-bbb222' '^deploy ' -- acme "$d2" bbb222 |
| 112 | |
| 113 | # Version match is exact: aaa111 listed as a prefix of the shipped version is not shipped. |
| 114 | FAKE_APPS="$all_apps" FAKE_RELEASES='aaa1112 - binary now -' \ |
| 115 | check "releases match is exact, not substring" zero '^ship ' '^deploy ' -- acme "$d" |
| 116 | |
| 117 | # Already shipped: promote to edge, don't ship or deploy. Existing apps: no create. |
| 118 | FAKE_APPS="$all_apps" FAKE_RELEASES='aaa111 - binary now -' \ |
| 119 | check "shipped version is only promoted, existing app not created" zero '^promote .* aaa111 edge$' '^(deploy|ship|apps create) ' -- acme "$d" |
| 120 | |
| 121 | # Fresh account: create, ship, promote to edge, enable, and never deploy or touch stable. |
| 122 | check "fresh account creates, ships, promotes to edge, enables" zero 'apps create .*scratchpad' '^deploy |stable' -- acme "$d" |
| 123 | grep -qE '^promote --app scratchpad --account acme --no-prompt aaa111 edge$' "$work/calls" || { echo "FAIL promote should target edge"; fails=$((fails + 1)); } |
| 124 | grep -qE '^instances enable --app scratchpad' "$work/calls" || { echo "FAIL instances should be enabled"; fails=$((fails + 1)); } |
| 125 | |
| 126 | # A failing promote stops the script before instances are enabled. |
| 127 | FAKE_APPS="$all_apps" FAKE_PROMOTE_FAIL=1 \ |
| 128 | check "failing promote stops the script" nonzero '^promote ' '^(deploy|instances) ' -- acme "$d" |
| 129 | |
| 130 | # "too many uploads" is retried until ship works. |
| 131 | rm -f "$work/calls.busy" |
| 132 | FAKE_APPS="$all_apps" FAKE_SHIP_BUSY=2 \ |
| 133 | check "ship is retried while uploads are rate limited" zero '^promote ' '^deploy ' -- acme "$d" |
| 134 | [ "$(grep -c '^ship .*welcome' "$work/calls")" -eq 3 ] || { echo "FAIL ship should be tried 3 times"; fails=$((fails + 1)); } |
| 135 | |
| 136 | [ $fails -eq 0 ] && echo "all passed" || { echo "$fails failed"; exit 1; } |
| 137 | |