Documentation
¶
Overview ¶
Package testdb provides shared test database utilities
Index ¶
- func ApplyIntegrationConnectionOverrides(cfg *config.Config)
- func CloseDB(db *gorm.DB)
- func CreateEmptyTestDB(ctx context.Context, log *logrus.Logger, prefix string, initDB InitDBFunc) (*config.Config, string, *gorm.DB, error)
- func CreateTestDB(ctx context.Context, log *logrus.Logger, prefix string, initDB InitDBFunc) (*config.Config, string, *gorm.DB, error)
- func CreateTestRedis(ctx context.Context, log *logrus.Logger) (host string, port uint, password domain.SecureString, cleanup func(), ...)
- func DeleteTestDB(ctx context.Context, log *logrus.Logger, cfg *config.Config, db *gorm.DB, ...) error
- func IntegrationPostgresAdminPassword() domain.SecureString
- func IntegrationPostgresAdminUser() string
- func IntegrationRedisPassword() domain.SecureString
- func OpenMinimalAdminDB(cfg *config.Config, log *logrus.Logger) (*gorm.DB, error)
- type InitDBFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyIntegrationConnectionOverrides ¶
ApplyIntegrationConnectionOverrides points DB and Alertmanager (when present in cfg) at published ports for the integration stack when flightctl-integration-postgres is running. If that container is absent, cfg is unchanged (e.g. unit tests using localhost defaults). Note: KV (Redis) is NOT configured here - each test suite creates its own ephemeral Redis via CreateTestRedis() and passes connection params explicitly.
func CreateEmptyTestDB ¶
func CreateEmptyTestDB(ctx context.Context, log *logrus.Logger, prefix string, initDB InitDBFunc) (*config.Config, string, *gorm.DB, error)
CreateEmptyTestDB creates a completely empty database for testing migration scenarios. Unlike CreateTestDB which clones from the migrated 'flightctl' template, this creates a truly empty database with no schema. The caller must run any needed migrations/schema setup after creation. IMPORTANT: Call DeleteTestDB in your test's cleanup (e.g., AfterEach or defer) to drop the database. Returns the config, db name, gorm.DB connection, and any error.
func CreateTestDB ¶
func CreateTestDB(ctx context.Context, log *logrus.Logger, prefix string, initDB InitDBFunc) (*config.Config, string, *gorm.DB, error)
CreateTestDB creates a temporary test database by cloning from the migrated 'flightctl' database. IMPORTANT: Call integrationstack.EnsureRunning() in your test suite's BeforeSuite before using this. Returns the config, db name, gorm.DB connection, and any error.
func CreateTestRedis ¶
func CreateTestRedis(ctx context.Context, log *logrus.Logger) (host string, port uint, password domain.SecureString, cleanup func(), err error)
CreateTestRedis starts an ephemeral Redis container for test isolation. Each test suite gets its own Redis instance, enabling parallel test execution. Returns connection params and a cleanup function. Call cleanup in AfterSuite.
func DeleteTestDB ¶
func DeleteTestDB(ctx context.Context, log *logrus.Logger, cfg *config.Config, db *gorm.DB, dbName string) error
DeleteTestDB drops the test database. Uses a minimal admin connection (postgres user) for DROP DATABASE - no prometheus/tracing plugins.
func IntegrationPostgresAdminPassword ¶
func IntegrationPostgresAdminPassword() domain.SecureString
IntegrationPostgresAdminPassword returns the postgres admin password for integration tests. Reads FLIGHTCTL_POSTGRESQL_MASTER_PASSWORD (same as integrationstack), else adminpass.
func IntegrationPostgresAdminUser ¶
func IntegrationPostgresAdminUser() string
IntegrationPostgresAdminUser returns "postgres" (the admin user for test infrastructure ops like CREATE/DROP DATABASE). Production app/migrator users don't have CREATEDB privilege.
func IntegrationRedisPassword ¶
func IntegrationRedisPassword() domain.SecureString
IntegrationRedisPassword returns the Redis password for integration tests. Reads KV_PASSWORD, then FLIGHTCTL_KV_PASSWORD (same as make integration-test), else adminpass to match test/integration/integrationstack Redis --requirepass.
func OpenMinimalAdminDB ¶
OpenMinimalAdminDB creates a minimal GORM connection for admin operations (CREATE/DROP DATABASE). Unlike store.InitDB, this does NOT register prometheus metrics server or tracing plugins, making it suitable for temporary throwaway connections that only run DDL statements. Uses postgres admin credentials from IntegrationPostgresAdminUser/Password.