Files
gitea-ci-in-gvisor/scripts/e2e-test.sh
T

159 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# End-to-end check for the gVisor-backed Gitea runner.
#
# Run from the deployment directory:
# ./scripts/e2e-test.sh
#
# Environment overrides:
# GITEA_URL default http://localhost:3000
# ADMIN_USER default ciadmin
# ADMIN_PASSWORD default CiAdminPass12345
# REPO default gvisor-e2e-<timestamp>
cd "$(dirname "$0")/.."
GITEA_URL="${GITEA_URL:-http://localhost:3000}"
GITEA_URL="${GITEA_URL%/}"
ADMIN_USER="${ADMIN_USER:-ciadmin}"
ADMIN_PASSWORD="${ADMIN_PASSWORD:-CiAdminPass12345}"
REPO="${REPO:-gvisor-e2e-$(date +%s)}"
WORKFLOW="${WORKFLOW:-scripts/workflow-gvisor.yml}"
SHOW_LOGS="${SHOW_LOGS:-0}"
api() {
curl -fsS -u "${ADMIN_USER}:${ADMIN_PASSWORD}" "$@"
}
echo "==> Building the runner image"
docker build -t gitea-gvisor-runner:latest runner >/dev/null
echo "==> Starting Gitea"
docker compose up -d --no-build gitea >/dev/null
echo "==> Waiting for Gitea at ${GITEA_URL}"
for _ in $(seq 1 90); do
if curl -fsS "${GITEA_URL}/api/healthz" >/dev/null 2>&1; then
break
fi
sleep 2
done
curl -fsS "${GITEA_URL}/api/healthz" >/dev/null
echo "==> Creating admin user (ignore error if it already exists)"
docker compose exec -T -u git gitea gitea admin user create \
--username "${ADMIN_USER}" \
--password "${ADMIN_PASSWORD}" \
--email "${ADMIN_USER}@example.com" \
--admin --must-change-password=false >/dev/null 2>&1 || true
echo "==> Starting runner (auto-registers through the Gitea API)"
GITEA_ADMIN_USER="${ADMIN_USER}" \
GITEA_ADMIN_PASSWORD="${ADMIN_PASSWORD}" \
docker compose up -d --no-build --force-recreate runner >/dev/null
echo "==> Waiting for the runner to come online"
online=0
for _ in $(seq 1 90); do
if api "${GITEA_URL}/api/v1/admin/actions/runners" | grep -q '"status":"online"'; then
online=1
break
fi
sleep 2
done
if [ "${online}" != 1 ]; then
docker compose logs --tail 100 runner
echo "Runner did not come online" >&2
exit 1
fi
echo "==> Creating repository ${ADMIN_USER}/${REPO}"
api -H 'Content-Type: application/json' \
-d "{\"name\":\"${REPO}\",\"private\":false,\"auto_init\":false}" \
"${GITEA_URL}/api/v1/user/repos" >/dev/null 2>&1 || true
tmp="$(mktemp -d)"
trap 'rm -rf "${tmp}"' EXIT
export GIT_CONFIG_GLOBAL="${tmp}/gitconfig"
git config --global user.name "gVisor CI Test"
git config --global user.email "gvisor-ci@example.com"
git config --global init.defaultBranch main
# URL-encode the password minimally for HTTP basic auth in the remote URL.
remote="http://${ADMIN_USER}:${ADMIN_PASSWORD}@${GITEA_URL#http://}/${ADMIN_USER}/${REPO}.git"
echo "==> Pushing workflow to ${ADMIN_USER}/${REPO}"
git clone -q "${remote}" "${tmp}/repo" 2>/dev/null || {
mkdir -p "${tmp}/repo"
git -C "${tmp}/repo" init -q
git -C "${tmp}/repo" remote add origin "${remote}"
}
mkdir -p "${tmp}/repo/.gitea/workflows"
cp "${WORKFLOW}" "${tmp}/repo/.gitea/workflows/gvisor-isolation.yml"
git -C "${tmp}/repo" add .gitea/workflows/gvisor-isolation.yml
git -C "${tmp}/repo" commit -q -m "test: run a job inside gVisor"
git -C "${tmp}/repo" push -q origin HEAD:main
echo "==> Waiting for the workflow run"
run_id=""
status=""
conclusion=""
for _ in $(seq 1 180); do
runs_json=$(api "${GITEA_URL}/api/v1/repos/${ADMIN_USER}/${REPO}/actions/runs" || true)
read -r run_id status conclusion <<< "$(printf '%s' "${runs_json}" | python3 -c '
import json, sys
try:
data = json.load(sys.stdin)
except Exception:
print("")
raise SystemExit(0)
runs = data.get("workflow_runs", data.get("runs", []))
if runs:
r = runs[0]
print(r.get("id", ""), r.get("status", ""), r.get("conclusion", ""))
')"
if [ -n "${run_id}" ] && [ "${status}" = "completed" ]; then
break
fi
sleep 2
done
if [ -z "${run_id}" ]; then
echo "No workflow run was created" >&2
docker compose logs --tail 120 runner
exit 1
fi
echo "==> Workflow run ${run_id}: status=${status} conclusion=${conclusion}"
jobs_json=$(api "${GITEA_URL}/api/v1/repos/${ADMIN_USER}/${REPO}/actions/runs/${run_id}/jobs" || true)
printf '%s' "${jobs_json}" | python3 -m json.tool || true
job_id=$(printf '%s' "${jobs_json}" | python3 -c '
import json, sys
try:
data = json.load(sys.stdin)
except Exception:
print("")
raise SystemExit(0)
jobs = data.get("jobs", data if isinstance(data, list) else [])
print(jobs[0]["id"] if jobs else "")
')
if [ "${conclusion}" != "success" ] || [ "${SHOW_LOGS}" = "1" ]; then
if [ "${conclusion}" != "success" ]; then
echo "Workflow failed; fetching logs" >&2
else
echo "==> Job logs"
fi
if [ -n "${job_id}" ]; then
api "${GITEA_URL}/api/v1/repos/${ADMIN_USER}/${REPO}/actions/jobs/${job_id}/logs" || true
fi
fi
if [ "${conclusion}" != "success" ]; then
exit 1
fi
echo "==> PASS: the workflow ran successfully inside gVisor"