containers

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package containers provides Docker container management for PostgreSQL test environments. It leverages testcontainers to automatically manage PostgreSQL container lifecycle, making it easy to spin up isolated PostgreSQL instances for integration testing.

The package provides:

  • Container type for managing PostgreSQL Docker containers
  • Automatic container creation and cleanup
  • Integration with the postgresenv Connector system
  • Flexible configuration options

Example Usage:

// Create a new PostgreSQL container
cnt := containers.NewContainer(
	containers.WithLogger(log.New(os.Stdout, "", log.LstdFlags)),
)

// Create connector with container options
connector := postgresenv.NewConnector(cnt.ConnectorOptions()...)

// Ensure cleanup
defer connector.Close()

// Connect to PostgreSQL
pool, err := connector.Connect(ctx, nil)
if err != nil {
	return err
}
defer pool.Close()

// Use the pool...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

func NewContainer

func NewContainer(
	opts ...ContainerOption,
) *Container

NewContainer creates a new PostgreSQL container for testing. This function initializes container configuration but does not immediately start the container. The container is started lazily when the first connection is requested via the ConnectorOptions.

Container Creation Process:

  1. Creates container configuration
  2. Sets up lifecycle hooks for automatic startup
  3. Returns container ready for use with connectors

The container uses sensible defaults:

  • PostgreSQL 16-alpine image
  • Automatic port mapping
  • Default credentials (user: test, password: test, database: test)
  • Health check waiting strategy

Example:

// Create with defaults
cnt := containers.NewContainer()

// Create with custom logger
cnt := containers.NewContainer(
	containers.WithLogger(myLogger),
)

func (*Container) Close

func (c *Container) Close() error

Close stops and removes the PostgreSQL container. This method cleans up all resources associated with the container.

Cleanup Process:

  1. Stops the Docker container
  2. Removes the container
  3. Releases all resources

Close should be called when the container is no longer needed. After Close is called, the container should not be used to create new connections.

This method is typically called automatically via the CloseHook when the connector is closed.

func (*Container) ConnectorOptions

func (c *Container) ConnectorOptions() []postgresenv.ConnectorOption

ConnectorOptions returns connector options for connecting to this container. These options can be passed to postgresenv.NewConnector to create a connector that automatically manages the container lifecycle.

The returned options include:

  • BeforeConnectHook: Starts the container before first connection
  • CloseHook: Stops the container when connector is closed
  • Connection parameters (host, port, credentials)

This method implements lazy container initialization - the container is only started when the first connection is requested.

Example:

cnt := containers.NewContainer()
connector := postgresenv.NewConnector(cnt.ConnectorOptions()...)
defer connector.Close()

// Container starts automatically on first Connect call
pool, err := connector.Connect(ctx, nil)

func (*Container) Launch

func (c *Container) Launch(
	ctx context.Context,
	cfg *pgxpool.Config,
) error

Launch starts the PostgreSQL container if not already running. This method is called automatically by the BeforeConnectHook when the first connection is requested.

Container Startup Process:

  1. Creates container request with PostgreSQL configuration
  2. Starts the Docker container
  3. Waits for PostgreSQL to be ready (health check)
  4. Retrieves connection information (host, port)

This method is idempotent - if the container is already running, it returns immediately without starting a new container.

type ContainerOption

type ContainerOption func(*Container)

ContainerOption is a function that configures a Container. Options can be passed to NewContainer to customize the container's behavior.

func WithLogger

func WithLogger(logger *log.Logger) ContainerOption

WithLogger sets a custom logger for the container. The logger is used to log container lifecycle events and any issues that occur during container management.

If not set, a default no-op logger is used that discards all log output.

Example:

cnt := containers.NewContainer(
	containers.WithLogger(log.New(os.Stdout, "", log.LstdFlags)),
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL