Documentation
¶
Overview ¶
Package statetest gives integration tests a database of their own, quickly.
One Postgres container is started per test binary, one database in it is migrated and granted once, and each test gets a copy of that database made with CREATE DATABASE … TEMPLATE. Copying files is much cheaper than replaying every migration, and a test that owns its database can run in parallel with every other (issue #31).
A database rather than truncated tables. Migrations seed rows — the built-in roles (R-081) and the host policy singleton (R-015) — so emptying every table between tests leaves an install whose administrator role does not exist, and the next bootstrap fails somewhere far from the cause. Which tables are seeded is also a thing migrations may change, and a test harness that has to be updated when they do is one that will not be.
A test about the Postgres cluster itself — creating roles, changing who owns what, connecting twice — needs a cluster nobody else is using, and should start its own container rather than use this one.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Archiver ¶
Archiver connects to a database Database made, as the audit archiver role (R-348). The pool is closed when the test ends.
func Connect ¶
Connect returns a connection to a fresh, migrated database, held as the application role the way a server holds it, and the owner URL of that database. The connection is closed when the test ends.
func Database ¶
Database creates a fresh, migrated database and returns its owner URL and the application role's password, for a test that connects on its own terms.
func Postgres ¶
func Postgres(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*postgres.PostgresContainer, error)
Postgres starts a test Postgres, retrying a start that failed for a reason the next attempt will not meet.
Every integration test that needs a cluster starts it here. Two failures were seen on a busy CI runner that are the host's, not the test's: Docker handing a container — the reaper, or Postgres itself — a random host port another process took first ("address already in use"), and a reaper that was shutting down as a package attached to it ("wait for reaper"). Neither is retried by testcontainers, and either failed a whole package.
Types ¶
This section is empty.