Documentation
¶
Overview ¶
Package testutil provides utility functions and types for testing.
Index ¶
- Constants
- func GetFreeTCPPort() (int, error)
- func Identifier(tb testing.TB) string
- func P[T any](v T) *T
- type APIServer
- type Base
- func (b *Base) ForController() *ControllerBase
- func (b *Base) GetFreeTCPPort() int
- func (b *Base) StartAPIServer(mongoURI string, databaseName string) *APIServer
- func (b *Base) StartAPIServerWithKafka(mongoURI, kafkaBroker, databaseName string) *APIServer
- func (b *Base) StartKafka() *Kafka
- func (b *Base) StartMongoDB() *MongoDB
- func (b *Base) StartOTelCollector(opampPort int) *OTelCollector
- func (b *Base) StartOTelCollectorHTTP(opampPort int) *OTelCollector
- func (b *Base) StartOTelCollectorWithAttributes(opampPort int, resourceAttrs map[string]string) *OTelCollector
- type Controller
- type ControllerBase
- type Dependency
- type Kafka
- type MongoDB
- type OTelCollector
- type TestLogWriter
Constants ¶
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
GetFreeTCPPort asks the kernel for a free open port that is ready to use.
func Identifier ¶ added in v0.1.30
Identifier can be used as a name of host, container, image, volume, network, etc.
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
AdminPassword returns the admin password configured for this test server.
func (*APIServer) AdminUsername ¶ added in v0.1.30
AdminUsername returns the admin username configured for this test server.
func (*APIServer) Client ¶ added in v0.1.30
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 (*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
GetFreeTCPPort returns a free TCP port that is ready to use.
func (*Base) StartAPIServer ¶ added in v0.1.30
StartAPIServer starts a new API server backed by the given MongoDB instance.
func (*Base) StartAPIServerWithKafka ¶ added in v0.1.30
StartAPIServerWithKafka starts a new API server backed by MongoDB and Kafka for event processing.
func (*Base) StartKafka ¶ added in v0.1.30
StartKafka starts a Kafka container and waits until it is ready to accept connections.
func (*Base) StartMongoDB ¶ added in v0.1.30
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 ¶
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
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).