48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package harness
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
testcontainers "github.com/testcontainers/testcontainers-go"
|
|
rediscontainer "github.com/testcontainers/testcontainers-go/modules/redis"
|
|
)
|
|
|
|
const defaultRedisContainerImage = "redis:7"
|
|
|
|
// RedisRuntime stores one started real Redis container together with the
|
|
// externally reachable endpoint used by black-box suites.
|
|
type RedisRuntime struct {
|
|
Container *rediscontainer.RedisContainer
|
|
Addr string
|
|
}
|
|
|
|
// StartRedisContainer starts one isolated real Redis container and registers
|
|
// automatic cleanup for the suite.
|
|
func StartRedisContainer(t testing.TB) *RedisRuntime {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
|
|
container, err := rediscontainer.Run(ctx, defaultRedisContainerImage)
|
|
if err != nil {
|
|
t.Fatalf("start redis container: %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
if err := testcontainers.TerminateContainer(container); err != nil {
|
|
t.Errorf("terminate redis container: %v", err)
|
|
}
|
|
})
|
|
|
|
addr, err := container.Endpoint(ctx, "")
|
|
if err != nil {
|
|
t.Fatalf("resolve redis container endpoint: %v", err)
|
|
}
|
|
|
|
return &RedisRuntime{
|
|
Container: container,
|
|
Addr: addr,
|
|
}
|
|
}
|