Skip to content

Commit e0d547e

Browse files
authored
on-dev: app/'s database tests run on any build box, with throwaway Postgres and RustFS in Docker (#154)
* on-dev: a database run on a build box without the test database starts its own Postgres and RustFS in Docker, and removes them when it ends On a pool host (or any host but the first .dev-host line), a run that names DATABASE_URL or 55433 starts postgres:17 and rustfs/rustfs:1.0.6 with CI's credentials, on ports Docker picks and named per run. It points the command's database URL and VISUALCI_APP_TEST_S3_ENDPOINT at them. A trap removes them on exit, Ctrl-C included. docspring-dev keeps its own Postgres, and database runs are no longer refused inside a pool lease. app/'s tests read the blob store from VISUALCI_APP_TEST_S3_ENDPOINT (default CI's 127.0.0.1:59000). The golden files write it back as CI's address. * AGENTS.md: app/'s tests run on any build box * on-dev: only app/'s tests get throwaway containers; widen the URL rewrite, fail clearly when a container isn't ready, and remove earlier runs' expired containers From the review: other database runs (the screenshots, journeys, a seeded dev server) need docspring-dev's MinIO and hosts as well, so they still lease and build there. The rewrite takes postgresql://, localhost and [::1], and the whole URL. A container that stops or isn't ready in 60 s stops the run with a message. Each container carries a 6-hour expiry label, and a later run removes any past it.
1 parent 6a7a1dc commit e0d547e

8 files changed

Lines changed: 94 additions & 22 deletions

File tree

‎AGENTS.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ design is in `docs/design.md`; read it before changing the architecture, and upd
4141
customer's document or data.
4242
- Build Rust in CI or on a build machine (docspring-dev), not on small agent VMs. The server is app/ (Laravel Rust,
4343
`visualci-app` in the root workspace, with the CLI and its crates in crates/, whose tests need no database).
44-
`scripts/on-dev test` runs the crates' tests; app/'s (`cargo test -p visualci-app`) need docspring-dev's Postgres:
45-
a `scripts/on-dev run` naming `DATABASE_URL` (such as `VISUALCI_APP_TEST_DATABASE_URL=...`) leases
46-
docspring-dev itself, never the pool's rented box, which has no Postgres. Point them at a
47-
database of your own, `visualci_app_test_<story>`.
44+
`scripts/on-dev test` runs the crates' tests. app/'s (`cargo test -p visualci-app`) need Postgres and a blob store:
45+
run them with `scripts/on-dev run` naming `VISUALCI_APP_TEST_DATABASE_URL=postgres://...@127.0.0.1:55433/visualci_app_test_<story>`.
46+
On docspring-dev that's its own Postgres (a database of your own per story). On a pool box the run starts its own
47+
Postgres and RustFS in Docker, points the command at them, and removes them when it ends.
4848
- VisualCI dev ports on docspring-dev: the app 5250 (`SERVER_PORT=5250`, which the Vite proxy defaults to), Vite 5173,
4949
previews 5180–5199. 5150 is
5050
Talira's dev server: never use it (on 1 Dec 2026 a VisualCI dev server took it, and Talira's localhost and MCP

‎app/.env.example‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ GITHUB_CLIENT_SECRET=
335335
# postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test.
336336
# VISUALCI_APP_TEST_DATABASE_URL=
337337

338+
# The tests' blob store (RustFS or MinIO). Default: CI's, http://127.0.0.1:59000.
339+
# VISUALCI_APP_TEST_S3_ENDPOINT=
340+
338341
# A folder the email tests write each email's HTML to, for review.
339342
# VISUALCI_EMAIL_SNAPSHOTS=
340343

