Files
openwaf/scripts/ci-linux-rehearsal.sh
T
Pavlo Ratushnyi 7e081da6fe The rehearsal tore down a stack it did not start and left the one it did running
The console stage starts the stack through the host daemon with whatever
COMPOSE_PROJECT_NAME the caller passed, so the CI-named project is what is
running. Its cleanup container passed no such name, so it removed the stack
named after this directory - nothing - and printed PASS while two containers
held the CI port. Re-running the stage with the CI console job's own
environment is what showed it: 53 assertions, 0 failed, and 2 containers still
up. With the name passed through, the same stage leaves 0.
2026-10-02 07:35:26 +02:00

236 lines
11 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run the CI gates on the operating system the runner uses, without a runner.
#
# Every green number this project has quoted was produced on macOS. The workflow
# runs on ubuntu-latest. That gap is where a first CI run dies: a gate that
# passes on darwin and fails on linux is not a gate, it is a local habit. This
# script closes the part of the gap that can be closed at a desk.
#
# scripts/ci-linux-rehearsal.sh # all three stages
# scripts/ci-linux-rehearsal.sh source # restore, build, unit, integration, smoke
# scripts/ci-linux-rehearsal.sh compose # compose-smoke against the host daemon
# scripts/ci-linux-rehearsal.sh console # the page, in a real Chromium on Linux
#
# Covered: the dotnet suites and smoke.sh (Ubuntu 24.04, the same SDK family the
# runner installs), compose-smoke.sh (Alpine container, linux image build, the
# host daemon doing the work) and the console gate (the official Playwright image
# with the same Playwright version the CI console job pins).
set -euo pipefail
cd "$(dirname "$0")/.."
STAGE=${1:-all}
SOURCE_IMAGE=${SOURCE_IMAGE:-mcr.microsoft.com/dotnet/sdk:10.0}
COMPOSE_IMAGE=${COMPOSE_IMAGE:-docker:cli}
BROWSER_IMAGE=${BROWSER_IMAGE:-mcr.microsoft.com/playwright:v1.63.0-noble}
# The version the CI console job pins. If these two ever disagree, the
# rehearsal is not rehearsing what CI will do.
PLAYWRIGHT_VERSION=${PLAYWRIGHT_VERSION:-1.63.0}
DOCKER_SOCKET=${DOCKER_SOCKET:-/var/run/docker.sock}
# The stack is created by the host daemon, so its published port lives in the
# host's namespace; from inside a container that is the gateway, not 127.0.0.1.
HOST_EDGE=${HOST_EDGE:-http://host.docker.internal:8080}
OPENWAF_ADMIN_TOKEN=${OPENWAF_ADMIN_TOKEN:-ci-only-throwaway}
export OPENWAF_ADMIN_TOKEN DOTNET_CLI_TELEMETRY_OPTOUT=1 DOTNET_NOLOGO=1
source_status=0
compose_status=0
console_status=0
source_ran=0
compose_ran=0
console_ran=0
# A stage the caller never asked for and a stage that could not run are
# different answers. Printing "skipped (no docker socket)" for a stage that was
# not selected is the kind of lie a summary line tells by accident.
verdict() {
ran=$1; status=$2; selected=$3; needs_socket=$4
if [ "$ran" -eq 1 ]; then
if [ "$status" -eq 0 ]; then echo PASS; else echo "FAIL ($status)"; fi
elif [ "$selected" -eq 0 ]; then
echo 'not run'
elif [ "$needs_socket" -eq 1 ]; then
echo 'skipped (no docker socket)'
else
echo 'not run'
fi
}
source_selected=0; compose_selected=0; console_selected=0
case "$STAGE" in
all) source_selected=1; compose_selected=1; console_selected=1 ;;
source) source_selected=1 ;;
compose) compose_selected=1 ;;
console) console_selected=1 ;;
esac
if [ "$STAGE" = all ] || [ "$STAGE" = source ]; then
WORK=$(mktemp -d /tmp/openwaf-linux.XXXXXX)
trap 'rm -rf "$WORK"' EXIT
echo "copying the tree (no .git, no build output) into $WORK"
# A copy, not a mount: Linux-built binaries landing in a macOS working tree
# make the next local `--no-build` try to exec an ELF file.
tar --exclude='./.git' --exclude='bin' --exclude='obj' -cf - . | tar -xf - -C "$WORK"
source_ran=1
echo "== source gates in $SOURCE_IMAGE =="
docker run --rm -v "$WORK:/src" -w /src \
-e DOTNET_CLI_TELEMETRY_OPTOUT=1 -e DOTNET_NOLOGO=1 \
"$SOURCE_IMAGE" bash -euo pipefail -c '
echo "-- what the container is --"
uname -srm
grep -m1 PRETTY_NAME /etc/os-release
cd dotnet && echo "sdk resolving global.json: $(dotnet --version)" && cd ..
echo "== gate: restore =="
dotnet restore dotnet/OpenWaf.slnx
echo "== gate: build (TreatWarningsAsErrors) =="
dotnet build dotnet/OpenWaf.slnx -c Release --no-restore
echo "== gate: unit =="
dotnet test dotnet/OpenWaf.Tests.Unit/OpenWaf.Tests.Unit.csproj -c Release --no-build
echo "== gate: integration =="
dotnet test dotnet/OpenWaf.Tests.Integration/OpenWaf.Tests.Integration.csproj -c Release --no-build
# smoke.sh uses `python3 -m http.server` as the upstream it knows nothing
# about. The runner image has python3; the dotnet sdk image does not.
echo "== python3 for the smoke upstream =="
command -v python3 || { apt-get update -qq && apt-get install -y -qq python3 >/dev/null; }
python3 --version
echo "== gate: smoke =="
./scripts/smoke.sh
' || source_status=$?
fi
if [ "$STAGE" = all ] || [ "$STAGE" = compose ]; then
if [ ! -S "$DOCKER_SOCKET" ]; then
echo "compose stage SKIPPED: no docker socket at $DOCKER_SOCKET"
else
compose_ran=1
echo "== compose gate in $COMPOSE_IMAGE, driven by the host daemon =="
# The repo is mounted at the SAME absolute path it has on the host, because
# the daemon that builds the image resolves the build context on its own
# side of the mount: a different path inside the container would send
# `compose build` looking for a context that does not exist there.
# docker:cli ships no bash and no curl, both of which the gate uses, so the
# container adds them itself.
# The stack is created by the host daemon, so its published port lives in
# the host's namespace, not this container's: the first run of this stage
# reached the docker API fine (the image's own HEALTHCHECK assertion passed)
# and got curl code 000 on every HTTP assertion, because 127.0.0.1 inside
# the container is the container. EDGE is the gate's own override for
# exactly this, pointed at the gateway.
docker run --rm \
-v "$PWD:$PWD" -w "$PWD" \
-v "$DOCKER_SOCKET:/var/run/docker.sock" \
--add-host=host.docker.internal:host-gateway \
-e OPENWAF_ADMIN_TOKEN \
-e EDGE="${EDGE:-http://host.docker.internal:8080}" \
"$COMPOSE_IMAGE" sh -euo pipefail -c '
echo "-- what the container is --"
uname -srm 2>/dev/null || grep -m1 NAME /etc/os-release
docker --version
docker compose version
# compose-smoke.sh asserts over JSON with python3 (jcount/jget) and
# reads the clock with it; the first Linux run of this stage failed 23
# of 27 assertions for nothing but a missing python3. The runner image
# has python3; a bare container does not.
command -v bash >/dev/null || apk add --no-cache bash >/dev/null
command -v curl >/dev/null || apk add --no-cache curl >/dev/null
command -v python3 >/dev/null || apk add --no-cache python3 >/dev/null
echo "== gate: compose-smoke =="
bash ./scripts/compose-smoke.sh
' || compose_status=$?
fi
fi
if [ "$STAGE" = all ] || [ "$STAGE" = console ]; then
if [ ! -S "$DOCKER_SOCKET" ]; then
echo "console stage SKIPPED: no docker socket at $DOCKER_SOCKET"
else
console_ran=1
echo "== console gate: seeded by $COMPOSE_IMAGE, driven in $BROWSER_IMAGE =="
# Start and seed the stack through the host daemon, and leave it up: the
# page is driven from the next container, which is the one with a Chromium.
docker run --rm \
-v "$PWD:$PWD" -w "$PWD" \
-v "$DOCKER_SOCKET:/var/run/docker.sock" \
--add-host=host.docker.internal:host-gateway \
-e OPENWAF_ADMIN_TOKEN -e EDGE="$HOST_EDGE" -e SEED_ONLY=1 \
-e COMPOSE_PROJECT_NAME -e EDGE_PORT \
"$COMPOSE_IMAGE" sh -euo pipefail -c '
command -v bash >/dev/null || apk add --no-cache bash >/dev/null
command -v curl >/dev/null || apk add --no-cache curl >/dev/null
bash ./scripts/console-smoke.sh
' || console_status=$?
if [ "$console_status" -eq 0 ]; then
# The seeding container chose SEED_ERROR for this run and wrote it out; the
# driver has to look at the same address, or the alert assertion checks
# nothing in particular.
SEED_ERROR=$(sed -n 's/^SEED_ERROR=//p' console-seed.env 2>/dev/null)
SEED_MARKUP_PATH=$(sed -n 's/^SEED_MARKUP_PATH=//p' console-seed.env 2>/dev/null)
SEED_MARKUP_BLOCK=$(sed -n 's/^SEED_MARKUP_BLOCK=//p' console-seed.env 2>/dev/null)
SEED_EXPIRED_CIDR=$(sed -n 's/^SEED_EXPIRED_CIDR=//p' console-seed.env 2>/dev/null)
[ -n "$SEED_ERROR" ] || { echo 'console stage: no SEED_ERROR in console-seed.env' >&2; console_status=1; }
# Empty here would make the expired-exemption assertion look at '' and fail,
# which is the right outcome but the wrong reason, so it is named instead.
[ -n "$SEED_EXPIRED_CIDR" ] || { echo 'console stage: no SEED_EXPIRED_CIDR in console-seed.env' >&2; console_status=1; }
fi
if [ "$console_status" -eq 0 ]; then
docker run --rm \
-v "$PWD:$PWD:ro" -w /tmp \
--add-host=host.docker.internal:host-gateway \
-e EDGE_URL="$HOST_EDGE" -e OPENWAF_ADMIN_TOKEN \
-e SEED_SOURCE=203.0.113.55 -e SEED_BLOCK=203.0.113.77 \
-e SEED_ERROR="$SEED_ERROR" \
-e SEED_MARKUP_PATH="$SEED_MARKUP_PATH" -e SEED_MARKUP_BLOCK="$SEED_MARKUP_BLOCK" \
-e SEED_EXPIRED_CIDR="$SEED_EXPIRED_CIDR" \
-e SCREENSHOT=/tmp/console-linux.png \
-e CHECK="$PWD/scripts/console-check.cjs" \
-e PW_VERSION="$PLAYWRIGHT_VERSION" \
"$BROWSER_IMAGE" bash -euo pipefail -c '
echo "-- what the container is --"
uname -srm
node --version
ls /ms-playwright | head -3
# The official image ships the browsers (PLAYWRIGHT_BROWSERS_PATH is
# /ms-playwright) but NOT the npm package — checked, there is no
# playwright-core anywhere in it. So the package is installed here at
# the version the CI console job pins, into /tmp: the repo is mounted
# read-only for this stage, and node_modules must not land in it.
npm init -y >/dev/null 2>&1
npm install --no-save "playwright@${PW_VERSION}" >/dev/null 2>&1
node -e "console.log(\"playwright-core: \" + require.resolve(\"playwright-core\"))"
echo "== gate: console =="
PLAYWRIGHT_CORE=/tmp/node_modules/playwright-core node "$CHECK"
' || console_status=$?
fi
# The teardown has to name the same project the seeding container named.
# Without COMPOSE_PROJECT_NAME this removes the stack named after this
# directory and leaves the CI-named one — and its port — running behind a
# summary line that says PASS.
docker run --rm -v "$PWD:$PWD" -w "$PWD" \
-v "$DOCKER_SOCKET:/var/run/docker.sock" \
-e COMPOSE_PROJECT_NAME -e EDGE_PORT \
"$COMPOSE_IMAGE" sh -c 'docker compose down >/dev/null 2>&1 || true'
fi
fi
source_verdict=$(verdict "$source_ran" "$source_status" "$source_selected" 0)
compose_verdict=$(verdict "$compose_ran" "$compose_status" "$compose_selected" 1)
console_verdict=$(verdict "$console_ran" "$console_status" "$console_selected" 1)
echo
echo '== rehearsal summary =='
printf ' source gates (restore/build/unit/integration/smoke): %s\n' "$source_verdict"
printf ' compose gate: %s\n' "$compose_verdict"
printf ' console gate: %s\n' "$console_verdict"
[ "$source_status" -eq 0 ] && [ "$compose_status" -eq 0 ] && [ "$console_status" -eq 0 ]