|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Smoke checks an installed WordPress single site over HTTP. |
| 4 | +# |
| 5 | +# Runs from the root of the installation, with WP-CLI available. Multisite is not supported because |
| 6 | +# a network's domain lives in wp-config.php and the database, not just an option. |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +: "${RUNNER_TEMP:?}" "${WP_ADMIN_USER:?}" "${WP_ADMIN_PASSWORD:?}" |
| 11 | + |
| 12 | +site_port=8889 |
| 13 | +site_url="http://127.0.0.1:${site_port}" |
| 14 | +server_log="${RUNNER_TEMP}/server.log" |
| 15 | +jar="${RUNNER_TEMP}/cookies.txt" |
| 16 | +: > "${jar}" |
| 17 | +response="${RUNNER_TEMP}/response.txt" |
| 18 | +code="${RUNNER_TEMP}/http-code.txt" |
| 19 | +http_code='' |
| 20 | + |
| 21 | +version="$(wp core version)" |
| 22 | + |
| 23 | +# Nothing undoes these, so the site is only good for this check afterwards. |
| 24 | +wp option update home "${site_url}" |
| 25 | +wp option update siteurl "${site_url}" |
| 26 | +# A fresh install shows posts here already, but the front page check below needs it. |
| 27 | +wp option update show_on_front posts |
| 28 | +# Both make requests no check asked for, and the update checks call api.wordpress.org. |
| 29 | +wp config set DISABLE_WP_CRON true --raw |
| 30 | +wp config set WP_HTTP_BLOCK_EXTERNAL true --raw |
| 31 | + |
| 32 | +# opcache counts php -S as a web SAPI, so the runner's JIT runs here and segfaults. |
| 33 | +php -d opcache.jit=disable -S "127.0.0.1:${site_port}" -t . > "${server_log}" 2>&1 & |
| 34 | +server_pid=$! |
| 35 | + |
| 36 | +stop_server() { |
| 37 | + local status=$? |
| 38 | + kill "${server_pid}" 2> /dev/null || true |
| 39 | + wait "${server_pid}" 2> /dev/null || true |
| 40 | + [ "${status}" -eq 0 ] || cat "${server_log}" || true |
| 41 | + exit "${status}" |
| 42 | +} |
| 43 | +trap stop_server EXIT |
| 44 | + |
| 45 | +# No connection prints 000 and exits 7, which set -e would take as fatal. |
| 46 | +deadline=$(( SECONDS + 30 )) |
| 47 | +while :; do |
| 48 | + ready_code="$(curl --silent --max-time 5 --output /dev/null --write-out '%{http_code}' "${site_url}/" || true)" |
| 49 | + [ "${ready_code}" = '000' ] || break |
| 50 | + [ "${SECONDS}" -lt "${deadline}" ] || { echo '::error::the server never answered'; exit 1; } |
| 51 | + sleep 1 |
| 52 | +done |
| 53 | + |
| 54 | +fail() { |
| 55 | + echo "::error::${1}" |
| 56 | + tail -n 20 "${response}" |
| 57 | + exit 1 |
| 58 | +} |
| 59 | + |
| 60 | +# An HTTP error returns to the caller. No reply at all is fatal. |
| 61 | +fetch() { |
| 62 | + local path="${1}" status=0 |
| 63 | + shift |
| 64 | + : > "${response}" |
| 65 | + curl --silent --show-error --max-time 30 --output "${response}" \ |
| 66 | + --write-out '%{http_code}' --cookie "${jar}" --cookie-jar "${jar}" \ |
| 67 | + "$@" "${site_url}${path}" > "${code}" || status=$? |
| 68 | + http_code="$(cat "${code}")" |
| 69 | + [ "${status}" -eq 0 ] || [ "${status}" -eq 22 ] \ |
| 70 | + || fail "${path} got no reply and curl exited ${status}" |
| 71 | + return "${status}" |
| 72 | +} |
| 73 | + |
| 74 | +check() { |
| 75 | + local path="${1}" marker status=0 |
| 76 | + shift |
| 77 | + fetch "${path}" --fail-with-body || status=$? |
| 78 | + [ "${status}" -eq 0 ] || fail "${path} returned HTTP ${http_code}" |
| 79 | + for marker in "$@"; do |
| 80 | + grep -qF -e "${marker}" "${response}" || fail "${path} did not contain: ${marker}" |
| 81 | + done |
| 82 | + echo "ok ${path}" |
| 83 | +} |
| 84 | + |
| 85 | +# A fatal mid-page still returns 200, so every check also asks for the closing tag. |
| 86 | +check '/' "content=\"WordPress ${version}\"" 'Hello world!' '</html>' |
| 87 | + |
| 88 | +fetch '/?rest_route=/' --fail-with-body || fail "/?rest_route=/ returned HTTP ${http_code}" |
| 89 | +jq -e --arg url "${site_url}" '.url == $url and ( .namespaces | index( "wp/v2" ) )' "${response}" > /dev/null \ |
| 90 | + || fail 'the REST index was not the expected JSON' |
| 91 | +echo 'ok /?rest_route=/' |
| 92 | + |
| 93 | +# It prints this for an empty database too, which the checks above rule out. |
| 94 | +check '/wp-admin/upgrade.php' 'No Update Required' '</html>' |
| 95 | + |
| 96 | +fetch '/?p=99999999' || true |
| 97 | +[ "${http_code}" = '404' ] || fail "a missing post returned ${http_code}, expected 404" |
| 98 | +grep -qF '</html>' "${response}" || fail 'the 404 page was cut short' |
| 99 | +echo 'ok 404 handling' |
| 100 | + |
| 101 | +login_status=0 |
| 102 | +fetch '/wp-login.php' --fail-with-body \ |
| 103 | + --data-urlencode "log=${WP_ADMIN_USER}" --data-urlencode "pwd=${WP_ADMIN_PASSWORD}" || login_status=$? |
| 104 | +[ "${login_status}" -eq 0 ] || fail "logging in returned HTTP ${http_code}" |
| 105 | +grep -qF 'wordpress_logged_in_' "${jar}" || fail 'logging in did not set an authentication cookie' |
| 106 | +echo 'ok login' |
| 107 | + |
| 108 | +# The admin bar renders only for a logged-in user. |
| 109 | +check '/wp-admin/' 'id="wpadminbar"' '</html>' |
| 110 | + |
| 111 | +# Catches a fatal during shutdown. An empty log would let the grep pass silently. |
| 112 | +[ -s "${server_log}" ] || { echo '::error::the server wrote no log'; exit 1; } |
| 113 | +if grep -qE 'Fatal error|Uncaught|Segmentation fault' "${server_log}"; then |
| 114 | + echo '::error::the server logged a fatal error' |
| 115 | + exit 1 |
| 116 | +fi |
| 117 | +echo 'ok server log' |
0 commit comments