nurse/protocols/redis/container_test.go
Peter Kurfer c52912c5aa
Some checks failed
agola/nurse/Test and lint The run failed
refactor: migrate to new Gitea URL
2022-09-22 11:46:36 +02:00

53 lines
1.3 KiB
Go

package redis_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"code.icb4dc0.de/prskr/nurse/config"
)
func PrepareRedisContainer(tb testing.TB) *config.Server {
tb.Helper()
const redisPort = "6379/tcp"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
tb.Cleanup(cancel)
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/redis:alpine",
ExposedPorts: []string{redisPort},
SkipReaper: true,
AutoRemove: true,
WaitingFor: wait.ForListeningPort(redisPort),
},
Started: true,
Logger: testcontainers.TestLogger(tb),
})
if err != nil {
tb.Fatalf("testcontainers.GenericContainer() err = %v", err)
}
tb.Cleanup(func() {
if err := container.Terminate(context.Background()); err != nil {
tb.Errorf("container.Terminate() err = %v", err)
}
})
ep, err := container.PortEndpoint(ctx, redisPort, "redis")
if err != nil {
tb.Fatalf("container.PortEndpoint() err = %v", err)
}
srv := new(config.Server)
if err := srv.UnmarshalURL(fmt.Sprintf("%s/0?MaxRetries=3", ep)); err != nil {
tb.Fatalf("config.ParseFromURL() err = %v", err)
}
return srv
}