testdb

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package testdb gives tests one shared Postgres and one shared MariaDB server per machine, and isolated databases on them.

`go test ./...` runs every package in its own process, so process-global state can't keep packages from starting their own containers. Instead:

  • In CI (EP_CI=1) the servers are the workflow's service containers (EP_TEST_POSTGRES_HOST/PORT, EP_TEST_MYSQL_HOST/PORT).
  • Locally each server is a testcontainers container with a fixed name and Reuse enabled. Finding or starting it happens under an OS file lock, so concurrent test processes agree on one container instead of racing. The containers keep running between `go test` runs (their data lives in RAM, without fsync); remove them with `docker rm -f etherpad-test-postgres-v2 etherpad-test-mysql-v2`.
  • Tests never share a database: each caller gets its own (NewDatabase), so packages can run in parallel against the same servers.

Set EP_TEST_SKIP_SQL_SERVERS=1 to skip everything that needs a server.

Index

Constants

View Source
const (
	// User and password of both servers (CI services and containers).
	User     = "test_user"
	Password = "test_password"
)

Variables

This section is empty.

Functions

func SkipSQLServers

func SkipSQLServers() bool

SkipSQLServers reports whether tests needing Postgres/MySQL should be skipped.

Types

type Kind

type Kind string

Kind is the database server type.

const (
	Postgres Kind = "postgres"
	MySQL    Kind = "mysql"
)

type Server

type Server struct {
	Kind Kind
	Host string
	Port int
}

Server is a running database server tests can create databases on.

func Acquire

func Acquire(kind Kind) (*Server, error)

Acquire returns the shared server of the given kind, starting or attaching to it on first use in this process.

func Get

func Get(t testing.TB, kind Kind) *Server

Get is Acquire for tests: it skips the test when EP_TEST_SKIP_SQL_SERVERS is set and fails it when the server is unavailable.

func (*Server) AdminUser

func (s *Server) AdminUser() string

AdminUser is the account tests should connect as: it owns the databases created by NewDatabase.

func (*Server) CreateDatabase

func (s *Server) CreateDatabase() (string, error)

CreateDatabase creates a new empty database and returns its name. The caller is responsible for dropping it (see NewDatabase for tests).

func (*Server) DSN

func (s *Server) DSN(database string) string

DSN returns a database/sql DSN for database (driver "pgx" or "mysql").

func (*Server) DriverName

func (s *Server) DriverName() string

DriverName is the database/sql driver for this server.

func (*Server) DropDatabase

func (s *Server) DropDatabase(name string) error

DropDatabase drops a database created by CreateDatabase.

func (*Server) NewDatabase

func (s *Server) NewDatabase(t testing.TB) string

NewDatabase creates a database for this test and drops it when the test and its subtests have finished.

Jump to

Keyboard shortcuts

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