Skip to content

Commit 49fb420

Browse files
matt2eclaude
andcommitted
test(db): drop scratch databases and roles even when a test fails
buzz-db tests that create their own databases or roles dropped them at the end of the test body, so a failed assertion or timeout skipped the cleanup and left the object on the shared server. Seven files carried their own copy of create_scratch_db_through and its drop helper. Add ScratchDb and ScratchRole guards to test_support. Their Drop runs DROP DATABASE IF EXISTS ... WITH (FORCE) or DROP ROLE IF EXISTS on a fresh admin connection, on a separate thread with its own runtime, the same approach ScratchSchema takes, so it works under both runtime flavours and while a panicking test unwinds. WITH (FORCE) ends any sessions still open on the database, so pools need not be closed first. Drop failures print a warning rather than panicking, because a panic during unwinding would abort the test binary. Replace the per-file helpers and every end-of-test DROP DATABASE in buzz-db with the guard, and use ScratchRole for the replica-fence masked-activity test's least-privilege role. Test behaviour is otherwise unchanged; admin pools and close() calls that only served cleanup are removed. TESTING.md now points new tests at the guards. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent cc04cdc commit 49fb420

18 files changed

Lines changed: 381 additions & 939 deletions

‎crates/buzz-db/TESTING.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ The wrapper isolates destructive tests by dropping the entire per-test
2121
database after the process exits. It does not `DELETE` rows or `TRUNCATE`
2222
shared tables, so tests may run concurrently without coordinating cleanup.
2323

24+
The wrapper drops only that per-test database. A test that needs another
25+
database or role on the server creates it with `test_support::ScratchDb` or
26+
`test_support::ScratchRole`, whose `Drop` removes it even when the test
27+
panics. Do not end a test with a manual `DROP DATABASE` or `DROP ROLE`: a
28+
failed assertion skips it and leaves the object behind.
29+
2430
## Choose the schema intentionally
2531

