nurse/protocols/redis/container_test.go

54 lines
1.3 KiB
Go
Raw Normal View History

2022-04-28 16:35:02 +00:00
package redis_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
2022-04-28 16:35:02 +00:00
"code.1533b4dc0.de/prskr/nurse/config"
2022-04-28 16:35:02 +00:00
)
func PrepareRedisContainer(tb testing.TB) *config.Server {
tb.Helper()
const redisPort = "6379/tcp"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
2022-04-28 16:35:02 +00:00
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),
2022-04-28 16:35:02 +00:00
},
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 {
2022-04-28 16:35:02 +00:00
tb.Fatalf("config.ParseFromURL() err = %v", err)
}
return srv
}