|
| 1 | +# Yokai Test Container Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai-contrib/actions/workflows/fxtestcontainer-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai-contrib/fxtestcontainer) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai-contrib/tree/main/fxtestcontainer) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai-contrib%2Ffxtestcontainer) |
| 7 | +[](https://pkg.go.dev/github.com/ankorstore/yokai-contrib/fxtestcontainer) |
| 8 | + |
| 9 | +> [Yokai](https://github.com/ankorstore/yokai) module for [Testcontainers](https://github.com/testcontainers/testcontainers-go) integration. |
| 10 | +
|
| 11 | +<!-- TOC --> |
| 12 | +* [Overview](#overview) |
| 13 | +* [Installation](#installation) |
| 14 | +* [Usage](#usage) |
| 15 | + * [Basic Usage](#basic-usage) |
| 16 | + * [Advanced Configuration](#advanced-configuration) |
| 17 | +* [Testing](#testing) |
| 18 | +<!-- TOC --> |
| 19 | + |
| 20 | +## Overview |
| 21 | + |
| 22 | +This module provides a simple and consistent API for creating test containers with [Testcontainers](https://github.com/testcontainers/testcontainers-go) in your [Yokai](https://github.com/ankorstore/yokai) applications. |
| 23 | + |
| 24 | +The module offers the `CreateGenericContainer` function that handles container creation with sensible defaults and flexible configuration options. |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +Install the module: |
| 29 | + |
| 30 | +```shell |
| 31 | +go get github.com/ankorstore/yokai-contrib/fxtestcontainer |
| 32 | +``` |
| 33 | + |
| 34 | +Then activate it in your application bootstrapper: |
| 35 | + |
| 36 | +```go |
| 37 | +// internal/bootstrap.go |
| 38 | +package internal |
| 39 | + |
| 40 | +import ( |
| 41 | + "github.com/ankorstore/yokai-contrib/fxtestcontainer" |
| 42 | + "github.com/ankorstore/yokai/fxcore" |
| 43 | +) |
| 44 | + |
| 45 | +var Bootstrapper = fxcore.NewBootstrapper().WithOptions( |
| 46 | + // load modules |
| 47 | + fxtestcontainer.FxTestContainerModule, |
| 48 | + // ... |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +### Basic Usage |
| 55 | + |
| 56 | +Use the `CreateGenericContainer` function to create test containers: |
| 57 | + |
| 58 | +```go |
| 59 | +package service_test |
| 60 | + |
| 61 | +import ( |
| 62 | + "context" |
| 63 | + "testing" |
| 64 | + |
| 65 | + "github.com/ankorstore/yokai-contrib/fxtestcontainer" |
| 66 | + "github.com/stretchr/testify/assert" |
| 67 | + "github.com/stretchr/testify/require" |
| 68 | +) |
| 69 | + |
| 70 | +func TestMyService_WithRedis(t *testing.T) { |
| 71 | + ctx := context.Background() |
| 72 | + |
| 73 | + // Define container configuration |
| 74 | + config := &fxtestcontainer.ContainerConfig{ |
| 75 | + Name: "test-redis", |
| 76 | + Image: "redis:alpine", |
| 77 | + Port: "6379/tcp", |
| 78 | + Environment: map[string]string{ |
| 79 | + "REDIS_PASSWORD": "", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + // Create test container |
| 84 | + container, err := fxtestcontainer.CreateGenericContainer(ctx, config) |
| 85 | + require.NoError(t, err) |
| 86 | + defer container.Terminate(ctx) |
| 87 | + |
| 88 | + // Get container endpoint |
| 89 | + endpoint, err := container.Endpoint(ctx, "") |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + // Use the container in your service |
| 93 | + service := NewMyService(endpoint) |
| 94 | + |
| 95 | + // Test your service methods |
| 96 | + err = service.Set(ctx, "key", "value") |
| 97 | + assert.NoError(t, err) |
| 98 | + |
| 99 | + value, err := service.Get(ctx, "key") |
| 100 | + assert.NoError(t, err) |
| 101 | + assert.Equal(t, "value", value) |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Advanced Configuration |
| 106 | + |
| 107 | +The `ContainerConfig` struct provides flexible configuration options: |
| 108 | + |
| 109 | +```go |
| 110 | +type ContainerConfig struct { |
| 111 | + // Name is a unique identifier for the container |
| 112 | + Name string |
| 113 | + // Image specifies the Docker image to use |
| 114 | + Image string |
| 115 | + // Port specifies the main port to expose (convenience field) |
| 116 | + Port string |
| 117 | + // ExposedPorts lists additional ports to expose |
| 118 | + ExposedPorts []string |
| 119 | + // Environment provides environment variables |
| 120 | + Environment map[string]string |
| 121 | + // WaitingFor specifies the wait strategy |
| 122 | + WaitingFor wait.Strategy |
| 123 | + // Cmd specifies the command to run in the container |
| 124 | + Cmd []string |
| 125 | + // Mounts specifies volume mounts |
| 126 | + Mounts testcontainers.ContainerMounts |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +Example with custom wait strategy and multiple configuration options: |
| 131 | + |
| 132 | +```go |
| 133 | +func TestWithPostgres(t *testing.T) { |
| 134 | + ctx := context.Background() |
| 135 | + |
| 136 | + config := &fxtestcontainer.ContainerConfig{ |
| 137 | + Name: "test-postgres", |
| 138 | + Image: "postgres:13", |
| 139 | + Port: "5432/tcp", |
| 140 | + Environment: map[string]string{ |
| 141 | + "POSTGRES_DB": "testdb", |
| 142 | + "POSTGRES_USER": "testuser", |
| 143 | + "POSTGRES_PASSWORD": "testpass", |
| 144 | + }, |
| 145 | + WaitingFor: wait.ForLog("database system is ready to accept connections"), |
| 146 | + } |
| 147 | + |
| 148 | + container, err := fxtestcontainer.CreateGenericContainer(ctx, config) |
| 149 | + require.NoError(t, err) |
| 150 | + defer container.Terminate(ctx) |
| 151 | + |
| 152 | + // Get connection details |
| 153 | + host, err := container.Host(ctx) |
| 154 | + require.NoError(t, err) |
| 155 | + |
| 156 | + port, err := container.MappedPort(ctx, "5432") |
| 157 | + require.NoError(t, err) |
| 158 | + |
| 159 | + // Use in your tests |
| 160 | + dsn := fmt.Sprintf("postgres://testuser:testpass@%s:%s/testdb?sslmode=disable", |
| 161 | + host, port.Port()) |
| 162 | + |
| 163 | + // Connect to database and run tests... |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +## Testing |
| 168 | + |
| 169 | +This module provides a simple and consistent API for creating test containers. See the [test examples](module_test.go) for usage patterns. |
| 170 | + |
| 171 | +The module automatically applies sensible defaults: |
| 172 | +- If no `WaitingFor` strategy is provided and a `Port` is specified, it will wait for the port to be listening |
| 173 | +- Exposed ports are automatically configured based on the `Port` and `ExposedPorts` fields |
| 174 | +- Container names are used for error reporting |
| 175 | +- Containers are started automatically and ready to use when the function returns |
0 commit comments