diff --git a/AGENTS.md b/AGENTS.md index 9a42287b..c5e50a79 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -41,10 +41,10 @@ design is in `docs/design.md`; read it before changing the architecture, and upd customer's document or data. - Build Rust in CI or on a build machine (docspring-dev), not on small agent VMs. The server is app/ (Laravel Rust, `visualci-app` in the root workspace, with the CLI and its crates in crates/, whose tests need no database). - `scripts/on-dev test` runs the crates' tests; app/'s (`cargo test -p visualci-app`) need docspring-dev's Postgres: - a `scripts/on-dev run` naming `DATABASE_URL` (such as `VISUALCI_APP_TEST_DATABASE_URL=...`) leases - docspring-dev itself, never the pool's rented box, which has no Postgres. Point them at a - database of your own, `visualci_app_test_`. + `scripts/on-dev test` runs the crates' tests. app/'s (`cargo test -p visualci-app`) need Postgres and a blob store: + run them with `scripts/on-dev run` naming `VISUALCI_APP_TEST_DATABASE_URL=postgres://...@127.0.0.1:55433/visualci_app_test_`. + On docspring-dev that's its own Postgres (a database of your own per story). On a pool box the run starts its own + Postgres and RustFS in Docker, points the command at them, and removes them when it ends. - VisualCI dev ports on docspring-dev: the app 5250 (`SERVER_PORT=5250`, which the Vite proxy defaults to), Vite 5173, previews 5180–5199. 5150 is Talira's dev server: never use it (on 1 Dec 2026 a VisualCI dev server took it, and Talira's localhost and MCP diff --git a/app/.env.example b/app/.env.example index f70ca7c8..8749382f 100644 --- a/app/.env.example +++ b/app/.env.example @@ -335,6 +335,9 @@ GITHUB_CLIENT_SECRET= # postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test. # VISUALCI_APP_TEST_DATABASE_URL= +# The tests' blob store (RustFS or MinIO). Default: CI's, http://127.0.0.1:59000. +# VISUALCI_APP_TEST_S3_ENDPOINT= + # A folder the email tests write each email's HTML to, for review. # VISUALCI_EMAIL_SNAPSHOTS= diff --git a/app/src/config.rs b/app/src/config.rs index 17693c0f..95f43085 100644 --- a/app/src/config.rs +++ b/app/src/config.rs @@ -405,6 +405,10 @@ const DEVELOPMENT: &[Variable] = &[ "The tests' Postgres (each test borrows a copy of it). Default: CI's, \ `postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test`.", ), + Variable::new( + "VISUALCI_APP_TEST_S3_ENDPOINT", + "The tests' blob store (RustFS or MinIO). Default: CI's, `http://127.0.0.1:59000`.", + ), Variable::new("VISUALCI_EMAIL_SNAPSHOTS", "A folder the email tests write each email's HTML to, for review."), Variable::new( "VISUALCI_GOLDEN_UPDATE", diff --git a/app/src/testing.rs b/app/src/testing.rs index 7fc11e6a..68c0c225 100644 --- a/app/src/testing.rs +++ b/app/src/testing.rs @@ -8,6 +8,7 @@ use laravel::Application; use laravel::foundation::ApplicationBuilder; const CI_DATABASE: &str = "postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test"; +const CI_S3_ENDPOINT: &str = "http://127.0.0.1:59000"; pub fn create() -> Application { configure().create() @@ -19,6 +20,12 @@ pub fn database_url() -> String { std::env::var("VISUALCI_APP_TEST_DATABASE_URL").unwrap_or_else(|_| CI_DATABASE.to_string()) } +/// The test blob store: `VISUALCI_APP_TEST_S3_ENDPOINT`, else CI's (RustFS on port 59000). +#[must_use] +pub fn s3_endpoint() -> String { + std::env::var("VISUALCI_APP_TEST_S3_ENDPOINT").unwrap_or_else(|_| CI_S3_ENDPOINT.to_string()) +} + pub fn configure() -> ApplicationBuilder { let url = database_url(); crate::configure() @@ -38,8 +45,6 @@ pub fn configure() -> ApplicationBuilder { ("SESSION_DRIVER", "database"), ("WORKFLOW_STORE", "memory"), ("DB_CONNECTION", "pgsql"), - // The test blob store: RustFS in CI, MinIO on a development machine, both on port 59000. - ("VISUALCI_S3_ENDPOINT", "http://127.0.0.1:59000"), ("VISUALCI_S3_BUCKET", "visualci-test"), ("VISUALCI_S3_ACCESS_KEY_ID", "visualci"), ("VISUALCI_S3_SECRET_ACCESS_KEY", "visualci-dev-secret"), @@ -49,6 +54,7 @@ pub fn configure() -> ApplicationBuilder { ("VISUALCI_METRICS_TOKEN", "metrics-test-token"), ]) .with_env("DATABASE_URL", url) + .with_env("VISUALCI_S3_ENDPOINT", s3_endpoint()) .refresh_database() } @@ -73,12 +79,12 @@ pub mod fixtures { /// The blob store, with its bucket made. Fails, naming the store, when it isn't running: a test never skips. /// /// # Errors - /// When the store (`VISUALCI_S3_ENDPOINT`, RustFS or MinIO on port 59000 in development and CI) isn't reachable. + /// When the store (`VISUALCI_APP_TEST_S3_ENDPOINT`, else RustFS on port 59000 as in CI) isn't reachable. pub async fn blob_store() -> laravel::Result { let store = crate::storage::BlobStore::current().await?; store.ensure_bucket().await.map_err(|e| { laravel::Error::msg(format!( - "the blob store isn't reachable (VISUALCI_S3_ENDPOINT, RustFS or MinIO on 127.0.0.1:59000 in CI): {e}" + "the blob store isn't reachable (VISUALCI_APP_TEST_S3_ENDPOINT, else RustFS on 127.0.0.1:59000 as in CI): {e}" )) })?; Ok(store) diff --git a/app/tests/golden/mod.rs b/app/tests/golden/mod.rs index f6e6904a..e91f2891 100644 --- a/app/tests/golden/mod.rs +++ b/app/tests/golden/mod.rs @@ -56,8 +56,10 @@ static NOISE: LazyLock> = LazyLock::new(|| { fn normalize(value: &serde_json::Value) -> serde_json::Value { let mut seen: HashMap<(&'static str, String), usize> = HashMap::new(); let mut counts: HashMap<&'static str, usize> = HashMap::new(); + // The blob store's address, as CI's: VISUALCI_APP_TEST_S3_ENDPOINT moves it to another port on a build box. + let endpoint = app::testing::s3_endpoint(); let mut fix = |s: &str| -> String { - let mut out = s.to_string(); + let mut out = s.replace(&endpoint, "http://127.0.0.1:59000"); for &(kind, ref re) in NOISE.iter() { out = re .replace_all(&out, |caps: ®ex::Captures| { diff --git a/app/tests/hosts.rs b/app/tests/hosts.rs index c69ab98f..2f6742f9 100644 --- a/app/tests/hosts.rs +++ b/app/tests/hosts.rs @@ -43,7 +43,7 @@ fn in_production_with_no_sign_ups_setting() -> laravel::foundation::ApplicationB ("QUEUE_CONNECTION", "sync"), ("DB_CONNECTION", "pgsql"), ("DATABASE_URL", app::testing::database_url().as_str()), - ("VISUALCI_S3_ENDPOINT", "http://127.0.0.1:59000"), + ("VISUALCI_S3_ENDPOINT", app::testing::s3_endpoint().as_str()), ("VISUALCI_S3_BUCKET", "visualci-test"), ("VISUALCI_S3_ACCESS_KEY_ID", "visualci"), ("VISUALCI_S3_SECRET_ACCESS_KEY", "visualci-dev-secret"), diff --git a/frontend/content/docs/self-hosting/environment-variables.md b/frontend/content/docs/self-hosting/environment-variables.md index eac73c03..156abe06 100644 --- a/frontend/content/docs/self-hosting/environment-variables.md +++ b/frontend/content/docs/self-hosting/environment-variables.md @@ -216,5 +216,6 @@ Never set these in production. | --- | --- | --- | --- | | `VISUALCI_NOW` | string | | Stands in for the clock in credit allowances (RFC 3339), for screenshots of seeded data. | | `VISUALCI_APP_TEST_DATABASE_URL` | string | | The tests' Postgres (each test borrows a copy of it). Default: CI's, `postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test`. | +| `VISUALCI_APP_TEST_S3_ENDPOINT` | string | | The tests' blob store (RustFS or MinIO). Default: CI's, `http://127.0.0.1:59000`. | | `VISUALCI_EMAIL_SNAPSHOTS` | string | | A folder the email tests write each email's HTML to, for review. | | `VISUALCI_GOLDEN_UPDATE` | string | | `1` makes the tests write their golden files (this page among them) instead of comparing. | diff --git a/scripts/on-dev b/scripts/on-dev index 72db0b2c..79843d20 100755 --- a/scripts/on-dev +++ b/scripts/on-dev @@ -21,8 +21,10 @@ # the command builds on the line whose slot name is the member the pool gave: docspring-dev first, a rented box only # when its queue is long. Run inside a lease you took yourself (`fleetlayer lease run docspring-dev-light -- ...`), it # builds on the line with that lease's slot name. A `run` that uses the test database (one naming 127.0.0.1:55433 or -# DATABASE_URL, such as app/'s tests with VISUALCI_APP_TEST_DATABASE_URL) always leases the first line's slot instead of -# the pool's: only that host has it. +# DATABASE_URL) builds on the first line's host, which has the test database (docspring-dev's Postgres on 55433), unless +# it's app/'s tests (one naming VISUALCI_APP_TEST_DATABASE_URL): those build anywhere, and on any other host start their +# own Postgres and RustFS in Docker on free ports, as CI does, point the command's database URL and +# VISUALCI_APP_TEST_S3_ENDPOINT at them, and remove them when they end (Ctrl-C included). set -euo pipefail remote_root=/opt/code/visualci/checkouts dir=$(git rev-parse --show-toplevel) @@ -33,11 +35,11 @@ hosts=$(grep -v '^[[:space:]]*\(#\|$\)' "$dir/.dev-host" 2>/dev/null || true) pool=$(printf '%s\n' "$hosts" | awk '$1 == "pool" {print $2; exit}') hosts=$(printf '%s\n' "$hosts" | awk 'NF && $1 != "pool"') [ -n "$hosts" ] || hosts=docspring-dev.hs -# app/'s tests need the test database, which only the first host has (docspring-dev's Postgres on 127.0.0.1:55433): -# they lease that host's slot rather than the pool's, which may hand out a rented box without one (every database test then -# fails with PoolTimedOut). +# The first host has the test database (docspring-dev's Postgres on 127.0.0.1:55433, MinIO on 59000). app/'s tests get +# throwaway containers anywhere else; other database runs (the screenshots, journeys, a seeded dev server) need that host. db_line=$(printf '%s\n' "$hosts" | head -n 1) db_kind=$(printf '%s\n' "$db_line" | awk '{print $2}') +db_host=$(printf '%s\n' "$db_line" | awk '{print $1}') # Inside a lease: the line whose slot name is the lease's kind (its light twin builds on the same host). kind=${FLEETLAYER_LEASE_KIND:-} line= @@ -46,14 +48,19 @@ line= host=${ON_DEV_HOST:-$(printf '%s\n' "$line" | awk '{print $1}')} die() { echo "scripts/on-dev: $*" >&2; exit 1; } -usage() { sed -n '2,25p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; } +usage() { sed -n '2,27p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; } [ $# -gt 0 ] || usage cmd=$1; shift -# Whether the command uses the test database: a `run` that names it. +# Whether the command uses the test database (a `run` that names it), and whether it's app/'s tests, which can have their +# own. needs_db= +app_tests= case $cmd in - run) case " $* " in *55433* | *DATABASE_URL=*) needs_db=1 ;; esac ;; + run) + case " $* " in *55433* | *DATABASE_URL=*) needs_db=1 ;; esac + case " $* " in *VISUALCI_APP_TEST_DATABASE_URL=*) app_tests=1 ;; esac + ;; esac # With a pool and no lease held yet: hold a slot from the pool for the whole command, and run this script again inside @@ -61,13 +68,13 @@ esac case $cmd in sync | clean) ;; *) if [ -n "$pool" ] && [ -z "$kind" ] && [ -z "${ON_DEV_HOST:-}" ]; then case $cmd in fmt | check | clippy) pool_kind=$pool-light ;; *) pool_kind=$pool ;; esac - [ -n "$needs_db" ] && [ -n "$db_kind" ] && pool_kind=$db_kind + [ -n "$needs_db" ] && [ -z "$app_tests" ] && [ -n "$db_kind" ] && pool_kind=$db_kind exec fleetlayer lease run "$pool_kind" --reason "visualci on-dev $cmd ($name)" -- "$0" "$cmd" "$@" fi ;; esac -if [ -n "$needs_db" ] && [ -z "${ON_DEV_HOST:-}" ] && [ "$line" != "$db_line" ]; then - die "$cmd needs the test database, which is on $(printf '%s\n' "$db_line" | awk '{print $1}'), but this lease ($kind) builds on $host: take a ${db_kind:-docspring-dev} lease, or run it outside one" +if [ -n "$needs_db" ] && [ -z "$app_tests" ] && [ "$host" != "$db_host" ]; then + die "$cmd needs the test database, which is on $db_host, but it would build on $host: take a ${db_kind:-docspring-dev} lease, or run it outside one (only app/'s tests, naming VISUALCI_APP_TEST_DATABASE_URL, get their own)" fi sync() { @@ -99,6 +106,42 @@ run() { return "$status" } +# A database run on a host without the test database: its own Postgres and RustFS (CI's images and credentials), named +# for this run so parallel runs on one box don't meet, on ports Docker picks, removed when this script exits. +services() { + local tag + tag=vci-$(printf '%s' "$name" | tr -c 'a-zA-Z0-9_.-' '-')-$$-$RANDOM + containers="$tag-pg $tag-s3" + trap 'ssh "$host" "docker rm -f $containers >/dev/null 2>&1" || true' EXIT + trap 'exit 130' INT TERM + # Each carries when it expires (6 hours on): a run removes earlier runs' that outlived it (a script killed with + # SIGKILL, or a box its cleanup couldn't reach). + local ports + ports=$(ssh "$host" "set -e + now=\$(date +%s) + for c in \$(docker ps -aq --filter label=visualci-on-dev-expires); do + [ \"\$(docker inspect -f '{{index .Config.Labels \"visualci-on-dev-expires\"}}' \$c)\" -lt \$now ] && docker rm -f \$c >/dev/null || true + done + label=visualci-on-dev-expires=\$((now + 21600)) + docker run -d --rm --name $tag-pg --label \$label -p 127.0.0.1::5432 -e POSTGRES_USER=visualci \ + -e POSTGRES_PASSWORD=visualci -e POSTGRES_DB=visualci_app_test postgres:17 >/dev/null + docker run -d --rm --name $tag-s3 --label \$label -p 127.0.0.1::9000 -e RUSTFS_ACCESS_KEY=visualci \ + -e RUSTFS_SECRET_KEY=visualci-dev-secret rustfs/rustfs:1.0.6 >/dev/null + pg=\$(docker port $tag-pg 5432/tcp | head -n1 | sed 's/.*://'); s3=\$(docker port $tag-s3 9000/tcp | head -n1 | sed 's/.*://') + [ -n \"\$pg\" ] && [ -n \"\$s3\" ] || { echo 'a container stopped as it started' >&2; exit 1; } + ready= + for _ in \$(seq 1 60); do docker exec $tag-pg pg_isready -q -U visualci -h 127.0.0.1 && ready=1 && break; sleep 1; done + [ -n \"\$ready\" ] || { echo 'Postgres not ready after 60 s' >&2; exit 1; } + ready= + for _ in \$(seq 1 60); do curl -s -o /dev/null http://127.0.0.1:\$s3/ && ready=1 && break; sleep 1; done + [ -n \"\$ready\" ] || { echo 'RustFS not answering after 60 s' >&2; exit 1; } + echo \$pg \$s3") || die "couldn't start the test Postgres and RustFS in Docker on $host" + local pg=${ports% *} s3=${ports#* } + db_url=postgres://visualci:visualci@127.0.0.1:$pg/visualci_app_test + s3_url=http://127.0.0.1:$s3 + echo "scripts/on-dev: test Postgres on 127.0.0.1:$pg and RustFS on 127.0.0.1:$s3 on $host" >&2 +} + case $cmd in sync) sync ;; fmt) sync; run cargo fmt --all --check ;; @@ -113,7 +156,20 @@ case $cmd in *) run cargo test -q --locked -j 24 --workspace --exclude visualci-app "$@" ;; esac ;; - run) sync; run "$@" ;; + run) + sync + if [ -n "$app_tests" ] && [ "$host" != "$db_host" ]; then + services + # The command's own database URL (docspring-dev's, on 55433), whole, becomes the container's + args=() + for a in "$@"; do + args+=("$(printf '%s' "$a" | sed -E "s#postgres(ql)?://[^[:space:]'\"]*(127\.0\.0\.1|localhost|\[::1\]):55433[^[:space:]'\"]*#$db_url#g")") + done + run env VISUALCI_APP_TEST_DATABASE_URL="$db_url" VISUALCI_APP_TEST_S3_ENDPOINT="$s3_url" "${args[@]}" + else + run "$@" + fi + ;; fix) sync run cargo clippy -q --locked -j 24 --workspace --all-targets --fix --allow-dirty --allow-no-vcs "$@" || true