Documentation
¶
Index ¶
- Variables
- type Config
- type ContainerService
- func (c *ContainerService[T]) Container() T
- func (c *ContainerService[T]) Key() string
- func (c *ContainerService[T]) Start(ctx context.Context) error
- func (c *ContainerService[T]) State(ctx context.Context) (string, error)
- func (c *ContainerService[T]) String() string
- func (c *ContainerService[T]) Terminate(ctx context.Context) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilConfig is returned when the config is nil. ErrNilConfig = errors.New("config is nil") // ErrContainerNotRunning is returned when the container is not running. ErrContainerNotRunning = errors.New("container is not running") // ErrEmptyServiceKey is returned when the service key is empty. ErrEmptyServiceKey = errors.New("service key is empty") // ErrImageEmpty is returned when the image is empty. ErrImageEmpty = errors.New("image is empty") // ErrRunNil is returned when the run is nil. ErrRunNil = errors.New("run is nil") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config[T tc.Container] struct { // ServiceKey is the key used to identify the service in the Fiber app's state. ServiceKey string // Image is the image name to use for the container. Image string // Run is the function to use to run the container. // It's usually the Run function from the testcontainers-go module, like redis.Run or postgres.Run, // although it could be the generic [tc.Run] function from the testcontainers-go package. Run func(ctx context.Context, img string, opts ...tc.ContainerCustomizer) (T, error) // Options are the functional options to pass to the [tc.Run] function. This argument is optional. // You can find the available options in the [testcontainers website]. // // [testcontainers website]: https://golang.tc.org/features/creating_container/#customizing-the-container Options []tc.ContainerCustomizer }
Config contains the configuration for a container service.
func NewContainerConfig ¶
func NewContainerConfig(serviceKey string, img string, opts ...tc.ContainerCustomizer) Config[*tc.DockerContainer]
NewContainerConfig creates a new container service config for a generic container type, not created by a Testcontainers module. So this function best used in combination with the AddService function to add a custom container to the Fiber app's state.
- The serviceKey is the key used to identify the service in the Fiber app's state. - The img is the image name to use for the container. - The opts are the functional options to pass to the tc.Run function. This argument is optional.
This function uses the tc.Run function as the run function.
func NewModuleConfig ¶
func NewModuleConfig[T tc.Container]( serviceKey string, img string, run func(ctx context.Context, img string, opts ...tc.ContainerCustomizer) (T, error), opts ...tc.ContainerCustomizer, ) Config[T]
NewModuleConfig creates a new container service config for a module.
- The serviceKey is the key used to identify the service in the Fiber app's state. - The img is the image name to use for the container. - The run is the function to use to run the container. It's usually the Run function from the module, like [redis.Run] or [postgres.Run]. - The opts are the functional options to pass to the run function. This argument is optional.
type ContainerService ¶
ContainerService represents a container that implements the fiber.Service interface. It manages the lifecycle of a tc.Container instance, and it can be retrieved from the Fiber app's state calling the fiber.MustGetService function with the key returned by the ContainerService.Key method.
The type parameter T must implement the tc.Container interface.
func AddService ¶
func AddService[T tc.Container](cfg *fiber.Config, containerConfig Config[T]) (*ContainerService[T], error)
AddService adds a Testcontainers container as a fiber.Service for the Fiber app. It returns a pointer to a [ContainerService[T]] object, which contains the key used to identify the service in the Fiber app's state, and an error if the config is nil. The container should be a function like redis.Run or postgres.Run that returns a container type which embeds tc.Container. - The cfg is the Fiber app's configuration, needed to add the service to the Fiber app's state. - The containerConfig is the configuration for the container, where:
- The containerConfig.ServiceKey is the key used to identify the service in the Fiber app's state.
- The containerConfig.Run is the function to use to run the container. It's usually the Run function from the module, like redis.Run or postgres.Run.
- The containerConfig.Image is the image to use for the container.
- The containerConfig.Options are the functional options to pass to the tc.Run function. This argument is optional.
Use NewModuleConfig or NewContainerConfig helper functions to create valid containerConfig objects.
Example (FromContainer) ¶
cfg := &fiber.Config{}
// Define the base key for the generic service.
// The service returned by the [testcontainers.AddService] function,
// using the [ContainerService.Key] method,
// concatenates the base key with the "using testcontainers-go" suffix.
const (
nginxKey = "nginx-generic"
)
// Adding a generic container, directly from the testcontainers-go package.
containerConfig := testcontainers.NewContainerConfig(nginxKey, "nginx:latest", tc.WithExposedPorts("80/tcp"))
nginxSrv, err := testcontainers.AddService(cfg, containerConfig)
if err != nil {
log.Println("error adding nginx generic:", err)
return
}
app := fiber.New(*cfg)
fmt.Println(app.State().ServicesLen())
srvs := app.State().Services()
fmt.Println(len(srvs))
nginxCtr := fiber.MustGetService[*testcontainers.ContainerService[*tc.DockerContainer]](app.State(), nginxSrv.Key())
fmt.Println(nginxCtr.String())
Output: 1 1 nginx-generic (using testcontainers-go)
Example (FromModule) ¶
cfg := &fiber.Config{}
// Define the base keys for the module services.
// The service returned by the [testcontainers.AddService] function,
// using the [ContainerService.Key] method,
// concatenates the base key with the "using testcontainers-go" suffix.
const (
redisKey = "redis-module"
postgresKey = "postgres-module"
)
// Adding containers coming from the testcontainers-go modules,
// in this case, a Redis and a Postgres container.
redisModuleConfig := testcontainers.NewModuleConfig(redisKey, "redis:latest", redis.Run)
redisSrv, err := testcontainers.AddService(cfg, redisModuleConfig)
if err != nil {
log.Println("error adding redis module:", err)
return
}
postgresModuleConfig := testcontainers.NewModuleConfig(postgresKey, "postgres:latest", postgres.Run)
postgresSrv, err := testcontainers.AddService(cfg, postgresModuleConfig)
if err != nil {
log.Println("error adding postgres module:", err)
return
}
// Create a new Fiber app, using the provided configuration.
app := fiber.New(*cfg)
// Verify the number of services in the app's state.
fmt.Println(app.State().ServicesLen())
// Retrieve all services from the app's state.
// This returns a slice of all the services registered in the app's state.
srvs := app.State().Services()
fmt.Println(len(srvs))
// Retrieve the Redis container from the app's state using the key returned by the [ContainerService.Key] method.
redisCtr := fiber.MustGetService[*testcontainers.ContainerService[*redis.RedisContainer]](app.State(), redisSrv.Key())
// Retrieve the Postgres container from the app's state using the key returned by the [ContainerService.Key] method.
postgresCtr := fiber.MustGetService[*testcontainers.ContainerService[*postgres.PostgresContainer]](app.State(), postgresSrv.Key())
// Verify the string representation of the Redis and Postgres containers.
fmt.Println(redisCtr.String())
fmt.Println(postgresCtr.String())
Output: 2 2 redis-module (using testcontainers-go) postgres-module (using testcontainers-go)
func (*ContainerService[T]) Container ¶
func (c *ContainerService[T]) Container() T
Container returns the Testcontainers container instance, giving full access to the T type methods. It's useful to access the container's methods, like tc.Container.MappedPort or tc.Container.Inspect.
func (*ContainerService[T]) Key ¶
func (c *ContainerService[T]) Key() string
Key returns the key used to identify the service in the Fiber app's state. Consumers should use string constants for service keys to ensure consistency when retrieving services from the Fiber app's state.
func (*ContainerService[T]) Start ¶
func (c *ContainerService[T]) Start(ctx context.Context) error
Start creates and starts the container, calling the [run] function with the [img] and [opts] arguments. It implements the fiber.Service interface.
func (*ContainerService[T]) State ¶
func (c *ContainerService[T]) State(ctx context.Context) (string, error)
State returns the status of the container. It implements the fiber.Service interface.
func (*ContainerService[T]) String ¶
func (c *ContainerService[T]) String() string
String returns the service key, which uniquely identifies the container service. It implements the fiber.Service interface.
func (*ContainerService[T]) Terminate ¶
func (c *ContainerService[T]) Terminate(ctx context.Context) error
Terminate stops and removes the container. It implements the fiber.Service interface.