Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

CI: run end-to-end Docker smoke tests in Docker Build workflow

sassanix495089012cb3
diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml
index 32a6036..3802f53 100644
--- a/.github/workflows/docker-build.yml
+++ b/.github/workflows/docker-build.yml
@@ -32,3 +32,6 @@ jobs:
- name: Report image size
run: docker images warracker:ci --format '{{.Repository}}:{{.Tag}} {{.Size}}'
+
+ - name: Run Docker smoke tests
+ run: bash tests/docker_smoke_test.sh warracker:ci
diff --git a/tests/README.md b/tests/README.md
index 927af45..393223f 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -65,6 +65,12 @@ CI runs the same suite on every pull request
all frontend JS files and inline HTML scripts, pure JS date/currency
helper tests, locale JSON completeness vs `SUPPORTED_LANGUAGES`, and
frontend version-string lockstep.
+- `docker_smoke_test.sh` — end-to-end container smoke tests run in
+ `.github/workflows/docker-build.yml`: Compose config validation,
+ default 4-var stack boot with gevent workers, nginx security headers
+ and `Server` banner check, API CRUD + persisted `SECRET_KEY` across
+ container recreation, `DATABASE_URL`-only boot, `DISABLE_NGINX=true`
+ mode, and fail-closed missing/partial DB credential checks.
- `test_notifications.py` — notification selection: the expiring-soon
query includes in-window warranties and excludes expired, lifetime,
archived and voided ones, and honors each user's own
diff --git a/tests/docker_smoke_test.sh b/tests/docker_smoke_test.sh
new file mode 100755
index 0000000..0115912
--- /dev/null
+++ b/tests/docker_smoke_test.sh
@@ -0,0 +1,288 @@
+#!/usr/bin/env bash
+# End-to-end Docker smoke tests for the built Warracker image.
+# Runs in CI (.github/workflows/docker-build.yml) right after building
+# warracker:ci, and can also be run locally against any image tag:
+# bash tests/docker_smoke_test.sh warracker:ci
+set -euo pipefail
+
+IMAGE="${1:-warracker:ci}"
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
+
+NET="warracker-ci-net-$$"
+VOL="warracker-ci-uploads-$$"
+PG_CTR="warracker-ci-pg-$$"
+APP_CTR=""
+
+DB_USER="warranty_user"
+DB_PASS="ci_db_password_9876"
+DB_NAME="warranty_db"
+
+EXPECTED_VERSION="$(
+ grep -o "currentVersion = '[^']*'" "${REPO_ROOT}/frontend/version-checker.js" \
+ | head -1 | cut -d"'" -f2
+)"
+
+log() {
+ echo "[docker-smoke] $*"
+}
+
+fail() {
+ echo "[docker-smoke] FAIL: $*" >&2
+ if [ -n "${APP_CTR}" ]; then
+ echo "--- Container logs (${APP_CTR}) ---" >&2
+ docker logs "${APP_CTR}" >&2 || true
+ fi
+ exit 1
+}
+
+cleanup() {
+ local rc=$?
+ for ctr in "${APP_CTR}" "${PG_CTR}"; do
+ if [ -n "${ctr}" ]; then
+ docker rm -f "${ctr}" >/dev/null 2>&1 || true
+ fi
+ done
+ docker network rm "${NET}" >/dev/null 2>&1 || true
+ docker volume rm "${VOL}" >/dev/null 2>&1 || true
+ exit "${rc}"
+}
+trap cleanup EXIT
+
+wait_for_health() {
+ local ctr="$1"
+ local url="$2"
+ local max_attempts="${3:-45}"
+
+ for i in $(seq 1 "${max_attempts}"); do
+ local running
+ running="$(docker inspect -f '{{.State.Running}}' "${ctr}" 2>/dev/null || echo false)"
+ if [ "${running}" != "true" ]; then
+ fail "Container ${ctr} exited before becoming healthy"
+ fi
+ if curl -fsS "${url}" 2>/dev/null | grep -q '"status":"ok"'; then
+ return 0
+ fi
+ sleep 1
+ done
+ fail "Timed out waiting for ${url} on container ${ctr}"
+}
+
+stop_app() {
+ if [ -n "${APP_CTR}" ]; then
+ docker rm -f "${APP_CTR}" >/dev/null 2>&1 || true
+ APP_CTR=""
+ fi
+}
+
+# ---------------------------------------------------------------------------
+# 1. Validate shipped Docker Compose files
+# ---------------------------------------------------------------------------
+log "1/6 Validating Docker Compose files..."
+docker compose -f "${REPO_ROOT}/docker-compose.yml" config -q
+docker compose -f "${REPO_ROOT}/Docker/docker-compose.yml" config -q
+
+# ---------------------------------------------------------------------------
+# 2. Start PostgreSQL on an isolated Docker network
+# ---------------------------------------------------------------------------
+log "2/6 Starting PostgreSQL 15 container..."
+docker network create "${NET}" >/dev/null
+docker volume create "${VOL}" >/dev/null
+
+docker run -d --name "${PG_CTR}" --network "${NET}" \
+ -e "POSTGRES_DB=${DB_NAME}" \
+ -e "POSTGRES_USER=${DB_USER}" \
+ -e "POSTGRES_PASSWORD=${DB_PASS}" \
+ postgres:15-alpine >/dev/null
+
+for i in $(seq 1 30); do
+ if docker exec "${PG_CTR}" pg_isready -U "${DB_USER}" -d "${DB_NAME}" >/dev/null 2>&1; then
+ break
+ fi
+ if [ "${i}" -eq 30 ]; then
+ fail "PostgreSQL did not become ready"
+ fi
+ sleep 1
+done
+
+# ---------------------------------------------------------------------------
+# 3. Default 4-variable stack (no DB_ADMIN_*, no SECRET_KEY, gevent workers)
+# + nginx security headers + API CRUD + persisted SECRET_KEY across recreate
+# ---------------------------------------------------------------------------
+log "3/6 Booting default 4-var stack (no DB_ADMIN_*, no SECRET_KEY, gevent workers)..."
+APP_CTR="warracker-ci-default-$$"
+docker run -d --name "${APP_CTR}" --network "${NET}" \
+ -p 127.0.0.1:18005:80 \
+ -v "${VOL}:/data/uploads" \
+ -e "DB_HOST=${PG_CTR}" \
+ -e "DB_NAME=${DB_NAME}" \
+ -e "DB_USER=${DB_USER}" \
+ -e "DB_PASSWORD=${DB_PASS}" \
+ "${IMAGE}" >/dev/null
+
+wait_for_health "${APP_CTR}" "http://127.0.0.1:18005/api/health"
+
+# Confirm banner & version header appear in container logs (allow banner loop to flush).
+banner_ok=false
+for i in $(seq 1 10); do
+ if docker logs "${APP_CTR}" 2>&1 | grep -q "Warracker is up and healthy."; then
+ banner_ok=true
+ break
+ fi
+ sleep 1
+done
+[ "${banner_ok}" = "true" ] || fail "Startup health banner not found in logs"
+docker logs "${APP_CTR}" 2>&1 | grep -q "=== Warracker ${EXPECTED_VERSION} ===" \
+ || fail "Startup log missing expected version header === Warracker ${EXPECTED_VERSION} ==="
+
+# Persisted generated SECRET_KEY must live in /data/uploads/.secret_key with 0600 & 999:999.
+key_meta="$(docker exec "${APP_CTR}" stat -c '%u:%g:%a:%s' /data/uploads/.secret_key)"
+log " .secret_key metadata (uid:gid:mode:bytes) = ${key_meta}"
+case "${key_meta}" in
+ 999:999:600:*) ;;
+ *) fail "Unexpected .secret_key ownership/permissions: ${key_meta}" ;;
+esac
+key_size="${key_meta##*:}"
+[ "${key_size}" -ge 32 ] || fail "Generated .secret_key is too short (${key_size} bytes)"
+
+# Check frontend HTML + Nginx security headers + Server version suppression.
+about_headers="$(mktemp)"
+about_body="$(mktemp)"
+http_code="$(curl -sS -D "${about_headers}" -o "${about_body}" -w '%{http_code}' http://127.0.0.1:18005/about.html)"
+[ "${http_code}" = "200" ] || fail "GET /about.html returned ${http_code}"
+grep -q "Version v${EXPECTED_VERSION}" "${about_body}" \
+ || fail "/about.html does not contain Version v${EXPECTED_VERSION}"
+grep -iq "^x-frame-options:.*SAMEORIGIN" "${about_headers}" \
+ || fail "Missing X-Frame-Options: SAMEORIGIN on /about.html"
+grep -iq "^referrer-policy:.*strict-origin-when-cross-origin" "${about_headers}" \
+ || fail "Missing Referrer-Policy on /about.html"
+grep -iq "^content-security-policy:" "${about_headers}" \
+ || fail "Missing Content-Security-Policy on /about.html"
+if grep -iq "upgrade-insecure-requests" "${about_headers}"; then
+ fail "CSP must not include upgrade-insecure-requests (breaks plain-HTTP self-hosted installs)"
+fi
+if grep -iqE "^server:.*nginx/[0-9]" "${about_headers}"; then
+ fail "Nginx Server header discloses version number"
+fi
+rm -f "${about_headers}" "${about_body}"
+
+# End-to-end API flow through Nginx -> Gunicorn (gevent) -> PostgreSQL.
+reg_resp="$(curl -fsS -X POST http://127.0.0.1:18005/api/auth/register \
+ -H 'Content-Type: application/json' \
+ -d '{"username":"smokeadmin","email":"[email protected]","password":"SmokePassword123!"}')"
+token="$(python3 -c 'import json,sys; print(json.loads(sys.stdin.read())["token"])' <<< "${reg_resp}")"
+[ -n "${token}" ] || fail "Register response did not include JWT token"
+
+create_resp="$(curl -fsS -X POST http://127.0.0.1:18005/api/warranties \
+ -H "Authorization: Bearer ${token}" \
+ -F "product_name=Docker Smoke Router" \
+ -F "purchase_date=2025-01-15" \
+ -F "exact_expiration_date=2027-01-15")"
+wid="$(python3 -c 'import json,sys; print(json.loads(sys.stdin.read())["id"])' <<< "${create_resp}")"
+[ -n "${wid}" ] || fail "Create warranty response did not include id"
+
+list_resp="$(curl -fsS http://127.0.0.1:18005/api/warranties \
+ -H "Authorization: Bearer ${token}")"
+grep -q "Docker Smoke Router" <<< "${list_resp}" \
+ || fail "GET /api/warranties did not return created warranty"
+
+# Recreate container with the same /data/uploads volume and no SECRET_KEY env var:
+# the JWT issued before recreation must remain valid.
+log " Recreating container to verify persisted SECRET_KEY across restarts..."
+stop_app
+APP_CTR="warracker-ci-recreate-$$"
+docker run -d --name "${APP_CTR}" --network "${NET}" \
+ -p 127.0.0.1:18005:80 \
+ -v "${VOL}:/data/uploads" \
+ -e "DB_HOST=${PG_CTR}" \
+ -e "DB_NAME=${DB_NAME}" \
+ -e "DB_USER=${DB_USER}" \
+ -e "DB_PASSWORD=${DB_PASS}" \
+ "${IMAGE}" >/dev/null
+
+wait_for_health "${APP_CTR}" "http://127.0.0.1:18005/api/health"
+after_recreate="$(curl -fsS http://127.0.0.1:18005/api/warranties \
+ -H "Authorization: Bearer ${token}")"
+grep -q "Docker Smoke Router" <<< "${after_recreate}" \
+ || fail "JWT failed after container recreate (SECRET_KEY did not persist)"
+stop_app
+
+# ---------------------------------------------------------------------------
+# 4. DATABASE_URL-only boot (no DB_HOST/DB_NAME/DB_USER/DB_PASSWORD/DB_ADMIN_*)
+# ---------------------------------------------------------------------------
+log "4/6 Booting with DATABASE_URL only..."
+docker exec "${PG_CTR}" psql -U "${DB_USER}" -d postgres \
+ -c "CREATE DATABASE warranty_url_db OWNER \"${DB_USER}\";" >/dev/null
+
+APP_CTR="warracker-ci-dburl-$$"
+docker run -d --name "${APP_CTR}" --network "${NET}" \
+ -p 127.0.0.1:18006:80 \
+ -e "DATABASE_URL=postgresql://${DB_USER}:${DB_PASS}@${PG_CTR}:5432/warranty_url_db" \
+ "${IMAGE}" >/dev/null
+
+wait_for_health "${APP_CTR}" "http://127.0.0.1:18006/api/health"
+if docker logs "${APP_CTR}" 2>&1 | grep -q "${DB_PASS}"; then
+ fail "DATABASE_URL password leaked into container logs"
+fi
+stop_app
+
+# ---------------------------------------------------------------------------
+# 5. DISABLE_NGINX=true direct-Gunicorn mode on port 5000
+# ---------------------------------------------------------------------------
+log "5/6 Booting with DISABLE_NGINX=true on :5000..."
+APP_CTR="warracker-ci-nonginx-$$"
+docker run -d --name "${APP_CTR}" --network "${NET}" \
+ -p 127.0.0.1:18007:5000 \
+ -e "DISABLE_NGINX=true" \
+ -e "DB_HOST=${PG_CTR}" \
+ -e "DB_NAME=${DB_NAME}" \
+ -e "DB_USER=${DB_USER}" \
+ -e "DB_PASSWORD=${DB_PASS}" \
+ "${IMAGE}" >/dev/null
+
+wait_for_health "${APP_CTR}" "http://127.0.0.1:18007/api/health"
+if docker exec "${APP_CTR}" python -c "
+import glob, sys
+comms = []
+for p in glob.glob('/proc/[0-9]*/comm'):
+ try:
+ comms.append(open(p).read().strip())
+ except OSError:
+ pass
+sys.exit(0 if 'nginx' in comms else 1)
+"; then
+ fail "nginx is running despite DISABLE_NGINX=true"
+fi
+stop_app
+
+# ---------------------------------------------------------------------------
+# 6. Fail-closed negative credential checks
+# ---------------------------------------------------------------------------
+log "6/6 Verifying fail-closed startup on missing/partial DB credentials..."
+set +e
+missing_out="$(docker run --rm --network "${NET}" \
+ -e "DB_HOST=${PG_CTR}" \
+ -e "DB_NAME=${DB_NAME}" \
+ -e "DB_USER=${DB_USER}" \
+ "${IMAGE}" 2>&1)"
+missing_rc=$?
+set -e
+[ "${missing_rc}" -ne 0 ] || fail "Container unexpectedly succeeded with missing DB_PASSWORD"
+grep -q "DB_PASSWORD" <<< "${missing_out}" \
+ || fail "Missing-password startup error did not name DB_PASSWORD: ${missing_out}"
+
+set +e
+partial_out="$(docker run --rm --network "${NET}" \
+ -e "DB_HOST=${PG_CTR}" \
+ -e "DB_NAME=${DB_NAME}" \
+ -e "DB_USER=${DB_USER}" \
+ -e "DB_PASSWORD=${DB_PASS}" \
+ -e "DB_ADMIN_USER=warracker_admin" \
+ "${IMAGE}" 2>&1)"
+partial_rc=$?
+set -e
+[ "${partial_rc}" -ne 0 ] || fail "Container unexpectedly succeeded with partial DB_ADMIN_USER"
+grep -q "DB_ADMIN_PASSWORD" <<< "${partial_out}" \
+ || fail "Partial-admin startup error did not name DB_ADMIN_PASSWORD: ${partial_out}"
+
+log "All Docker smoke tests passed!"