mirror of
https://github.com/getgrav/grav.git
synced 2026-05-09 17:35:53 +02:00
163 lines
6.7 KiB
Bash
Executable File
163 lines
6.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# E2E test for family-aware GPM upgrade gate.
|
|
# Run from the Grav install root: bash bin/gpm-e2e-test
|
|
#
|
|
# Requires: php (CLI), bin/gpm-test-server.php, updated Upgrader.php
|
|
|
|
set -uo pipefail
|
|
|
|
GRAV_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
|
PORT=8043
|
|
SERVER_PID=""
|
|
MODE_FILE=/tmp/gpm-test-mode
|
|
|
|
# ── Colours ───────────────────────────────────────────────────────────────────
|
|
GREEN='\033[0;32m'; RED='\033[0;31m'; YELLOW='\033[1;33m'; RESET='\033[0m'
|
|
PASS=0; FAIL=0
|
|
|
|
pass() { echo -e " ${GREEN}PASS${RESET} $1"; PASS=$((PASS + 1)); }
|
|
fail() { echo -e " ${RED}FAIL${RESET} $1"; FAIL=$((FAIL + 1)); }
|
|
section() { echo -e "\n${YELLOW}── $1 ──${RESET}"; }
|
|
|
|
# ── Server lifecycle ──────────────────────────────────────────────────────────
|
|
start_server() {
|
|
stop_server
|
|
|
|
# Kill anything still bound to the port
|
|
lsof -ti tcp:$PORT | xargs kill -9 2>/dev/null || true
|
|
sleep 0.3
|
|
|
|
php -S "localhost:$PORT" \
|
|
-t "$GRAV_DIR" \
|
|
"$GRAV_DIR/bin/gpm-test-server.php" \
|
|
> /tmp/gpm-test-server.log 2>&1 &
|
|
SERVER_PID=$!
|
|
sleep 0.5
|
|
|
|
# Sanity check
|
|
if ! curl -sf "http://localhost:$PORT/grav.json?v=1.8.0-beta.25&stable=1" > /dev/null; then
|
|
echo "ERROR: test server failed to start. Log:" >&2
|
|
cat /tmp/gpm-test-server.log >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
stop_server() {
|
|
if [ -n "${SERVER_PID:-}" ]; then
|
|
kill "$SERVER_PID" 2>/dev/null || true
|
|
SERVER_PID=""
|
|
fi
|
|
}
|
|
|
|
set_mode() { echo "$1" > "$MODE_FILE"; }
|
|
|
|
trap 'stop_server; rm -f "$MODE_FILE"' EXIT
|
|
|
|
# ── Helpers ───────────────────────────────────────────────────────────────────
|
|
clear_gpm_cache() {
|
|
rm -rf "$GRAV_DIR/cache/gpm"
|
|
}
|
|
|
|
# Run selfupgrade non-interactively: pipe two empty lines (defaults both
|
|
# changelog and upgrade prompts to "No") so the command exits cleanly
|
|
# without trying to download anything.
|
|
run_selfupgrade() {
|
|
printf '\n\n' | php "$GRAV_DIR/bin/gpm" selfupgrade 2>&1 || true
|
|
}
|
|
|
|
assert_contains() {
|
|
local output="$1" pattern="$2" label="$3"
|
|
if echo "$output" | grep -qiE "$pattern"; then
|
|
pass "$label"
|
|
else
|
|
fail "$label"
|
|
echo " Expected (regex): $pattern"
|
|
echo " Actual output:"
|
|
echo "$output" | sed 's/^/ /'
|
|
fi
|
|
}
|
|
|
|
assert_not_contains() {
|
|
local output="$1" pattern="$2" label="$3"
|
|
if echo "$output" | grep -qiE "$pattern"; then
|
|
fail "$label"
|
|
echo " Should NOT match (regex): $pattern"
|
|
echo " Actual output:"
|
|
echo "$output" | sed 's/^/ /'
|
|
else
|
|
pass "$label"
|
|
fi
|
|
}
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
GRAV_VERSION=$(grep -m1 '^# v' "$GRAV_DIR/CHANGELOG.md" | sed 's/^# v//')
|
|
|
|
echo ""
|
|
echo "Install : $GRAV_DIR"
|
|
echo "Version : $GRAV_VERSION"
|
|
echo ""
|
|
|
|
start_server
|
|
|
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
# SCENARIO A — old server behaviour (no family filter, returns 2.0.1 globally)
|
|
# Client gate in Upgrader.php must block the cross-family upgrade.
|
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
section "Scenario A: Old server returns 2.0.1 — client must block"
|
|
|
|
set_mode global
|
|
clear_gpm_cache
|
|
A=$(run_selfupgrade)
|
|
|
|
# Server log so we can see what was actually served
|
|
echo " Server: $(cat /tmp/gpm-test-server.log | tail -1)"
|
|
|
|
assert_contains "$A" "already running the latest" \
|
|
"selfupgrade reports no upgrade available"
|
|
assert_not_contains "$A" "Preparing to upgrade|Downloading" \
|
|
"selfupgrade does not attempt to download"
|
|
|
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
# SCENARIO B — new server behaviour (family filter, returns 1.8.0-beta.29)
|
|
# selfupgrade should recognise the 1.8 update and offer it.
|
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
section "Scenario B: Family server returns 1.8.0-beta.29 — upgrade should be offered"
|
|
|
|
set_mode family
|
|
clear_gpm_cache
|
|
B=$(run_selfupgrade)
|
|
|
|
echo " Server: $(cat /tmp/gpm-test-server.log | tail -1)"
|
|
|
|
assert_contains "$B" "1\.8\.0-beta\.29.*is now available|is now available.*1\.8\.0-beta\.29" \
|
|
"selfupgrade reports 1.8.0-beta.29 is available"
|
|
assert_not_contains "$B" "already running the latest" \
|
|
"selfupgrade does not report 'already up to date'"
|
|
assert_not_contains "$B" "2\.0\." \
|
|
"selfupgrade does not mention 2.0.x as a target"
|
|
|
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
# SCENARIO C — server returns same version (no upgrade needed)
|
|
# ═════════════════════════════════════════════════════════════════════════════
|
|
section "Scenario C: Server returns same version — already up to date"
|
|
|
|
set_mode same
|
|
clear_gpm_cache
|
|
C=$(run_selfupgrade)
|
|
|
|
echo " Server: $(cat /tmp/gpm-test-server.log | tail -1)"
|
|
|
|
assert_contains "$C" "already running the latest" \
|
|
"selfupgrade reports already up to date"
|
|
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
echo ""
|
|
if [ "$FAIL" -eq 0 ]; then
|
|
echo -e "${GREEN}All $PASS tests passed.${RESET}"
|
|
exit 0
|
|
else
|
|
echo -e "${RED}$FAIL test(s) failed, $PASS passed.${RESET}"
|
|
exit 1
|
|
fi
|