#!/usr/bin/env bats # crosscode deploy against a local git "origin", with stub Events hooks that log what they do # instead of running npm, Prisma or PM2. Covers the checks, requirements, order, rollback and # --every-church. What needs a real server is tested on a VM (EVENTS-2.2-PLAN ยง6). git_q() { git -c user.name=test -c user.email=test@test -c init.defaultBranch=main "$@"; } setup() { TMP="$BATS_TEST_TMPDIR" export CROSSCODE_TEST=1 CROSSCODE_NO_UPDATE=1 export CROSSCODE_SITES="$TMP/sites" export CROSSCODE_REPO="$TMP/origin" export LOG="$TMP/log" : > "$LOG" # The crosscode script, with stub hooks for Events. export CROSSCODE_HOME="$TMP/tools" mkdir -p "$CROSSCODE_HOME/products" cp -R "$BATS_TEST_DIRNAME/../lib" "$BATS_TEST_DIRNAME/../crosscode" "$CROSSCODE_HOME/" cat > "$CROSSCODE_HOME/products/events.sh" <<'EOF' ref() { git -C "$1" describe --tags --exact-match 2>/dev/null || echo main; } events_backup() { echo "backup $(ref "$1")" >> "$LOG"; [ "${FAIL_AT:-}" != backup ]; } events_install_deps() { echo "install $(ref "$1")" >> "$LOG"; } events_migrate() { echo "migrate $(ref "$1")" >> "$LOG"; [ "${FAIL_AT:-}" != migrate ]; } events_build() { echo "build $(ref "$1")" >> "$LOG"; [ "${FAIL_AT:-}" != build ]; } events_rollback_build() { echo "rollback_build $(ref "$1")" >> "$LOG"; } events_expected_version() { cat "$1/apps/events/VERSION"; } events_reload() { echo "reload $(ref "$1")" >> "$LOG"; } events_health_url() { echo http://stub/health; } wait_for_version() { echo "health $2" >> "$LOG"; [ "${FAIL_HEALTH:-}" != "$2" ]; } EOF # origin: events@2.2.0, events@2.2.1, events@2.3.0 (which requires accounts 1.0.0). git_q init -q "$TMP/origin" mkdir -p "$TMP/origin/apps/events" release 2.2.0 "## 2.2.0" release 2.2.1 "## 2.2.1" release 2.3.0 "## 2.3.0 โ€” requires accounts 1.0.0" add_church hope events@2.2.0 } release() { printf '%s\n' "$1" > "$TMP/origin/apps/events/VERSION" printf '# What is new\n\n%s\n\n- Notes.\n' "$2" > "$TMP/origin/apps/events/WHATS-NEW.md" git_q -C "$TMP/origin" add -A git_q -C "$TMP/origin" commit -q -m "events $1" git_q -C "$TMP/origin" tag "events@$1" } add_church() { local church="$1" at="$2" mkdir -p "$CROSSCODE_SITES/$church" git_q clone -q "$CROSSCODE_REPO" "$CROSSCODE_SITES/$church/events" git_q -C "$CROSSCODE_SITES/$church/events" checkout -q --detach "$at" cat > "$CROSSCODE_SITES/$church/church.env" </dev/null || echo main; } cc() { bash "$CROSSCODE_HOME/crosscode" "$@"; } @test "deploy --check refuses a release whose requirement isn't met, and changes nothing" { run cc deploy hope events --check [ "$status" -eq 1 ] [[ "$output" == *"events 2.3.0 requires accounts 1.0.0"* ]] [[ "$output" == *"Nothing was changed"* ]] [ "$(on hope)" = events@2.2.0 ] [ ! -s "$LOG" ] } @test "deploy to the latest release is refused while its requirement isn't met" { run cc deploy hope events [ "$status" -eq 1 ] [[ "$output" == *"Requirements not met"* ]] [ "$(on hope)" = events@2.2.0 ] [ ! -s "$LOG" ] } @test "deploy --check with a met release says it's ready" { run cc deploy hope events --check --version events@2.2.1 [ "$status" -eq 0 ] [[ "$output" == *"events@2.2.0 โ†’ events@2.2.1"* ]] [[ "$output" == *"ready to deploy"* ]] [ ! -s "$LOG" ] } @test "deploy runs backup, checkout, install, migrate, build, reload and health, in that order" { run cc deploy hope events --version events@2.2.1 [ "$status" -eq 0 ] [ "$(on hope)" = events@2.2.1 ] [ "$(cat "$LOG")" = "$(printf '%s\n' 'backup events@2.2.0' 'install events@2.2.1' 'migrate events@2.2.1' 'build events@2.2.1' 'reload events@2.2.1' 'health 2.2.1')" ] [[ "$output" == *"updated events@2.2.0 โ†’ events@2.2.1"* ]] } @test "a church already on the release is left alone" { run cc deploy hope events --version events@2.2.0 [ "$status" -eq 0 ] [[ "$output" == *"already on events@2.2.0"* ]] [ ! -s "$LOG" ] } @test "a failed backup changes nothing" { FAIL_AT=backup run cc deploy hope events --version events@2.2.1 [ "$status" -eq 1 ] [ "$(on hope)" = events@2.2.0 ] [[ "$output" == *"failed at the backup"* ]] run grep -q install "$LOG" [ "$status" -ne 0 ] } @test "a failed migration goes back to the previous release, without reloading" { FAIL_AT=migrate run cc deploy hope events --version events@2.2.1 [ "$status" -eq 1 ] [ "$(on hope)" = events@2.2.0 ] [[ "$output" == *"rolled back to events@2.2.0 (the running version was never stopped)"* ]] grep -qx "rollback_build events@2.2.0" "$LOG" run grep -q reload "$LOG" [ "$status" -ne 0 ] } @test "a release that doesn't come up is rolled back, reloaded and checked again" { FAIL_HEALTH=2.2.1 run cc deploy hope events --version events@2.2.1 [ "$status" -eq 1 ] [ "$(on hope)" = events@2.2.0 ] [ "$(grep -c reload "$LOG")" -eq 2 ] grep -qx "health 2.2.0" "$LOG" [[ "$output" == *"rolled back to events@2.2.0 and running again"* ]] } @test "files changed by hand stop the deploy" { echo "hand edit" >> "$CROSSCODE_SITES/hope/events/apps/events/VERSION" run cc deploy hope events --version events@2.2.1 --check [ "$status" -eq 1 ] [[ "$output" == *"changed or added by hand"* ]] [[ "$output" == *"apps/events/VERSION"* ]] run cc deploy hope events --version events@2.2.1 [ "$status" -eq 1 ] [ ! -s "$LOG" ] } @test "--allow-local-changes deploys with a file added by hand" { echo "notes" > "$CROSSCODE_SITES/hope/events/apps/events/notes.txt" run cc deploy hope events --version events@2.2.1 [ "$status" -eq 1 ] [ ! -s "$LOG" ] run cc deploy hope events --version events@2.2.1 --allow-local-changes [ "$status" -eq 0 ] [[ "$output" == *"deploying anyway"* ]] [ "$(on hope)" = events@2.2.1 ] # --check never passes with changes, even with the flag. run cc deploy hope events --check --allow-local-changes [ "$status" -eq 1 ] } @test "ignored files (.env, ecosystem.config.js) don't count as changes" { printf '.env\n/apps/events/ecosystem.config.js\n' > "$CROSSCODE_SITES/hope/events/.git/info/exclude" echo "SECRET=1" > "$CROSSCODE_SITES/hope/events/.env" echo "module.exports = {}" > "$CROSSCODE_SITES/hope/events/apps/events/ecosystem.config.js" run cc deploy hope events --version events@2.2.1 [ "$status" -eq 0 ] [ "$(on hope)" = events@2.2.1 ] } @test "a product the church doesn't have is refused; nothing named does nothing" { run cc deploy hope website [ "$status" -eq 1 ] [[ "$output" == *"hope has no website"* ]] run cc deploy hope [ "$status" -eq 0 ] [[ "$output" == *"Nothing to deploy"* ]] } @test "deploy --main checks out the latest main" { release 2.4.0-dev "## 2.4.0-dev" git_q -C "$TMP/origin" tag -d events@2.4.0-dev >/dev/null run cc deploy hope events --main [ "$status" -eq 0 ] [ "$(on hope)" = main ] grep -qx "health 2.4.0-dev" "$LOG" } @test "deploy --every-church updates each church, one after the other" { add_church grace events@2.2.0 run cc deploy --every-church events --version events@2.2.1 [ "$status" -eq 0 ] [ "$(on hope)" = events@2.2.1 ] [ "$(on grace)" = events@2.2.1 ] [ "$(grep -c '^health 2.2.1' "$LOG")" -eq 2 ] } @test "deploy --every-church stops at the first church that fails" { add_church grace events@2.2.0 FAIL_AT=build run cc deploy --every-church events --version events@2.2.1 [ "$status" -eq 1 ] [[ "$output" == *"Stopped at "* ]] [ "$(grep -c '^backup' "$LOG")" -eq 1 ] } @test "unknown church" { run cc deploy nowhere events [ "$status" -eq 1 ] [[ "$output" == *'No church "nowhere"'* ]] }