testutils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: GPL-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTestDB

func GetTestDB(ctx context.Context, t *testing.T, dbConfig *database.Config) *database.DB

GetTestDB returns a database.DB instance for testing purposes.

It connects to the database using the provided configuration and ensures that the connection is successful. If the connection or ping fails, it will fail the test.

func GetTestLogging added in v1.0.0

func GetTestLogging(t *testing.T) *logging.Logging

GetTestLogging returns a logging.Logging instance for testing purposes.

It sets the logging level to Debug and uses a zaptest logger to capture logs during tests.

func LoadTestConfig added in v1.0.0

func LoadTestConfig[T config.Validator](t *testing.T, configFile T)

LoadTestConfig loads the configuration from environment variables into the provided config struct for testing purposes, populating it with values from the environment variables prefixed with "ICINGA_NOTIFICATIONS_".

Callers must first ensure the test database is configured, e.g. via SkipTestIfDBConfigIsMissing.

func MakeRandomString

func MakeRandomString(t *testing.T) string

MakeRandomString returns a 20 byte random hex string.

func SkipTestIfDBConfigIsMissing added in v1.0.0

func SkipTestIfDBConfigIsMissing(t *testing.T)

SkipTestIfDBConfigIsMissing skips the calling test if the required test database environment variable is not set.

Call this before daemon.InjectTestConfig, not from inside its callback: InjectTestConfig only ever invokes its callback once per test binary (it guards a package-wide singleton), so if the first test to reach it is the one that skips, every later test calling InjectTestConfig silently gets an empty, never-loaded config instead of being skipped itself. Checking the environment independently in each test, before touching the singleton, ensures every test in the package skips consistently when the database isn't configured.

Types

type DBCleaner added in v1.0.0

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

DBCleaner deletes only the rows a test created from a shared test database, instead of truncating whole tables, so that tests running concurrently against the same database don't clobber each other's data.

Register conditions for the registered tables with Add or AddLazy, and then call Clean (typically via t.Cleanup) to delete all rows matching those conditions. Clean will only ever delete rows from tables that have registered conditions, and won't touch any other tables, so it's safe to use in parallel tests that share a database.

func NewDBCleaner added in v1.0.0

func NewDBCleaner(tables ...string) *DBCleaner

NewDBCleaner returns a DBCleaner that will only ever touch the given tables, deleted in the given order.

The order must respect foreign key constraints, i.e. children before their parents, since Clean will perform plain filtered DELETEs rather than relying on cascading deletes.

func (*DBCleaner) Add added in v1.0.0

func (c *DBCleaner) Add(table, condition string)

Add registers a SQL WHERE condition for rows this test owns in the given table.

The condition is used verbatim without any adjustments. You have to build it with fmt.Sprintf and embed values directly into your condition, since the cleaner doesn't support parameterized queries. Multiple conditions added for the same table are OR-ed together.

func (*DBCleaner) AddLazy added in v1.0.0

func (c *DBCleaner) AddLazy(table string, f func(context.Context) string)

AddLazy registers a SQL WHERE condition for the given table, but the condition is only evaluated when Clean is called.

This is useful if the condition depends on values that are only known at the time of cleaning, or you just want to defer whatever logic is needed to build the condition until the last possible moment. f is called with the context passed to Clean, and should return the actual SQL WHERE condition string. If it returns an empty string, Clean will skip it if no other conditions were registered for it.

func (*DBCleaner) Clean added in v1.0.0

func (c *DBCleaner) Clean(ctx context.Context, t *testing.T, db *database.DB)

Clean deletes all registered tables, one by one, in the order given to NewDBCleaner.

It is safe to call concurrently from parallel subtests, but it will only delete rows from tables that have registered conditions. If no conditions are registered for a table, it will be skipped.

Jump to

Keyboard shortcuts

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