fix: feat: restore smoke-init CI pipeline using mock Forgejo (#124)
Some checks failed
ci/woodpecker/push/ci Pipeline failed

This commit is contained in:
Agent 2026-04-02 07:48:59 +00:00
parent 19969586e5
commit 8520952acb
4 changed files with 188 additions and 62 deletions

View file

@ -1,32 +1,28 @@
#!/usr/bin/env bash
# tests/smoke-init.sh — End-to-end smoke test for disinto init
# tests/smoke-init.sh — End-to-end smoke test for disinto init using mock Forgejo
#
# Expects a running Forgejo at SMOKE_FORGE_URL with a bootstrap admin
# user already created (see .woodpecker/smoke-init.yml for CI setup).
# Validates the full init flow: Forgejo API, user/token creation,
# repo setup, labels, TOML generation, and cron installation.
#
# Uses mock Forgejo server (started by .woodpecker/smoke-init.yml).
#
# Required env: SMOKE_FORGE_URL (default: http://localhost:3000)
# Required tools: bash, curl, jq, python3, git
# Required tools: bash, curl, jq, git
set -euo pipefail
FACTORY_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
FORGE_URL="${SMOKE_FORGE_URL:-http://localhost:3000}"
SETUP_ADMIN="setup-admin"
SETUP_PASS="SetupPass-789xyz"
TEST_SLUG="smoke-org/smoke-repo"
MOCK_BIN="/tmp/smoke-mock-bin"
MOCK_STATE="/tmp/smoke-mock-state"
FAILED=0
fail() { printf 'FAIL: %s\n' "$*" >&2; FAILED=1; }
pass() { printf 'PASS: %s\n' "$*"; }
cleanup() {
rm -rf "$MOCK_BIN" "$MOCK_STATE" /tmp/smoke-test-repo \
"${FACTORY_ROOT}/projects/smoke-repo.toml" \
"${FACTORY_ROOT}/docker-compose.yml"
rm -rf "$MOCK_BIN" /tmp/smoke-test-repo \
"${FACTORY_ROOT}/projects/smoke-repo.toml"
# Restore .env only if we created the backup
if [ -f "${FACTORY_ROOT}/.env.smoke-backup" ]; then
mv "${FACTORY_ROOT}/.env.smoke-backup" "${FACTORY_ROOT}/.env"
@ -40,11 +36,11 @@ trap cleanup EXIT
if [ -f "${FACTORY_ROOT}/.env" ]; then
cp "${FACTORY_ROOT}/.env" "${FACTORY_ROOT}/.env.smoke-backup"
fi
# Start with a clean .env (setup_forge writes tokens here)
# Start with a clean .env (init writes tokens here)
printf '' > "${FACTORY_ROOT}/.env"
# ── 1. Verify Forgejo is ready ──────────────────────────────────────────────
echo "=== 1/6 Verifying Forgejo at ${FORGE_URL} ==="
# ── 1. Verify mock Forgejo is ready ─────────────────────────────────────────
echo "=== 1/6 Verifying mock Forgejo at ${FORGE_URL} ==="
retries=0
api_version=""
while true; do
@ -55,43 +51,24 @@ while true; do
fi
retries=$((retries + 1))
if [ "$retries" -gt 30 ]; then
fail "Forgejo API not responding after 30s"
fail "Mock Forgejo API not responding after 30s"
exit 1
fi
sleep 1
done
pass "Forgejo API v${api_version} (${retries}s)"
pass "Mock Forgejo API v${api_version} (${retries}s)"
# Verify bootstrap admin user exists
if curl -sf --max-time 5 "${FORGE_URL}/api/v1/users/${SETUP_ADMIN}" >/dev/null 2>&1; then
pass "Bootstrap admin '${SETUP_ADMIN}' exists"
else
fail "Bootstrap admin '${SETUP_ADMIN}' not found — was Forgejo set up?"
exit 1
fi
# ── 2. Set up mock binaries ─────────────────────────────────────────────────
# ── 2. Set up mock binaries (docker, claude, tmux) ───────────────────────────
echo "=== 2/6 Setting up mock binaries ==="
mkdir -p "$MOCK_BIN" "$MOCK_STATE"
# Store bootstrap admin credentials for the docker mock
printf '%s:%s' "${SETUP_ADMIN}" "${SETUP_PASS}" > "$MOCK_STATE/bootstrap_creds"
mkdir -p "$MOCK_BIN"
# ── Mock: docker ──
# Routes 'docker exec' user-creation calls to the Forgejo admin API,
# using the bootstrap admin's credentials.
# Routes 'docker exec' user-creation calls to the Forgejo API mock
cat > "$MOCK_BIN/docker" << 'DOCKERMOCK'
#!/usr/bin/env bash
set -euo pipefail
FORGE_URL="${SMOKE_FORGE_URL:-http://localhost:3000}"
MOCK_STATE="/tmp/smoke-mock-state"
if [ ! -f "$MOCK_STATE/bootstrap_creds" ]; then
echo "mock-docker: bootstrap credentials not found" >&2
exit 1
fi
BOOTSTRAP_CREDS="$(cat "$MOCK_STATE/bootstrap_creds")"
# docker ps — return empty (no containers running)
if [ "${1:-}" = "ps" ]; then
@ -139,9 +116,8 @@ if [ "${1:-}" = "exec" ]; then
exit 1
fi
# Create user via Forgejo admin API
# Create user via Forgejo API
if ! curl -sf -X POST \
-u "$BOOTSTRAP_CREDS" \
-H "Content-Type: application/json" \
"${FORGE_URL}/api/v1/admin/users" \
-d "{\"username\":\"${username}\",\"password\":\"${password}\",\"email\":\"${email}\",\"must_change_password\":false,\"login_name\":\"${username}\",\"source_id\":0}" \
@ -150,8 +126,7 @@ if [ "${1:-}" = "exec" ]; then
exit 1
fi
# Patch user: ensure must_change_password is false (Forgejo admin
# API POST may ignore it) and promote to admin if requested
# Patch user: ensure must_change_password is false
patch_body="{\"must_change_password\":false,\"login_name\":\"${username}\",\"source_id\":0"
if [ "$is_admin" = "true" ]; then
patch_body="${patch_body},\"admin\":true"
@ -159,7 +134,6 @@ if [ "${1:-}" = "exec" ]; then
patch_body="${patch_body}}"
curl -sf -X PATCH \
-u "$BOOTSTRAP_CREDS" \
-H "Content-Type: application/json" \
"${FORGE_URL}/api/v1/admin/users/${username}" \
-d "${patch_body}" \
@ -187,7 +161,7 @@ if [ "${1:-}" = "exec" ]; then
exit 1
fi
# PATCH user via Forgejo admin API to clear must_change_password
# PATCH user via Forgejo API to clear must_change_password
patch_body="{\"must_change_password\":false,\"login_name\":\"${username}\",\"source_id\":0"
if [ -n "$password" ]; then
patch_body="${patch_body},\"password\":\"${password}\""
@ -195,7 +169,6 @@ if [ "${1:-}" = "exec" ]; then
patch_body="${patch_body}}"
if ! curl -sf -X PATCH \
-u "$BOOTSTRAP_CREDS" \
-H "Content-Type: application/json" \
"${FORGE_URL}/api/v1/admin/users/${username}" \
-d "${patch_body}" \
@ -290,19 +263,21 @@ if [ "$repo_found" = false ]; then
fail "Repo not found on Forgejo under any expected path"
fi
# Labels exist on repo — use bootstrap admin to check
setup_token=$(curl -sf -X POST \
-u "${SETUP_ADMIN}:${SETUP_PASS}" \
# Labels exist on repo
# Create a token to check labels (using disinto-admin which was created by init)
disinto_admin_pass="Disinto-Admin-456"
verify_token=$(curl -sf -X POST \
-u "disinto-admin:${disinto_admin_pass}" \
-H "Content-Type: application/json" \
"${FORGE_URL}/api/v1/users/${SETUP_ADMIN}/tokens" \
"${FORGE_URL}/api/v1/users/disinto-admin/tokens" \
-d '{"name":"smoke-verify","scopes":["all"]}' 2>/dev/null \
| jq -r '.sha1 // empty') || setup_token=""
| jq -r '.sha1 // empty') || verify_token=""
if [ -n "$setup_token" ]; then
if [ -n "$verify_token" ]; then
label_count=0
for repo_path in "${TEST_SLUG}" "dev-bot/smoke-repo" "disinto-admin/smoke-repo"; do
label_count=$(curl -sf \
-H "Authorization: token ${setup_token}" \
-H "Authorization: token ${verify_token}" \
"${FORGE_URL}/api/v1/repos/${repo_path}/labels?limit=50" 2>/dev/null \
| jq 'length' 2>/dev/null) || label_count=0
if [ "$label_count" -gt 0 ]; then
@ -316,7 +291,7 @@ if [ -n "$setup_token" ]; then
fail "Expected >= 5 labels, found ${label_count}"
fi
else
fail "Could not obtain verification token from bootstrap admin"
fail "Could not obtain verification token"
fi
# ── 5. Verify local state ───────────────────────────────────────────────────
@ -364,6 +339,26 @@ else
fail "Repo not cloned to /tmp/smoke-test-repo"
fi
# ── Mock state verification ─────────────────────────────────────────────────
echo "=== Verifying mock Forgejo state ==="
# Query /mock/state to verify all expected API calls were made
mock_state=$(curl -sf \
-H "Authorization: token ${verify_token}" \
"${FORGE_URL}/mock/state" 2>/dev/null) || mock_state=""
if [ -n "$mock_state" ]; then
# Verify users were created
users=$(echo "$mock_state" | jq -r '.users | length' 2>/dev/null) || users=0
if [ "$users" -ge 3 ]; then
pass "Mock state: ${users} users created (expected >= 3)"
else
fail "Mock state: expected >= 3 users, found ${users}"
fi
else
fail "Could not query /mock/state endpoint"
fi
# ── 6. Verify cron setup ────────────────────────────────────────────────────
echo "=== 6/6 Verifying cron setup ==="
cron_output=$(crontab -l 2>/dev/null) || cron_output=""