Help us shape the future of Go! Take the 2026 Go Developer Survey

statetest

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

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

func Archiver(t testing.TB, ownerURL string) *pgxpool.Pool

Archiver connects to a database Database made, as the audit archiver role (R-348). The pool is closed when the test ends.

func Connect

func Connect(t testing.TB) (*state.DB, string)

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

func Database(t testing.TB) (string, secret.Value)

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

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.

Jump to

Keyboard shortcuts

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