2632
Most tests use the committed desired-state schema from `schema/schema.sql`,

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ mod retirement_postgres_tests;
195195
#[cfg(test)]
196196
mod postgres_tests {
197197
use super::*;
198+
use crate::test_support::ScratchDb;
198199
use std::{
199200
collections::BTreeSet,
200201
fs,
@@ -2567,23 +2568,13 @@ mod postgres_tests {
25672568
async fn cancelled_migration_cannot_expose_shared_lock_while_ddl_backend_lives() {
25682569
use std::time::Instant;
25692570

2570-
use sqlx::AssertSqlSafe;
2571-
25722571
// Dedicated database: the probe table and the orphaned backend must
25732572
// stay invisible to concurrent tests in the shared database.
25742573
let base_url = std::env::var("BUZZ_TEST_DATABASE_URL")
25752574
.or_else(|_| std::env::var("DATABASE_URL"))
25762575
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
2577-
let admin = PgPool::connect(&base_url)
2578-
.await
2579-
.expect("connect admin database");
2580-
let probe_db = format!("buzz_lock_cancel_{}", uuid::Uuid::new_v4().simple());
2581-
sqlx::query(AssertSqlSafe(format!("CREATE DATABASE {probe_db}")))
2582-
.execute(&admin)
2583-
.await
2584-
.expect("create probe database");
2585-
let (base_prefix, _) = base_url.rsplit_once('/').expect("database url has a path");
2586-
let pool = PgPool::connect(&format!("{base_prefix}/{probe_db}"))
2576+
let probe_db = ScratchDb::create(&base_url, "buzz_lock_cancel").await;
2577+
let pool = PgPool::connect(probe_db.url())
25872578
.await
25882579
.expect("connect probe database");
25892580
sqlx::query("CREATE TABLE schema_lock_cancel_probe (id int)")
@@ -2716,14 +2707,6 @@ mod postgres_tests {
27162707
committed,
27172708
"orphaned migration DDL commits after cancellation; the lock must cover it"
27182709
);
2719-
2720-
pool.close().await;
2721-
sqlx::query(AssertSqlSafe(format!(
2722-
"DROP DATABASE {probe_db} WITH (FORCE)"
2723-
)))
2724-
.execute(&admin)
2725-
.await
2726-
.expect("drop probe database");
27272710
}
27282711

27292712
pub(super) async fn connect_test_pool() -> PgPool {
@@ -2787,8 +2770,6 @@ mod postgres_tests {
27872770
#[tokio::test]
27882771
#[ignore = "requires Postgres"]
27892772
async fn admin_schema_parity_between_desired_state_and_migrations() {
2790-
use sqlx::AssertSqlSafe;
2791-
27922773
async fn columns(
27932774
pool: &PgPool,
27942775
table: &str,
@@ -2869,33 +2850,21 @@ mod postgres_tests {
28692850
.or_else(|_| std::env::var("DATABASE_URL"))
28702851
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
28712852
let conn = parse_pg_url(&base_url);
2872-
let admin = PgPool::connect(&base_url)
2873-
.await
2874-
.expect("connect admin database");
2875-
let (base_prefix, _) = base_url.rsplit_once('/').expect("database url has a path");
28762853

2877-
let desired_db = format!("buzz_admin_desired_{}", uuid::Uuid::new_v4().simple());
2878-
let migrated_db = format!("buzz_admin_migrated_{}", uuid::Uuid::new_v4().simple());
2879-
sqlx::query(AssertSqlSafe(format!("CREATE DATABASE {desired_db}")))
2880-
.execute(&admin)
2881-
.await
2882-
.expect("create desired-state probe database");
2883-
sqlx::query(AssertSqlSafe(format!("CREATE DATABASE {migrated_db}")))
2884-
.execute(&admin)
2885-
.await
2886-
.expect("create migrated probe database");
2854+
let desired_db = ScratchDb::create(&base_url, "buzz_admin_desired").await;
2855+
let migrated_db = ScratchDb::create(&base_url, "buzz_admin_migrated").await;
28872856

28882857
// Bootstrap the desired-state probe the way the test-relay launchers do:
28892858
// the real pgschema binary, then the post-apply reconcile script.
2890-
let apply = pgschema(&conn, &desired_db, &["apply", "--auto-approve"]);
2859+
let apply = pgschema(&conn, desired_db.name(), &["apply", "--auto-approve"]);
28912860
assert!(
28922861
apply.status.success(),
28932862
"pgschema apply failed: {}\n{}",
28942863
String::from_utf8_lossy(&apply.stdout),
28952864
String::from_utf8_lossy(&apply.stderr),
28962865
);
28972866

2898-
let desired = PgPool::connect(&format!("{base_prefix}/{desired_db}"))
2867+
let desired = PgPool::connect(desired_db.url())
28992868
.await
29002869
.expect("connect desired-state probe database");
29012870
sqlx::raw_sql(include_str!(
@@ -2904,7 +2873,7 @@ mod postgres_tests {
29042873
.execute(&desired)
29052874
.await
29062875
.expect("reconcile desired-state probe database");
2907-
let migrated = PgPool::connect(&format!("{base_prefix}/{migrated_db}"))
2876+
let migrated = PgPool::connect(migrated_db.url())
29082877
.await
29092878
.expect("connect migrated probe database");
29102879
MIGRATOR
@@ -2965,17 +2934,6 @@ mod postgres_tests {
29652934
"constraint parity mismatch for {table}"
29662935
);
29672936
}
2968-
2969-
desired.close().await;
2970-
migrated.close().await;
2971-
for probe_db in [desired_db, migrated_db] {
2972-
sqlx::query(AssertSqlSafe(format!(
2973-
"DROP DATABASE {probe_db} WITH (FORCE)"
2974-
)))
2975-
.execute(&admin)
2976-
.await
2977-
.expect("drop probe database");
2978-
}
29792937
}
29802938

29812939
/// Re-applying `schema/schema.sql` to a database bootstrapped the way every
@@ -2997,27 +2955,18 @@ mod postgres_tests {
29972955
use crate::store::partition::{
29982956
maintain_partitions, PartitionMaintenancePolicy, PARTITION_MANAGER_MONTHS_AHEAD,
29992957
};
3000-
use sqlx::AssertSqlSafe;
30012958

30022959
let base_url = crate::test_support::database_url();
30032960
let conn = parse_pg_url(&base_url);
3004-
let admin = PgPool::connect(&base_url)
3005-
.await
3006-
.expect("connect admin database");
3007-
let (base_prefix, _) = base_url.rsplit_once('/').expect("database url has a path");
3008-
let database = format!("buzz_pgschema_reapply_{}", uuid::Uuid::new_v4().simple());
3009-
sqlx::query(AssertSqlSafe(format!("CREATE DATABASE {database}")))
3010-
.execute(&admin)
3011-
.await
3012-
.expect("create probe database");
2961+
let database = ScratchDb::create(&base_url, "buzz_pgschema_reapply").await;
30132962

3014-
let pool = PgPool::connect(&format!("{base_prefix}/{database}"))
2963+
let pool = PgPool::connect(database.url())
30152964
.await
30162965
.expect("connect probe database");
30172966
// The bootstrap every pgschema caller runs; the reconcile script
30182967
// attaches the declared children.
30192968
let bootstrap = |round: &str| {
3020-
let apply = pgschema(&conn, &database, &["apply", "--auto-approve"]);
2969+
let apply = pgschema(&conn, database.name(), &["apply", "--auto-approve"]);
30212970
assert!(
30222971
apply.status.success(),
30232972
"{round} pgschema apply failed: {}\n{}",
@@ -3102,7 +3051,7 @@ mod postgres_tests {
31023051
);
31033052
}
31043053

3105-
let plan = pgschema(&conn, &database, &["plan", "--output-sql", "stdout"]);
3054+
let plan = pgschema(&conn, database.name(), &["plan", "--output-sql", "stdout"]);
31063055
let plan_sql = String::from_utf8_lossy(&plan.stdout);
31073056
assert!(
31083057
plan.status.success(),
@@ -3117,14 +3066,6 @@ mod postgres_tests {
31173066
bootstrap("second")
31183067
.await
31193068
.expect("second post-apply reconciliation");
3120-
pool.close().await;
3121-
3122-
sqlx::query(AssertSqlSafe(format!(
3123-
"DROP DATABASE {database} WITH (FORCE)"
3124-
)))
3125-
.execute(&admin)
3126-
.await
3127-
.expect("drop probe database");
31283069
}
31293070

31303071
#[tokio::test]

‎crates/buzz-db/src/runtime/replica_fence.rs‎

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -823,37 +823,14 @@ pub async fn run_probe(writer: PgPool, fence: Arc<ReplicaFence>) {
823823
#[cfg(test)]
824824
mod postgres_tests {
825825
use super::*;
826+
use crate::test_support::{ScratchDb, ScratchRole};
826827

827828
/// A private scratch database with migrations applied: the probe tests
828829
/// mutate the singleton heartbeat row (rewind/rotate), which must never
829-
/// race the shared dev database or each other.
830-
async fn scratch_db() -> (PgPool, PgPool, String) {
831-
let admin = PgPool::connect(&crate::test_support::database_url())
832-
.await
833-
.expect("connect admin");
834-
let name = format!("fence_probe_{}", uuid::Uuid::new_v4().simple());
835-
sqlx::query(sqlx::AssertSqlSafe(format!("CREATE DATABASE {name}")))
836-
.execute(&admin)
837-
.await
838-
.expect("create scratch db");
839-
let base = crate::test_support::database_url();
840-
let idx = base.rfind('/').expect("db url has a path segment");
841-
let pool = PgPool::connect(&format!("{}/{}", &base[..idx], name))
842-
.await
843-
.expect("connect scratch db");
844-
crate::migration::run_migrations(&pool)
845-
.await
846-
.expect("migrate scratch db");
847-
(admin, pool, name)
848-
}
849-
850-
async fn drop_scratch_db(admin: &PgPool, pool: PgPool, name: &str) {
851-
pool.close().await;
852-
let _ = sqlx::query(sqlx::AssertSqlSafe(format!(
853-
"DROP DATABASE IF EXISTS {name} WITH (FORCE)"
854-
)))
855-
.execute(admin)
856-
.await;
830+
/// race the shared dev database or each other. Dropping the guard drops
831+
/// the database.
832+
async fn scratch_db() -> (PgPool, ScratchDb) {
833+
ScratchDb::migrated(&crate::test_support::database_url(), "fence_probe").await
857834
}
858835

859836
#[test]
@@ -1051,7 +1028,7 @@ mod postgres_tests {
10511028
#[tokio::test]
10521029
#[ignore = "requires Postgres"]
10531030
async fn cluster_global_sample_writer_waits_for_shared_floor_lock_before_sampling_time() {
1054-
let (admin, pool, name) = scratch_db().await;
1031+
let (pool, _scratch) = scratch_db().await;
10551032

10561033
let mut blocker = pool.begin().await.expect("begin shared-lock blocker");
10571034
sqlx::query("SELECT pg_advisory_xact_lock_shared($1)")
@@ -1090,8 +1067,6 @@ mod postgres_tests {
10901067
sample.sampled_at,
10911068
marker_before_release
10921069
);
1093-
1094-
drop_scratch_db(&admin, pool, &name).await;
10951070
}
10961071

10971072
/// An unprivileged probe role sees NULL `state`/`xact_start` for other
@@ -1101,10 +1076,6 @@ mod postgres_tests {
11011076
#[tokio::test]
11021077
#[ignore = "requires Postgres"]
11031078
async fn cluster_global_sample_writer_fails_closed_when_activity_is_masked() {
1104-
let admin = PgPool::connect(&crate::test_support::database_url())
1105-
.await
1106-
.expect("connect");
1107-
11081079
// Hold a transaction open as the privileged user: this is the row
11091080
// the unprivileged probe must notice it cannot classify.
11101081
let tx_pool = PgPool::connect(&crate::test_support::database_url())
@@ -1118,19 +1089,22 @@ mod postgres_tests {
11181089

11191090
// An unprivileged login role (no pg_monitor): pg_stat_activity masks
11201091
// other sessions' state columns for it.
1121-
let role = format!("fence_probe_{}", uuid::Uuid::new_v4().simple());
1122-
sqlx::query(sqlx::AssertSqlSafe(format!(
1123-
"CREATE ROLE {role} LOGIN PASSWORD 'fence_probe_test'" // sadscan:disable np.postgres.1
1124-
)))
1125-
.execute(&admin)
1126-
.await
1127-
.expect("create unprivileged role");
1092+
let role = ScratchRole::create(
1093+
&crate::test_support::database_url(),
1094+
"fence_probe",
1095+
"LOGIN PASSWORD 'fence_probe_test'", // sadscan:disable np.postgres.1
1096+
)
1097+
.await;
11281098

11291099
let base = crate::test_support::database_url();
11301100
let unpriv_url = {
11311101
let rest = base.strip_prefix("postgres://").expect("pg url");
11321102
let at = rest.rfind('@').expect("credentials in url");
1133-
format!("postgres://{role}:fence_probe_test@{}", &rest[at + 1..])
1103+
format!(
1104+
"postgres://{}:fence_probe_test@{}",
1105+
role.name(),
1106+
&rest[at + 1..]
1107+
)
11341108
};
11351109
let unpriv = PgPool::connect(&unpriv_url).await.expect("connect unpriv");
11361110

@@ -1144,10 +1118,6 @@ mod postgres_tests {
11441118

11451119
tx.rollback().await.expect("rollback");
11461120
unpriv.close().await;
1147-
sqlx::query(sqlx::AssertSqlSafe(format!("DROP ROLE {role}")))
1148-
.execute(&admin)
1149-
.await
1150-
.expect("drop role");
11511121
}
11521122

11531123
/// The Aurora PostgreSQL identity function name is exact — the
@@ -1191,7 +1161,7 @@ mod postgres_tests {
11911161
#[tokio::test]
11921162
#[ignore = "requires Postgres"]
11931163
async fn cluster_global_probe_commits_tokens_and_sessions_prove_coverage() {
1194-
let (admin, pool, name) = scratch_db().await;
1164+
let (pool, _scratch) = scratch_db().await;
11951165
let fence = ReplicaFence::new();
11961166

11971167
let first = probe_once(&pool, &fence).await.expect("first probe");
@@ -1236,7 +1206,6 @@ mod postgres_tests {
12361206
);
12371207

12381208
drop(conn);
1239-
drop_scratch_db(&admin, pool, &name).await;
12401209
}
12411210

12421211
/// A same-epoch token rewind on the writer (restore adversary) must not
@@ -1246,7 +1215,7 @@ mod postgres_tests {
12461215
#[tokio::test]
12471216
#[ignore = "requires Postgres"]
12481217
async fn cluster_global_probe_rotates_epoch_on_same_epoch_token_regression() {
1249-
let (admin, pool, name) = scratch_db().await;
1218+
let (pool, _scratch) = scratch_db().await;
12501219
let fence = ReplicaFence::new();
12511220

12521221
let before = probe_once(&pool, &fence).await.expect("probe");
@@ -1286,6 +1255,5 @@ mod postgres_tests {
12861255
);
12871256

12881257
drop(conn);
1289-
drop_scratch_db(&admin, pool, &name).await;
12901258
}
12911259
}

0 commit comments

Comments
 (0)