Documentation
¶
Overview ¶
Package testcontainers provides utilities for working with testcontainers.
Index ¶
- func FlushRedis(ctx context.Context, client *redisclient.Client) error
- func GenerateTestJWT(userID uuid.UUID, roles []string) (string, error)
- func GetFreePort() (int, error)
- func ParseRedisAddr(connStr string) string
- func RunMigrations(ctx context.Context, pool *pgxpool.Pool, migrationPaths []string) error
- func TruncateTables(ctx context.Context, pool *pgxpool.Pool, tables []string) error
- type ContainerManager
- func (m *ContainerManager) Cleanup() error
- func (m *ContainerManager) CleanupData() error
- func (m *ContainerManager) GetErrors() []error
- func (m *ContainerManager) GetPostgresConnectionString() (string, error)
- func (m *ContainerManager) GetPostgresPool() (*pgxpool.Pool, error)
- func (m *ContainerManager) GetRedisAddr() (string, error)
- func (m *ContainerManager) GetRedisClient() (*redisclient.Client, error)
- func (m *ContainerManager) Start() error
- func (m *ContainerManager) WithContext(ctx context.Context) *ContainerManager
- func (m *ContainerManager) WithPostgres(config *PostgresConfig) *ContainerManager
- func (m *ContainerManager) WithRedis(config *RedisConfig) *ContainerManager
- type PostgresConfig
- type PostgresContainer
- type RedisConfig
- type RedisContainer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FlushRedis ¶
func FlushRedis(ctx context.Context, client *redisclient.Client) error
FlushRedis flushes all data from Redis. Use with caution - this clears ALL databases in the Redis instance.
func GenerateTestJWT ¶
GenerateTestJWT generates a JWT token for testing.
func GetFreePort ¶
GetFreePort asks the kernel for a free open port that is ready to use.
func ParseRedisAddr ¶
ParseRedisAddr strips the redis:// prefix from a connection string.
func RunMigrations ¶
RunMigrations executes SQL migration files from the given paths. Supports glob patterns like "../../db/migrations/*.sql".
Types ¶
type ContainerManager ¶
type ContainerManager struct {
// contains filtered or unexported fields
}
ContainerManager manages multiple testcontainers for integration testing.
func NewContainerManager ¶
func NewContainerManager() *ContainerManager
NewContainerManager creates a new container manager.
func (*ContainerManager) Cleanup ¶
func (m *ContainerManager) Cleanup() error
Cleanup terminates all containers and closes connections.
func (*ContainerManager) CleanupData ¶
func (m *ContainerManager) CleanupData() error
CleanupData truncates all tables in PostgreSQL and flushes Redis.
func (*ContainerManager) GetErrors ¶
func (m *ContainerManager) GetErrors() []error
GetErrors returns all errors encountered during container operations.
func (*ContainerManager) GetPostgresConnectionString ¶
func (m *ContainerManager) GetPostgresConnectionString() (string, error)
GetPostgresConnectionString returns the PostgreSQL connection string.
func (*ContainerManager) GetPostgresPool ¶
func (m *ContainerManager) GetPostgresPool() (*pgxpool.Pool, error)
GetPostgresPool returns the PostgreSQL connection pool.
func (*ContainerManager) GetRedisAddr ¶
func (m *ContainerManager) GetRedisAddr() (string, error)
GetRedisAddr returns the Redis address.
func (*ContainerManager) GetRedisClient ¶
func (m *ContainerManager) GetRedisClient() (*redisclient.Client, error)
GetRedisClient returns the Redis client.
func (*ContainerManager) Start ¶
func (m *ContainerManager) Start() error
Start initializes and starts all configured containers concurrently.
func (*ContainerManager) WithContext ¶
func (m *ContainerManager) WithContext(ctx context.Context) *ContainerManager
WithContext sets a custom context for container operations.
func (*ContainerManager) WithPostgres ¶
func (m *ContainerManager) WithPostgres(config *PostgresConfig) *ContainerManager
WithPostgres configures PostgreSQL container. Parameters:
- config: PostgreSQL configuration
func (*ContainerManager) WithRedis ¶
func (m *ContainerManager) WithRedis(config *RedisConfig) *ContainerManager
WithRedis configures Redis container. Parameters:
- config: Redis configuration (optional, uses defaults if nil)
type PostgresConfig ¶
type PostgresConfig struct {
// Database name
Database string
// Username for authentication
Username string
// Password for authentication
Password string
// Docker image
Image string
// Migration file paths (e.g., "../../db/migrations/*.sql")
MigrationPaths []string
// Tables to cleanup between tests (for CleanupData)
CleanupTables []string
// Custom initialization SQL commands
InitSQL []string
}
PostgresConfig holds configuration for PostgreSQL testcontainer.
func DefaultPostgresConfig ¶
func DefaultPostgresConfig(database string) *PostgresConfig
DefaultPostgresConfig returns a default PostgreSQL configuration.
type PostgresContainer ¶
type PostgresContainer struct {
// contains filtered or unexported fields
}
PostgresContainer wraps the testcontainers PostgreSQL container.
func NewPostgresContainer ¶
func NewPostgresContainer(ctx context.Context, config *PostgresConfig) *PostgresContainer
NewPostgresContainer creates a new PostgreSQL container instance.
func (*PostgresContainer) GetConnectionString ¶
func (p *PostgresContainer) GetConnectionString() (string, error)
GetConnectionString returns the PostgreSQL connection string.
func (*PostgresContainer) GetPool ¶
func (p *PostgresContainer) GetPool() (*pgxpool.Pool, error)
GetPool returns the PostgreSQL connection pool.
func (*PostgresContainer) RunMigrations ¶
func (p *PostgresContainer) RunMigrations() error
RunMigrations executes all configured migration files.
func (*PostgresContainer) Start ¶
func (p *PostgresContainer) Start() error
Start initializes and starts the PostgreSQL container.
func (*PostgresContainer) Terminate ¶
func (p *PostgresContainer) Terminate() error
Terminate stops and removes the PostgreSQL container.
type RedisConfig ¶
type RedisConfig struct {
// Docker image (defaults to redis:7-alpine)
Image string
// Additional Redis arguments (e.g., ["--maxmemory", "100mb"])
Args []string
}
RedisConfig holds configuration for Redis testcontainer.
func DefaultRedisConfig ¶
func DefaultRedisConfig() *RedisConfig
DefaultRedisConfig returns a default Redis configuration.
type RedisContainer ¶
type RedisContainer struct {
// contains filtered or unexported fields
}
RedisContainer wraps the testcontainers Redis container.
func NewRedisContainer ¶
func NewRedisContainer(ctx context.Context, config *RedisConfig) *RedisContainer
NewRedisContainer creates a new Redis container instance.
func (*RedisContainer) GetAddr ¶
func (r *RedisContainer) GetAddr() (string, error)
GetAddr returns the Redis address.
func (*RedisContainer) GetClient ¶
func (r *RedisContainer) GetClient() (*redisclient.Client, error)
GetClient returns the Redis client.
func (*RedisContainer) Start ¶
func (r *RedisContainer) Start() error
Start initializes and starts the Redis container.
func (*RedisContainer) Terminate ¶
func (r *RedisContainer) Terminate() error
Terminate stops and removes the Redis container.