#!/usr/bin/env bash # Teste scripts/deploy.sh sans serveur : un faux « ssh » exécute les commandes en local (dans un dossier # temporaire), ce qui permet de vérifier les versions, la bascule atomique, le nettoyage et le retour arrière. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" command -v rsync >/dev/null || { echo "rsync absent : test de déploiement ignoré"; exit 0; } WORK="$(mktemp -d)" trap 'rm -rf "${WORK}"' EXIT FAKE="${WORK}/fake-ssh" # Même interface que ssh : [options] hôte commande... ; rsync l'appelle avec « hôte rsync --server … ». cat > "${FAKE}" <<'SH' #!/usr/bin/env bash [[ "${1:-}" == "-l" ]] && shift 2 # rsync passe « -l utilisateur hôte » shift # hôte exec bash -c "$*" SH chmod +x "${FAKE}" export DEPLOY_HOST="tester@example.invalid" DEPLOY_PATH="${WORK}/www/pyquest" DEPLOY_KEEP=2 DEPLOY_SSH="${FAKE}" fail() { echo "ÉCHEC : $1" >&2; exit 1; } # dist/ minimal factice (le vrai peut manquer en CI avant le build). FAKE_ROOT="${WORK}/repo" mkdir -p "${FAKE_ROOT}/scripts" "${FAKE_ROOT}/dist/vendor/pyodide" cp "${ROOT}/scripts/deploy.sh" "${FAKE_ROOT}/scripts/" echo "v1" > "${FAKE_ROOT}/dist/index.html" touch "${FAKE_ROOT}/dist/vendor/pyodide/pyodide.asm.wasm" DEPLOY="${FAKE_ROOT}/scripts/deploy.sh" "${DEPLOY}" >/dev/null [[ "$(cat "${DEPLOY_PATH}/current/index.html")" == v1 ]] || fail "la première version n'est pas en ligne" sleep 1; echo "v2" > "${FAKE_ROOT}/dist/index.html"; "${DEPLOY}" >/dev/null [[ "$(cat "${DEPLOY_PATH}/current/index.html")" == v2 ]] || fail "la seconde version n'est pas en ligne" sleep 1; echo "v3" > "${FAKE_ROOT}/dist/index.html"; "${DEPLOY}" >/dev/null [[ "$(ls "${DEPLOY_PATH}/releases" | wc -l)" == 2 ]] || fail "le nettoyage doit garder 2 versions" [[ -L "${DEPLOY_PATH}/current" ]] || fail "current doit être un lien" "${DEPLOY}" --rollback >/dev/null [[ "$(cat "${DEPLOY_PATH}/current/index.html")" == v2 ]] || fail "le retour arrière doit remettre v2" "${DEPLOY}" --rollback >/dev/null 2>&1 && fail "pas de version plus ancienne : le retour arrière doit échouer" || true [[ "$(cat "${DEPLOY_PATH}/current/index.html")" == v2 ]] || fail "un retour arrière impossible ne doit rien changer" # --dry-run ne modifie rien. before="$(ls "${DEPLOY_PATH}/releases")" echo "v4" > "${FAKE_ROOT}/dist/index.html"; "${DEPLOY}" --dry-run >/dev/null [[ "$(ls "${DEPLOY_PATH}/releases")" == "${before}" ]] || fail "--dry-run ne doit rien envoyer" # Valeurs dangereuses refusées avant toute connexion. DEPLOY_HOST='x; rm -rf /' "${DEPLOY}" >/dev/null 2>&1 && fail "hôte dangereux accepté" DEPLOY_PATH='/var/www/a b' "${DEPLOY}" >/dev/null 2>&1 && fail "chemin avec espace accepté" DEPLOY_PATH='/' "${DEPLOY}" >/dev/null 2>&1 && fail "chemin racine accepté" rm "${FAKE_ROOT}/dist/index.html"; "${DEPLOY}" >/dev/null 2>&1 && fail "dist absent accepté" # --backend : code envoyé sans les fichiers locaux (base, caches), puis redémarrage demandé via sudo (faux sudo ici). mkdir -p "${FAKE_ROOT}/backend/app1/__pycache__" "${WORK}/bin" echo "print('manage')" > "${FAKE_ROOT}/backend/manage.py" echo "x = 1" > "${FAKE_ROOT}/backend/app1/models.py" echo "cache" > "${FAKE_ROOT}/backend/app1/__pycache__/models.pyc" echo "secret-db" > "${FAKE_ROOT}/backend/db.sqlite3" printf '#!/usr/bin/env bash\necho "sudo $*" >> "%s/sudo.log"\n' "${WORK}" > "${WORK}/bin/sudo" chmod +x "${WORK}/bin/sudo" export DEPLOY_BACKEND_PATH="${WORK}/opt/pyquest/backend" PATH="${WORK}/bin:${PATH}" "${DEPLOY}" --backend >/dev/null [[ -f "${DEPLOY_BACKEND_PATH}/manage.py" && -f "${DEPLOY_BACKEND_PATH}/app1/models.py" ]] || fail "le code du backend n'est pas envoyé" [[ ! -e "${DEPLOY_BACKEND_PATH}/db.sqlite3" && ! -e "${DEPLOY_BACKEND_PATH}/app1/__pycache__" ]] || fail "base locale ou caches envoyés" grep -q "systemctl restart pyquest" "${WORK}/sudo.log" || fail "le redémarrage du service n'est pas demandé" rm "${WORK}/sudo.log" PATH="${WORK}/bin:${PATH}" "${DEPLOY}" --backend --no-restart >/dev/null [[ ! -e "${WORK}/sudo.log" ]] || fail "--no-restart ne doit pas redémarrer" echo "deploy.sh : ok"