From 55213b478bfa9dc554e7ab363e0e7a73f8a4921f Mon Sep 17 00:00:00 2001 From: akshaydeo Date: Tue, 21 Jul 2026 20:23:16 +0530 Subject: [PATCH] framework test fixes --- framework/configstore/migrations_test.go | 22 +++++++++++++------ .../configstore/rdb_deadlock_postgres_test.go | 6 +++-- .../configstore/tables/encryption_test.go | 14 +++++++++++- framework/logstore/migrations_test.go | 14 +++++++++++- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/framework/configstore/migrations_test.go b/framework/configstore/migrations_test.go index ba00d311303..697f1ec0762 100644 --- a/framework/configstore/migrations_test.go +++ b/framework/configstore/migrations_test.go @@ -28,9 +28,15 @@ import ( // functions directly. var testMigrationLogger = bifrost.NewDefaultLogger(schemas.LogLevelInfo) +// pgTestSchema is this package's dedicated Postgres schema. Test packages +// (configstore, configstore/tables, logstore) run in parallel against the same +// database, so each one works in its own schema to avoid clobbering the +// others' tables and rows. +const pgTestSchema = "configstore_test" + // postgresDSN matches the postgres service in tests/docker-compose.yml and // framework/docker-compose.yml. -const postgresDSN = "host=localhost user=bifrost password=bifrost_password dbname=bifrost port=5432 sslmode=disable" +const postgresDSN = "host=localhost user=bifrost password=bifrost_password dbname=bifrost port=5432 sslmode=disable search_path=" + pgTestSchema // namedDB pairs a backend name with its GORM connection for use in subtests. type namedDB struct { @@ -622,14 +628,17 @@ func trySetupPostgresDBWithoutStoreRawColumn(t *testing.T, testSuffix string) *g return nil } + // All objects live in this package's dedicated schema (via search_path in + // the DSN), isolated from other test packages sharing the same database. + if err := db.Exec("CREATE SCHEMA IF NOT EXISTS " + pgTestSchema).Error; err != nil { + return nil + } + // Drop config_providers to start fresh (for this specific test). // Use CASCADE to drop dependent objects (composite types, sequences, etc.). db.Exec("DROP TABLE IF EXISTS config_providers CASCADE") - // Clear migration tracking without dropping the table — other test packages - // (e.g. logstore) may share this Postgres instance and use the same table - // concurrently. CREATE IF NOT EXISTS is safe even if the table already - // exists from a previous test or a concurrent package. + // Clear migration tracking so the migration under test always runs. db.Exec(`CREATE TABLE IF NOT EXISTS migrations ( id VARCHAR(255) PRIMARY KEY )`) @@ -662,8 +671,7 @@ func trySetupPostgresDBWithoutStoreRawColumn(t *testing.T, testSuffix string) *g return nil } - // Clean up after the test — drop config_providers but leave migrations - // intact for concurrent test packages. + // Clean up after the test so later tests in this package start fresh. t.Cleanup(func() { db.Exec("DELETE FROM migrations") db.Exec("DROP TABLE IF EXISTS config_providers CASCADE") diff --git a/framework/configstore/rdb_deadlock_postgres_test.go b/framework/configstore/rdb_deadlock_postgres_test.go index 794964ab81a..9c21676ab24 100644 --- a/framework/configstore/rdb_deadlock_postgres_test.go +++ b/framework/configstore/rdb_deadlock_postgres_test.go @@ -28,8 +28,10 @@ func setupPostgresDeadlockStore(t *testing.T) *RDBConfigStore { t.Skipf("postgres not available: %v", err) } - require.NoError(t, db.Exec("DROP SCHEMA public CASCADE").Error) - require.NoError(t, db.Exec("CREATE SCHEMA public").Error) + // Reset only this package's schema — other test packages share the same + // database and must not lose their tables. + require.NoError(t, db.Exec("DROP SCHEMA IF EXISTS "+pgTestSchema+" CASCADE").Error) + require.NoError(t, db.Exec("CREATE SCHEMA "+pgTestSchema).Error) require.NoError(t, triggerMigrations(context.Background(), db, testMigrationLogger)) store := &RDBConfigStore{logger: bifrost.NewDefaultLogger(schemas.LogLevelInfo)} diff --git a/framework/configstore/tables/encryption_test.go b/framework/configstore/tables/encryption_test.go index aad4759ba8e..c62a153a2c6 100644 --- a/framework/configstore/tables/encryption_test.go +++ b/framework/configstore/tables/encryption_test.go @@ -1546,9 +1546,15 @@ func TestTableVectorStoreConfig_EncryptionDisabled_StoresPlaintext(t *testing.T) // Multi-backend helpers — run the same tests on SQLite and Postgres // ============================================================================ +// pgTestSchema is this package's dedicated Postgres schema. Test packages +// (configstore, configstore/tables, logstore) run in parallel against the same +// database, so each one works in its own schema to avoid clobbering the +// others' tables and rows. +const pgTestSchema = "configstore_tables_test" + // postgresDSN matches the postgres service in tests/docker-compose.yml and // framework/docker-compose.yml. -const postgresDSN = "host=localhost user=bifrost password=bifrost_password dbname=bifrost port=5432 sslmode=disable" +const postgresDSN = "host=localhost user=bifrost password=bifrost_password dbname=bifrost port=5432 sslmode=disable search_path=" + pgTestSchema // namedDB pairs a backend name with its GORM connection for use in subtests. type namedDB struct { @@ -1587,6 +1593,12 @@ func trySetupPostgresDB(t *testing.T) *gorm.DB { return nil } + // All objects live in this package's dedicated schema (via search_path in + // the DSN), isolated from other test packages sharing the same database. + if err := db.Exec("CREATE SCHEMA IF NOT EXISTS " + pgTestSchema).Error; err != nil { + return nil + } + // Order matters on Postgres: referenced tables must be created before // tables that FK-reference them (SQLite defers FK checks; Postgres does not). // TableProvider must precede TableKey (config_keys.provider_id → config_providers). diff --git a/framework/logstore/migrations_test.go b/framework/logstore/migrations_test.go index 3409993c67b..0ecdc815b7c 100644 --- a/framework/logstore/migrations_test.go +++ b/framework/logstore/migrations_test.go @@ -13,9 +13,15 @@ import ( "gorm.io/gorm/logger" ) +// pgTestSchema is this package's dedicated Postgres schema. Test packages +// (configstore, configstore/tables, logstore) run in parallel against the same +// database, so each one works in its own schema to avoid clobbering the +// others' tables and rows. +const pgTestSchema = "logstore_test" + // postgresDSN matches the postgres service in tests/docker-compose.yml and // framework/docker-compose.yml. -const postgresDSN = "host=localhost user=bifrost password=bifrost_password dbname=bifrost port=5432 sslmode=disable" +const postgresDSN = "host=localhost user=bifrost password=bifrost_password dbname=bifrost port=5432 sslmode=disable search_path=" + pgTestSchema // trySetupPostgresDB attempts to connect to Postgres and returns the connection. // Returns nil if Postgres is unavailable. @@ -37,6 +43,12 @@ func trySetupPostgresDB(t *testing.T) *gorm.DB { return nil } + // All objects live in this package's dedicated schema (via search_path in + // the DSN), isolated from other test packages sharing the same database. + if err := db.Exec("CREATE SCHEMA IF NOT EXISTS " + pgTestSchema).Error; err != nil { + return nil + } + return db }