testutil

package
v0.1.34 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package testutil provides utility functions and types for testing.

Index

Constants

View Source
const (

	// DefaultAdminUsername is the admin username used in test API servers.
	DefaultAdminUsername = "test-admin"
	// DefaultAdminPassword is the admin password used in test API servers.
	DefaultAdminPassword = "test-password"
)

Variables

This section is empty.

Functions

func GetFreeTCPPort added in v0.1.0

func GetFreeTCPPort() (int, error)

GetFreeTCPPort asks the kernel for a free open port that is ready to use.

func Identifier added in v0.1.30

func Identifier(tb testing.TB) string

Identifier can be used as a name of host, container, image, volume, network, etc.

func P added in v0.1.0

func P[T any](v T) *T

P is a utility function that returns a pointer to the value passed in.

Types

type APIServer added in v0.1.30

type APIServer struct {
	*Base

	Server   *apiserver.Server
	ServerID string

	Endpoint string
	Port     int

	ManagementEndpoint string
	ManagementPort     int

	MongoURI string

	Settings config.ServerSettings
	// contains filtered or unexported fields
}

APIServer holds a running test API server and its configuration.

func (*APIServer) AdminPassword added in v0.1.30

func (a *APIServer) AdminPassword() string

AdminPassword returns the admin password configured for this test server.

func (*APIServer) AdminUsername added in v0.1.30

func (a *APIServer) AdminUsername() string

AdminUsername returns the admin username configured for this test server.

func (*APIServer) Client added in v0.1.30

func (a *APIServer) Client() *client.Client

Client returns a pre-authenticated client pointing at this test server.

func (*APIServer) Stop added in v0.1.30

func (a *APIServer) Stop()

Stop signals the API server to shut down by cancelling its running context. The server's Run goroutine handles graceful shutdown with a built-in timeout.

func (*APIServer) WaitForReady added in v0.1.30

func (a *APIServer) WaitForReady()

WaitForReady blocks until the API server responds to ping or the timeout expires.

type Base added in v0.1.0

type Base struct {
	Logger       *slog.Logger
	Dependencies map[string]Dependency

	CacheDir string
	// contains filtered or unexported fields
}

Base is a utility struct that provides common resources and utilities for testing.

func NewBase added in v0.1.0

func NewBase(tb testing.TB) *Base

NewBase creates a new instance of Base.

func (*Base) ForController added in v0.1.0

func (b *Base) ForController() *ControllerBase

ForController creates a new instance of ControllerBase with a Base.

func (*Base) GetFreeTCPPort added in v0.1.0

func (b *Base) GetFreeTCPPort() int

GetFreeTCPPort returns a free TCP port that is ready to use.

func (*Base) StartAPIServer added in v0.1.30

func (b *Base) StartAPIServer(
	mongoURI string,
	databaseName string,
) *APIServer

StartAPIServer starts a new API server backed by the given MongoDB instance.

func (*Base) StartAPIServerWithKafka added in v0.1.30

func (b *Base) StartAPIServerWithKafka(mongoURI, kafkaBroker, databaseName string) *APIServer

StartAPIServerWithKafka starts a new API server backed by MongoDB and Kafka for event processing.

func (*Base) StartKafka added in v0.1.30

func (b *Base) StartKafka() *Kafka

StartKafka starts a Kafka container and waits until it is ready to accept connections.

func (*Base) StartMongoDB added in v0.1.30

func (b *Base) StartMongoDB() *MongoDB

StartMongoDB starts a MongoDB container and returns a MongoDB instance.

func (*Base) StartOTelCollector added in v0.1.30

func (b *Base) StartOTelCollector(opampPort int) *OTelCollector

StartOTelCollector starts an OTel Collector container connected to the given OpAMP port.

func (*Base) StartOTelCollectorHTTP added in v0.1.30

func (b *Base) StartOTelCollectorHTTP(opampPort int) *OTelCollector

StartOTelCollectorHTTP starts an OTel Collector container using HTTP polling to connect to the given OpAMP port.

func (*Base) StartOTelCollectorWithAttributes added in v0.1.30

func (b *Base) StartOTelCollectorWithAttributes(opampPort int, resourceAttrs map[string]string) *OTelCollector

StartOTelCollectorWithAttributes starts an OTel Collector container with custom resource attributes.

type Controller

type Controller interface {
	RoutesInfo() gin.RoutesInfo
}

Controller is an interface that defines the methods for a controller.

type ControllerBase

type ControllerBase struct {
	*Base

	Router *gin.Engine
}

ControllerBase is a struct that provides a base for controllers.

func (*ControllerBase) SetupRouter

func (b *ControllerBase) SetupRouter(controller Controller)

SetupRouter sets up the router for the controller.

type Dependency added in v0.1.0

type Dependency interface {
	Name() string
	Configure(config any)
	Start()
	Stop()
	Info() map[string]string
}

Dependency is an interface that represents a dependency in the test environment.

type Kafka added in v0.1.30

type Kafka struct {
	*Base
	testcontainers.Container

	Broker string
}

Kafka wraps a testcontainer running a Kafka broker.

type MongoDB added in v0.1.30

type MongoDB struct {
	*Base
	testcontainers.Container

	URI string
}

MongoDB wraps a testcontainer running a MongoDB instance.

type OTelCollector added in v0.1.30

type OTelCollector struct {
	*Base
	testcontainers.Container

	UID uuid.UUID
	// contains filtered or unexported fields
}

OTelCollector wraps a testcontainer running an OpenTelemetry Collector.

type TestLogWriter added in v0.1.26

type TestLogWriter struct {
	T testing.TB
}

TestLogWriter is an io.Writer that writes to the test log. This is useful for capturing logs in tests, especially when working with libraries that write to io.Writer (e.g., slog.TextHandler).

func (TestLogWriter) Write added in v0.1.26

func (w TestLogWriter) Write(p []byte) (int, error)

Write implements io.Writer by logging to the test.

Jump to

Keyboard shortcuts

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