‎app/src/config.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ const DEVELOPMENT: &[Variable] = &[
405405
"The tests' Postgres (each test borrows a copy of it). Default: CI's, \
406406
`postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test`.",
407407
),
408+
Variable::new(
409+
"VISUALCI_APP_TEST_S3_ENDPOINT",
410+
"The tests' blob store (RustFS or MinIO). Default: CI's, `http://127.0.0.1:59000`.",
411+
),
408412
Variable::new("VISUALCI_EMAIL_SNAPSHOTS", "A folder the email tests write each email's HTML to, for review."),
409413
Variable::new(
410414
"VISUALCI_GOLDEN_UPDATE",

‎app/src/testing.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use laravel::Application;
88
use laravel::foundation::ApplicationBuilder;
99

1010
const CI_DATABASE: &str = "postgres://visualci:visualci@127.0.0.1:55433/visualci_app_test";
11+
const CI_S3_ENDPOINT: &str = "http://127.0.0.1:59000";
1112

1213
pub fn create() -> Application {
1314
configure().create()
@@ -19,6 +20,12 @@ pub fn database_url() -> String {
1920
std::env::var("VISUALCI_APP_TEST_DATABASE_URL").unwrap_or_else(|_| CI_DATABASE.to_string())
2021
}
2122

23+
/// The test blob store: `VISUALCI_APP_TEST_S3_ENDPOINT`, else CI's (RustFS on port 59000).
24+
#[must_use]
25+
pub fn s3_endpoint() -> String {
26+
std::env::var("VISUALCI_APP_TEST_S3_ENDPOINT").unwrap_or_else(|_| CI_S3_ENDPOINT.to_string())
27+
}
28+
2229
pub fn configure() -> ApplicationBuilder {
2330
let url = database_url();
2431
crate::configure()
@@ -38,8 +45,6 @@ pub fn configure() -> ApplicationBuilder {
3845
("SESSION_DRIVER", "database"),
3946
("WORKFLOW_STORE", "memory"),
4047
("DB_CONNECTION", "pgsql"),
41-
// The test blob store: RustFS in CI, MinIO on a development machine, both on port 59000.
42-
("VISUALCI_S3_ENDPOINT", "http://127.0.0.1:59000"),
4348
("VISUALCI_S3_BUCKET", "visualci-test"),
4449
("VISUALCI_S3_ACCESS_KEY_ID", "visualci"),
4550
("VISUALCI_S3_SECRET_ACCESS_KEY", "visualci-dev-secret"),
@@ -49,6 +54,7 @@ pub fn configure() -> ApplicationBuilder {
4954
("VISUALCI_METRICS_TOKEN", "metrics-test-token"),
5055
])
5156
.with_env("DATABASE_URL", url)
57+
.with_env("VISUALCI_S3_ENDPOINT", s3_endpoint())
5258
.refresh_database()
5359
}
5460

@@ -73,12 +79,12 @@ pub mod fixtures {
7379
/// The blob store, with its bucket made. Fails, naming the store, when it isn't running: a test never skips.
7480
///
7581
/// # Errors
76-
/// When the store (`VISUALCI_S3_ENDPOINT`, RustFS or MinIO on port 59000 in development and CI) isn't reachable.
82+
/// When the store (`VISUALCI_APP_TEST_S3_ENDPOINT`, else RustFS on port 59000 as in CI) isn't reachable.
7783
pub async fn blob_store() -> laravel::Result<crate::storage::BlobStore> {
7884
let store = crate::storage::BlobStore::current().await?;
7985
store.ensure_bucket().await.map_err(|e| {
8086
laravel::Error::msg(format!(
81-
"the blob store isn't reachable (VISUALCI_S3_ENDPOINT, RustFS or MinIO on 127.0.0.1:59000 in CI): {e}"
87+
"the blob store isn't reachable (VISUALCI_APP_TEST_S3_ENDPOINT, else RustFS on 127.0.0.1:59000 as in CI): {e}"
8288
))
8389
})?;
8490
Ok(store)

‎app/tests/golden/mod.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ static NOISE: LazyLock<Vec<(&'static str, Regex)>> = LazyLock::new(|| {
5656
fn normalize(value: &serde_json::Value) -> serde_json::Value {
5757
let mut seen: HashMap<(&'static str, String), usize> = HashMap::new();
5858
let mut counts: HashMap<&'static str, usize> = HashMap::new();
59+
// The blob store's address, as CI's: VISUALCI_APP_TEST_S3_ENDPOINT moves it to another port on a build box.
60+
let endpoint = app::testing::s3_endpoint();
5961
let mut fix = |s: &str| -> String {
60-
let mut out = s.to_string();
62+
let mut out = s.replace(&endpoint, "http://127.0.0.1:59000");
6163
for &(kind, ref re) in NOISE.iter() {
6264
out = re
6365
.replace_all(&out, |caps: &regex::Captures| {

‎app/tests/hosts.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn in_production_with_no_sign_ups_setting() -> laravel::foundation::ApplicationB
4343
("QUEUE_CONNECTION", "sync"),
4444
("DB_CONNECTION", "pgsql"),
4545
("DATABASE_URL", app::testing::database_url().as_str()),
46-
("VISUALCI_S3_ENDPOINT", "http://127.0.0.1:59000"),
46+
("VISUALCI_S3_ENDPOINT", app::testing::s3_endpoint().as_str()),
4747
("VISUALCI_S3_BUCKET", "visualci-test"),
4848
("VISUALCI_S3_ACCESS_KEY_ID", "visualci"),
4949
("VISUALCI_S3_SECRET_ACCESS_KEY", "visualci-dev-secret"),

‎frontend/content/docs/self-hosting/environment-variables.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,6 @@ Never set these in production.
216216
| --- | --- | --- | --- |
217217
| `VISUALCI_NOW` | string | | Stands in for the clock in credit allowances (RFC 3339), for screenshots of seeded data. |
218218
| `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`. |
219+
| `VISUALCI_APP_TEST_S3_ENDPOINT` | string | | The tests' blob store (RustFS or MinIO). Default: CI's, `http://127.0.0.1:59000`. |
219220
| `VISUALCI_EMAIL_SNAPSHOTS` | string | | A folder the email tests write each email's HTML to, for review. |
220221
| `VISUALCI_GOLDEN_UPDATE` | string | | `1` makes the tests write their golden files (this page among them) instead of comparing. |

‎scripts/on-dev‎

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
# the command builds on the line whose slot name is the member the pool gave: docspring-dev first, a rented box only
2222
# when its queue is long. Run inside a lease you took yourself (`fleetlayer lease run docspring-dev-light -- ...`), it
2323
# 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
24-
# DATABASE_URL, such as app/'s tests with VISUALCI_APP_TEST_DATABASE_URL) always leases the first line's slot instead of
25-
# the pool's: only that host has it.
24+
# DATABASE_URL) builds on the first line's host, which has the test database (docspring-dev's Postgres on 55433), unless
25+
# it's app/'s tests (one naming VISUALCI_APP_TEST_DATABASE_URL): those build anywhere, and on any other host start their
26+
# own Postgres and RustFS in Docker on free ports, as CI does, point the command's database URL and
27+
# VISUALCI_APP_TEST_S3_ENDPOINT at them, and remove them when they end (Ctrl-C included).
2628
set -euo pipefail
2729
remote_root=/opt/code/visualci/checkouts
2830
dir=$(git rev-parse --show-toplevel)
@@ -33,11 +35,11 @@ hosts=$(grep -v '^[[:space:]]*\(#\|$\)' "$dir/.dev-host" 2>/dev/null || true)
3335
pool=$(printf '%s\n' "$hosts" | awk '$1 == "pool" {print $2; exit}')
3436
hosts=$(printf '%s\n' "$hosts" | awk 'NF && $1 != "pool"')
3537
[ -n "$hosts" ] || hosts=docspring-dev.hs
36-
# app/'s tests need the test database, which only the first host has (docspring-dev's Postgres on 127.0.0.1:55433):
37-
# they lease that host's slot rather than the pool's, which may hand out a rented box without one (every database test then
38-
# fails with PoolTimedOut).
38+
# The first host has the test database (docspring-dev's Postgres on 127.0.0.1:55433, MinIO on 59000). app/'s tests get
39+
# throwaway containers anywhere else; other database runs (the screenshots, journeys, a seeded dev server) need that host.
3940
db_line=$(printf '%s\n' "$hosts" | head -n 1)
4041
db_kind=$(printf '%s\n' "$db_line" | awk '{print $2}')
42+
db_host=$(printf '%s\n' "$db_line" | awk '{print $1}')
4143
# Inside a lease: the line whose slot name is the lease's kind (its light twin builds on the same host).
4244
kind=${FLEETLAYER_LEASE_KIND:-}
4345
line=
@@ -46,28 +48,33 @@ line=
4648
host=${ON_DEV_HOST:-$(printf '%s\n' "$line" | awk '{print $1}')}
4749

4850
die() { echo "scripts/on-dev: $*" >&2; exit 1; }
49-
usage() { sed -n '2,25p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
51+
usage() { sed -n '2,27p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2; }
5052
[ $# -gt 0 ] || usage
5153
cmd=$1; shift
5254

53-
# Whether the command uses the test database: a `run` that names it.
55+
# Whether the command uses the test database (a `run` that names it), and whether it's app/'s tests, which can have their
56+
# own.
5457
needs_db=
58+
app_tests=
5559
case $cmd in
56-
run) case " $* " in *55433* | *DATABASE_URL=*) needs_db=1 ;; esac ;;
60+
run)
61+
case " $* " in *55433* | *DATABASE_URL=*) needs_db=1 ;; esac
62+
case " $* " in *VISUALCI_APP_TEST_DATABASE_URL=*) app_tests=1 ;; esac
63+
;;
5764
esac
5865

5966
# With a pool and no lease held yet: hold a slot from the pool for the whole command, and run this script again inside
6067
# it, where the pool's member picks the host. sync and clean only copy or delete files.
6168
case $cmd in sync | clean) ;; *)
6269
if [ -n "$pool" ] && [ -z "$kind" ] && [ -z "${ON_DEV_HOST:-}" ]; then
6370
case $cmd in fmt | check | clippy) pool_kind=$pool-light ;; *) pool_kind=$pool ;; esac
64-
[ -n "$needs_db" ] && [ -n "$db_kind" ] && pool_kind=$db_kind
71+
[ -n "$needs_db" ] && [ -z "$app_tests" ] && [ -n "$db_kind" ] && pool_kind=$db_kind
6572
exec fleetlayer lease run "$pool_kind" --reason "visualci on-dev $cmd ($name)" -- "$0" "$cmd" "$@"
6673
fi ;;
6774
esac
6875

69-
if [ -n "$needs_db" ] && [ -z "${ON_DEV_HOST:-}" ] && [ "$line" != "$db_line" ]; then
70-
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"
76+
if [ -n "$needs_db" ] && [ -z "$app_tests" ] && [ "$host" != "$db_host" ]; then
77+
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)"
7178
fi
7279

7380
sync() {
@@ -99,6 +106,42 @@ run() {
99106
return "$status"
100107
}
101108

109+
# A database run on a host without the test database: its own Postgres and RustFS (CI's images and credentials), named
110+
# for this run so parallel runs on one box don't meet, on ports Docker picks, removed when this script exits.
111+
services() {
112+
local tag
113+
tag=vci-$(printf '%s' "$name" | tr -c 'a-zA-Z0-9_.-' '-')-$$-$RANDOM
114+
containers="$tag-pg $tag-s3"
115+
trap 'ssh "$host" "docker rm -f $containers >/dev/null 2>&1" || true' EXIT
116+
trap 'exit 130' INT TERM
117+
# Each carries when it expires (6 hours on): a run removes earlier runs' that outlived it (a script killed with
118+
# SIGKILL, or a box its cleanup couldn't reach).
119+
local ports
120+
ports=$(ssh "$host" "set -e
121+
now=\$(date +%s)
122+
for c in \$(docker ps -aq --filter label=visualci-on-dev-expires); do
123+
[ \"\$(docker inspect -f '{{index .Config.Labels \"visualci-on-dev-expires\"}}' \$c)\" -lt \$now ] && docker rm -f \$c >/dev/null || true
124+
done
125+
label=visualci-on-dev-expires=\$((now + 21600))
126+
docker run -d --rm --name $tag-pg --label \$label -p 127.0.0.1::5432 -e POSTGRES_USER=visualci \
127+
-e POSTGRES_PASSWORD=visualci -e POSTGRES_DB=visualci_app_test postgres:17 >/dev/null
128+
docker run -d --rm --name $tag-s3 --label \$label -p 127.0.0.1::9000 -e RUSTFS_ACCESS_KEY=visualci \
129+
-e RUSTFS_SECRET_KEY=visualci-dev-secret rustfs/rustfs:1.0.6 >/dev/null
130+
pg=\$(docker port $tag-pg 5432/tcp | head -n1 | sed 's/.*://'); s3=\$(docker port $tag-s3 9000/tcp | head -n1 | sed 's/.*://')
131+
[ -n \"\$pg\" ] && [ -n \"\$s3\" ] || { echo 'a container stopped as it started' >&2; exit 1; }
132+
ready=
133+
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
134+
[ -n \"\$ready\" ] || { echo 'Postgres not ready after 60 s' >&2; exit 1; }
135+
ready=
136+
for _ in \$(seq 1 60); do curl -s -o /dev/null http://127.0.0.1:\$s3/ && ready=1 && break; sleep 1; done
137+
[ -n \"\$ready\" ] || { echo 'RustFS not answering after 60 s' >&2; exit 1; }
138+
echo \$pg \$s3") || die "couldn't start the test Postgres and RustFS in Docker on $host"
139+
local pg=${ports% *} s3=${ports#* }
140+
db_url=postgres://visualci:visualci@127.0.0.1:$pg/visualci_app_test
141+
s3_url=http://127.0.0.1:$s3
142+
echo "scripts/on-dev: test Postgres on 127.0.0.1:$pg and RustFS on 127.0.0.1:$s3 on $host" >&2
143+
}
144+
102145
case $cmd in
103146
sync) sync ;;
104147
fmt) sync; run cargo fmt --all --check ;;
@@ -113,7 +156,20 @@ case $cmd in
113156
*) run cargo test -q --locked -j 24 --workspace --exclude visualci-app "$@" ;;
114157
esac
115158
;;
116-
run) sync; run "$@" ;;
159+
run)
160+
sync
161+
if [ -n "$app_tests" ] && [ "$host" != "$db_host" ]; then
162+
services
163+
# The command's own database URL (docspring-dev's, on 55433), whole, becomes the container's
164+
args=()
165+
for a in "$@"; do
166+
args+=("$(printf '%s' "$a" | sed -E "s#postgres(ql)?://[^[:space:]'\"]*(127\.0\.0\.1|localhost|\[::1\]):55433[^[:space:]'\"]*#$db_url#g")")
167+
done
168+
run env VISUALCI_APP_TEST_DATABASE_URL="$db_url" VISUALCI_APP_TEST_S3_ENDPOINT="$s3_url" "${args[@]}"
169+
else
170+
run "$@"
171+
fi
172+
;;
117173
fix)
118174
sync
119175
run cargo clippy -q --locked -j 24 --workspace --all-targets --fix --allow-dirty --allow-no-vcs "$@" || true

0 commit comments

Comments
 (0)