testcontainer

package
v0.0.0-...-a798bc3 Latest Latest
Warning

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

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

Documentation

Overview

Package testcontainer prepares the services the framework's tests run against, in containers started through testcontainers-go.

What testcontainers-go provides is used as it is: a container started for one test binary alone is the library's to create, wait for and clean up, its reaper included, and nothing here duplicates any of that. Everything this package builds on top exists for one reason only, reuse: the mysql, postgres, redis, clickhouse and minio containers are shared by every test binary and outlive every run to keep the tests fast, which the library does not provide. Their fixed names, the isolation slot every binary claims inside them, the cleanup of slots whose binary is gone and keeping them out of the reaper's reach are this package's own for that reason alone. A container that is not reused takes none of it, and a need the library meets is met through the library, never through a mechanism of this package's own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyConfigToEnv

func ApplyConfigToEnv(cfg any)

ApplyConfigToEnv exports the non-zero fields of a config section as the environment variables config.Init reads them back from, so a test hands over a config struct instead of spelling out every variable name.

A variable name is the upper-cased config path: the section name followed by one mapstructure tag per nesting level, joined by "_". The nested section config.Logger.HTTPBody therefore lands on LOGGER_HTTP_BODY_ENABLED.

Zero-valued fields are skipped so a partially filled section leaves the remaining framework defaults alone, which also means a false bool cannot be exported this way. Slices and maps are skipped as well.

func SetupClickhouse

func SetupClickhouse() (config.Clickhouse, func() error, error)

SetupClickhouse prepares a clickhouse database and returns the configuration for building an application-held analytical instance with clickhouse.New, together with the function that releases it.

Unlike SetupDatabase it never points the framework default database at the container: clickhouse is an analytical instance held next to the default database, not a replacement for it, so tests build their own handle and pass it to DatabaseOn, SelectOn, and UnionAllOn.

func SetupDatabase

func SetupDatabase(dbType config.DBType) (release func() error, afterMigrate func(), err error)

SetupDatabase prepares the database that dbType names and points the framework at it, returning the function that releases it and the one to run once the framework has migrated it, see prepareSchemaTemplate. An empty dbType selects the framework default.

A database the framework can connect to but that has no preparation here is rejected rather than quietly replaced, so a test never runs against a different database than the one it asked for.

func SetupElasticsearch

func SetupElasticsearch() (func() error, error)

SetupElasticsearch starts an Elasticsearch container of its own and points the framework at it, returning the function that terminates it.

Like kafka's and rocketmq's, the container is dedicated even by default and left to testcontainers alone, the reaper included: an index would be a slot a shared container could hand out, but a resident Elasticsearch is one more JVM kept running for the tests of one package, which the start it saves does not buy.

Security is switched off, so the framework reaches the cluster over plain HTTP without credentials and the fixture hands over an address alone; with security on, the module generates a certificate authority the client would have to be given as a file.

func SetupKafka

func SetupKafka() (func() error, error)

SetupKafka starts a kafka container of its own and points the framework at it, returning the function that terminates it.

Unlike the other services this one is dedicated even by default: kafka has no server-side namespace a shared container could hand out per test binary, so isolation would take a topic-prefix contract imposed on business code, which is not the framework's call to make. A container per binary keeps topics and consumer groups apart by construction. Not being reused, it is left to testcontainers alone: should the binary die before calling the returned function, the reaper removes the container.

It is the one container started here that is not given readyTimeout: the module waits for the broker inside a hook of its own, a minute for the port and a minute for the line the broker logs once it runs, with strategies it builds there, which no option reaches (see patientStart).

func SetupMinio

func SetupMinio() (func() error, error)

SetupMinio prepares a minio bucket and points the framework at it as the default bucket, returning the function that releases it. A test that needs further buckets names them after the default one, such as <bucket>-list: they belong to the test binary as well, and the release removes them with it.

By default the bucket lives in the shared minio container, see shared.go: every binary gets a bucket named like a shared database, gst-test-<pid>-<unix>-<hex>, and releasing removes its buckets, objects included, but never the container.

func SetupRedis

func SetupRedis() (func() error, error)

SetupRedis prepares a redis database and points the framework at it, returning the function that releases it. Modules that keep sessions or cache entries need it.

func SetupRocketMQ

func SetupRocketMQ() (func() error, error)

SetupRocketMQ starts a RocketMQ name server and broker in a container of their own and points the framework at them, returning the function that terminates the container.

Like kafka's, the container is dedicated even by default and left to testcontainers alone, the reaper included. A shared container would be possible, a namespace is a slot the client applies by itself, but a resident broker and name server idle at about a gigabyte of memory and a tenth of a core, which the twenty seconds they would save per start do not buy.

A client asks the name server for a topic's route and dials the broker at the address the broker registered, which is the one its configuration names, so that address has to be reachable from the test process: the container publishes the broker's port under the same number on the host, chosen free on the host before the container starts, and the broker announces the docker host at that port. The client reads a name server address as an IP literal, which is why a local daemon's host is written as 127.0.0.1 rather than localhost.

func SetupStandaloneMySQL

func SetupStandaloneMySQL(database, username, password string) (config.MySQL, func() error, error)

SetupStandaloneMySQL starts a mysql container of its own with the given database name and credentials, and returns its connection settings without touching the process environment or the framework's default database. Tests use it for topologies the single default database cannot express — a non-replicating stand-in replica for routing assertions, a second instance for cross-instance behavior, a server reached over TLS.

The server runs with a certificate an authority made for it signed for the hosts it is reached at, and takes plaintext connections as before. The settings returned name the authority's certificate (CAFile) and a client certificate it signed (CertFile and KeyFile) and leave TLS off: a test of TLS turns TLSEnabled on in them, and every other test dials as it always did. The returned function terminates the container and removes those files.

func SetupStandalonePostgres

func SetupStandalonePostgres(database, username, password string) (config.Postgres, func() error, error)

SetupStandalonePostgres starts a postgres container of its own with the given database name and credentials, and returns its connection settings without touching the process environment or the framework's default database. Tests use it for topologies the single default database cannot express — a non-replicating stand-in replica for routing assertions, a second instance for cross-instance behavior. The returned function terminates the container.

Types

This section is empty.

Jump to

Keyboard shortcuts

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